串口基本认识
串行接口简称串口,也称串行通信接口或串行通讯接口(通常指COM接口),是采用串行通信方
式的扩展接口。串行接口(Serial Interface)是指数据一位一位地顺序传送。其特点是通信线路简
单,只要一对传输线就可以实现双向通信(可以直接利用电话线作为传输线),从而大大降低了成
本,特别适用于远距离通信,但传送速度较慢
串口接线方式
- RXD:数据输入引脚,数据接受;STC89系列对应P3.0口。
TXD:数据发送引脚,数据发送;STC89系列对应P3.1口。
接线方式
串口编程要素
输入/输出数据缓冲器都叫做SBUF, 都用99H地址码,但是是两个独立的8位寄存器
代码体现为: 想要接收数据 char data = SBUF 想要发送数据 SBUF = data
回忆UART是异步串行接口,通信双方使用时钟不同,因为双方硬件配置不同,但是需要约定通信
速度,叫做波特率
对于电脑来说,别人做好了软件,鼠标点点点就能配置好,而苦逼单片机的波特率配置需要我们写
代码
点点点配置什么,我们代码也要配置对应参数。
51发送字符到PC
SCON
选择SM0和SM1为01,工作模式为方式一,
REN = 0,禁止接收
REN = 1,允许接收
SCON = 0100 0000; //禁止接收
SCON = 0101 0000; //允许接收
PCON
PCON &= 0x7F;//0111 1111 按位与将SMOD设置为0。
AUXR
TMOD
目标:0010 xxxx
TMOD &= 0x0F; //先将高位清零,低位不变
TMOD |= 0x20; //0010 xxxx
TH1
TH1 = 0xFD; //smod = 0 9600 = (2^smod) / 32 x 11059200 / 12/(256 - THL)
TL1 = 0xFD; // THL = 253 = FD(HEX)
代码实现
#include "reg52.h"
#include "intrins.h"sfr AUXR = 0x8E;void Delay250ms() //@11.0592MHz
{unsigned char i, j, k;_nop_();i = 2;j = 193;k = 57;do{do{while (--k);} while (--j);} while (--i);
}void UartInit(void) //9600bps@11.0592MHz
{PCON &= 0x7F; SCON = 0x50; AUXR &= 0xBF; AUXR &= 0xFE; TMOD &= 0x0F; TMOD |= 0x20; TL1 = 0xFD; TH1 = 0xFD; ET1 = 0; TR1 = 1;
}void my_UartInit()
{SCON = 0x40;//选择SM0和SM1为01,工作模式为方式一,SCON = 0100 0000;PCON &= 0x7F;//0111 1111 按位与将SMOD设置为0AUXR |= 0x01;//0000 0001TMOD &= 0x0F;//目标:0010 xxxx //先将高位清零,低位不变TMOD |= 0x20;//0010 xxxxTH1 = 0xFD; //smod = 0 9600 = (2^smod) / 32 x 11059200 / 12 /(256 - THL)TL1 = 0xFD; //THL = 253 = FD(HEX)ET1 = 0;//禁止定时器1中断TR1 = 1;//启动定时器1
}void main()
{char msg_data = 's';my_UartInit(); while(1){Delay250ms();SBUF = msg_data;}
}
51发字符串给PC
#include "reg52.h"
#include "intrins.h"sfr AUXR = 0x8E;void Delay250ms() //@11.0592MHz
{unsigned char i, j, k;_nop_();i = 2;j = 193;k = 57;do{do{while (--k);} while (--j);} while (--i);
}void UartInit(void) //9600bps@11.0592MHz
{PCON &= 0x7F; SCON = 0x50; AUXR &= 0xBF; AUXR &= 0xFE; TMOD &= 0x0F; TMOD |= 0x20; TL1 = 0xFD; TH1 = 0xFD; ET1 = 0; TR1 = 1;
}void my_UartInit()
{SCON = 0x40;//选择SM0和SM1为01,工作模式为方式一,SCON = 0100 0000;PCON &= 0x7F;//0111 1111 按位与将SMOD设置为0AUXR |= 0x01;//0000 0001TMOD &= 0x0F;//目标:0010 xxxx //先将高位清零,低位不变TMOD |= 0x20;//0010 xxxxTH1 = 0xFD; //smod = 0 9600 = (2^smod) / 32 x 11059200 / 12 /(256 - THL)TL1 = 0xFD; //THL = 253 = FD(HEX)ET1 = 0;//禁止定时器1中断TR1 = 1;//启动定时器1
}void sendByte(char msg_data)
{SBUF = msg_data;while(TI == 0);//结束TI=1,TI==0为假TI = 0;
}void sendString(char *str)
{while(*str != '\0'){sendByte(*str);str++;}
}void main()
{my_UartInit();while(1){Delay250ms();sendString("hello world!\r\n");}
}
51接收PC发送的字符,达到点灯关灯的目的
#include "reg52.h"
#include "intrins.h"sbit led1 = P3^7;sfr AUXR = 0x8E;char cmd; void Delay250ms() //@11.0592MHz
{unsigned char i, j, k;_nop_();i = 2;j = 193;k = 57;do{do{while (--k);} while (--j);} while (--i);
}void UartInit(void) //9600bps@11.0592MHz
{PCON &= 0x7F;SCON = 0x50;AUXR &= 0xBF;AUXR &= 0xFE;TMOD &= 0x0F;TMOD |= 0x20;TL1 = 0xFD;TH1 = 0xFD;ET1 = 0;TR1 = 1;
}void my_UartInit()
{SCON = 0x50;//选择SM0和SM1为01,工作模式为方式一,SCON = 0101 0000; 允许接收PCON &= 0x7F;//0111 1111 按位与将SMOD设置为0AUXR |= 0x01;//0000 0001TMOD &= 0x0F;//目标:0010 xxxx //先将高位清零,低位不变TMOD |= 0x20;//0010 xxxxTH1 = 0xFD; //smod = 0 9600 = (2^smod) / 32 x 11059200 / 12 /(256 - THL)TL1 = 0xFD; //THL = 253 = FD(HEX)ET1 = 0;//禁止定时器1中断TR1 = 1;//启动定时器1
}void sendByte(char msg_data)
{SBUF = msg_data;while(TI == 0);//结束时TI=1,TI == 0 为假TI = 0;
}void sendString(char *str)
{while(*str != '\0'){sendByte(*str);str++;}
}void main()
{my_UartInit();led1 = 1;while(1){Delay250ms();sendString("hello world!\r\n");}
}void UartHandler() interrupt 4
{if(RI){RI = 0;cmd = SBUF;if(cmd == 'o'){led1 = 0;}if(cmd == 'c'){led1 = 1;}}if(TI){}
}
51接收PC发送的字符串,达到点灯关灯的目的
#include "reg52.h"
#include "intrins.h"
#include "string.h"sbit led1 = P3^7;sfr AUXR = 0x8E;char cmd[12] = {'\0'};void Delay250ms() //@11.0592MHz
{unsigned char i, j, k;_nop_();i = 2;j = 193;k = 57;do{do{while (--k);} while (--j);} while (--i);
}void UartInit(void) //9600bps@11.0592MHz
{PCON &= 0x7F;SCON = 0x50;AUXR &= 0xBF;AUXR &= 0xFE;TMOD &= 0x0F;TMOD |= 0x20;TL1 = 0xFD;TH1 = 0xFD;ET1 = 0;TR1 = 1;
}void my_UartInit()
{SCON = 0x50;//选择SM0和SM1为01,工作模式为方式一,SCON = 0101 0000; 允许接收PCON &= 0x7F;//0111 1111 按位与将SMOD设置为0AUXR |= 0x01;//0000 0001TMOD &= 0x0F;//目标:0010 xxxx //先将高位清零,低位不变TMOD |= 0x20;//0010 xxxxTH1 = 0xFD; //smod = 0 9600 = (2^smod) / 32 x 11059200 / 12 /(256 - THL)TL1 = 0xFD; //THL = 253 = FD(HEX)ET1 = 0;//禁止定时器1中断TR1 = 1;//启动定时器1
}void sendByte(char msg_data)
{SBUF = msg_data;while(TI == 0);//结束TI=1,TI==0为假TI = 0;
}void sendString(char *str)
{while(*str != '\0'){sendByte(*str);str++;}
}void main()
{my_UartInit();led1 = 1;while(1){Delay250ms();sendString("hello world!\r\n");}
}void UartHandler() interrupt 4
{static int i = 0;//静态变量 只被初始化一次if(RI)//中断处理函数中,对于接收中断的响应{ RI = 0;//清除接收中断标志位cmd[i] = SBUF;i++;if(strstr(cmd,"open")){led1 = 0;i = 0;memset(cmd,'\0',12);}if(strstr(cmd,"close")){led1 = 1;i = 0;memset(cmd,'\0',12);}}if(TI){}
}