再次發(fā)模擬串口,51自適應波特率
順便說一句,這個程序也有另外一個用途,在晶振不準的時候仍然可以和電腦以標準波特率通訊。方法就是規(guī)定好幀格式,然后上位機不停換波特率找到?jīng)]有誤碼率的波特率,然后根據(jù)下位機的應答計算出下位機的時鐘頻率,這個是受了老妖ISP的啟發(fā)才想到的~大家可以參考我發(fā)布的另2篇文章:模擬串口自動測量波特率的單片機程序http://www.51hei.com/mcu/1537.html,下面這個網(wǎng)頁是用11.0592兆的晶振模擬串口接收發(fā)發(fā)送的已經(jīng)通過本人測試http://www.51hei.com/mcu/1418.html
本文引用地址:http://www.ex-cimer.com/article/201611/323397.htm上代碼:
/** 自適應波特率模擬串口程序,* BY 萬致遠@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;//開定時器TX1=0;WaitTF1();//發(fā)送8位數(shù)據(jù)位while(i--){TX1=(tmp&0x01); //先傳低位tmp=tmp>>1;WaitTF1();}//發(fā)送校驗位(無)//發(fā)送結束位TX1=1;WaitTF1();TR1=0;}
評論