/* * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return. Joerg Wunsch * ---------------------------------------------------------------------------- * * Bargraph demo. * * Infinitely converts the value on ADC0, and outputs a LED bargraph * on port B. The ADC is used in free-running mode, and the LED port * is updated within the ADC ISR. * * $Id$ */ #include #include #include #include #if defined(__AVR_ATtiny26__) # define ADCINIT() ADCSR = _BV(ADEN) | _BV(ADSC) | _BV(ADFR) | _BV(ADIE) #elif defined(__AVR_ATtiny261__) || defined(__AVR_ATtiny461__) \ || defined(__AVR_ATtiny861__) # define ADCINIT() ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADATE) | _BV(ADIE) #else # error "Unknown MCU type." #endif ISR (ADC_vect) { uint8_t adc, i; adc = ADCH; adc &= 0xE0; for (i = 0; adc != 0; adc -= 0x20) i = (i << 1) | 1; PORTB = ~i; } void ioinit (void) { /* Make port B all outputs, to drive the LEDs. */ DDRB = 0xFF; /* Turn all LEDs off -- they are low-active. */ PORTB = 0xFF; /* Set up the ADC. */ ADMUX = _BV(ADLAR); ADCINIT(); sei(); } int main (void) { ioinit (); /* loop forever, the interrupts are doing the rest */ for (;;) sleep_mode(); return (0); }