Arduinoのmillis()/micros()が利用しているTIMER0を流用するライブラリを作成してみた。他のタイマーを使わないのがメリットだが中途半端な時間であるのがデメリット。正確なミリ秒でなくても良い用途なら使えるか...
【参考情報】Arduinoのタイマー処理
このライブラリはArduinoのタイマー割り込み周期で動作し同時に2チャンネル利用できる。
Arduinoの割り込み周期は次の通り。
16.5MHz … 993us
16MHz … 1024us
8MHz … 2048us
4MHz … 4096us
2MHz … 8192us
1MHz … 16384us
【修正履歴】
[2020-03-28]
TIMER0のPWMピンがanalogWrite()/tone()で指定されたとき、このライブラリ機能に影響があることが判明。一時的に割り込み周期が狂ってしまう。回避策はないので排他的にしか利用できない。
[2020-02-27]
まだstart()のタイミング・バグがとり切れてなかったので再修正。
[2020-02-24]
start()処理が微妙な感じだったので改良してみた。これでタイミング・バグはなくなったと思う。たぶん。
タイマーはCTCではなくPWMで使っているのでしょうがないんだけどPWMの2重緩衝機能のお陰で処理タイミング的には少々厳しいものがある...が、不定期に発生する誤動作というやっかいな不具合を防ぐためにもタイミングは見切る必要があるし重要な問題だ。
【サンプル・スケッチ】
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 |
#include "TimerZero.h" #define LED1 1 #define LED2 2 void timeup0(void) { digitalWrite(LED1, !digitalRead(LED1)); } void timeup1(void) { digitalWrite(LED2, !digitalRead(LED2)); } void setup(void) { Timer0.setup(0, 200, timeup0); Timer0.start(0); Timer0.setup(1, 1000, timeup1); Timer0.start(1); } void loop(void) { } |
【TimerZero.h】
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
/* TimerZero.h - AVR Timer0 Library for Arduino AVR-Board Copyright (c) 2020 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef __TIMERZERO_H #define __TIMERZERO_H #include <stdint.h> #include <stdbool.h> #include <avr/io.h> #include <avr/interrupt.h> #ifdef TIFR #define TIFR0 TIFR #endif #ifdef TIMSK #define TIMSK0 TIMSK #endif class TimerZero { private: volatile bool _badint[2]; volatile unsigned long _counts[2]; unsigned long _reload[2]; void (*_func[2])(void); public: TimerZero(void) { } virtual ~TimerZero(void) { } void setup(uint8_t ch, unsigned long tick, void (*func)(void)) { _reload[ch] = tick; _counts[ch] = tick; _func[ch] = func; } void start(uint8_t ch) { if (_func[ch]) { uint8_t c, t; cli(); if (ch) { t = OCR0B; do { c = OCR0B = TCNT0; TIFR0 |= _BV(OCF0B); } while (c != TCNT0); _badint[ch] = (c <= t); TIMSK0 |= _BV(OCIE0B); } else { t = OCR0A; do { c = OCR0A = TCNT0; TIFR0 |= _BV(OCF0A); } while (c != TCNT0); _badint[ch] = (c <= t); TIMSK0 |= _BV(OCIE0A); } sei(); } } void stop(uint8_t ch) { if (ch) TIMSK0 &= ~_BV(OCIE0B); else TIMSK0 &= ~_BV(OCIE0A); } void timeup(uint8_t ch) { if (_badint[ch]) _badint[ch] = false; else if (--_counts[ch] == 0) { _counts[ch] = _reload[ch]; _func[ch](); } } }; extern TimerZero Timer0; #endif |
【TimerZero.cpp】
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 |
/* TimerZero.h - AVR Timer0 Library for Arduino AVR-Board Copyright (c) 2020 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "TimerZero.h" TimerZero Timer0; ISR(TIMER0_COMPA_vect) { Timer0.timeup(0); } ISR(TIMER0_COMPB_vect) { Timer0.timeup(1); } |