Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to do pwm with pic1865k22?

Status
Not open for further replies.
T

treez

Guest
Newbie level 1
We are using xc8 c compiler, in MPLAB.X
Any ideas appreciated.
I would have thought that all one had to do was set a value for pwm frequency by loading a particular register, then set a value for duty cycle by loading another register, and that should be it?...I mean, can it be any more complicated? There seem to be multiple jargonesque parameters that need setting through reading the pic datasheet, though we cannot assess what is the way to go.
Any ideas greatly appreciated.
 

i did not understand the question correctly, but i will try to answer.
Using timer2 and CCP module, PWM can be generated. The PR2 register of timer2 acts as period register and CCPR register of CCP module acts as duty cycle register. When timer2 increments and match interrupt occurs between TMR2 and CCPR register the output goes low and when TMR2 and PR2 matches the output goes high. for TMR2 match with PR2 interrupt, u can change the value of CCPR register for duty cycle change.
There are some modules/on-chip peripherals with PWM output in both normal and complementary output called COG or complementary output generator. You can find them in PIC16F161x devices with CWG, PIC16F170x with COG. They have built in blanking and dead band as well for complementary output to drive H bridge circuit.

For sure, one has to set other control register for any peripheral to set for operation which can be done only once i.e. during initialization. But TBH, the settings are very very simple to do. I managed to get PWM from CCP in just 2 hours and complementary PWM from COG in nearly same time.

Hope that helps.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
thanks, I thus assume that interrupts have to be enabled when using CCP8 pin for PWM?
 

Yes, the interrupt for timer2 match with PR2, it will give period interrupt, in this u can change both CCPR value as duty cycle change and PR2 value as period change.
You can also get an interrupt for TMR2 match with CCPR register. You can same or any other operation here as well.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Please check my pwm resolution calculation, from the datasheet...
I wish to use the PIC18F65K22 to get a pwm signal on the RE4 (ccp8) pin
of 25KHz frequency.
The chip oscillator is running at 4MHz.
From the equations on page 256 of the pic18f65k22 datasheet, it appears that I will only get 7 bit resolution. The actual on time (which they actually call the “duty cycle”) is set by 10 bits…so does this mean that the pic18f65k22 will only “look” at the upper 7 bits of that 10 bit value when setting the “on time”?

Pic18f65k22 datasheet
https://ww1.microchip.com/downloads/en/DeviceDoc/39960d.pdf
 

Also, please could you check that this code is correct to set up for PWM at 25KHz...
(its in microchip xc8 c in mplab.x)

Code:
//This function sets up the CCP module into PWM mode.
//The PWM frequency is set to 25KHz for the Sanyo Denki fan
//(9HV0848P1G001)
//Note FOSC= 4MHz
void    setup_CCP(void) {
    T2CON = 0x00;   //prescalar = 1;
    PR2 = 0x27; //(pwm period = 40us by equation page 255 of pic datasheet)
    CCPTMRS2 = 0x14;    //CCP8 based on TMR1/TMR2
    CCP4CON = 0x00; //Disable CCP4
    CCP5CON = 0x00; //Disable CCP5
    CCP6CON = 0x00; //Disable CCP6
    CCP7CON = 0x00; //Disable CCP7
    //CCP9CON = 0x00; //Disable CCP9
    //CCP10CON = 0x00; //Disable CCP10
    CCP8CON = 0x0C; //Enable PWM on CCP8, also set lower 2 bits of on-time

    CCPR8L = 0x00;  //zero duty (fan off) to start off with.
    T2CONbits.TMR2ON = 1;   //Enable the timer and thus the PWM.
//Note the following duty cycle settings.....
//CCP8RL = 0x0A (decimal 40 = 0.25 dutycycle
//CCP8RL = 0x14 (decimal 80 = 0.5 dutycycle
//CCP8RL = 0x1E (decimal 120 = 0.75 dutycycle
//CCP8RL = 0xFF (decimal >512  = 100% dutycycle
 

Hi,
Sorry for late reply. I was busy in some personal stuff. I made a simple code for using CCP4 in ur MCU to generate 25kHz PWM with variable duty cycle with 8-bit resolution. I have explained whatever i Can. If you have any issues with code, let me know.

Hope that helps.
Thanks

- - - Updated - - -

Also, for 4MHz of clock, all u need to do is change the values of PR2 and CCPR4L register with few other bits for complete 10 bit, along with the clock settings for internal oscillator. The code is using internal oscillator at 64MHz.
Also, I forgot to put infinite while loop in main and the timer 2 start bit in initialization. Plz do correct that. I was in hurry.
 

Attachments

  • PWM_test.X.rar
    1.9 KB · Views: 127
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top