【发布时间】:2019-01-23 20:59:37
【问题描述】:
我正在尝试实现我在 YouTube 上看到的一个简单的 timer1 示例:http://youtu.be/Tj6xGtwOlB4?t=22m7s。这个例子是用 c++ 编写的,用于独立的 ATMEGA328 芯片,我正试图让它在 Arduino UNO 上工作。这是我的工作代码:
void setup() {
//initialize port for LED
DDRB = 0b11111111; //initialize port B as output (really only care about 5th bit)
PORTB = 0b00000000; //set ouput values to zero
TCCR1A = 0; //clear control register A (not sure that I need this)
TCCR1B |= 1<<CS10; //no prescaler, turns on CS10 bit of TCCR1B
}
void loop() {
if (TCNT1 >= 255){
TCNT1 = 0; //resets timer to zero
PORTB ^=1<<PINB5; //1<<PINB5 is same as 0b00100000, so this toggles bit five of port b which is pin 13 (red led) on Arduino
}
}
一切正常,但 TCNT1 最多只能计数 255。如果我将 if 语句中的值设置为更高的值,则 if 语句中的代码永远不会执行。 Timer1 应该是一个 16 位定时器,所以为什么计数停止在 255 是没有意义的。arduino 是在幕后做些什么来搞砸这个吗?在 youtube 上的示例中它似乎工作得很好(没有 arduino)。
【问题讨论】: