PWM module of PIC18f46k22

Hayee

Member level 3
Joined
Mar 4, 2022
Messages
57
Helped
1
Reputation
2
Reaction score
3
Trophy points
8
Activity points
572
Hi
I am using CCP Module of PIC18f46k22 to generate PWM signal on CCP2 (Pin no 16) . All is working well if i load values only in CCPR2L register in run time and load the value in CCP2CON at starting of program.
but when I try to change the value of CCP2CON while running then there is no PWM on Pin.
I want to know that can I change the CCP2CON register value while running.
I want to check it weather it works or not because datasheet says that PWM is of 10 bit. Upper 8-Bits are in CCPR2L and Lower 2-Bits are in CCP2CON <5:4>
Kindly clear me if I am wrong.

Code:
void main(void) {
    
    uint8_t count = 0;
    uint8_t temp = 0;
    uint8_t temp2 = 0;
    uint8_t value = 0;
    
    uint8_t pwmArray[] = {71,73,76,79,81,83,86,89,92,96,97,101,104,107,109,112,114};
    
    OSCILLATOR_Init();
    PORT_Init();
    SPI1_Init();
    
    LCD(&PORTD, 0,1,2,3,4,5);   // set the connection between the MCU and the LCD
    LCD_Begin(16,2);            // initialize the LCD module with 16 rows and 2 columns (1602 LCD)
    
    // EUSART Settings   
    // Port, TXSTA1, RCSTA1, BAUDCON1, BaudRate Value
    setup_uart(UART_1, ASYNC_MODE | TXMIT_EN | HIGH_SPEED, SPEN_EN | RCVR_EN, BAUD_16BIT, 9600);
    setup_uart(UART_2, ASYNC_MODE | TXMIT_EN | HIGH_SPEED, SPEN_EN | RCVR_EN, BAUD_16BIT, 9600);
    
    setup_timer_3(T3_DIV_8 | T3_NO_SYNC_EXT_CLK | T3_ENABLE);
    set_timer_3(T3_Loadvalue);
    
    setup_timer_1(T1_DIV_1 | T1_NO_SYNC_EXT_CLK | T1_ENABLE);
    set_timer_1(T1_Loadvalue);
    
    setup_pwm();
    setup_timer_2(T2_ENABLE);
    set_timer_2(T2_Loadvalue);
    
    
    enable_interrupt(INT_TIMER1);
    enable_interrupt(INT_TIMER3);
    
    PERIPHERAL_Interrupts(ENABLE);
    GLOBAL_Interrupts(ENABLE);
    
    while(1)
    {
        if(!PORTBbits.RB7){
            while(!PORTBbits.RB7);
            
            if(count > 17) count = 0;
            else count++;
            
            value = pwmArray[count];
            
            temp = (uint8_t)(value << 4);
            temp = CCP2CON | temp;
            CCP2CON = temp;

            temp2 = (value >> 2);
            CCPR2L = temp2;
            
            LCD_Goto(1,2); printf("%x", temp);
            LCD_Goto(7,2); printf("%u", CCPR2L);
        }
    }
    return;
}

Code:
void setup_pwm(){
    CCP2CON = 0x0C; // CCP2M P2A,P2C: active high; P2B,P2D: active high;
                    // DC2B 3; P2M single;   
    CCPR2H = 0x00;  // CCPR2H 0;     
    CCPR2L = 0x3C;  // CCPR2L 60;   
    
    CCPTMRS0bits.C2TSEL = 0x0;// Selecting Timer2
}

Frequency of Timer 2 is set to 20KHz and can be seen on Proteus, while to change the duty cycle i have to load the values in CCPR2L and CCP2CON registers.
 

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…