|
這有PC 機(jī)與單片機(jī)通信(RS232 協(xié)議)的資料希望對(duì)大家有用
[C語(yǔ)言源程序]
#include"reg52.h" //包函8051 內(nèi)部資源的定義
unsigned char dat;//用于存儲(chǔ)單片機(jī)接收發(fā)送緩沖寄存器SBUF里面的內(nèi)容
sbit gewei=P2^4;//個(gè)位選通定義
sbit shiwei=P2^5;//十位選通定義
sbit baiwei=P2^6;//百位選通定義
unsigned char codetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,}; //1~10
voidDelay(unsigned int tc) //延時(shí)程序
{
while( tc != 0 )
{unsigned int i;
for(i=0; i<100;i++);
tc--;}
}
void LED() //LED顯示接收到的數(shù)據(jù)(十進(jìn)制)
{
gewei=0;P0=table[dat%10]; Delay(10); gewei=1;
shiwei=0;P0=table[dat/10]; Delay(10); shiwei=1;
baiwei=0;P0=table[dat/100]; Delay(10); baiwei=1;
}
///////功能:串口初始化,波特率9600,方式1/////////
voidInit_Com(void)
{
TMOD = 0x20;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xFd;
TL1 = 0xFd;
TR1 = 1;
}
/////主程序功能:實(shí)現(xiàn)接收數(shù)據(jù)并把接收到的數(shù)據(jù)原樣發(fā)送回去///////
void main()
{
Init_Com();//串口初始化
while(1)
{
if ( RI ) //掃描判斷是否接收到數(shù)據(jù),
{
dat = SBUF; //接收數(shù)據(jù)SBUF賦與dat
RI=0; //RI 清零。
SBUF = dat; //在原樣把數(shù)據(jù)發(fā)送回去(接收數(shù)據(jù)為發(fā)送數(shù)據(jù)的ASCII碼,如發(fā)送q顯示為113)
}
LED(); //顯示接收到的數(shù)據(jù)
}
}
///這一個(gè)例子是以掃描的方式編寫的,還可以以中斷的方式編寫,請(qǐng)大家思考////// |
評(píng)分
-
查看全部評(píng)分
|