091_Arduino中断与定时器_#include


看起来,还是有几个基于中断的计数器的。或许,可以根据这个做一个任务调度的调度器。先测试一下计数器:

#include "SoftwareSerial.h"

unsigned long counter_ms;
unsigned long counter_us;

void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial)
{
/* 等待连接 */
}
Serial.println("start running...");
}

void loop()
{
// put your main code here, to run repeatedly:
counter_ms = millis();
counter_us = micros();
Serial.print("counter_ms: ");
Serial.print(counter_ms);
Serial.print("\t");
Serial.print("counter_us: ");
Serial.println(counter_us);
Serial.println("-----------------------------------------------------------");
delay(1000);
}

         效果:

091_Arduino中断与定时器_任务调度_02

         有一点不准确,是差在了什么地方呢?尝试了中断的开关,依然没有奏效。

         尝试做个任务调度:

#include "SoftwareSerial.h"

void task_100ms(void);
void led_toogle(void);

unsigned long counter_ms;
unsigned long counter_us;

void setup()
{
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
while (!Serial)
{
/* 等待连接 */
}
Serial.println("start running...");
}

void task_1000ms(void)
{
led_toogle();
}

void led_toogle(void)
{
static unsigned long n = 0U;

if(n % 2 == 0)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
}
else
{
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
n++;
}

void loop()
{
// put your main code here, to run repeatedly:
counter_ms = millis();
counter_us = micros();
#ifdef TEST_TIMER
Serial.print("counter_ms: ");
Serial.print(counter_ms);
Serial.print("\t");
Serial.print("counter_us: ");
Serial.println(counter_us);
Serial.println("-----------------------------------------------------------");
delay(1000);
#endif
if(counter_ms % 1000 == 0)
{
task_1000ms();
}
}

         测试下来,感觉还是不是很准确。中断接口的增加对此没有改善效果,反而让效果变差了。接下来应该自己设计一下中断定时了。