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;
}
Previous article:Studying MSP430 memory alignment issues based on FAT table
Next article:Several methods of writing delay programs for microcontrollers
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
Guess you like
- Design of Watchdog Circuit Based on DSP Processor TMS320F2812
- Sitara AM335x issues
- Seeking long-term cooperation engineers to do part-time STM32 51 8S FPGA image processing
- 【K210 Series】6. Performance Test
- Capacitor selection problem after single-phase rectifier bridge
- Sensortile.box installation and testing
- How to start DSP c6678
- Improving system performance using external memory solutions.pdf
- [Review of Arteli Development Board AT32F421] 2. Use of peripheral ERTC
- [Review of SGP40] + Unboxing & Basic Use