先說說PPM波,從圖上可以看出周期為20ms,冠麗控實際是6通控,但預留兩通沒有使用。通道的高電平區間為0.765~1.6ms 低電平時間為0.395ms引導波形的高電平 時間為10.165ms。
我的單片機是 11.0592MHZ 這樣算的話, 顯示的值 應該是 706 到 1491 之間,中立位為 1100正好和理論值相符。這里在提點單片機相關的知識,51單片機是低電平觸 發中斷,我這里用的是while(int0==0)來等待高電平 的到來。不知道這樣做是否合理,不過實踐看來還是可以的。
明說一點,程序中 LCD* 都在1602.h文件中,自己加個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;//定時器0的溢出標志,如果溢出,則有問題;
unsigned char qian,bai,shi,ge;
void DepartNum(unsigned int temp) ;
unsigned int filter(unsigned int *s); //這不函數的作用?
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; //負跳變觸發中斷;
EX0=1;//外中斷 0中斷允許
EA=1; //總中斷
// DelayMs(10000);
while(1)
{
temp=filter(PPM_channel1);//數組
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);
}
}
/****************************************
用于顯示的數位分解函數,把數據轉化為ASCII碼
入口參數是要顯示的數據
****************************************/
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; 主要在中斷函數這一塊。
{
unsigned int PPM_temp=0;
if(TR0)
{
TR0=0;//停止計數; //TR0置1時,T1開始工作;TR0置0時,T1停止工作
PPM_temp=TH0;
PPM_temp=(PPM_temp《《8)“TL0;
while(INT0==0); //等待到高電平
TH0=0;
TL0=0;
TR0=1;//給TH0和TL0賦初值后重新啟動定時器
}
else
{
while(INT0==0); //等待到高電平
TH0=0;//如果是第一次啟動中斷,則啟動timer0
TL0=0;
TR0=1; //啟動計時器
}
if(PPM_temp》3000|| Timer0_OverFlowFlag)//判斷引導區
{
channel=0;
Timer0_OverFlowFlag=0;
i++;
if(i==10) i=0;
}
switch(channel) //當第一次啟動里,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 指明是定時器中斷0;
{
Timer0_OverFlowFlag=1; //timer0最大值是65536us,理論上不會溢出,如果溢出則出錯,重新從通道1開始
}
unsigned int filter(unsigned int *s) //這個函數是先排序后進行求平均值 (直接求平均值即可)。
{
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;
}
}
}
*/
//到這里是形成一個從小到在的數組
for(k=1;k《9;k++)
{//這里既然求平均值 ,為何還要排列大小 呢?
sum+=s[k];//我覺得這個方法完全沒有必要。
}
sum=sum/8;
return sum;
}
評論
查看更多