Skip to content

Instantly share code, notes, and snippets.

@fisher0251
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fisher0251/a070dac6a090f2dec0a8 to your computer and use it in GitHub Desktop.
Save fisher0251/a070dac6a090f2dec0a8 to your computer and use it in GitHub Desktop.
// THIS IS A CODE SNIPPET ONLY AND WILL NOT RUN AT ALL ON ITS OWN!!
// Variables declared globally for interrupt use
// Start of channel interval
volatile uint16_t ch1_start = 0;
volatile uint16_t ch2_start = 0;
volatile uint16_t ch3_start = 0;
volatile uint16_t ch4_start = 0;
volatile uint16_t ch5_start = 0;
volatile uint16_t ch6_start = 0;
// State of channel (high 1 or low 0)
volatile uint8_t ch1_state = 0;
volatile uint8_t ch2_state = 0;
volatile uint8_t ch3_state = 0;
volatile uint8_t ch4_state = 0;
volatile uint8_t ch5_state = 0;
volatile uint8_t ch6_state = 0;
// Stick positions in us, initialized to neutral stick positions
volatile int16_t ch1 = 1500;
volatile int16_t ch2 = 1500;
volatile int16_t ch3 = 1500;
volatile int16_t ch4 = 1500;
volatile int16_t ch5 = 1500;
volatile int16_t ch6 = 1500;
// Flag is set after each cycle of RC pulses
volatile uint8_t start_flag = 0;
// THIS IS ONLY A FRAGMENT OF THE WORKING main()!!
int main(void) {
// Setup Timer/Counter1 in normal mode with prescaler of 8
// At F_CPU = 16MHz, F_TIMER1 = 2MHz, and T_TIMER1 = 0.5 us
TCCR1B |= (1 << CS11);
// Enable external pin change interrupt 1 (PCINT1) for all 6 RC channels
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT8) | (1 << PCINT9) | (1 << PCINT10) | (1 << PCINT11) | (1 << PCINT12) | (1 << PCINT13);
sei(); // Set the Global Interrupt Enable flag so that interrupts can be processed
// Loop forever
while(1) {
// Only update motor speeds after each full cycle of RC pulses
if(start_flag == 1) {
// CODE HERE WOULD USE THE RC CHANNEL VALUES TO UPDATE MOTOR SPEEDS AND DIRECTIONS
start_flag = 0; // Reset flag after speeds have updated
}
}
}
ISR (PCINT1_vect) {
// Read six RC channels on PC0/PCINT8 (Ch1) to PC5/PCINT13 (Ch6)
// Check to see if it is a rising edge for each channel to start timing
// Falling edge to stop timing
uint16_t currentTime = TCNT1 / 2; // Read Timer/Counter1 in microseconds
if(PINC & (1 << PORTC0)) { // Channel 1
if(ch1_state == 0) {
TCNT1 = 0; // Reset timer at start of ch1 only
ch1_start = 0;
ch1_state = 1;
start_flag = 1; // Set flag to update motor speeds & directions
}
} else if((PINC & (1 << PORTC0)) == 0) {
if(ch1_state == 1) {
ch1 = currentTime - ch1_start;
ch1_state = 0;
}
}
if(PINC & (1 << PORTC1)) { //Channel 2
if(ch2_state == 0) {
ch2_start = currentTime;
ch2_state = 1;
}
} else if((PINC & (1 << PORTC1)) == 0) {
if(ch2_state == 1) {
ch2 = currentTime - ch2_start;
ch2_state = 0;
}
}
if(PINC & (1 << PORTC2)) { //Channel 3
if(ch3_state == 0) {
ch3_start = currentTime;
ch3_state = 1;
}
} else if((PINC & (1 << PORTC2)) == 0) {
if(ch3_state == 1) {
ch3 = currentTime - ch3_start;
ch3_state = 0;
}
}
if(PINC & (1 << PORTC3)) { //Channel 4
if(ch4_state == 0) {
ch4_start = currentTime;
ch4_state = 1;
}
} else if((PINC & (1 << PORTC3)) == 0) {
if(ch4_state == 1) {
ch4 = currentTime - ch4_start;
ch4_state = 0;
}
}
if(PINC & (1 << PORTC4)) { //Channel 5
if(ch5_state == 0) {
ch5_start = currentTime;
ch5_state = 1;
}
} else if((PINC & (1 << PORTC4)) == 0) {
if(ch5_state == 1) {
ch5 = currentTime - ch5_start;
ch5_state = 0;
}
}
if(PINC & (1 << PORTC5)) { //Channel 6
if(ch6_state == 0) {
ch6_start = currentTime;
ch6_state = 1;
}
} else if((PINC & (1 << PORTC5)) == 0) {
if(ch6_state == 1) {
ch6 = currentTime - ch6_start;
ch6_state = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment