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

控制/MCU

1882人已加入

描述

第一种方法:在中断中处理

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());

}

}

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分