先說(shuō)說(shuō)PPM波,從圖中可以看出周期為20ms,冠麗控實(shí)際是6通控,但預(yù)留兩通沒(méi)有使用。通道的高電平區(qū)間為0.765~1.6ms低電平時(shí)間為0.395ms引導(dǎo)波形的高電平 時(shí)間為10.165ms。我的單片機(jī)是11.0592MHZ這樣算的話, 顯示
我的單片機(jī)是11.0592MHZ這樣算的話, 顯示的值 應(yīng)該是706到1491之間,中立位為1100正好和理論值相符。這里在提點(diǎn)單片機(jī)相關(guān)的知識(shí),51單片機(jī)是低電平觸 發(fā)中斷,我這里用的是while(int0==0)來(lái)等待高電平 的到來(lái)。不知道這樣做是否合理,不過(guò)實(shí)踐看來(lái)還是可以的。
明說(shuō)一點(diǎn),程序中LCD* 都在1602.h文件中,自己加個(gè)1602的頭文件即可。
下面是程序的主要部分
#include
#include “1602.h”
unsigned char channel=0,i=0;
unsigned int xdata PPM_channel1[10],PPM_channel2[10],PPM_channel3[10],PPM_channel4[10],PPM_channel5[10],PPM_channel6[10];
bit Timer0_OverFlowFlag=0;//定時(shí)器0的溢出標(biāo)志,如果溢出,則有問(wèn)題;
unsigned char qian,bai,shi,ge;
void DepartNum(unsigned int temp) ;
unsigned int filter(unsigned int *s); //這不函數(shù)的作用?
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i{
for(j=0;j《121;j++)
{;}
}
}
void main(void)
{
unsigned char *InitIform;
unsigned int temp;
InitIform=“PPM Decoding”;
delay(500);
LCD_init(8); //在1602.h中
LCD_Write_String(3,0,InitIform); //在1602.h中
delay(800);
TMOD=0x01; //timer0 方式1
ET0=1; //允許timer0中斷
//初始化INT0
IT0=1; //負(fù)跳變觸發(fā)中斷;
EX0=1;//外中斷 0中斷允許
EA=1; //總中斷
// DelayMs(10000);
while(1)
{
temp=filter(PPM_channel1);//數(shù)組
DepartNum(temp);
LCD_Write_Char(0,0,‘ ’); //在1602.h中
LCD_Write_Char(1,0,qian);
LCD_Write_Char(2,0,bai);
LCD_Write_Char(3,0,shi);
LCD_Write_Char(4,0,ge);
temp=filter(PPM_channel2);
DepartNum(temp);
LCD_Write_Char(5,0,‘ ’);
LCD_Write_Char(6,0,qian);
LCD_Write_Char(7,0,bai);
LCD_Write_Char(8,0,shi);
LCD_Write_Char(9,0,ge);
temp=filter(PPM_channel3);
DepartNum(temp);
LCD_Write_Char(10,0,‘ ’);
LCD_Write_Char(11,0,qian);
LCD_Write_Char(12,0,bai);
LCD_Write_Char(13,0,shi);
LCD_Write_Char(14,0,ge);
temp=filter(PPM_channel4);
DepartNum(temp);
LCD_Write_Char(0,1,‘ ’);
LCD_Write_Char(1,1,qian);
LCD_Write_Char(2,1,bai);
LCD_Write_Char(3,1,shi);
LCD_Write_Char(4,1,ge);
temp=filter(PPM_channel5);
DepartNum(temp);
LCD_Write_Char(5,1,‘ ’);
LCD_Write_Char(6,1,qian);
LCD_Write_Char(7,1,bai);
LCD_Write_Char(8,1,shi);
LCD_Write_Char(9,1,ge);
temp=filter(PPM_channel6);
DepartNum(temp);
LCD_Write_Char(10,1,‘ ’);
LCD_Write_Char(11,1,qian);
LCD_Write_Char(12,1,bai);
LCD_Write_Char(13,1,shi);
LCD_Write_Char(14,1,ge);
}
}
/****************************************
用于顯示的數(shù)位分解函數(shù),把數(shù)據(jù)轉(zhuǎn)化為ASCII碼
入口參數(shù)是要顯示的數(shù)據(jù)
****************************************/
void DepartNum(unsigned int temp)
{
qian=temp/1000+0x30;
bai=temp%1000/100+0x30;
shi=temp%100/10+0x30;
ge= temp%10+0x30;
}
void ISIR_INT0(void) interrupt 0 //interrupt 0 指明是外部中斷0; 主要在中斷函數(shù)這一塊。
{
unsigned int PPM_temp=0;
if(TR0)
{
TR0=0;//停止計(jì)數(shù); //TR0置1時(shí),T1開始工作;TR0置0時(shí),T1停止工作
PPM_temp=TH0;
PPM_temp=(PPM_temp《《8)“TL0;
while(INT0==0); //等待到高電平
TH0=0;
TL0=0;
TR0=1;//給TH0和TL0賦初值后重新啟動(dòng)定時(shí)器
}
else
{
while(INT0==0); //等待到高電平
TH0=0;//如果是第一次啟動(dòng)中斷,則啟動(dòng)timer0
TL0=0;
TR0=1; //啟動(dòng)計(jì)時(shí)器
}
if(PPM_temp》3000|| Timer0_OverFlowFlag)//判斷引導(dǎo)區(qū)
{
channel=0;
Timer0_OverFlowFlag=0;
i++;
if(i==10) i=0;
}
switch(channel) //當(dāng)?shù)谝淮螁?dòng)里,channel的值為0 ;
{
case 1: PPM_channel1[i]=PPM_temp; break;
case 2: PPM_channel2[i]=PPM_temp; break;
case 3: PPM_channel3[i]=PPM_temp; break;
case 4: PPM_channel4[i]=PPM_temp; break;
case 5: PPM_channel5[i]=PPM_temp; break;
case 6: PPM_channel6[i]=PPM_temp; break;
case 7: break;
case 8: break;
default:break;
}
channel++;
}
void ISIR_Timer0(void) interrupt 1 //interrupt 1 指明是定時(shí)器中斷0;
{
Timer0_OverFlowFlag=1; //timer0最大值是65536us,理論上不會(huì)溢出,如果溢出則出錯(cuò),重新從通道1開始
}
unsigned int filter(unsigned int *s) //這個(gè)函數(shù)是先排序后進(jìn)行求平均值 (直接求平均值即可)。
{
unsigned char k,j;
unsigned int sum=0;
/*unsigned int temp;
for(k=0;k《9;k++)
{
for(j=k+1;j《10;j++)
{
if(s[k]》s[j])
{
temp=s[k];
s[k]=s[j];
s[j]=temp;
}
}
}
*/
//到這里是形成一個(gè)從小到在的數(shù)組
for(k=1;k《9;k++)
{//這里既然求平均值 ,為何還要排列大小 呢?
sum+=s[k];//我覺(jué)得這個(gè)方法完全沒(méi)有必要。
}
sum=sum/8;
return sum;
}
責(zé)任編輯;zl
評(píng)論
查看更多