====== Start ====== ===== Light a LED ===== This small program allows to light a LED on port B. \\ The initPort function configure port B in output mode and force its outputs to zero. \\ The PORTB outputs are set to 1 in the main function. #include /* register definitions*/ void initPort (void) { /* port configuration : '1' for output */ DDRB=0b11111111; /* PORTB in output mode */ PORTB=0b00000000; /* PORTB = 0 */ } int main (void) { initPort(); while(1) { PORTB = 0b11111111; /* PORTB force a 1 */ } return 1; } ===== Make a LED blinking ===== The purpose of this programme is to make a LED blinking using timer 0 and interrupts. ==== Initialisation du timer en mode timer ==== TCCR0B initializes timer 0 and its prescaler. \\ TCNT0 register is the counter. It increases at each clock cycle. \\ Bit 0 of TIFR0 register is the interrupt flag. \\ Bit TOIE0 from TIMSK0 register activates interrupt on timer 0 overflow. TCNT0 overflow launch an interrupt. In interrupt handler « ISR(TIMER0_OVF_vect) », we increment « cmp » variable which will serve to make the led blinking by reversing PORTB value when cmp is greater than 100. #include /* register definition */ #include /* interrupts */ volatile unsigned char cmp; /* interrupt on TCNT0 overflow : every 13ms (1/20MHz * 256*1024) */ ISR(TIMER0_OVF_vect) { /* at each TCNT0 overflow, cmp is incremented */ cmp++; TIFR0 = 0b00000001; /* TOV0 = 1 : Interrupt flag is forced to 0 */ } void initPort (void) { /* port configuration : '1' for output */ DDRB=0b11111111; /* PORTB en sortie */ PORTB=0b00000000; /* PORTB a 0 */ } /* * Initialize timer 0 in timer mode with a prescaler equals to 1024 * Interruption enabled on timer overflow */ void initTimer0(void) { TCCR0B= 0b00000101; /* timer mode with prescaler equals to 1024 */ TCNT0 = 0; /* Force counter to 0 */ TIFR0 = 0b00000001; /* TOV0 = 1 : Interrupt flag is forced to 0 */ TIMSK0 = 0b00000001; /* TOIE0 = 1: enable interrupt on TCNT0 overflow */ } int main (void) { initPort(); initTimer0(); SREG = 0b10000000; /* enable interrupts */ while(1) { if (cmp>100) /* 13*100 = 1.3s */ { PORTB = ~PORTB; /* reverse PORTB value */ cmp=0; } } return 1; }