init()

概要

init()は、ATmega328Pのタイマ/カウンタとAD変換器、シリアル通信の初期化を行います。

ソースコード

init()は、hardware/arduino/avr/cores/arduino/wiring.c に定義されています。以下に全ソースコードを示します。Arduino Unoの場合に適用される部分だけを抜粋しています。実際には#if によりさまざまなチップに対応しています。

 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;
}

init()には、入力・出力ともありません。

まず、割り込みを許可します。

5
    sei();

sei()は、割り込みを許可するマクロです。

次に、analogWrite()で利用するタイマ/カウンタ0から2のの初期化を行います。詳細は、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);

次に、AD変換器の設定を行います。詳細は、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);

最後に、シリアルポートの初期化を行います。

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;

バージョン

Arduino AVR Boards 1.8.6

最終更新日

March 21, 2023

inserted by FC2 system