//--------------------------------------------------------------------------------------- // Programm Nr.: 05 // Content : LED blinking with delay-fuction and change timing with keys // Author : digital AG Halle // Date : 13.07.2008 // System : ATmega8-16PU - Dai Hoc Hue Development Board //--------------------------------------------------------------------------------------- //--included Files----------------------------------------------------------------------- #include #include //delay.h only works with compiler optimization 00 !!! //--declarations------------------------------------------------------------------------- #ifndef F_CPU #define F_CPU 8000000 //CPU frequency = 8 MHz #endif /*---------------------------------------------------------------- The longest possible time for waiting with delay.h is limited: function _delay_ms(); 262.14 ms / F_CPU in MHz -> here: 252.14 ms / 8 MHz = max. 31.5 ms helping idea: work with _delay_ms(); in a loop ----------------------------------------------------------------*/ void long_delay(uint16_t ms) //external routine { for(; ms>0; ms--) _delay_ms(1); //wait 1 ms for ms times } uint8_t KEYS; //8-Bit integer variable (0...255) for actual key-value uint8_t KEYS_OLD; //variable for last key-value uint8_t TIME; //Variable for controlling the blinking frequency //--MAIN PROGRAMM------------------------------------------------------------------------ int main(void) { //--INITIALISATION--------------------------------------------------------------- DDRC=225; //PORT C: Bit 1,2,3,4 are inputs (Keys 1 - 4) PORTC=30; //PORT C: Bit 1,2,3,4 internal Pullup-Resistors active DDRD=255; //Port D: all Bits defined as output (for the status LEDs) TIME=20; //init-value for time (25 ms *20 = 0.5 sec) //--PROGRAMM CODE BEFORE MAIN LOOP (USED JUST ONE TIME)-------------------------- PORTD=255; //Every LED at PORT D is off KEYS_OLD=PINC; //initialise KEYS_OLD (PINC should be 255 at that moment) //--MAIN LOOP-------------------------------------------------------------------- while(1) { KEYS=PINC; //read input byte from PORT C into variable KEYS if (KEYS_OLD!=KEYS) //check of a change at PORT C inputs //it also works against chatter at the keys { if ((KEYS&226)==!(0x02)) //if key 1 was pressed { TIME=TIME+5; //increase TIME with 5 } if ((KEYS&228)==!(0x04)) //if key 2 was pressed { TIME=TIME-5; //decrease TIME with 5 } } PORTD ^= (1<