-
Notifications
You must be signed in to change notification settings - Fork 4
/
SUM_PPM.cpp
93 lines (72 loc) · 2.95 KB
/
SUM_PPM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "Config.h"
volatile uint8_t ppmPin = 0xFF; // initialized to invalid pin
volatile int16_t ppmValueArray [CABELL_NUM_CHANNELS];
volatile uint8_t ppmChannelCount;
bool ppmEnabled = false;
//------------------------------------------------------------------------------------------------------------------------
void ppmSetup(uint8_t pin, uint8_t channelCount){
//this program will put out a PPM signal
// from: https://code.google.com/archive/p/generate-ppm-signal/
//////////////////////CONFIGURATION///////////////////////////////
#define PPM_FrLen 22500 //set the PPM frame length in microseconds (1ms = 1000µs)
#define PPM_MaxChannels 8 //The maximum number of channels that can be sent in a frame
#define PPM_PulseLen 300 //set the pulse length
#define onState 1 //set polarity of the pulses: 1 is positive, 0 is negative
//////////////////////////////////////////////////////////////////
ppmPin = pin;
ppmChannelCount = min(PPM_MaxChannels,channelCount);
pinMode(ppmPin, OUTPUT);
digitalWrite(ppmPin, !onState); //set the PPM signal pin to the default state (off)
noInterrupts();
TCCR1A = 0; // set entire TCCR1 register to 0
TCCR1B = 0;
OCR1A = 100; // compare match register, change this
TCCR1B |= (1 << WGM12); // turn on CTC mode
TCCR1B |= (1 << CS11); // 8 prescaler: 0,5 microseconds at 16mhz
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts();
ppmEnabled = true;
}
bool PPMEnabled() {
return ppmEnabled;
}
//------------------------------------------------------------------------------------------------------------------------
void ppmDisable(){
noInterrupts();
TCCR1A = 0; // set entire TCCR1 register to 0
TCCR1B = 0;
TIMSK1 &= ~(1<<OCIE1A); // Disable Interrupt Counter 1, output compare A (TIMER1_CMPA_vect)
interrupts();
ppmEnabled = false;
}
//------------------------------------------------------------------------------------------------------------------------
void setPPMOutputChannelValue(uint8_t channel, int value) {
ppmValueArray[channel] = value;
}
//------------------------------------------------------------------------------------------------------------------------
void SUM_PPM_ISR() {
static boolean state = true;
static byte cur_chan_numb = 0;
static unsigned int calc_rest = 0;
if(state) { //start pulse
digitalWrite(ppmPin, onState);
OCR1A = PPM_PulseLen * 2;
state = false;
}
else{ //end pulse and calculate when to start the next pulse
digitalWrite(ppmPin, !onState);
state = true;
if(cur_chan_numb >= ppmChannelCount){
cur_chan_numb = 0;
calc_rest = calc_rest + PPM_PulseLen;
OCR1A = (PPM_FrLen - calc_rest) * 2;
calc_rest = 0;
}
else{
int16_t ppmValue = constrain(ppmValueArray[cur_chan_numb],CHANNEL_MIN_VALUE,CHANNEL_MAX_VALUE);
OCR1A = (ppmValue - PPM_PulseLen) * 2;
calc_rest = calc_rest + ppmValue;
cur_chan_numb++;
}
}
}