Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ISR(TIMER1_CAPT_vect){
- if(isrbusy)uart_sendl("ISR collision"); // We never see this
- isrbusy = true;
- // Flip looking for rising or falling edges
- // FIXME: This can be greatly abbreviated
- if(TCCR1B & (1<<ICES1)){
- TCCR1B &= ~(1<<ICES1);
- } else {
- TCCR1B |= (1<<ICES1);
- }
- // Figure out time since last edge
- uint16_t this_edge_duration;
- if(ICR1 > ltc_last_edge_time){
- this_edge_duration = ICR1 - ltc_last_edge_time;
- } else {
- ltc_timer_overflows++;
- this_edge_duration = ICR1 + (0xFFFFUL - ltc_last_edge_time);
- }
- // Record the time of this edge
- ltc_last_edge_time = ICR1;
- // Figure out whether this was a long or short bitperiod
- // and if it was short, figure out if it's the second short
- // one in a row. Push the result to the buffer.
- if(this_edge_duration>ltc_bitperiod_threshold){
- // It's a zero. Zero the last bit in the buffer.
- wptr[9] &= ~(0x80);
- if(ltc_short_wait_flag==true){
- // This really never fires, other than maybe once
- ltc_short_wait_flag = false;
- }
- } else {
- // It's possibly part of a 1
- if(ltc_short_wait_flag){
- // It's a one. Set the last bit in the buffer.
- wptr[9] |= 0x80;
- ltc_short_wait_flag = false;
- } else {
- // It's presmably the beginning of a one
- ltc_short_wait_flag = true;
- isrbusy = false;
- return;
- }
- }
- // Check to see if we've found a full frame
- if(wptr[8] == ltc_magic_number_A_rev && wptr[9] == ltc_magic_number_B_rev){
- // Calculate the precision frame duration
- ltc_frametime = 0xFFFFUL - ltc_frame_start_time;
- ltc_frametime += 0xFFFFUL * (ltc_timer_overflows-1);
- ltc_frametime += ICR1;
- uart_sendl(ltc_frametime);
- glitches = 0;
- ltc_frametime = 0;
- ltc_frame_start_time = ICR1;
- ltc_timer_overflows = 0;
- //ltc_decode_frame();
- // Swap buffers
- if (rptr == ltc_buf){
- rptr = ltc_buf2;
- wptr = ltc_buf;
- } else {
- rptr = ltc_buf;
- wptr = ltc_buf2;
- }
- }
- // Shuffle the buffer
- for(uint8_t i = 0; i < 10; i++){ // Remember we made an extra byte on the end we don't need?
- wptr[i] = (wptr[i] >> 1) | (wptr[i+1] << 7);
- }
- isrbusy= false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement