Timer0 PIC16f88

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
automat2013
Posts: 2
Joined: Fri Jun 21, 2013 12:29 pm
Contact:

Timer0 PIC16f88

Post 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]

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Timer0 PIC16f88

Post 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.
Martin

Post Reply