Page 1 of 1

Timer0 PIC16f88

Posted: Fri Jun 21, 2013 12:42 pm
by automat2013
Hy.
I want to generate two diferent delays, using timer0 pic16f88.
step 1 : led 1 stay ON 4 sec
step 2 : led 1 stay OFF 3 sec

By the way I use a Matrix multimedia development board.

Look at my cod : in this moment the led 1 stay ON - 4 sec and then stay OFF - 4 sec.
I know that my mistake is in interrupt function, but i am not able to correct this mistake.

[code][/code]
#include <system.h>
#pragma DATA _CONFIG1, _EXTRC_CLKOUT & _WDT_OFF & _LVP_OFF

unsigned int counter=0;

void interrupt()
{
if(intcon & 0x04)
{
clear_bit(intcon, 2);
counter++;
if(counter == 3906)
{
portb = ~portb;
counter=0;
}
}
}

void main()
{
trisb = 0xf0;
portb = 0;
tmr0 = 0;
cmcon = 0x07;
option_reg = 0b00000001; // prescaler = 4
intcon = 0b10100000;
while(1)
{

}
}
[code][/code]

Re: Timer0 PIC16f88

Posted: Fri Jun 21, 2013 6:01 pm
by medelec35
If it was me, I would do something like:
if(counter == 3906)
{
portb = ~portb;
if (portb==1)
counter=0;
Else
counter=976;

This may not be formatted correctly but the principle is if port b is 1 then time for 3 seconds so counter starts off at 976 and not 0
else if port b is 0 then 4 seconds is required so set counter to 0.

Are you entering this directly in a flowcode C box?

Are you running with a external osc with a clock speed of 4MHz?
If not then timing will be wrong.