Initialization

Explanation

This code listing shows an example program to initialize the microcontroller. It shows how to initialize the timers and how to configure the ports, initialization of global variables could be included here.

  • Timer 0 is configured to interrupt when an overflow occurs. The clock that it is using is pre-scaled by 1024.
  • Timer 1 is configured to interrupt when the value of the counter is equal to the value in OCR1A. It is working on CTC mode (Clear Timer on Compare).
  • PortB and PortD are configured as output ports.
  • PortC have both directions. PortC(O) is an output and the rest of the pins are inputs.
  • sei() and cli() are the Set Enable Interrupt and CLear enable Interrupt routines.
void iniconf(void){
    cli();            // Disable interrupts
 
    /* Program timer0    */
    TCNT0 = 0x00;        // Reset timer count
    TCCR0A |= 0x00;        // Normal functioning, clk_io/1024
    TCCR0B |= 0x05;
    TIMSK0 |= _BV(TOIE0);    // Enable interrupts when overflow
 
    /* Program timer1    */
    TCNT1H = 0x00;        // Reset timer count
    TCNT1L = 0x00;
    OCR1AH = 0x01;        // Set output compare value
    OCR1AL = 0xFF;
    TCCR1A |= 0x00;        // Clear timer on compare CTC, clk_io
    TCCR1B |= 0x09;
    TIMSK1 |= _BV(OCIE1A);    // Enable interrupts when overflow
 
    sei();            // Enable interrupts
 
    /* Program the ports    */
    DDRB = 0xff;        // PortB as output port
    DDRC = 0x01;        // PortC0 for wave output
    DDRD = 0xff;
}
page_revision: 0, last_edited: 1209612268|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License