STC MCU dual serial port usage settings and routines

Publisher:才富五车330Latest update time:2014-12-29 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
When using the STC_60s2 microcontroller to transmit and receive dual serial ports simultaneously, pay attention to some places where configuration errors are easy to occur. The AUXR register has many functions. When setting and resetting a bit, it is likely to be done multiple times in different places. You should use the AUXR |= bitx and AUXR &=~bitx statements to set and reset a certain bit. This will not affect other function bits.
 
Attached program: 
 
 #include
 
//sfr AUXR = 0x8E;
 
#define uchar unsigned char //
 
#define uint unsigned int //
 
#define BUADRATE 38400
 
#define FOSC 24000000
 
#define S2RI 0x01
 
#define S2TI 0x02
 
sbit LED1 = P2^7;
 
uchar ReceBuf[200]={0};
 
bit finishflag = 0;
 
uchar revcnt = 0;
 
uchar startflag = 0;
 
void InitUART1(void) //baudrate 115200
 
{
 
TMOD = 0x20;
 
SCON = 0x50;
 
TH1 = 0xf3; //115384
 
TL1 = TH1;
 
PCON = 0x80; //SMOD = 1;
 
ES = 1;
 
TR1 = 1;
 
}
 
void InitUART2() //baudrate 38400
 
{
 
S2CON = 0x50; //8-Bit serial port mode allows receiving
 
BRT = 236; //[24000000/(256-236)]/32 = 37500 error2.34%
 
AUXR |= 0x14; //BRTR=1 enables BRT BRTx12=1 does not divide the frequency (AUXR appears for the second time, cannot use '=' should use "|=")
 
IE2 = 0x01; //enable UART2 interrupt
 
AUXR1 = 0; //UART2 TXD->P1.3 RXD->P1.2
 
}
 
void SendOneByte(unsigned char c)
 
{
 
SBUF = c;
 
while(!TI);
 
TI = 0;
 
}
 
void U1SendString(uchar *dat)
 
{
 
while(*(dat)!='\0')
 
{
 
SendOneByte(*dat);
 
dat ;
 
}
 
}
 
void U2SendOneByte(unsigned char c)
 
{
 
S2BUF = c;
 
while(!(S2CON & S2TI));
 
S2CON &= ~S2TI;
 
}
 
void U2SendString(uchar *dat)
 
{
 
while(*(dat)!='\0')
 
{
 
U2SendOneByte(*dat);
 
dat ;
 
}
 
}
 
void main()
 
{ uchar temp = 0x01;
 
AUXR = AUXR|0x40; // T1, 1T Mode AUXR appears for the first time
 
InitUART1();
 
EA = 1;
 
SendOneByte(0xaa);
 
SendOneByte(0xbb);
 
SendOneByte(0xcc);
 
InitUART2();
 
SendOneByte(0xdd);
 
while(1)
 
{// temp = ~temp;
 
// U2SendOneByte(temp);
 
if(finishflag)
 
{ LED1 = 1;
 
finishflag = 0;
 
U1SendString(ReceBuf);
 
LED1 = 0;
 
}
 
}
 
}
 
void UART1ISR(void) interrupt 4
 
{
 
if(RI)
 
{
 
RI = 0;
 
//add your code here!
 
}
 
else
 
TI = 0;
 
}
 
void UART2ISR(void) interrupt 8
 
{ static bit flash=0;
 
static uchar i=0;
 
static uchar endcnt = 0;
 
uchar tempdat = 0;
 
if(S2CON & S2RI)
 
{
 
S2CON &= ~S2RI;
 
if(~finishflag)
 
{
 
flash=~flash;
 
//LED1 = flash;
 
tempdat = S2BUF;
 
if(tempdate=='$')
 
{
 
startflag = 1;
 
}
 
///////////////////////////////////////////////////// //////////
 
if(startflag)
 
{
 
ReceBuf[i] = tempdat;
 
i ;
 
///////////////////////////////////////////////////// //////////
 
if(i==6&&ReceBuf[3]!='R'&&ReceBuf[5]!='C') //Judge whether it is a $GPRMC frame
 
{
 
startflag = 0;
 
i = 0;
 
}
 
else if(ReceBuf[i-1]=='*')
 
{
 
endcnt ;
 
if(endcnt==2)
 
{
 
revcnt = i;
 
endcnt = 0;
 
i = 0;
 
finishflag = 1;
 
startflag = 0;
 
}
 
}
 
else ;
 
}
 
}
 
else ;
 
}
 
else
 
S2CON &= ~S2TI;
 
}
 
Reference address:STC MCU dual serial port usage settings and routines

Previous article:Studying MSP430 memory alignment issues based on FAT table
Next article:Several methods of writing delay programs for microcontrollers

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号