① PC機與單片機之間的串口通信的程序
#include<reg52.h>
#define uchar unsigned char
void main()
{
uchar send=234;
TMOD=0X20;
SCON=0X40;
TL1=0XF3;
TH1=0XF3;
PCON=0X80;
TR1=1;
EA=1;
ES=1;
SBUF=send;
while(!TI)
TI=0;
while(1);
}
#include<reg52.h>
#define uchar unsigned char
sbit sda=P2^1;
sbit clock=P2^0;
uchar recive;
void disp();
void delay10(del);
void main()
{
TMOD=0X20;
clock=1;
TL1=0XF3;
TH1=0XF3;
PCON=0X80;
TR1=1;
EA=1;
ES=1;
SCON=0X50;
while (1)
{
while(1)
}
}
/************中斷接收*******************/
void send_rev() interrupt 4 using 2
{
while (!RI);
RI=0;
recive=SBUF;
}
最簡單的串口發送接收程序,接收你也可以用查詢方式。