PWM program structure

Discussion in 'Programmer's Corner' started by friskyy, May 16, 2011.

  1. friskyy

    Thread Starter New Member

    Mar 27, 2011
    13
    0
    Dear all,

    Could someone explain what is the general structure of a PWM program in C for a PIC. It would be great if you provide a sample code with some explanation next to it. I would also like to know how the duty cycle of the output is calculated.

    Thank you.
     
  2. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    PIC with a built-in PWM generator, or not?
     
  3. friskyy

    Thread Starter New Member

    Mar 27, 2011
    13
    0
    HI

    Yes, I have a 16 f 917 PIC with built-in PWM generator.
     
  4. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    Then what do you make of section 15.3 of the user manual?
     
  5. friskyy

    Thread Starter New Member

    Mar 27, 2011
    13
    0
    I looked at it but it's not very detailed and doesn't answer all of my questions. I am just starting with PICs.

    I found this program somewhere. Could you explain to me what it is doing:


    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. __CONFIG(UNPROTECT & BORDIS & PWRTEN & WDTDIS & INTIO &
    4. MCLREN & FCMDIS & IESODIS);
    5. #define _XTAL_FREQ 8000000
    6. void init(void)
    7. {.
    8. // Configure Ports directions: 1=input, 0=output
    9. TRISD = 0x00;
    10. TRISC = 0xFF;
    11. TRISA = 0xFF;
    12. // 8Mhz Internal Clock
    13. OSCCON = 0b01110000;
    14. // Turn off comparators
    15. CMCON0 = 0x07;
    16. //Turn on ADC
    17. ANSEL = 0b00000001; // select AN0 as an analogue channel (RA0)
    18. ADCON1 = 0b01010000; // select A/D conversation clock Fosc/16
    19. ADCON0 = 0b00000001; // left justified, Vss and Vdd as refs, AN0 is on
    20. // Clear PortD
    21. PORTD = 0x00;
    22. // PWM Setup
    23. PR2 = 0x9A;
    24. CCP2CON = 0b00001100; //capture /compare/pwm module
    25. CCPR2L = 5;
    26. TMR2ON = 1;
    27. }
    28. unsigned int ADCval;
    29. void main(void)
    30. {
    31. init();
    32. RD7 = 1;
    33. while (1){
    34. if(GODONE == 0){
    35. ADCval = ADRESH ;//0-1023 //A/D result high register
    36. ADCval = ADCval >> 2;
    37. CCPR2L = ADCval;
    38. GODONE = 1; // Start the next conversion
    39. }
    40. }
    41. }
    42.  
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Nope. We're not gonna read some code you got from somewhere that does something.

    Try this instead:Go to section 15.3 of the fine manual as John P suggested. Copy the set of instructions they have there, and paste it into your code where you would do the PWM setup. Highlight these lines, then right click, select Advanced, select Comment Block.

    Now you have an outline to follow. Try to convert each step they give you into code. See if it compiles, then see if it works (I'd do a simulation first). That's how I set up the PWM for my first time on a PIC.

    If that fails, then post YOUR code and YOUR ideas and we will try to help YOU.
     
  7. friskyy

    Thread Starter New Member

    Mar 27, 2011
    13
    0
    Interesting. If you don't want to help, then why do you even bother writing? If you have already learned how to program PICs then why don't you just explain it in plain English, instead of sending me to read some useless manual?The fact that you have had difficult experience with learning to program PICs doesn't mean that everyone else should.
     
  8. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    Sorry Friskyy, what we see here fairly often is someone who says "Show me how to do X Y and Z" when it's quite obvious that they've done nothing to try to figure it out themselves, they have no understanding of the techniques and components, and (most often) they don't even bother to give a good description of what they want to do. For example, without wanting to cause you pain, you didn't tell us which processor you plan to use, which is pretty fundamental! What gets people in the mood to help is if someone comes in and says "I've tried to accomplish something, I've got it working to some point, and the manual says whatever, can someone show me what to do next?"

    Honestly, the PIC manuals are not useless. The company makes money if we use its products, so it's to their advantage to be clear. That section does explain how to get PWM working. What it's saying is that you have to set up an oscillator using Timer 2, and then you have to set certain registers to make the PWM operation work. What are you finding there?
     
  9. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Take a step back, please. We are here to aid in your learning. That is different from simply handing over whatever code/circuit that will do the job. Why not try to learn how to write the code?

    The principle behind PWM is pretty simple. Coding a control loop to drive a motor at a set speed with a varying load can be more challenging. Learning to do that beats watching tv anyday.
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Nope, guess again, except that *everyone* stumbles over a few of their nuances. That is not to say they are easy or trivial. Those of us who are past some of those stumbles appreciate the help we got and look to pay it forward.

    As it happens I used the PWM for my first time not so long ago that I remember how I did it, and I took the time to type out how I did it, step by step, along with a helpful hint about how to let MPLAB do some work for you that took me years and years to discover.

    However, it seems you cannot be troubled to even read the fine manual.

    That's fine by me.

     
    Last edited: May 18, 2011
  11. Rk17

    Member

    Apr 23, 2011
    43
    0
    @ErnieM what manual is it? mplab manual?
     
  12. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    He's referring to that particular uC's datasheet. Section 15.3 of the datasheet is regarding the PWM function.

    It's not always section 15.3. You need to look in the specific datasheet for your uC under the "Capture/Compare/PWM Modules" heading.
     
  13. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
Loading...