Advertisement
Guest User

Untitled

a guest
Mar 9th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. #define icpPin            8         // this interrupt handler must use pin 8
  2. #define TICKS_PER_uS      2          // number of timer ticks per microsecond
  3. #define MAX_CHANNELS    8         // maximum number of channels we can store  
  4. #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)
  5. volatile unsigned int Pulses[ MAX_CHANNELS + 1]; // array holding channel pulses width value in microseconds
  6. volatile uint8_t  Channel;      // number of channels detected so far in the frame (first channel is 1)
  7. volatile uint8_t State;         // this will be one of the following states:
  8. #define NOT_SYNCHED_state  0    // the system is not synched so the data is random
  9. #define ACQUIRING_state  1      // one sync pulse detected but not all channels have been received
  10. #define READY_state     2       // synched and all channel data is valid
  11.  
  12.                    
  13. ISR(TIMER1_CAPT_vect){
  14.    if(! bit_is_set(TCCR1B ,ICES1)){       // was falling edge detected ?  
  15.        TCNT1 = 0;               // reset the counter      
  16.        if(Channel <= MAX_CHANNELS) {
  17.            Pulses[Channel++] = ICR1 / TICKS_PER_uS;  // store pulse length as microsoeconds
  18.         }      
  19.    }
  20.    else {                          // rising  edge was detected  
  21.         TCNT1 = 0;               // reset the counter      
  22.         if(ICR1 >= SYNC_GAP_LEN){   // is the space between pulses big enough to be the SYNC
  23.             Channel = 1;       // if so, reset the channel counter to 1      
  24.               if(State == NOT_SYNCHED_state)
  25.                   State = ACQUIRING_state;        // this is the first sync pulse, we need one more to fill the channel data array
  26.               else if( State == ACQUIRING_state)    
  27.                    State = READY_state;           // this is the second sync so flag that channel data is valid
  28.         }    
  29.    }    
  30.    TCCR1B ^= _BV(ICES1);                 // toggle bit value to trigger on the other edge    
  31. }
  32.  
  33. void setup()                    // run once, when the sketch starts
  34. {
  35.   Serial.begin(9600);  
  36.   pinMode(icpPin,INPUT);
  37.   Channel = 1;            
  38.   State = NOT_SYNCHED_state;
  39.   TCCR1A = 0x00;         // COM1A1=0, COM1A0=0 => Disconnect Pin OC1 from Timer/Counter 1 -- PWM11=0,PWM10=0 => PWM Operation disabled
  40.   TCCR1B = 0x02;         // 16MHz clock with prescaler means TCNT1 increments every .5 uS (cs11 bit set
  41.   TIMSK1 = _BV(ICIE1);   // enable input capture interrupt for timer 1
  42. }
  43.  
  44. int GetChannelPulseWidth( uint8_t channel) {
  45.   // this is the access function for channel data
  46.   int result;  
  47.   if( (State == READY_state)  && (channel > 0) && (channel <=  MAX_CHANNELS)  ) {
  48.      cli();             //disable interrupts
  49.      result =  Pulses[channel] ;
  50.      sei();             // enable interrupts
  51.   }
  52.   else
  53.      result = 0;        // return 0 if no valid pulse is available  
  54.  
  55.   return result;
  56.  
  57. }
  58.  
  59. void loop()                     // run over and over again
  60. {
  61. int pulsewidth;
  62.  
  63. /*
  64.    // print the decoder state
  65.    if(State == NOT_SYNCHED_state)
  66.    //    Serial.println("The decoder has not detected a synch pulse ");  
  67.    else if ( State == ACQUIRING_state)
  68.   //     Serial.println("The decoder has detected one synch pulse and has started filling channel data");  
  69.    else if( State == READY_state)
  70.     // Serial.println("The decoder is synched and the channel data is valid");  
  71.    else
  72.      Serial.println("Unknown decoder state, this should never happen!");
  73.   */
  74.  
  75.   // now print the channel pulse widths
  76.   // they should be 0 if the state is not ready
  77.  // for ( int i =1; i <=4; i++ ){ // print the status of the first four channels
  78.       Serial.print("Channel ");
  79.       Serial.print(1);
  80.       Serial.print(" has width ");
  81.       pulsewidth = GetChannelPulseWidth(1);
  82.       Serial.println(pulsewidth);
  83.  // }
  84.   delay(100); // update 10 times a second        
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement