我正在使用带中断的PIC10F322和timer0,尝试闪烁发光二极管,亮起1秒,然后熄灭1秒。我试着编写了一个叫中断的定时器,它运行良好。但是时间的计算是不正确的。计数持续了大约2秒,然后又关闭了2秒,这是不正确的。我想知道是不是计算有问题?芯片的主频为16 The,timer0为8位,预分频器设置为256。我的尝试是每1ms触发一次中断,然后执行999次循环计数,达到1秒。
我的计算是:
256 - [(Delay * Fosc) / (prescaler*4)] = 256 - [(1ms * 16000000)/(256*4)] = 240
#define _XTAL_FREQ 16000000
#include <xc.h>
#pragma config FOSC = INTOSC // Oscillator Selection bits (INTOSC oscillator: CLKIN function disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
#pragma config LPBOR = ON // Brown-out Reset Selection bits (BOR disabled)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
int z = 0,count=0;
void main(void) {
ANSELA = 0x00;
TRISA = 0b0100;
TRISAbits.TRISA2 = 1;
LATAbits.LATA0 = 0;
INTCONbits.GIE=1; /* Enable Global Interrupt*/
INTCONbits.PEIE = 1; /* Enable peripheral Interrupt */
OPTION_REGbits.T0CS = 0;
OPTION_REGbits.PSA = 0;
OPTION_REGbits.PS0 = 1; /* set prescaler to 256 */
OPTION_REGbits.PS1 = 1;
OPTION_REGbits.PS2 = 1;
OPTION_REGbits.INTEDG = 0;
TMR0 = 240;
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 1;
while(1){
}
return;
}
void __interrupt(high_priority) tcInt(void)
{
if (TMR0IE && TMR0IF)
{
TMR0 = 240;
TMR0IF = 0;
if (count == 999)
{
z = 0;
LATAbits.LATA0 = ~LATAbits.LATA0;
count =0;
}
count++;
}
return;
}发布于 2019-01-09 14:47:20
您的计时器计算很好,但我猜您的微控制器运行在8 MHz,这是复位后的默认值,如果您使用的是内部振荡器(#pragma config FOSC = INTOSC)。如果您喜欢16 MHz,则必须在OSCCON寄存器中进行选择。
OSCCON = 0b01110000;https://stackoverflow.com/questions/54104203
复制相似问题