STC Dual Serial Port sending example

Source: Internet
Author: User

#include "Reg51.h"


#define FOSC 12000000L//Crystal oscillator 12M
#define BAUD 9600


#define NONE_PARITY 0//No check digit
#define ODD_PARITY 1//Odd check
#define Even_parity 2//even check
#define MARK_PARITY 3//Mark Check
#define SPACE_PARITY 4//null check


#define Paritybit Even_parity


#define S2RI 0x01
#define S2TI 0x02
#define S2RB8 0x04
#define S2TB8 0x08


SFR AUXR = 0x8e;
SFR s2con = 0x9a;
SFR s2buf = 0x9b;
SFR BRT = 0x9c;
SFR IE2 = 0xaf;


Bit busy;


void SendData (char dat);
void Sendstring (char *s);
unsigned char string[]={"Hello world!\r\n"};
void Main ()
{
#if (Paritybit = = none_parity)
S2con = 0x5a; 8-bit variable baud rate (no check bit)
#elif (Paritybit = = odd_parity) | | (Paritybit = = even_parity) | | (Paritybit = = mark_parity)
S2con = 0xDA; 9-bit variable baud rate, initial check digit is 1
#elif (Paritybit = = space_parity)
S2con = 0xd5; 9-bit variable baud rate, initial check digit is 0
#endif
---Serial 1 initialization------------------------------
scon=0x40; Set the serial port mode 1,ren=0;
pcon=0x80; Double the baud rate
tmod=0x20; Timer works on Mode 2
Th1=0xf3; Baud rate 4800
Tr1=1; Turn on Timer 1


---------------Serial 2 Initialization-------------
BRT =-(Fosc/32/baud); Setting the overloaded initial value of the independent baud rate generator
AUXR = 0x14; Independent baud rate generator works in 1T mode
IE2 = 0x01; Enable serial port 2 to interrupt
EA = 1; Open Total Interrupt


Sendstring ("Stc12c5a60s2\r\nuart2 Test!\r\n");Serial 2 Send




while (1)
{
unsigned char i=0;
while (String[i])//wait for the string to be sent out Serial 1 Send
{
Sbuf= string[i++]; Data loading buffers
while (ti==0); Waiting to be sent out
ti=0; Clear Send flag bit
}
}
while (1);
}


void Uart2 () Interrupt 8 using 1
{
if (S2con & S2ri)
{
S2con &= ~s2ri; Clear Receive Completion flag
P0 = S2buf; P0 Display serial data
P2 = (S2con & S2rb8); P2.2 Display Check digit
}
if (S2con & S2ti)
{
S2con &= ~s2ti; Clear Send Completion flag
busy = 0;
}
}


void SendData (char dat)
{
while (busy); Wait for the last data send to complete
ACC = dat; Get parity bit P
if (P) Set the check digit of serial port data according to P
{
#if (Paritybit = = odd_parity)
S2con &= ~s2tb8; Parity bit is 0
#elif (Paritybit = = even_parity)
S2con |= S2tb8; Parity bit is 1
#endif
}
Else
{
#if (Paritybit = = odd_parity)
S2con |= S2tb8; Parity bit is 1
#elif (Paritybit = = even_parity)
S2con &= ~s2tb8; Parity bit is 0
#endif
}
busy = 1;
S2buf = ACC; Send data
}


void Sendstring (char *s)
{
while (*s)//Judgment string End flag
{
SendData (*s++); Send character
}


}

STC Dual Serial Port sending example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.