need help to use PWM on the CCP module on pic

Thread Starter

chaosenvoy

Joined Aug 6, 2017
54
C:
//30th May 2021
//pic 18f2550
//Pickit2 programmer
//MikroC Pro compiler
// PWM using ccp module

void InitTimer2 () {
T2CON = 0b00000010; //TMR2 is off 1:16 prescale, no postscale
CCP1IF_bit = 0; //CCP1IF is disable
CCP1IE_bit = 0; //CCP1IE is enable
CCP1IP_bit = 1; //CCP1IP is enabble
CCP1CON = 0b00001100; //CCP1CON is set to PWM mode
CCPR1L = 0b000011; //CCPR1L is loaded with 12 plus 2 bits
PR2 = 250; // PR2 load value
TMR2ON_bit =1; //turn on TMR2

}
void interrupt() {
    if (CCP1IF_bit){ //if CCP1IF interrupt flag is 1
TMR2IF_bit =0;
CCP1IF_bit = 0; //reset CCP1IF_bit to 0

}
}
void main() {

OSCCON = 0b1111110; //internal oscilator is set to 8Mhz
TRISC = 0b00000000; // make all PORTA pins output
PORTC = 0; //turn off PORTA pins
InitTimer2 ();
while (1){
}
}
 
Last edited by a moderator:

Thread Starter

chaosenvoy

Joined Aug 6, 2017
54
C#:
//30th May 2021
//pic 18f2550
//Pickit2 programmer
//MikroC Pro compiler
// PWM using ccp module

void InitTimer2 () {
T2CON = 0b00000010; //TMR2 is off 1:16 prescale, no postscale
CCP1IF_bit = 0; //CCP1IF is disable
CCP1IE_bit = 0; //CCP1IE is enable
CCP1IP_bit = 1; //CCP1IP is enabble
CCP1CON = 0b00001100; //CCP1CON is set to PWM mode
CCPR1L = 0b000011; //CCPR1L is loaded with 12 plus 2 bits
PR2 = 250; // PR2 load value
TMR2ON_bit =1; //turn on TMR2

}
void interrupt() {
    if (CCP1IF_bit){ //if CCP1IF interrupt flag is 1
TMR2IF_bit =0;
CCP1IF_bit = 0; //reset CCP1IF_bit to 0

}
}
void main() {

OSCCON = 0b1111110; //internal oscilator is set to 8Mhz
TRISC = 0b00000000; // make all PORTA pins output
PORTC = 0; //turn off PORTA pins
InitTimer2 ();
while (1){
}
}
Hello i have been trying to create a PWM on the CCP1 pin of my pic 18f2550 but i can't seem to get it to work, i have read the data sheet & watch some videos & i'm not getting anything out on the pin. my code is below & i would really appreciate any help i can get, thank you in advance.
Sorry still trying to post for help properly.
 

hexreader

Joined Apr 16, 2011
581
I do it using PWM library.
Be sure that there is a tick against PWM option in library manager

Code:
// 22 June 2021   PIC18F2550
// PICkit2 programmer    MikroC Pro compiler
// PWM using CCP module with output on RC2

void main() {
    OSCCON = 0b1111110;                               // internal oscilator is set to 8Mhz
    TRISC = 0;                                        // all output
    PORTC = 0;                                        // all low
    PWM1_Init(1000);                                  // choose frequency here
    PWM1_Set_Duty(127);                               // choose mark/space ratio here
    PWM1_Start();                                     // start PWM1 running
  
    while (1);                                        // loop forever (not strictly necessary for mikroC)
}
 

peterdeco

Joined Oct 8, 2019
484
I found it very hard for me to write programs in different languages. That's why I chose Basic. This one line sets CCP port, dutycycle and frequency all in one.

HPwm 1, 127, 15000
 

Papabravo

Joined Feb 24, 2006
21,159
Hello i have been trying to create a PWM on the CCP1 pin of my pic 18f2550 but i can't seem to get it to work, i have read the data sheet & watch some videos & i'm not getting anything out on the pin. my code is below & i would really appreciate any help i can get, thank you in advance.
Sorry still trying to post for help properly.
After reading the datasheet, did you make a checklist of all the things that are required to make the CCP module work?
It is often the case, that with a limited number of pins on a package, each pin will have multiple functions, so it is NOT enough to enable the function you want, you must also disable the default or the alternate function in order to get what you want. It always pays to understand the default behavior or configuration of each and every pin on a part. There are no shortcuts.
 
Last edited:

Thread Starter

chaosenvoy

Joined Aug 6, 2017
54
I do it using PWM library.
Be sure that there is a tick against PWM option in library manager

Code:
// 22 June 2021   PIC18F2550
// PICkit2 programmer    MikroC Pro compiler
// PWM using CCP module with output on RC2

void main() {
    OSCCON = 0b1111110;                               // internal oscilator is set to 8Mhz
    TRISC = 0;                                        // all output
    PORTC = 0;                                        // all low
    PWM1_Init(1000);                                  // choose frequency here
    PWM1_Set_Duty(127);                               // choose mark/space ratio here
    PWM1_Start();                                     // start PWM1 running
 
    while (1);                                        // loop forever (not strictly necessary for mikroC)
}
thank you all for your response @hexreader i know howto do it that way but i just wanted to learn how to use the CCP module on my pic, so i was trying to use the PWM option then the Compare option, just a learning process.

@Papabravo yes i look at the datasheet & the 5 steps in section 15.4.4 says,
1# set the PWM period by writing to PR2
PR2 = 250 ;
2# set PWM duty cycle by writing to CCPR1L & the CCP1CON<5:4> bits
CCPRIL = 0b000011; and CCP1CON = 0b00001100; or do i need to write to CCP1CON<5:4> on a separate line
3# make CCP1 pin as an out put
TRISC= 0b00000000;
4# set TMR2 prescaler & turn on TMR2
T2CON = 0b00000010; set the prescaler and TMR2ON_bit =1; turn on the timer
5# configure CCP1 module for PWM mode
CCP1CON = 0b00001100;

do i need to write all 5 steps in their own line like this but besides the PR2 =250; from step #2
step 2# CCPR1L = 0b000011;
CCP1CON = 0b000000;
step 3# i have in main
step 4# T2CON = 0b00000010; set to 1:16 prescale no postscaler and TMR is off
TMR2ON_bit =1; to turn on timer 2
step 5# CCP1M3_bit =1;
CCP1M2_bit =1;
CCP1M1_bit =0;
CCP1M0_bit =0;
to set to PWM mode
Thank you again for all the help & the replies.
 

geekoftheweek

Joined Oct 6, 2013
1,201
Your original program should actually work from what I remember from earlier projects. You don't need the interrupt handler or need to do anything interrupt wise when using PWM. There is the option with CCP1 to remap it to PORTB, 3 using config bits. Check both RC2 and RB3.

Not related to this exactly, but whenever writing to a pin use LATA, LATB, etc... instead of PORTx. LATx for write, and PORTx for read.
 

Thread Starter

chaosenvoy

Joined Aug 6, 2017
54
@click_here thank you again i had that page bookmark i got it working & my old code is working, my problem was hardware lol, here is my working code,
thanks everybody for the help.

C:
// 23rd June 2021
// pic 18f2550
// MikroC pro compiler
// Pickit2 programmer
// PMW mode using CCP module




void main (){
 OSCCON = 0b01000010; // internal oscillator set to run at 1Mhz
 TRISC = 0b00000000;  // all P0RTC pins are output
 PORTC = 0b00000000;  // turn off all pins on PORTC
 PR2 = 156;           // set PR2 value
 CCPR1L = 118;        // duty cycle is at 75%
 T2CON = 10;          // prescaler is set at 1;16 no postscaler
 CCP1CON = 12;        // CCP module is set to PWM mode
 TMR2 =0;             // TMR2 starts from 0
 TMR2ON_bit = 1;      // turn on timer
 
 while(1);

}
 
Top