Home > Electronics > Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h)

Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h)

Pulse Width Modulation (PWM) is used everywhere from motors to communication! It uses timer as its foundation to toggle the pins high-low at a defined period (1/frequency) and duty cycle (10 bit resolution – 210 = 1024 steps). If you use high frequency, then you won’t get the 10 bit resolution. Read the document bellow to find more information.

Read this document and you’ll know a lot about PWM : http://ww1.microchip.com/downloads/en/devicedoc/31014a.pdf

So in previous tutorial we saw about using a timer. Now my task is to make a LED do the heart beat effect (gradually increase/decrease the brightness of an LED using PWM).

To set a PWM:

  • Setup the timer (to the frequency/period)
  • Set the duty cycle (you will be using the functions from pwm.h)

    So I am going to set the PWM frequency to 2KHz (since using 8MHz as clock source and prescale of 16, getting less than 2KHz is difficult), so the formula according to the datasheet will be  (we are going to find PR2)

PWM period = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value)

where PWM period = 1/ Frequency (that will be 1/2000 = .0005)

.0005 = [PR2 + 1] • [1 / 8000000] • 16

PR2 + 1 = [.0005 • 8000000] / 16

PR2 + 1 =  250

PR2 = 249

PR2 = 0xF9 ( 249 in hex)

 

Note : Read the datasheet and peripheral library document. PWM1 uses Timer 2. You can use other timers as source for your PWM, but read and make sure if you can or can’t in the datasheet of the device you are using.

To set timer2 for PWM, we just need to set the presale value

unsigned char Timer2Config = T2_PS_1_16; OpenTimer2(Timer2Config);

And then open the PWM

OpenPWM1(0xF9); //2KHz

So here is the whole sample PWM program and the video below it

#define _XTAL_FREQ 8000000 //The speed of your internal(or)external oscillator #define USE_AND_MASKS #include <xc.h> #include "config.h" #include <plib/timers.h> #include <plib/pwm.h> int i = 0; int countup = 1; //our flag to set up/down dutycycle int DutyCycle = 0; unsigned char Timer2Config; //PWM clock source void SetupClock(void); void main(int argc, char** argv) { SetupClock(); // Internal Clock to 8MHz TRISCbits.RC2 = 0; //PWM pin set as output Timer2Config = T2_PS_1_16; //prescale 16 OpenTimer2(Timer2Config); OpenPWM1(0xF9); //Open pwm at 2KHz while(1) //infinite loop { if(countup == 1) { SetDCPWM1(DutyCycle); //Change duty cycle DutyCycle++; if(DutyCycle == 1024) countup = 0; } else { SetDCPWM1(DutyCycle); //Change duty cycle DutyCycle--; if(DutyCycle == 0) countup = 1; } __delay_ms(1); } } void SetupClock() { OSCCONbits.IRCF0 = 1; OSCCONbits.IRCF1 = 1; OSCCONbits.IRCF2 = 1; }

And here is the video

  1. October 4th, 2017 at 08:20 | #1

    Good day! Would you mind if I share your blog with my
    facebook group? There’s a lot of folks that I think would really appreciate your content.
    Please let me know. Cheers

  2. fabm
    August 3rd, 2015 at 16:35 | #2

    @singularengineer
    Hi,
    I use PIC18F8722 mcu.
    but i can’t find the library with the functions as putsUSART, openUSART, etc. are they included in the compilers? Please send me the link to let me use these functions in my code of USART, thank you.

    Fabm

    • August 3rd, 2015 at 16:37 | #3

      Yes, included with your compiler installation. You can find the documentation file in C:\Program Files (x86)\Microchip\xc8\{compiler-version}\docs\MPLAB_XC8_Peripheral_Libraries.pdf

    • August 3rd, 2015 at 16:40 | #4

      Ok.. now I understand your question. Your part has two UART ports. So you have to use puts1USART and puts2USART for which one you are trying to use. Similarly Open1USART to open USART1 and Open2USART to open USART2. It is all mentioned in the post.

  3. ionutu
    January 10th, 2015 at 13:56 | #5

    salve a tutti
    come si fa a creare il file config.h e dove devo meterlo?
    grazie

  4. Jose
    May 7th, 2014 at 10:51 | #7

    Hi,
    I can’t find config.h
    When I compile, the error message is shown “plib / pwm.h: 32: error: (141) can not open include file” pconfig.h “: No such file or directory”.
    What library is this?

    • May 19th, 2014 at 13:41 | #8

      You got to create your own config.h. This link will help you to create your config.h file

      plib/pwm.h –> plib is a folder where you got a bunch of microchip’s library header files. Use angular bracket.

  5. February 26th, 2014 at 08:24 | #9

    Hi, very nice post and explanation.
    One thing I’m not too clear on: When you used OpenTimer2(), you passed it an argument of Timer2Config, which you set to be the defined macro containing the prescale settings.

    When I look at the Xc8 standard peripheral library documentation however, the bit about OpenTimer2 (page 1124) says that the arugment to that function should be all of the a number of configuration settings ORed or ANDed together. If you scroll down to the input parameter bit it says these should be not only Prescale Value, but also postscale value and Enable Timer2 Interrupt.

    In addition. in the example they provide in that same document (page 1147), OpenTimer1() (which I appreciate is a different function with, but still works in the same way in relation to how its input parameter is defined), is written like
    config1 = T1_8BIT_RW | T1_SOURCE_EXT | T1_PS_1_8
    | T1_OSC1EN_ON | T1_SYNC_EXT_ON | TIMER_INT_ON;

    So my question is, how and why can you get away with only using the prescaler value as the input to ur OpenTimer2 function, without anding/oring it with the other configuration setting values? Or am I misunderstanding it?

    Thanks in advanced 🙂

    • March 15th, 2014 at 01:26 | #10

      Sorry about the delayed response.

      I’m not sure if I understood your question, but let me guess. Why am I not using all the parameters? By default they all have 1:1 on scale value. Unless you want it to be changed there is no need to add that parameter. In simple words, just AND/OR only the parameters you use/change.

      Hope I answered your question.

  6. Ruben
    November 9th, 2013 at 16:48 | #11

    @mihaistoica
    Could it be because you’re using plibs? I would recommend you not to use plibs. They can be an easy way for programming, but they come with a lot of trouble when you can’t use them. I think it is due to “ifdefs”. Try looking at the plib document and change the code to write/read the registers instead of using the plibs.
    Please, give your feedback.

  7. mihaistoica
    November 9th, 2013 at 14:27 | #12

    @singularengineer

    Let me be more accurate. I tried with MPLABX 1.95 and XC8 1.21
    Your code compiled successfully with PIC18F4550, with error with PIC18F45K50 (same error “:0: error: undefined symbols:”
    Try. It takes 5 minutes

    • November 11th, 2013 at 01:02 | #13

      Well, please read the datasheet.

      18F4550 – has CCP1 and CCP2
      18F45k50 – has ECCP1 and CCP2

      Ref : Chapter 15 in both the datasheets. and read C:\Program Files (x86)\Microchip\xc8\v1.21\docs\MPLAB_XC8_Peripheral_Libraries.pdf on how to use ECCP

  8. mihaistoica
    November 5th, 2013 at 04:06 | #14

    Hello

    I tried your code using MPLABX for PIC18F4550. It compiles fine
    With PIC18F45K50 it does not. I receive an error : “:0: error: undefined symbols:”
    Why am I telling you this ? Because I am working with PIC18F45K80 and I am receiving the same error
    How can I fix it ?

    Thanks in advance

    • November 7th, 2013 at 00:19 | #15

      Hmm.. very limited information has been presented. Need more info (complete compiler output) and your code. Will take ages for me to check back in the comments section. Microchip forum is a place where you will get solution in few hours. If you already found a solution, please reply so that it will be useful for others.

      Thanks.

  9. October 24th, 2013 at 15:26 | #16

    @Dave
    You are right. I missed it. Editing a post is difficult since the format messes up. Will make sure wont’ happen again. Thanks for looking in.

  10. Dave
    October 24th, 2013 at 12:02 | #17

    Looks like you dropped the 4 in the equation.
    0.0005 = [PR2 + 1] * 4 * [1/8000000] * 16
    so PR2 = 61.5

  11. Ruben
    August 30th, 2013 at 05:50 | #18

    @singularengineer
    Hahaha. Sorry, i forgot to write the rest of the code. I wanted to say the code line “Timer2Config T2_PS_1_16”. It doesn’t accept the T2_PS_1_16 because timer2 isn’t enabled in timers.h plib. The code it’s in grey unlike the timer0 that is in blue and black.

  12. August 30th, 2013 at 00:45 | #19

    Timer2Config is a variable (look above the main function where a bunch of variables are declared)

    unsigned char Timer2Config;

  13. Ruben
    August 29th, 2013 at 19:38 | #20

    Hi. I can’t use/access the code “Timer2Config”. I opened the library and i found that the corresponding code to timer2 is written in grey and don’t have that comment trace and asterisk /*. Do you possibly know why that is and how i can enable the timer2 plib easy coding?

    Thanks in advance.

  1. No trackbacks yet.