先说说PPM波,从图中可以看出周期为20ms,冠丽控实际是6通控,但预留两通没有使用。通道的高电平区间为0.765~1.6ms低电平时间为0.395ms引导波形的高电平 时间为10.165ms。我的单片机是11.0592MHZ这样算的话, 显示
我的单片机是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;
}