先说说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;
}