单片机解码PPM信号
![](http://m.amcfsurvey.com/editerupload/fetch/20161101/322110_1_0.jpg)
遥控器发射机、接收机原理
每个通道信号脉宽0~2ms,变化范围为1~2ms之间。1帧PPM信号长度为20ms,理论上最多可以有10个通道,但是同步脉冲也需要时间,模型遥控器最多9个通道。
本文引用地址://m.amcfsurvey.com/article/201611/322110.htm![](http://m.amcfsurvey.com/editerupload/fetch/20161101/322110_1_1.jpg)
PPM格式
![](http://m.amcfsurvey.com/editerupload/fetch/20161101/322110_1_2.jpg)
只连接了通道3(油门)
arduino要测量脉宽时间很简单。有专门的库函数pulseIn( )。问题在于这个库函数使用查询方式,程序在测量期间一直陷在这里,CPU利用率太低。因此下面代码采用中断方式,效率很高。
代码参考:http://arduino.cc/forum/index.php/topic,42286.0.html
ARDUINO 代码复制打印
-
//read PPM signals from 2 channels of an RC reciever
-
//http://arduino.cc/forum/index.php/topic,42286.0.html
-
-
//接收机两个通道分别接arduino的数字口2、3脚
-
//Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3).
-
//The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18).
-
intppm1 =2;
-
intppm2 =3;
-
-
unsignedlongrc1_PulseStartTicks,rc2_PulseStartTicks;
-
volatileintrc1_val, rc2_val;
-
-
voidsetup(){
-
-
Serial.begin(9600); -
-
//PPM inputs from RC receiver -
pinMode(ppm1,INPUT); -
pinMode(ppm2,INPUT); -
-
// 电平变化即触发中断 -
attachInterrupt(0, rc1, CHANGE); -
attachInterrupt(1, rc2, CHANGE); -
}
-
-
voidrc1()
-
{
-
// did the pin change to high or low? -
if(digitalRead(ppm1)==HIGH) -
rc1_PulseStartTicks =micros(); // store the current micros() value -
else -
rc1_val =micros()- rc1_PulseStartTicks; -
}
-
-
voidrc2()
-
{
-
// did the pin change to high or low? -
if(digitalRead(ppm2)==HIGH) -
rc2_PulseStartTicks =micros(); -
else -
rc2_val =micros()- rc2_PulseStartTicks; -
}
-
-
voidloop(){
-
-
//print values -
Serial.print("channel 1: "); -
Serial.print(rc1_val); -
Serial.print(" "); -
Serial.print("channel 2: "); -
Serial.println(rc2_val); -
}
评论