如何使Timer1作为实时时钟更准确? [英] How to make Timer1 more accurate as a real time clock?
问题描述
我有 PIC18F87J11 与8 MHz振荡器,我正在使用timer1作为实时时钟。在这一刻,我每隔1分钟就可以切换LED。我注意到它在前几次工作完美很好,但慢慢地每59秒开始切换LED。然后每隔几分钟就会下降到58,57等。我不知道是不是可以使用内部振荡器获得准确的时钟,或者我需要外部振荡器。我的设置看起来对于timer1,我只是希望我可以解决这个问题与当前的硬件。
I have PIC18F87J11 with 8 MHz oscillator and I am using timer1 as real time clock. At this moment I have it toggle an LED every 1 minute. I noticed it does work perfect fine the first few times but slowly it starts toggling the LED every 59 seconds. Then every few minutes it keeps going down to 58, 57, etc. I don't know if its impossible to get an accurate clock using internal oscillator or if I need external oscillator. My settings look right for timer1, I just hope I can resolve this issue with the current hardware.
预分频器1:8,TMR1 Preload = 15536,实际中断时间:200 ms
Prescaler 1:8, TMR1 Preload = 15536, Actual Interrupt Time : 200 ms
// Timer 1 Settings
RCONbits.IPEN = 1; // Enable interrupt system priority feature
INTCONbits.GIEL = 1; // Enable low priority interrupts
// 1:8 prescalar
T1CONbits.T1CKPS1 = 1;
T1CONbits.T1CKPS0 = 1;
// Use Internal Clock
T1CONbits.TMR1CS = 0;
// Timer1 overflow interrupt
PIE1bits.TMR1IE = 1;
IPR1bits.TMR1IP = 0; // Timer 1 -> Low priority interrupt group
PIE1bits.TMR1IE = 1; // Enable Timer1 interrupt
// TMR1 Preload = 15536;
TMR1H = 0x3C;
TMR1L = 0xB0;
中断例程
void interrupt low_priority lowISR(void) {
if (PIR1bits.TMR1IF == 1) {
oneSecond++;
if (oneSecond == 5) {
minute_Counter++;
if (minute_Counter >= 60) {
// One minute passed
Printf("\r\n One minute Passed");
ToggleLed();
minute_Counter = 0;
}
oneSecond = 0;
}
// TMR1 Preload = 15536;
TMR1H = 0x3C;
TMR1L = 0xB0;
PIR1bits.TMR1IF = 0;
}}
推荐答案
您链接的数据表 ,2.5.3内部振荡器输出频率和调谐,p38
There are some stuff in the datasheet you linked, "2.5.3 INTERNAL OSCILLATOR OUTPUT FREQUENCY AND TUNING", on p38
数据表说
INTOSC频率可能随着V DD或/或温度变化而漂移。
The INTOSC frequency may drift as VDD or temperature changes".
是V
它注意到通过调整OSCTUNE寄存器来处理这种情况的三种方法,其中三种将需要一个外部振荡器:
It notes three ways to deal with this by tuning the OSCTUNE register. The three of them would need an external "oscillator" :
- 处理EUSART的错误...此信号应来自某处。
<您可以使用任何稳定的交流信号作为输入。
祝你好运!
这篇关于如何使Timer1作为实时时钟更准确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!