51单片机~串口通信
(一)计算机串行通信原理:
综上:所以在设置的时候,经常将SCON设置为0X50==0101 0000 ,使用方式1.
(二)工作:
中断控制 led灯,每发送一条指令灯亮或灭(反转一下)
#include"reg52.h"
typedef signed char int8;
typedef signed int int16;
typedef signed long int32;
typedef unsigned char uint8; //字符型
typedef unsigned int uint16;
typedef unsigned long uint32;
sbit led=P2^0;
void InitUART(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xF3;
TL1 = TH1;
PCON = 0x80;
EA = 1;
ES = 1;
TR1 = 1;
}
void SendOneByte(unsigned char c)
{
SBUF = c;
while(!TI);
TI = 0;
}
void main(void)
{
InitUART();
}
void UARTInterrupt(void) interrupt 4
{
if(RI)
{
RI = 0;
//add your code here!
led=~led;
}
else
TI = 0;
}