/* * Digital I/O (dual 7-segment LED) lab program. * * This program demonstrates the use of buttons as input to control the count direction * (and contents of) the two-digit display. * * Initially it displays just a different number based on the state of the switches * (SW0 and SW1 on the STK500). However, you wneed to modify it to count up/down in * the range 00..99, as well as handling rollover up through 99 and down through 0. * It's up to you to choose between rolling over between 00 and 99, or stopping at the * two extremes. * * All eight bits of PORTB are fed into 7447 decoder chips. The high nibble (bits 4-7) * are for the left (upper) digit, and the low nibble (bits 0-3) for the right (low) digit * on the display. * * Last Modified 2010/09/29 17:20 (ebw) */ #include // for PORTx, DDRx, AD*, etc. #include // for _delay_ms() // _BV(n) values for switches #define PORTA_SW_DOWN 0 // SW0 is on PA0, and used for downcounting #define PORTA_SW_UP 1 // SW1 is on PA1, and used for upcounting void init(void) { DDRA = 0x00; // our two switches are inputs DDRB = 0xFF; // all eight bits of PORTB are outputs PORTA = _BV(PORTA_SW_DOWN) | _BV(PORTA_SW_UP); // turn on pullup resistors for our switches } int main(void) { uint8_t counter=0; // for keeping track of the current count value init(); // set up PORTS while (1) { uint8_t switches; switches = ~PINA; // Q: why is it useful to do this instead of just using PINA...? switch (switches) { case 0b00000001: // SW0 pressed, count down PORTB = 0x11; // display 11, just for testing // FIXME: After the initial test deal with counting down, rolling over and // perhaps displaying the digits here. Decide what you will do when you get // to 00 -- do you stop at 00 or do you rollover to 99 ? break; case 0b00000010: // SW1 pressed, count up PORTB = 0x22; // display 22, just for testing // FIXME: Same issues as above, but for counting up vs. down break; default: // when switches are not in any of the specified states listed above PORTB = 0x00; // display 00, again, just for testing // FIXME: how do we even get to this case, and what should we do ? break; } } return 0; // never executed... }