Hy.
I want to make a program who toggle a led using compare mode, set output on match (CCP1IF bit is set).
I dont know if it is possible what i want to do. Anyway the RB0 is always ON.
I know, it must be ON because when the value loaded in ccpr1 register matches with the value of tmr1 register the RB0 is set.
But if want to reset the RB0 output ?
I have doing this in interrupt() function writing the next statement : ccp1con=0b00001001;
And now the RB0 stay always OFF.
#include <system.h>
#pragma DATA _CONFIG1, _EXTRC_CLKOUT & _WDT_OFF & _LVP_OFF
void interrupt()
{
if((pir1 & 0x01) && (pir1 & 0x04))
{
clear_bit(pir1, 0); // clear TMR1IF
clear_bit(pir1, 2); // clear CCP1IF
ccp1con = 0b00001001; // clear output on match
}
}
void main()
{
trisb = 0xf0;
portb = 0x00;
tmr1h = 0;
tmr1l = 0;
ccpr1l = 0x60;
ccpr1h = 0xEA;
cmcon = 0x07;
t1con = 0b00000001; // prescaler = 1
intcon = 0b11000000;
pie1 = 0b00000101;
ccp1con = 0b00001000; // set output on match
while(1)
{
}
}
Best Answer
I've done this by having the compare match trigger an interrupt, and then the interrupt toggles the pin by reading the data.
Sorry, my example is in assembler, but you should get the gist:
(inside the interrupt service code)
(in the setup code)
then turn on timer 1 when you are ready to start toggling
Good luck with your project.