單片機(jī)解碼PPM信號(hào)
遙控器發(fā)射機(jī)、接收機(jī)原理
每個(gè)通道信號(hào)脈寬0~2ms,變化范圍為1~2ms之間。1幀PPM信號(hào)長(zhǎng)度為20ms,理論上最多可以有10個(gè)通道,但是同步脈沖也需要時(shí)間,模型遙控器最多9個(gè)通道。
本文引用地址:http://m.butianyuan.cn/article/201611/322110.htmPPM格式
只連接了通道3(油門(mén))
arduino要測(cè)量脈寬時(shí)間很簡(jiǎn)單。有專門(mén)的庫(kù)函數(shù)pulseIn( )。問(wèn)題在于這個(gè)庫(kù)函數(shù)使用查詢方式,程序在測(cè)量期間一直陷在這里,CPU利用率太低。因此下面代碼采用中斷方式,效率很高。
代碼參考:http://arduino.cc/forum/index.php/topic,42286.0.html
ARDUINO 代碼復(fù)制打印
- //read PPM signals from 2 channels of an RC reciever
- //http://arduino.cc/forum/index.php/topic,42286.0.html
- //接收機(jī)兩個(gè)通道分別接arduino的數(shù)字口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); // 電平變化即觸發(fā)中斷 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); - }
評(píng)論