Skip to content

Commit

Permalink
增加示例、文档,修复bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Silver-Fang committed Jul 4, 2021
1 parent d41cedc commit 919208c
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 385 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Currently all APIs are function templates, which means all arguments must be kno
# Timers
All public APIs are under namespace TimersOneForAll, and require TimerCode as the first template argument. TimerCode indicates which hardware timer you want to use. Hardware timers vary by CPUs. For ATMega2560, there're 6 timers:
## Timer 0
This timer is 8-bit, which means it has 2^8=256 states. It can generate COMPA and COMPB interrupts, but not OVF, because it's occupied by Arduino builtin function `millis()`. This means this timer is the last one you want to use among all the timers. Only use it if you really have strong reasons.
This timer is 8-bit, which means it has 2^8=256 states. It can generate COMPA and COMPB interrupts, but not OVF, because it's occupied by Arduino builtin function `millis();delay();micros();`. This means this timer is the last one you want to use among all the timers. Only use it if you really have strong reasons.
## Timer 1, 3, 4, 5
These timers are all 16-bit, with 65536 states, which means that they're more accurate than 8-bit timers. COMPA, COMPB and OVF interrupts are all available. You may want to use these timers for most scenarios.
## Timer 2
Expand Down Expand Up @@ -76,6 +76,10 @@ void SquareWave()
//Generate the square wave for only RepeatTimes cycles.
template <uint8_t TimerCode, uint8_t PinCode, uint16_t HighMilliseconds, uint16_t LowMilliseconds, int16_t RepeatTimes>
void SquareWave()
//阻塞当前代码执行指定毫秒数
//Block current code from running for DelayMilliseconds
template <uint8_t TimerCode, uint16_t DelayMilliseconds>
void Delay()
//取消指定给特定计时器的所有任务。其它计时器不受影响。
//Abort all tasks assigned to TimerCode. Other timers won't be affected.
template <uint8_t TimerCode>
Expand Down
12 changes: 0 additions & 12 deletions examples/LedAndBuzzerShow.ino

This file was deleted.

29 changes: 29 additions & 0 deletions examples/LedAndBuzzerShow/LedAndBuzzerShow.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//上传该示例之前,请在7号口连接一个LED灯,8号口连接一个无源蜂鸣器的IO端口
#include <TimersOneForAll.h>
using namespace TimersOneForAll;
constexpr uint8_t LED = 7;
constexpr uint8_t Buzzer = 8;
void setup()
{
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
digitalWrite(LED,HIGH);
//设置计时器3在5秒后熄灭LED灯,但不阻断程序
DoAfter<3,5000,LightDown>();
//设置4号计时器,每隔2秒,就用5号计时器生成2000㎐脉冲1秒,重复3次
RepeatAfter<4,2000,PlayTone<5,Buzzer,2000,1000>,3>();
//设置计时器1,将程序阻断7秒
Delay<1,7000>();
//设置计时器1,将LED灯先亮2秒,再熄灭1秒,无限循环
SquareWave<1,LED,2000,1000>();
//设置计时器3在8秒后停止计时器1
DoAfter<3,8000,ShutDown<1>>();
//观察到,LED灯明暗循环两次后,最终停在了亮状态,因为1号计时器尚未触发暗事件就被停止了
}
void LightDown()
{
digitalWrite(LED,LOW);
}
void loop()
{
}
17 changes: 17 additions & 0 deletions examples/SerialTimer/SerialTimer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <TimersOneForAll.h>
using namespace TimersOneForAll;
//上传该示例之前,请确保你的PC准备好收发串口信号
void setup()
{
Serial.begin(9600);
//使用1号计时器开始计时
StartTiming<1>();
Serial.setTimeout(-1);
}
void loop()
{
static uint8_t Instruction = 0;
Serial.readBytes(&Instruction, 1);
Serial.write((uint8_t*)&MillisecondsElapsed<1>, 2);
}
//上传后,可以从PC上向串口发送任意单字节,每次发送都会受到一个16位整数,指示经过的毫秒数。达到65535毫秒后会归零。
5 changes: 1 addition & 4 deletions src/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
"F_CPU=16000000",
"ARDUINO_ARCH_AVR"
],
//支持__attribute__,不能用msvc
"intelliSenseMode": "gcc-x64",
"cStandard": "c17",
"cppStandard": "c++17"
"compilerPath": "C:\\Users\\vhtmf\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7\\bin\\avr-gcc.exe"
}
],
"version": 4
Expand Down
Loading

0 comments on commit 919208c

Please sign in to comment.