51单片机实现串口接收和发送数据的两种方法解析

第一种方法:在中断中处理
typedef unsigned char uint8;
volatile uint8 u8uart_data;
void initialuart0_timer1()
{
scon = 0x50; //2015-05-04
tmod = 0x20;
th1 = 0xfd; // 9600bps 2015-05-04
tl1 = 0xfd;
tr1 = 1; // start timer0
es = 1; // enable serial interrupt
ea = 1; // enable global interrupt
}
//-----------------------------------------------------------------------------------------------------------
void main (void)
{
//--------------------------------------------------------------------------------
initialuart0_timer1(); // 9600 baud rate @ 11.0592mhz
while(1)
{
// receive_data_from_pc();
//send_data_to_pc(receive_data_from_pc());
while(recv_data)
{
recv_data = 0;
ti = 0;
sbuf = u8uart_data;
}
}
}
//-----------------------------------------------------------------------------------------------------------
void uart_isr(void) interrupt 4
{
if (ri)
{ // if reception occur
ri = 0; // clear reception flag for next reception
u8uart_data = 0;
u8uart_data = sbuf; // read receive data
recv_data = 1;
//sbuf = u8uart_data; // send back same data on uart
}
if (ti) // if emission occur
{ // clear emission flag for next emission
ti = 0;
}
}
//-----------------------------------------------------------------------------------------------------------
第二种方法:不在中断中处理
//-----------------------------------------------------------------------------------------------------------
// use timer1 as baud rate generator
void initialuart0_timer1()
{
scon = 0x50; //2015-05-04
tmod = 0x20;
#if 0
#ifdef fosc_110592
th1 = 256 - (28800/u32baudrate); /* 11.059m/384=28800 */
#endif
#ifdef fosc_184320
th1 = 256 - (48000/u32baudrate); /* 18.4320m/384=48000 */
#endif
#ifdef fosc_221184
th1 = 256 - (57600/u32baudrate); /* 22.1184m/384=57600 */
#endif
#ifdef fosc_331776
th1 = 256 - (86400/u32baudrate); /* 33.1776m/384=86400 */
#endif
#ifdef fosc_368640
th1 = 256 - (96000/u32baudrate); /* 36.8640m/384=96000 */
#endif
#endif /* #if 0 */
th1 = 0xfd; // 9600bps 2015-05-04
tl1 = 0xfd;
tr1 = 1; // start timer0
es = 1; // enable serial interrupt
ea = 1; // enable global interrupt
}
//-----------------------------------------------------------------------------------------------------------
uint8 receive_data_from_pc(void)
{
uint8 c;
while (!ri);
c = sbuf;
ri = 0;
return (c);
}
//-----------------------------------------------------------------------------------------------------------
void send_data_to_pc (uint8 c)
{
while (!ti);
ti = 0;
sbuf = c;
}
//-----------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------
void main (void)
{
//--------------------------------------------------------------------------------
initialuart0_timer1(); // 9600 baud rate @ 11.0592mhz
//---------------------------------------------------------------------------------
while(1)
{
receive_data_from_pc();
send_data_to_pc(receive_data_from_pc());
}
}

助力企业数字化转型,无线安灯系统智能呼叫器
蔡司工业CT纳米X射线技术在锂离子电池中的应用
单片集成的小型调频发射电路图
LED死灯原因分析探讨
首款国产超小体积5G通信模组研发成功,有效提升上网速率体验
51单片机实现串口接收和发送数据的两种方法解析
扁平柔性电缆的间距怎么算?用这个公式!
你了解过Linux字符设备驱动框架?
比特币将会取代美元的主导地位成为全球主要的支付货币
华为P10Plus亮黑版开箱,一部让人爱上拍照的手机
欧司朗Duris L38:在应用和设计两方面皆能实现出色的灵活性
2020年全球5G移动网络的详细情况
红外测温仪工作波长的选择
iOS10.3怎么样?值得更新吗?苹果升级iOS10.3立大功,更新后内存增加8GB?
存储领域磁带被取代
企业在机器学习时代如何前行
印制电路板的基本参数及漏电距离及电气间隙的检测方法
国产北汽绅宝CC, 2.0T动力更强劲, 现仅14.98万起
华为海思半导体公司已将注册资本提高至20亿元来提高手机芯片的开发
2019年的这场MWC毫无疑问属于“5G”这个热得发烫的词