Advertisement
Guest User

EZK Wasted spark

a guest
Jun 13th, 2011
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. //Original by Karl Buchka. Modded by blabla
  2.  
  3. #include <avr/io.h>
  4. #include <avr/interrupt.h>
  5. #include <util/delay.h>
  6. uint16_t current = 0;
  7. uint16_t lastcount = 0;
  8. uint16_t average = 0;
  9. uint8_t toothCount = 0;
  10. uint8_t avgIndex = 0;
  11. uint8_t firstRun = 2;
  12. uint16_t lastFour[4] = {0,0,0,0};
  13.  
  14. int main(void)
  15. {
  16.     cli();
  17.     _delay_ms(10); // Wait
  18.     DDRC |= ( (1 << DDC0) | (1 << DDC1) );  //Enable PC0 and PC1 as output
  19.     _delay_ms(10); // Wait
  20.     DDRA |= ( (1 << DDA0) | (1 << DDA1) );
  21.     _delay_ms(10); // Wait
  22.     TCCR1B = 0b01000011;        // Rising edge input capture, Timer1 t/256 prescaling.
  23.     _delay_ms(10); // Wait
  24.     TIMSK = 0b00100000;     // Interrupt on Input Capture
  25.     _delay_ms(10); // Wait
  26.     sei();
  27.  
  28.  
  29.     while(1) {
  30.         //Do stuff
  31.     }  
  32. }
  33.  
  34. ISR(TIMER1_CAPT_vect)
  35. {
  36.     // Rising edge only
  37.  
  38.         if (firstRun == 2) // First pulse, cannot calculate time between pulses. Just save captured TIMER value.
  39.         {
  40.         lastcount = ICR1;
  41.         firstRun=1;
  42.         return;
  43.         }
  44.  
  45.  
  46.         if (ICR1 < lastcount) {    
  47.             current = (0xFFFF - lastcount) + ICR1; // Counter overflow
  48.         } else {
  49.             current = ICR1 - lastcount;
  50.         }
  51.         lastcount =ICR1;
  52.         // Second run of code, fill running average with first real tooth time.
  53.         if (firstRun == 1) {
  54.             for (uint8_t i = 0; i < 4; i++) {
  55.                 lastFour[i] = current;
  56.             }
  57.             firstRun = 0;
  58.         }
  59.        
  60.         // Calculate average of last 4 non-missing teeth.
  61.         for (uint8_t i = 0; i < 4; i++) {
  62.             average += lastFour[i];
  63.         }
  64.         average >>= 1;      // Divide by 2
  65.        
  66.         // If missing tooth, zero tooth count, else (non-missing), check for trigger
  67.         // teeth, add tooth time to running average, and increment toothCount.
  68.         if (current > average) {
  69.             toothCount = 0;
  70.         } else {
  71.            
  72.             if (toothCount == 15) {
  73.                 PORTC |= (1<<PORTC0);
  74.                 PORTC &= ~(1<<PORTC1);
  75.                 PORTA |= (1<<PORTA0);
  76.                 PORTA &= ~(1<<PORTA1);
  77.             }
  78.            
  79.             if (toothCount == 45) {
  80.                 PORTC &= ~(1<<PORTC0);
  81.                 PORTC |= (1<<PORTC1);
  82.                 PORTA &= ~(1<<PORTA0);
  83.                 PORTA |= (1<<PORTA1);
  84.             }
  85.            
  86.             lastFour[avgIndex] = current;
  87.             avgIndex += 1;
  88.             avgIndex %= 4;
  89.            
  90.             toothCount++;
  91.             average = 0;       
  92.         }
  93.        
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement