Hi All,
This is my first time working on PIC chip, so I started by blinking an LED using timer and interrupt first. I wanted it to On for 1s and then Off for another 1s. I had tried coded the timer called for interrupt, and it work good. But the counting for time isn't correct. The counting was On for about 2s and then Off for another 2s, which is isn't correct. Is there something wrong with the calculation or is there a misunderstanding? The chip is 16MHZ, timer0 is 8 bits and no prescaler.
My formulas is:
Fosc/4 = 16MHZ/4 = 4MHZ ( no prescaler)
period for a tick= 1 / 4MHZ = 0.00000025s
time for 8 bit count = 0.00000025 * 256 = 0.000064s
Loop count = 1/0.000064 = 15625
my code:
Greatly appreciate your helps
This is my first time working on PIC chip, so I started by blinking an LED using timer and interrupt first. I wanted it to On for 1s and then Off for another 1s. I had tried coded the timer called for interrupt, and it work good. But the counting for time isn't correct. The counting was On for about 2s and then Off for another 2s, which is isn't correct. Is there something wrong with the calculation or is there a misunderstanding? The chip is 16MHZ, timer0 is 8 bits and no prescaler.
My formulas is:
Fosc/4 = 16MHZ/4 = 4MHZ ( no prescaler)
period for a tick= 1 / 4MHZ = 0.00000025s
time for 8 bit count = 0.00000025 * 256 = 0.000064s
Loop count = 1/0.000064 = 15625
my code:
Code:
#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;
unsigned long int 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 = 1;
//OPTION_REGbits.PS0 = 1; /* set prescaler to 256 */
//OPTION_REGbits.PS1 = 1;
//OPTION_REGbits.PS2 = 1;
OPTION_REGbits.INTEDG = 0;
TMR0 = 0;
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 1;
while(1){
}
return;
}
void __interrupt(high_priority) tcInt(void)
{
INTCONbits.TMR0IF = 0;
if (count == 15625)
{
z = 0;
LATAbits.LATA0 = ~LATAbits.LATA0;
count =0;
}
++count;
return;
}
Last edited: