Playing with Arduino
A page to record my playing with Arduino
init()

Abstract

The init() initializes the timer counter, AD converter and serial communication of ATmega328P.

Source Code

The init() is defined in hardware/arduino/avr/cores/arduino/wiring.c as below. Only the souce code for Arduino UNO is quoted here. The original source code supports many tips using #if’s.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
void init()
{
    // this needs to be called before setup() or some functions won't
    // work there
    sei();
     
    // on the ATmega168, timer 0 is also used for fast hardware pwm
    // (using phase-correct PWM would mean that timer 0 overflowed half as often
    // resulting in different millis() behavior on the ATmega8 and ATmega168)
    sbi(TCCR0A, WGM01);
    sbi(TCCR0A, WGM00);
 
    // set timer 0 prescale factor to 64
    // this combination is for the standard 168/328/1280/2560
    sbi(TCCR0B, CS01);
    sbi(TCCR0B, CS00);
 
    // enable timer 0 overflow interrupt
    sbi(TIMSK0, TOIE0);
 
    // timers 1 and 2 are used for phase-correct hardware pwm
    // this is better for motors as it ensures an even waveform
    // note, however, that fast pwm mode can achieve a frequency of up
    // 8 MHz (with a 16 MHz clock) at 50% duty cycle
    TCCR1B = 0;
 
    // set timer 1 prescale factor to 64
    sbi(TCCR1B, CS11);
    sbi(TCCR1B, CS10);
 
    // put timer 1 in 8-bit phase correct pwm mode
    sbi(TCCR1A, WGM10);
 
    // set timer 2 prescale factor to 64
    sbi(TCCR2B, CS22);
 
    // configure timer 2 for phase correct pwm (8-bit)
    sbi(TCCR2A, WGM20);
 
    // set a2d prescaler so we are inside the desired 50-200 KHz range.
        sbi(ADCSRA, ADPS2);
        sbi(ADCSRA, ADPS1);
        sbi(ADCSRA, ADPS0);
 
    // enable a2d conversions
    sbi(ADCSRA, ADEN);
 
    // the bootloader connects pins 0 and 1 to the USART; disconnect them
    // here so they can be used as normal digital i/o; they will be
    // reconnected in Serial.begin()
 
    UCSR0B = 0;
}

The init() has no input argument and no return value.

First it enables the interruption.

5
    sei();

The sei() is a macro to enable the interruption.

Next it initializes the timer/counter 0 to 2 that are used in analogWrite(). A detailed explanation is in the page of analogWrite().

 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    // on the ATmega168, timer 0 is also used for fast hardware pwm
    // (using phase-correct PWM would mean that timer 0 overflowed half as often
    // resulting in different millis() behavior on the ATmega8 and ATmega168)
    sbi(TCCR0A, WGM01);
    sbi(TCCR0A, WGM00);
 
    // set timer 0 prescale factor to 64
    // this combination is for the standard 168/328/1280/2560
    sbi(TCCR0B, CS01);
    sbi(TCCR0B, CS00);
 
    // enable timer 0 overflow interrupt
    sbi(TIMSK0, TOIE0);
 
    // timers 1 and 2 are used for phase-correct hardware pwm
    // this is better for motors as it ensures an even waveform
    // note, however, that fast pwm mode can achieve a frequency of up
    // 8 MHz (with a 16 MHz clock) at 50% duty cycle
 
    TCCR1B = 0;
 
    // set timer 1 prescale factor to 64
    sbi(TCCR1B, CS11);
    sbi(TCCR1B, CS10);
 
    // put timer 1 in 8-bit phase correct pwm mode
    sbi(TCCR1A, WGM10);
 
    // set timer 2 prescale factor to 64
    sbi(TCCR2B, CS22);
 
    // configure timer 2 for phase correct pwm (8-bit)
    sbi(TCCR2A, WGM20);

Next it sets the AD converter. A detailed explanation is in the page of analogRead().

40
41
42
43
44
45
46
47
48
49
    // set a2d prescale factor to 128
    // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
    // XXX: this will not work properly for other clock speeds, and
    // this code should use F_CPU to determine the prescale factor.
    sbi(ADCSRA, ADPS2);
    sbi(ADCSRA, ADPS1);
    sbi(ADCSRA, ADPS0);
 
    // enable a2d conversions
    sbi(ADCSRA, ADEN);

Lastly it initializes the serial port.

48
49
50
51
52
    // the bootloader connects pins 0 and 1 to the USART; disconnect them
    // here so they can be used as normal digital i/o; they will be
    // reconnected in Serial.begin()

    UCSR0B = 0;

Version

Arduino 1.8.13

Last Update

June 19, 2020

inserted by FC2 system