Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. /* function to read out switches */
  2. uint8_t PollSwitch(void)
  3. {
  4.     unsigned int i;
  5.     _Bool ec_bak = autoencode;
  6.     autoencode = FALSE;
  7.     DDRD |= SWITCHES;               // Switches as Output
  8.     SWITCH_ON;                      // Output HIGH for measurement
  9.     ADMUX = (1 << REFS0) | SWITCH;  // AVCC reference with external capacitor
  10.     Sleep(10);
  11.    
  12.     ADCSRA |= (1 << ADSC);          // Start conversion
  13.     while (!(ADCSRA & (1 << ADIF)));// wait for conversion complete
  14.     ADCSRA |= (1 << ADIF);          // clear ADCIF
  15.     i = ADCL + (ADCH << 8);
  16.    
  17.     SWITCH_OFF;
  18.     Sleep(5);
  19.     autoencode = ec_bak;
  20.  
  21.     return ((10240000/i - 10000) * 63 + 5000)/10000;
  22. }
  23.  
  24. /*!
  25.  * binds switch press to external Interrupt 1
  26.  * catch it with ISR(INT1_vect)
  27.  * see avr-libc documentation for details
  28.  */
  29. void StartSwitch(void)
  30. {
  31.     SWITCH_OFF;
  32.     DDRD &= ~SWITCHES;                  // disable Switches as Input
  33.  
  34.     Sleep(100); // cool down from pollswitch
  35.    
  36.     MCUCR &= ~((1 << ISC11) | (1 << ISC10));// Low level generates interrupt
  37.     GICR |= (1 << INT1);                    // Enable external Interrupt 1
  38. }
  39.  
  40. void StopSwitch(void)
  41. {
  42.     GICR &= ~(1 << INT1);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement