再次發(fā)模擬串口,51自適應(yīng)波特率
順便說(shuō)一句,這個(gè)程序也有另外一個(gè)用途,在晶振不準(zhǔn)的時(shí)候仍然可以和電腦以標(biāo)準(zhǔn)波特率通訊。方法就是規(guī)定好幀格式,然后上位機(jī)不停換波特率找到?jīng)]有誤碼率的波特率,然后根據(jù)下位機(jī)的應(yīng)答計(jì)算出下位機(jī)的時(shí)鐘頻率,這個(gè)是受了老妖ISP的啟發(fā)才想到的~大家可以參考我發(fā)布的另2篇文章:模擬串口自動(dòng)測(cè)量波特率的單片機(jī)程序http://www.51hei.com/mcu/1537.html,下面這個(gè)網(wǎng)頁(yè)是用11.0592兆的晶振模擬串口接收發(fā)發(fā)送的已經(jīng)通過(guò)本人測(cè)試http://www.51hei.com/mcu/1418.html
本文引用地址:http://m.butianyuan.cn/article/201611/323397.htm上代碼:
/** 自適應(yīng)波特率模擬串口程序,* BY 萬(wàn)致遠(yuǎn)@rwzy.co.cc* CRYSTAL:任意*/#include#include #include #define MIS_0 0#define MIS_2 1#define MIS_4 2#define MIS_8 3 #define MIS_16 4 BYTE min_mode;//減倍模式void WaitTF1(){while(!TF1);TF1=0;if(min_mode==MIS_2){// /2while(!TF1);TF1=0;}else if(min_mode == MIS_4){// /4while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;}else if(min_mode == MIS_8){// /8while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;}else if(min_mode == MIS_16){// /16while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;while(!TF1);TF1=0;}}void WByte(BYTE out){//發(fā)送啟始位BYTE i=8;BYTE tmp=out;TR1=1;//開(kāi)定時(shí)器TX1=0;WaitTF1();//發(fā)送8位數(shù)據(jù)位while(i--){TX1=(tmp&0x01); //先傳低位tmp=tmp>>1;WaitTF1();}//發(fā)送校驗(yàn)位(無(wú))//發(fā)送結(jié)束位TX1=1;WaitTF1();TR1=0;}
評(píng)論