/* * TIMERwithIRQ.c * * Created: 5.6.2014 15:27:41 * Author: maticpi */ #include #include void Init(); volatile long v_systick; //system tick (counter of milliseconds) long systick; //non-volatile copy of the system tick (safe to use outside of interrupts) int main(void) { Init(); sei(); while(1) { cli(); //disable interrupts to prevent any interruptions to the following instruction systick=v_systick; //safely copy the system tick to its non-volatile copy sei(); //re-enable interrupts PORTB=systick/1000; //display the system tick (in seconds) on PORTB } } ISR(TIMER0_COMPA_vect) //every ms increment the system tick { v_systick++; } void Init() { //IO port PORTA=0x08; //0000 1000 PORTB=0x00; //0000 0000 PORTC=0x3E; //0011 1110 PORTD=0x7D; //0111 1101 //1-output, 0-input DDRA=0xF0; //1111 0000 DDRB=0xFF; //1111 1111 DDRC=0xC1; //1100 0001 DDRD=0x82; //1000 0010 //Init timer0 TCCR0A=(1<