我为带有Atmega328和通用16x2 LCD的Arduino使用以下代码。 LCD在工作,但始终显示定时器1计数器TCNT1的起始值“ 333”。为什么?我已经一遍又一遍地阅读了328的数据表,但是我不明白。
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int lcdContrastPin = 6, lcdBackligthPin = 10;
void setup()
{
// tutn on LCD backlight and contrast
pinMode(lcdContrastPin, OUTPUT);
pinMode(lcdBackligthPin, OUTPUT);
// fine-tuning contrast could be done by PWM on lcdContrastPin
digitalWrite(lcdContrastPin, LOW);
digitalWrite(lcdBackligthPin, HIGH);
lcd.begin(16, 2);
// configure Timer1
TCCR1A = 0; // no waveform generation
TCCR1B = 0x00000010; // frequency divider 8 (i.e. counting with 2 MHz)
TCCR1C = 0;
TIFR1 = 0x00100000; // clear Input Capture Flag
TCNT1 = 333;
}
void loop()
{
int currentTimerValue = TCNT1;
lcd.setCursor(0, 0);
lcd.print("TCNT1=");
lcd.print(currentTimerValue);
lcd.println(" ");
delay(50);
}
答案 0 :(得分:1)
愚蠢的我!出于意识失落,我将0x00000010当作二进制数而不是十六进制数。结果,我将所有时钟选择位都设置为0,这意味着计时器停止。
在将0x00000010替换为0b00000010(真实的二进制数)之后,现在一切都将按预期工作:
TCCR1B = 0b00000010; // frequency divider 8 (i.e. counting with 2 MHz)
TCCR1C = 0;
TIFR1 = 0b00100000; // clear Input Capture Flag