pic16f917 - problem with Tmr0 incrementation

Status
Not open for further replies.

mircea2012

Newbie level 4
Joined
Jan 2, 2013
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,321
Hy everybody.

I want to make a programm who accomplish the next tasks : generate a
delay of 3 seconds.
The RD7 pin must stay ON 3 seconds, then must stay OFF 3 seonds, and so
on.

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. ( Period = 1 / freq; internal instruction cycle is 1
microsec).

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.

#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.
I dont know if my computations is good.
All the best.
 

Place a break point at the following place and have a free run to the code. When the conditions are matched MPSIM automatically stop to the following place

PORTDbits.RD7 ^= 1; // toggle the RD7 pin
 
I made some modification : if(counter == 46)
{
PORTDbits.RD7 ^= 1
counter = 0;
}
but still no works.
I use the MPLAB IDE, and compiler is HI TECH ANSI C Compiler.
I am begginer in microcontroller programming and believe me I struggled with this program a couple of days.
I dont know what I supposed to do to work this program.
I have a board from microchip PICDEM Mechatronics.
Thank you for your interest.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…