PWM program structure

Thread Starter

friskyy

Joined Mar 27, 2011
13
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.
 

Thread Starter

friskyy

Joined Mar 27, 2011
13
Then what do you make of section 15.3 of the user manual?
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:


Rich (BB code):
#include <htc.h>
__CONFIG(UNPROTECT & BORDIS & PWRTEN & WDTDIS & INTIO &
MCLREN & FCMDIS & IESODIS);
#define _XTAL_FREQ 8000000
void init(void)
{.
// Configure Ports directions: 1=input, 0=output
TRISD = 0x00;
TRISC = 0xFF;
TRISA = 0xFF;
// 8Mhz Internal Clock
OSCCON = 0b01110000;
// Turn off comparators
CMCON0 = 0x07;
//Turn on ADC
ANSEL = 0b00000001; // select AN0 as an analogue channel (RA0)
ADCON1 = 0b01010000; // select A/D conversation clock Fosc/16
ADCON0 = 0b00000001; // left justified, Vss and Vdd as refs, AN0 is on
// Clear PortD
PORTD = 0x00;
// PWM Setup
PR2 = 0x9A;
CCP2CON = 0b00001100; //capture /compare/pwm module
CCPR2L = 5;
TMR2ON = 1;
}
unsigned int ADCval;
void main(void)
{
init();
RD7 = 1;
while (1){
if(GODONE == 0){
ADCval = ADRESH ;//0-1023 //A/D result high register
ADCval = ADCval >> 2;
CCPR2L = ADCval;
GODONE = 1; // Start the next conversion
}
}
}
 

ErnieM

Joined Apr 24, 2011
8,377
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.
 

Thread Starter

friskyy

Joined Mar 27, 2011
13
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.
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.
 

John P

Joined Oct 14, 2008
2,026
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?
 

beenthere

Joined Apr 20, 2004
15,819
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.
 

ErnieM

Joined Apr 24, 2011
8,377
The fact that you have had difficult experience with learning to program PICs doesn't mean that everyone else should.
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.

Airplane! said:
Chump don't want no help, chump don't get no help.
 
Last edited:

SgtWookie

Joined Jul 17, 2007
22,230
@ErnieM what manual is it? mplab manual?
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.
 
Top