Hi,
Recently I work with compare mode. This module work very good and is easy.
how it works:
- First you have to set the compare mode how the intterupt will be set. For example set for high on match and a value will put in ccp registers.
-The timer1 is compare agaist the CCPR1H and CCPR1L registers. When the a match was found then the CCP1IF is set. Then you can decide what to do (like a change on low on match) set again the ccp registers and the cycle repeat.
An practical example(I use the Hi-tec compiler 9.6):
void interrupt isr(void)
{
uint16_t value = 0;
if(CCP1IE && CCP1IF)
{
CCP_Mode_CtrlBits(CCP_Mode_Off);// Turn CCP off (as fast as possible)
FlagIntCCP1IF(OFF); //reset the ccp flag
Timer1Control(OFF); //stop timer1
Timer1RegisterReset(); //reset timer1 registers
switch(CCP_State)
{
case LowOnMatch:
CCPR1H = HIGH_BYTE(Registers.LowOnMatchValue); //load the new time
CCPR1L = LOW_BYTE(Registers.LowOnMatchValue);
CCP_Mode_CtrlBits(CCP_Mode_CompareHighOnMatch); //set for high on match
CCP_State = HighOnMatch; //go to next state
break;
case HighOnMatch:
CCPR1H = HIGH_BYTE(Registers.HighOnMatchValue); //load the new time
CCPR1L = LOW_BYTE(Registers.HighOnMatchValue);
CCP_Mode_CtrlBits(CCP_Mode_CompareLowOnMatch); //set for low on match
CCP_State = LowOnMatch; //go to next state
break;
};
Timer1Control(ON); //start timer1
}
}