- This topic has 0 replies, 1 voice, and was last updated 11 years, 8 months ago by .
Viewing 1 post (of 1 total)
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.
|
Microcontroller › PIC › Timer0 PIC16F917
freq. out = freq. clk / (Prescaler*(256 – TMR0)*count)
In my program I work with internal clk (T0CS=0) so the freq. clk is 4Mhz/4 = 1Mhz. (internal instruction cycle is 1 microsec).
I want to generate a delay of 3 seconds.
The RD7 pin must stay 3 seconds ON, then must stay 3 seonds OFF and so on.
freq. out = 1 / T = 1 / 3 sec = 0.33Hz and replacing 0.33Hz = 1MHz / (256 * (256 – 0) * count)
count = 1MHz / 65536 * 0.33Hz
count = 46.
In previous programm I made some mistakes. I do not set up the interrupts : INTCONbits.T0IE = 1; // Timer 0 Interrupt Enable is set
INTCONbits.GIE = 1; // Global Interrupt Enable is set
#include<pic.h>
unsigned char counter;
void interrupt Timer0_ISR(void)
{
if ( T0IE && T0IF ) // are TMR0 interrupts enabled and//is the TMR0 interrupt flag set?
{
T0IF = 0; // TMR0 interrupt flag must be cleared in software
++counter; // increment the counter variable by 1
if(counter == 46)
PORTDbits.RD7 ^= 1; // toggle the RD7 pin
}
}
Init(void)
{
TMR0 = 0; // clear the TMR0 register
OPTION_REG = 0B00000111; // Use the internal instruction clock, choosing to work with a Prescaler (1 : 256)
INTCONbits.T0IE = 1; // enable TMR0 overflow
INTCONbits.GIE = 1; // enable Global interrupts
}
main(void)
{
TRISD = 0B01111111;
PORTDbits.RD7 = 0;
Init();
while(1)
{
}
}
I simulate this program with MPLAB SIM but timer0 dont want to increment.
Please help me.
All the best.