Author Topic: PIC MCU CCP module help  (Read 2586 times)

0 Members and 1 Guest are viewing this topic.

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
PIC MCU CCP module help
« on: February 10, 2014, 07:35:47 am »
I am trying to test the CCP module on a PIC18F2550. I have written the code & have connected an LED to the RC1 pin (CCP1 pin). I have configured the module in compare mode & I am using timer 3 as the time base for the module. So the expected output is a PWM wave at 50% duty cycle. However, I am getting a pure DC output of 5V (since I defined the initial value on RC1 as high). Can someone tell me where I am going wrong please.

Here is the code I used -

void interrupt isr(void);
void sys_init(void);
void CCP1_compare_init(void);
void timer3_init(void);
void interrupt_init(void);
void ccp1_isr(void);
void delay(unsigned int x);

void main()
{
    sys_init();         //initialize the system
    while(1)
    {
        while(TMR3IF==0);
        if(TMR3IF==1)
        {
            TMR3H=0x00;
            TMR3L=0x00;
            TMR3IF=0;
        }
    }
}
//overall system initialize function
void sys_init(void)
{
    CCP1_compare_init();      //initialize CCP1
    timer3_init();            //configure T3CON to use timer 3 as time base for CCP1
    interrupt_init();         //enable interrupts
}
//CCP initialize function for compare
void CCP1_compare_init(void)
{
    TRISC=0x00;              //RC1 is output
    RC1=1;
    CCP1CON=0x02;            //toggle output on match
    CCPR1L=0x00;
    CCPR1H=0xFF;
    CCP1IF=0;                //interrupt flag
}
//timer 3 initialize function
void timer3_init(void)
{
    T3CON=0xC0;     //read/write timer register in a single 16-bit operation, timer 3 is time base for both CCP1 & CCP2, no prescale, use   as timer
    TMR3H=0x00;
    TMR3L=0x00;
    T3CON|=0x01;        //start timer
}
//interrupt initialize function
void interrupt_init(void)
{
    IPEN=0;        //disable priority
    GIE=1;         //enable interrupts
    PEIE=1;        //enable peripheral interrupts
    CCP1IE=1;      //enable CCP1 interrupt
}
//ISR
void interrupt isr(void)
{
    asm("CALL _ccp1_isr");
}
//CCP1 ISR function
void ccp1_isr(void)
{
    CCP1IF=0;         //reset interrupt flag
}
//delay function
void delay(unsigned int x)
{
    unsigned char y;
    unsigned int i;
    for(i=0; i<x; i++)
    {
        for(y=0; y<255; y++);
    }
}

Let me know if schematics or configuration bit settings I used are required.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->