Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define icpPin 8 // this interrupt handler must use pin 8
- #define TICKS_PER_uS 2 // number of timer ticks per microsecond
- #define MAX_CHANNELS 8 // maximum number of channels we can store
- #define SYNC_GAP_LEN (400 * TICKS_PER_uS) // we assume a space at least 3000us is sync (note clock counts in 0.5 us ticks)
- volatile unsigned int Pulses[ MAX_CHANNELS + 1]; // array holding channel pulses width value in microseconds
- volatile uint8_t Channel; // number of channels detected so far in the frame (first channel is 1)
- volatile uint8_t State; // this will be one of the following states:
- #define NOT_SYNCHED_state 0 // the system is not synched so the data is random
- #define ACQUIRING_state 1 // one sync pulse detected but not all channels have been received
- #define READY_state 2 // synched and all channel data is valid
- ISR(TIMER1_CAPT_vect){
- if(! bit_is_set(TCCR1B ,ICES1)){ // was falling edge detected ?
- TCNT1 = 0; // reset the counter
- if(Channel <= MAX_CHANNELS) {
- Pulses[Channel++] = ICR1 / TICKS_PER_uS; // store pulse length as microsoeconds
- }
- }
- else { // rising edge was detected
- TCNT1 = 0; // reset the counter
- if(ICR1 >= SYNC_GAP_LEN){ // is the space between pulses big enough to be the SYNC
- Channel = 1; // if so, reset the channel counter to 1
- if(State == NOT_SYNCHED_state)
- State = ACQUIRING_state; // this is the first sync pulse, we need one more to fill the channel data array
- else if( State == ACQUIRING_state)
- State = READY_state; // this is the second sync so flag that channel data is valid
- }
- }
- TCCR1B ^= _BV(ICES1); // toggle bit value to trigger on the other edge
- }
- void setup() // run once, when the sketch starts
- {
- Serial.begin(9600);
- pinMode(icpPin,INPUT);
- Channel = 1;
- State = NOT_SYNCHED_state;
- TCCR1A = 0x00; // COM1A1=0, COM1A0=0 => Disconnect Pin OC1 from Timer/Counter 1 -- PWM11=0,PWM10=0 => PWM Operation disabled
- TCCR1B = 0x02; // 16MHz clock with prescaler means TCNT1 increments every .5 uS (cs11 bit set
- TIMSK1 = _BV(ICIE1); // enable input capture interrupt for timer 1
- }
- int GetChannelPulseWidth( uint8_t channel) {
- // this is the access function for channel data
- int result;
- if( (State == READY_state) && (channel > 0) && (channel <= MAX_CHANNELS) ) {
- cli(); //disable interrupts
- result = Pulses[channel] ;
- sei(); // enable interrupts
- }
- else
- result = 0; // return 0 if no valid pulse is available
- return result;
- }
- void loop() // run over and over again
- {
- int pulsewidth;
- /*
- // print the decoder state
- if(State == NOT_SYNCHED_state)
- // Serial.println("The decoder has not detected a synch pulse ");
- else if ( State == ACQUIRING_state)
- // Serial.println("The decoder has detected one synch pulse and has started filling channel data");
- else if( State == READY_state)
- // Serial.println("The decoder is synched and the channel data is valid");
- else
- Serial.println("Unknown decoder state, this should never happen!");
- */
- // now print the channel pulse widths
- // they should be 0 if the state is not ready
- // for ( int i =1; i <=4; i++ ){ // print the status of the first four channels
- Serial.print("Channel ");
- Serial.print(1);
- Serial.print(" has width ");
- pulsewidth = GetChannelPulseWidth(1);
- Serial.println(pulsewidth);
- // }
- delay(100); // update 10 times a second
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement