Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. ISR(TIMER1_CAPT_vect){
  2.  
  3.   if(isrbusy)uart_sendl("ISR collision"); // We never see this
  4.   isrbusy = true;
  5.  
  6.   // Flip looking for rising or falling edges
  7.   // FIXME: This can be greatly abbreviated
  8.  
  9.   if(TCCR1B & (1<<ICES1)){
  10.     TCCR1B &= ~(1<<ICES1);
  11.   } else {
  12.     TCCR1B |= (1<<ICES1);
  13.   }
  14.  
  15.   // Figure out time since last edge
  16.  
  17.   uint16_t this_edge_duration;
  18.  
  19.   if(ICR1 > ltc_last_edge_time){
  20.     this_edge_duration = ICR1 - ltc_last_edge_time;
  21.   } else {
  22.     ltc_timer_overflows++;
  23.     this_edge_duration = ICR1 + (0xFFFFUL - ltc_last_edge_time);
  24.   }
  25.  
  26.   // Record the time of this edge
  27.  
  28.   ltc_last_edge_time = ICR1;
  29.  
  30.   // Figure out whether this was a long or short bitperiod
  31.   // and if it was short, figure out if it's the second short
  32.   // one in a row. Push the result to the buffer.
  33.  
  34.   if(this_edge_duration>ltc_bitperiod_threshold){
  35.     // It's a zero. Zero the last bit in the buffer.
  36.     wptr[9] &= ~(0x80);
  37.     if(ltc_short_wait_flag==true){
  38.       // This really never fires, other than maybe once
  39.       ltc_short_wait_flag = false;
  40.     }
  41.   } else {
  42.     // It's possibly part of a 1
  43.     if(ltc_short_wait_flag){
  44.       // It's a one. Set the last bit in the buffer.
  45.       wptr[9] |= 0x80;
  46.       ltc_short_wait_flag = false;
  47.     } else {
  48.       // It's presmably the beginning of a one
  49.       ltc_short_wait_flag = true;
  50.       isrbusy = false;
  51.       return;
  52.     }
  53.   }
  54.  
  55.   // Check to see if we've found a full frame
  56.  
  57.   if(wptr[8] == ltc_magic_number_A_rev && wptr[9] == ltc_magic_number_B_rev){
  58.  
  59.     // Calculate the precision frame duration
  60.  
  61.     ltc_frametime = 0xFFFFUL - ltc_frame_start_time;
  62.     ltc_frametime += 0xFFFFUL * (ltc_timer_overflows-1);
  63.     ltc_frametime += ICR1;
  64.    
  65.     uart_sendl(ltc_frametime);
  66.     glitches = 0;
  67.     ltc_frametime = 0;
  68.     ltc_frame_start_time = ICR1;
  69.     ltc_timer_overflows = 0;
  70.     //ltc_decode_frame();
  71.    
  72.     // Swap buffers
  73.  
  74.     if (rptr == ltc_buf){
  75.       rptr = ltc_buf2;
  76.       wptr = ltc_buf;
  77.     } else {
  78.       rptr = ltc_buf;
  79.       wptr = ltc_buf2;
  80.     }
  81.  
  82.   }
  83.  
  84.   // Shuffle the buffer
  85.  
  86.   for(uint8_t i = 0; i < 10; i++){ // Remember we made an extra byte on the end we don't need?
  87.     wptr[i] = (wptr[i] >> 1) | (wptr[i+1] << 7);
  88.   }
  89.   isrbusy= false;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement