void putchar(char ch){WByte(ch);}BYTE RByte(){BYTE in=0;BYTE cnt;while(RX1==1);//等待RXD變低,啟動(dòng)定時(shí)器,這個(gè)是阻塞模式TR1=1;//同步開(kāi)定時(shí)器//這里……//while(!TF1);//TF1=0; WaitTF1();if(min_mode !=0){while(!TF1);//注意這里的周期稍微長(zhǎng)。要補(bǔ)償TF1=0;}for(cnt=0;cnt<8;cnt++){in=in >>1;//從高移到低if(RX1==1) in = in | 0x80;//如果RXD=1,則最高置位WaitTF1();//等待一位過(guò)去}while(!TF1);//注意這里的周期稍微長(zhǎng)。要補(bǔ)償TF1=0;TR1=0;//關(guān)閉定時(shí)器return in;}UINT f_Test(void)//測(cè)試脈寬{TMOD=0x10;//設(shè)置計(jì)數(shù)器1為方式一計(jì)數(shù)器模式TH1=0;TL1=0;//定時(shí)器CLRwhile(!RX1);//等待頻率腳變高,這個(gè)是測(cè)低電平的while(RX1);//等待腳變低,更換符號(hào)可以測(cè)正脈沖TR1=1;//開(kāi)啟定時(shí)器while(!RX1);//等待變高TR1=0;//停止計(jì)數(shù)//cyc=TH0<<8;//cyc=cyc+TL0;return (TH1<<8)+TL1;}void baud_t(){BYTE k;//復(fù)用變量 ULONG frq=0; //周期變量for(k=0;k<5;k++)// 變量復(fù)用大法{frq=frq+f_Test();//測(cè)試}//測(cè)量5次取平均frq=frq/5;if(frq<0xff){k=0x100-(frq&0xff);min_mode=MIS_0;}else{if(frq / 2 < 0xff){//2400baudk=0x100-((frq/2)&0xff); //2分頻min_mode=MIS_2;}else if(frq / 4 < 0xff){//1200baudk=0x100-((frq/4)&0xff);//4分頻min_mode=MIS_4;}else if(frq / 8 < 0xff){//1200baudk=0x100-((frq/8)&0xff);//8分頻min_mode=MIS_8;}else if(frq / 16 < 0xff){//1200baudk=0x100-((frq/16)&0xff);//16分頻min_mode=MIS_16;}}if(k > 0x50){k=k+6;//加補(bǔ)償,因?yàn)閕f語(yǔ)句讓機(jī)器周期加長(zhǎng)//如果對(duì)于STC的新MCU,這里要按照情況調(diào)整}TMOD=0x20;//設(shè)置定時(shí)器1為自動(dòng)裝載模式TH1=k;//載入新波特率TL1=k;}void main(){while(1){baud_t();//測(cè)量波特率,阻塞模式printf("Hello world!");printf("Here:mode=%d,T1=0x%X",min_mode,TH1);printf("Could you please test another baudrate?");printf("But I think that I couldnt to do.....");}}完整的源代碼下載:http://www.51hei.com/f/molic.rar
轉(zhuǎn)自:萬(wàn)致遠(yuǎn)的博客http://www.rwzy.co.cc
本文引用地址:http://m.butianyuan.cn/article/201611/323397.htm順便說(shuō)一下,根據(jù)重載值計(jì)算波特率的公式是:
R=重載值
Clock=系統(tǒng)時(shí)鐘(HZ)
B=波特率
Clock=12(256-R)*B
評(píng)論