忍者ブログ
趣味+メモ用のブログです。 GNU/Linux関連、OSS関連情報、調査事項になるでしょうが、何を書くか分かりません。
[61] [60] [59] [58] [56] [55] [54] [53] [52] [51] [50]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

ATTiny2313でタイマーの学習をしている。
とりあえず関連する情報をまとめてみた。


■ タイマー関連の割り込み 
 No. Address Source Interrupt Definition
4   0x0003 TIMER1 CAPT Timer/Counter1 Capture Event
5 0x0004 TIMER1 COMPA Timer/Counter1 Compare Match A
6 0x0005 TIMER1 OVF Timer/Counter1 Overflow
7 0x0006 TIMER0 OVF Timer/Counter0 Overflow
13 0x000C TIMER1 COMPB Timer/Counter1 Compare Match B
14 0x000D TIMER0 COMPA Timer/Counter0 Compare Match A
15 0x000E TIMER0 COMPB Timer/Counter0 Compare Match B

 ■ 関連するレジスター
Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 備考
0x39 (0x59) TIMSK TOIE1 OCIE1A OCIE1B ICIE1 OCIE0B TOIE0 OCIE0A Timer/Counter Interrupt Mask Register
0x38 (0x58) TIFR TOV1 OCF1A OCF1B ICF1 OCF0B TOV0 OCF0A Timer/Counter Interrupt Flag Register
0x3C (0x5C) OCR0B Timer/Counter0 – Compare Register B Output Compare Register B
0x36 (0x56) OCR0A Timer/Counter0 – Compare Register A Output Compare Register A
0x33 (0x53) TCCR0B FOC0A FOC0B WGM02 CS02 CS01 CS00 Timer/Counter Control Register B
0x30 (0x50) TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 WGM01 WGM00 Timer/Counter Control Register A
0x2F (0x4F) TCCR1A COM1A1 COM1A0 COM1B1 COM1BO WGM11 WGM10
Timer/Counter1 Control
Register A 
0x2E (0x4E) TCCR1B ICNC1 ICES1 WGM13 WGM12 CS12 CS11 CS10 Timer/Counter1 ControlRegister B
0x32 (0x52) TCNT0 Timer/Counter0 (8-bit) Timer/Counter Register 
0x2D (0x4D) TCNT1H Timer/Counter1 – Counter Register High Byte
Timer/Counter1 – TCNT1H
and TCNT1L
0x2C (0x4C) TCNT1L Timer/Counter1 – Counter Register Low Byte
0x2B (0x4B) OCR1AH  Timer/Counter1 – Compare Register A High Byte
Output Compare Register 1 A
– OCR1AH and OCR1AL
0x2A (0x4A) OCR1AL Timer/Counter1 – Compare Register A Low Byte
0x29 (0x49) OCR1BH Timer/Counter1 – Compare Register B High Byte
Output Compare Register 1 B
- OCR1BH and OCR1BL
0x28 (0x48) OCR1BL Timer/Counter1 – Compare Register B Low Byte
 
■ ATTyny2313タイマーの特徴
・Timer0(8bit)/Timer1(16bit)の2つのタイマーがある。
・カウントオーバーと、コンペアマッチでの割り込みを得ることができる。
・カウントオーバーは、FF or 0で割り込みをかけられる(これにどれだけの意味があるのか??)
・コンペアはA/Bの2種類を使用できる。
・PWM(パルス幅変調)に関連する??? (このあたりが少しわからない)

■ タイマー実装例
「AVR/news46 - 千秋ゼミ」のArduinoライブラリ内で、1ミリ秒をカウントする
ライブラリが存在し、Timer0で64パルスでの割り込みにて実装されている。
●wiring.c
#include "wiring_private.h"
 
volatile unsigned long timer0_overflow_count = 0;
volatile unsigned long timer0_clock_cycles = 0;
volatile unsigned long timer0_millis = 0;
 
SIGNAL(TIMER0_OVF_vect)
{
    timer0_overflow_count++;
    // timer 0 prescale factor is 64 and the timer overflows at 256
    timer0_clock_cycles += 64UL * 256UL;
    // ■ 64クロック x 256カウント
    while (timer0_clock_cycles > clockCyclesPerMicrosecond() * 1000UL) {
        timer0_clock_cycles -= clockCyclesPerMicrosecond() * 1000UL;
        timer0_millis++;
    }
}
 
・・・・
 
void init()
{
    // this needs to be called before setup() or some functions won't
    // work there
    sei();
    // ■⇒ 割り込み開始
 
    // on the ATtiny2313, 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 ATtiny2313)
    sbi(TCCR0A, WGM01);
    sbi(TCCR0A, WGM00);
  // ■⇒ Timer/Counter0 Control Register A
    // ■⇒ TCCR0A WGMA01 | WGMA00 ビットON
    // ■⇒ Fast PWMモード、0xFFになったときに動作
 
    // set timer 0 prescale factor to 64
    sbi(TCCR0B, CS01);
    sbi(TCCR0B, CS00);
 // ■⇒ Timer/Counter0 Control Register B
 // ■⇒CPUクロックを1/64で使用する。
 
    sbi(TIMSK, TOIE0);
 // ■⇒「TOIE0:Timer/Counter0 Overflow Interrupt Enable」
 // ■⇒ タイマー0のオーバーフロー割り込みを有効にしています。

 
    // timers 1 is 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
    // ■⇒タイマー1についても設定しています。
 
    // set timer 1 prescale factor to 64
    sbi(TCCR1B, CS11);
    sbi(TCCR1B, CS10);
  // ■⇒CPUクロックを1/64で使用する。
 
    // put timer 1 in 8-bit phase correct pwm mode
    sbi(TCCR1A, WGM10);
 // ■⇒ Timer/Counter1 Control Register A 
 // ■⇒ PWM, Phase Correct, 8-bit モード
 
    // 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()
 
    UCSRB = 0;
    // ■⇒ USART Control and Status Register B
    // ■⇒ USARTを一旦無効にしています。
}


■ 参考文献
・ATTiny2313 データシート(英語)
・始める電子回路(AVRタイマー)
 http://www9.plala.or.jp/fsson/NewHP_elc/AVR/Avr_100timrMemo.html
・こけおん(マイコン講座/タイマーユニット)
 http://delegate.uec.ac.jp:8081/club/koken/wiki/?%A5%DE%A5%A4%A5%B3%A5%F3%B9%D6%BA%C2%2F%A5%BF%A5%A4%A5%DE%A1%BC%A5%E6%A5%CB%A5%C3%A5%C8
・AVR/news46 - 千秋ゼミ(ATTiny2313用 Arduinoライブラリ)

以上

PR


トラックバック
この記事にトラックバックする:


忍者ブログ [PR]
カレンダー
04 2024/05 06
S M T W T F 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
フリーエリア
最新コメント
最新トラックバック
プロフィール
HN:
一乗寺 又兵衛
性別:
男性
職業:
コンパイル
趣味:
コンパイル
バーコード
ブログ内検索