我正在使用带有中断和计时器的程序进行倒计时: 我正在使用内部振荡器(带PLL的4MHz x 4)。
1)中断的频率与我想要达到的频率(1000 Hz)不一致。
2)如果我在char计数器中修改变量int counter变量,它就不起作用。
#include <xc.h>
#include "afficheur.h"
#include "eeprom.h"
#include <stdio.h>
#include <string.h>
#include "pause.h"
#include "timer2.h"
#define _XTAL_FREQ 16000000
//unsigned int counter = 0; //work with bad frequency
unsigned char counter = 0; //doesn't work
void interrupt isr2 (void) {
if (INTCONbits.TMR0IF == 1 )
{
TMR0 = 6;
INTCONbits.TMR0IF = 0;
++counter;
}
}
void timerCount(void)
{
LCD_init();
initTimer();
while(left_time!=0)
{
if (counter == 0)
{
display(left_time);
}
updateTimer();
}
}
void initTimer(void)
{
T0CON &= 11000011 ;
TMR0 = 6;
INTCON = 0; // clear the interrpt control register
INTCONbits.TMR0IE = 1; // bit5 TMR0 Overflow Interrupt
INTCONbits.TMR0IF = 0;
INTCONbits.GIE = 1; // bit7 global interrupt enable
}
void updateTimer(void)
{
if (counter == 1000) //1000 milliseconds => 1 second
{
counter = 0;
left_time-=1;
}
}
void display(int time)
{
char str [20];
char h_left [10];
char m_left [10];
char s_left [10];
char compt [10];
//time calculation
sprintf(m_left, "%02d", time/60);
sprintf(s_left, "%02d", time%60);
sprintf(h_left, "%02d", (time/60)/60);
sprintf(m_left, "%02d", (time/60)%60);
sprintf(compt,"%04d",counter);
//**time display HH:MM:SS
char *h_disp = h_left;
char *m_disp = m_left;
char *s_disp = s_left;
char *compt_disp = compt;
strcpy(str, h_disp);
strcat(str, ":");
strcat(str, m_disp);
strcat(str, ":");
strcat(str, s_disp);
strcat(str, ":");
strcat(str,compt_disp);
//lcd rendering
command(0xC3);
LCD_write("Temps restant:");
command(0x9A);
LCD_write(str);
//LCD_write(compt_disp);
command(0xD4);
}
我事先感谢你的帮助,
克里斯