本人是單片機(jī)新手,最近做一個(gè)紅外光通信的小項(xiàng)目,在網(wǎng)上查閱了相關(guān)資料,但其中有幾段看不太懂,向各位大牛們求教。
程序中的注釋不是我加到,是資料中已經(jīng)加號(hào)的
程序片段一
void khz(uchar aa) //是發(fā)射38KHZ的程序
{
for(a=aa;a>0;a--) //這個(gè)for語句可以得到準(zhǔn)確的26.3波特率
{
out=1;
i=7; //低了17us
while(i>0)i--; // 38kHZ
out=0;
//高了9us 17+9=26us 比26.3快一點(diǎn)點(diǎn)
}
}
這個(gè)程序完全看不懂啊,怎么就能產(chǎn)生38Khz的信號(hào)呢?
程序片段二
unsigned char KeyScan(void) //鍵盤掃描函數(shù),使用行列逐級(jí)掃描法
{
unsigned char Val;
KeyPort=0xf0; //P1口高四位置高,低四位置低
if(KeyPort!=0xf0) //表示有按鍵按下
{
DelayMs(10); //延時(shí)去抖
if(KeyPort!=0xf0) //表示真的有按鍵按下
{
KeyPort=0xfe; //檢測(cè)第一行
if(KeyPort!=0xfe)
{
Val=KeyPort&0xf0;
Val+=0x0e;
while(KeyPort!=0xfe);
DelayMs(10); //去抖
while(KeyPort!=0xfe);
return Val;
}
KeyPort=0xfd; //檢測(cè)第二行
if(KeyPort!=0xfd)
{
Val=KeyPort&0xf0;
Val+=0x0d;
while(KeyPort!=0xfd);
DelayMs(10); //去抖
while(KeyPort!=0xfd);
return Val;
}
KeyPort=0xfb; //檢測(cè)第三行
if(KeyPort!=0xfb)
{
Val=KeyPort&0xf0;
Val+=0x0b;
while(KeyPort!=0xfb);
DelayMs(10); //去抖
while(KeyPort!=0xfb);
return Val;
}
KeyPort=0xf7; //檢測(cè)第四行
if(KeyPort!=0xf7)
{
Val=KeyPort&0xf0;
Val+=0x07;
while(KeyPort!=0xf7);
DelayMs(10); //去抖
while(KeyPort!=0xf7);
return Val;
}
}
}
return 0xff;
}
我知道這是一個(gè)鍵盤掃描函數(shù),但我自學(xué)時(shí)看的是郭天祥書上的矩陣按鍵掃描實(shí)例程序,按照郭書上的思路來看,這段鍵盤掃描函數(shù)邏輯好混亂啊,看不懂干啥,請(qǐng)大牛們?cè)敿?xì)說明其工作邏輯流程吧
程序片段三
void delayms1(uchar aa)//延時(shí)程序 { for(a=aa;a>0;a--) { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } } 我知道這是一個(gè)延時(shí)程序,_nop_()表示延時(shí)一個(gè)機(jī)器周期,這兒有八個(gè)_nop_();那就是延時(shí)了吧個(gè)機(jī)器周期,假如晶振用的12M的,這個(gè)函數(shù)就延時(shí)了12*8=96微秒但程序中卻給出了如下參考時(shí)間,與我設(shè)想的不同,那請(qǐng)問這么一個(gè)函數(shù)真實(shí)情況下延時(shí)是多少呢? //delayms(45);//0.642ms //delayms(35);//0.502ms //delayms(115);//1.623ms //delayms(72);//1.02ms //delayms(84);//1.188ms
//delayms(31);//0.446ms 程序片段四 void sieasdf() interrupt 0 { EX0=0; for(a=5;a>0;a--) { delayms(35);//延時(shí)0.5ms 判斷5次 5*0.5=2.5ms if(in) fleg=0; } if(fleg) { delayms(72);//延時(shí)1ms 判斷是不是高電平了 if(in) { delayms(115);//延時(shí)讓它超過2ms; 2.5+1+1.623=5.123ms 開始讀數(shù)據(jù) for(a=8;a>0;a--) { while(!in); delayms(86);//延時(shí)1.188ms 判斷IO高低,從而得0或1 num=num>>1; if(in) { num=num|0x80; delayms(31);//延時(shí)0.6ms 因?yàn)樯厦嫜訒r(shí)1.2ms+0.6 剛好跳過1.5ms } } } } fleg=1; EX0=1; } 這段應(yīng)該是通過外部中斷,來進(jìn)行相關(guān)解碼的功能吧,但為什么連外部中斷0的標(biāo)志位IE0都沒有看見,而且這兒函數(shù)整體的一個(gè)邏輯流程不理解(因?yàn)榇蟛糠终Z句單獨(dú)拎出來看的懂,但組合在一起不知道在干啥)
|