RS485轉(zhuǎn)TTL與單片機通訊
程序簡單表述如下:
本文引用地址:http://m.butianyuan.cn/article/201611/316739.htm#include
void Uart_Init(void)
{
SCON=0x50; //serial mode 1
TMOD|=0x20; //timer1 mode2 8bit auto reload
PCON|=0x80; //SMOD=1;
TH1=0xfa; //9600 11.0592
ES=1; //enable serial interrupt
TR1=1; //enable timer1
EA=1;
}
void Uart_PutChar(unsigned char ch)
{
SBUF=ch;
while(TI==0);
TI=0;
}
void Uart_GetChar(void) interrupt 4
{
unsigned char rdat=0;
if(RI)
{
RI=0;
rdat=SBUF;
Uart_PutChar(rdat);
}
}
void Uart_PutStr(unsigned char *pstr)
{
while(*pstr != 0)
{
Uart_PutChar(*pstr++);
}
}
void main(void)
{
Uart_Init();
while(1)
{
//Uart_PutChar(A);
Uart_PutStr("This is a RS485 transmission test.r");
}
}
Proteus 仿真圖示如下:
評論