標(biāo)題:
51單片機(jī)io口模擬串口UART協(xié)議不用串口接口實(shí)現(xiàn)程序
[打印本頁(yè)]
作者:
JN0717
時(shí)間:
2022-12-7 20:42
標(biāo)題:
51單片機(jī)io口模擬串口UART協(xié)議不用串口接口實(shí)現(xiàn)程序
支持4800,9600波特率半雙工串口通訊
支持最高19200波特率串口發(fā)送
sim_uart.c
#include "sim_uart.h"
bit SU_TI,SU_RI;
unsigned char tx_sbuf,tx_count,rx_sbuf,rx_count;
void SimUart_Config(unsigned int baud)
{
switch(baud)
{
case(4800): TH0=TL0=60; break; //53-66
case(9600): TH0=TL0=160; break; //155-166
case(19200): TH0=TL0=208; break; //206-211
}
TMOD=0x02;
EA=1;
ET0=1;
TR0=1;
}
void SU_Send(unsigned char byte)
{
tx_sbuf=byte;
tx_count=0;
SU_TI=1;
while(SU_TI);
}
unsigned char SU_Read(void)
{
rx_count=0;
SU_RI=1;
while(SU_RI);
return rx_sbuf;
}
void Clear_RxSbuf(void)
{
rx_sbuf=0;
}
//----------------------------------------------------
// baud=4800or9600.
//----------------------------------------------------
void Simulative_Uart(void) interrupt 1
{
unsigned int i=65536;
if(SU_TI)
{
if(!tx_count)
SU_TXD=0;
else if(tx_count<9)
{
SU_TXD=tx_sbuf&0x01;
tx_sbuf=tx_sbuf>>1;
}
else if(tx_count==9)
{
SU_TXD=1;
SU_TI=0;
}
tx_count++;
}
else if(SU_RI)
{
if(!rx_count)
{
TR0=0;
while(SU_RXD||i--);
TR0=1;
rx_count++;
}
else if(rx_count<9)
{
rx_sbuf=rx_sbuf|SU_RXD;
rx_sbuf=_cror_(rx_sbuf,1);
rx_count++;
}
else if(rx_count==9)
SU_RI=0;
}
}
//----------------------------------------------------
// baud=19200,TX_Mode
//----------------------------------------------------
//void Simulative_Uart(void) interrupt 1
//{
// if(SU_TI)
// {
// if(!tx_count)
// SU_TXD=0;
// else if(tx_count<9)
// {
// SU_TXD=tx_sbuf&0x01;
// tx_sbuf=tx_sbuf>>1;
// }
// else if(tx_count==9)
// {
// SU_TXD=1;
// SU_TI=0;
// }
// tx_count++;
// }
//}
復(fù)制代碼
sim_uart.h
#ifndef _SIM_UART_H_
#define _SIM_UART_H_
#include <reg52.h>
#include <intrins.h>
sbit SU_TXD=P3^2;
sbit SU_RXD=P3^3;
void SimUart_Config(unsigned int baud);
void SU_Send(unsigned char byte);
unsigned char SU_Read(void);
void Clear_RxSbuf(void);
#endif
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1