PIC-Timer0和中断以计数并闪烁LED

时间:2019-01-09 06:15:56

标签: pic

我正在使用带有中断的PIC10F322和Timer0,试图使LED闪烁,先点亮1秒,然后再熄灭1秒。我曾尝试对要求中断的计时器进行编码,并且它工作良好。但是时间的计算是不正确的。计数大约为2s,然后再关闭2s,这是不正确的。我想知道计算有什么问题吗?芯片为16MHZ,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;
}

1 个答案:

答案 0 :(得分:0)

您的计时器计算很好,但是我猜您的微控制器正在以8 MHz运行,如果您使用内部振荡器(#pragma config FOSC = INTOSC),则这是复位后的默认值。如果您喜欢16 MHz,则必须在OSCCON寄存器中进行选择。

OSCCON = 0b01110000;