All About Circuits Forum  

Go Back   All About Circuits Forum > Software, Microcomputing, and Communications Forums > Embedded Systems and Microcontrollers

Notices

Embedded Systems and Microcontrollers Discussion forum for projects and working with embedded systems and microcontrollers (FPGAs, PICs, AVRs). Get help with hardware issues and embedded programming.

Reply   Post New Thread
 
Thread Tools Display Modes
  #1  
Old 06-16-2010, 08:33 AM
KansaiRobot KansaiRobot is offline
Member
 
Join Date: Jan 2010
Location: Kyoto Japan
Posts: 79
Default PWM for PIC18F2550

in the documentation of the compiler C18 we can find

Code:
/******************************************************************
* NOTES:
* Code uses the Peripheral library support available with MCC18 Compiler
* Code Tested on:
* PicDem2+ demo board with PIC18F4685 controller
* PWM output is obtained on CCP1 pin. duty cycle is gievn by
*
*Formula for Period and Duty cycle calculatio
*
*        PWM period =   [(period  ) + 1] x 4 x Tosc x TMR2 prescaler
*
*        PWM x Duty cycle = (DCx<9:0>) x Tosc
*
*        Resolution (bits) = log(Fosc/Fpwm) / log(2)
**********************************************************************/

#define USE_OR_MASKS
#include <p18cxxx.h>
#include "pwm.h"

void main(void)
{
  char period=0x00;
  unsigned char outputconfig=0,outputmode=0,config=0;
  unsigned int duty_cycle=0;

//----Configure pwm ----
    period = 0xFF;
    OpenPWM1( period);            //Configure PWM module and initialize PWM period

//-----set duty cycle----
        duty_cycle = 0x0F00;
        SetDCPWM1(duty_cycle);        //set the duty cycle

//----set pwm output----
    outputconfig = FULL_OUT_FWD ;
    outputmode = PWM_MODE_1;
    SetOutputPWM1( outputconfig, outputmode);    //output PWM in respective modes

    while(1);                    //observe output on CCP1 pin

//-----close pwm----
  ClosePWM1();

}
Select All
Now... I am unexperienced on PWM, so I have a hard time understanding this.

For example, lets see the duty cycle

Code:
duty_cycle = 0x0F00;
        SetDCPWM1(duty_cycle);
Select All

Isn't it supposed that the duty cycle is 10 bits??
but 0x0F00= 1111 0000 0000 right? so 12 bits....
in this example, how much percentage (%) are we putting...?




(as an aside note, I tried to compile it for my PIC18F2550 and it didnt work. It didnt recognize the macros FULL_OUT_FWD and PWM_MODE_1)

Can anybody help me on this???
Reply With Quote
  #2  
Old 06-17-2010, 02:01 PM
Vaughanabe13 Vaughanabe13 is offline
Senior Member
 
Join Date: May 2009
Posts: 102
Default

Sorry, but the reason you haven't gotten any responses is because your question is confusing. Please explain what you want to do and what you don't understand.

The duty cycle controls the percentage of a PWM period that is "ON" (logic high). So if your duty cycle is 50%, you will have a square wave with equal-width positive and negative pulses, and if it is 75%, it will be "ON" for 75% of a period and "OFF" for 25%. The duty cycle is a 10-bit value, but if you give it more than 10 bits it will take the lower 10 bits as the duty cycle. 0x0F00 is actually = 1100000000 = 768.

According to the C18 compiler libraries PDF:
Quote:
The value of dutycycle can be any 10-bit number. Only the lower
10-bits of dutycycle are written into the duty cycle registers. The duty
cycle, or more specifically the high time of the PWM waveform, can be
calculated from the following formula:
PWM x Duty cycle = (DCx<9:0>) x TOSC
where DCx<9:0> is the 10-bit value specified in the call to this function.
Reply With Quote
  #3  
Old 06-17-2010, 03:17 PM
AlexR's Avatar
AlexR AlexR is offline
Senior Member
 
Join Date: Jan 2008
Location: Australia (UTC+10)
Posts: 735
Default

Take a look at the C18 library documentation.

The SetDCPWM1() function takes an int (16 bit) value as an argument to set the duty cycle but only used the the 10 least significant bits to configure the duty cycle. The 6 most significant bits are simply ignored.

So the statement:
Code:
//-----set duty cycle----
        duty_cycle = 0x0F00;
        SetDCPWM1(duty_cycle);
Select All
will have the same effect as far as duty cycle is concerned as:
Code:
//-----set duty cycle----
        duty_cycle = 0x0300;
        SetDCPWM1(duty_cycle);
Select All
__________________
Alex

"A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools."-Douglas Adams
Reply With Quote
  #4  
Old 06-18-2010, 01:59 AM
KansaiRobot KansaiRobot is offline
Member
 
Join Date: Jan 2010
Location: Kyoto Japan
Posts: 79
Default

Quote:
Originally Posted by Vaughanabe13 View Post
Sorry, but the reason you haven't gotten any responses is because your question is confusing. Please explain what you want to do and what you don't understand.

The duty cycle controls the percentage of a PWM period that is "ON" (logic high). So if your duty cycle is 50%, you will have a square wave with equal-width positive and negative pulses, and if it is 75%, it will be "ON" for 75% of a period and "OFF" for 25%. The duty cycle is a 10-bit value, but if you give it more than 10 bits it will take the lower 10 bits as the duty cycle. 0x0F00 is actually = 1100000000 = 768.

According to the C18 compiler libraries PDF:
I am sorry for my confusing post... maybe it was because I am confused.
In theory I know what you are saying. If we could just program in percentages like

dutycycle=50; // meaning 50%

that would be wonderful, but instead we have to make some transformation to an hexadecimal and I cant figure it out how...

in the example above for instance. how can I put a dutycicle of 100%?
and how can I put it to 10%
or 20%
Reply With Quote
  #5  
Old 06-18-2010, 08:06 AM
AlexR's Avatar
AlexR AlexR is offline
Senior Member
 
Join Date: Jan 2008
Location: Australia (UTC+10)
Posts: 735
Default

Part of the problem is Microchip's use of the term "duty cycle" when what they really mean is "on period". You have to manually calculate the on period of your PWM waveform and plug that value into the formula as the "PWM duty cycle".

Alternately you can just plug the relevant values into the on-line PWM calculator at
http://www.micro-examples.com/public...alculator.html
and let it do all the hard work for you.
__________________
Alex

"A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools."-Douglas Adams
Reply With Quote
  #6  
Old 07-07-2010, 05:17 AM
KansaiRobot KansaiRobot is offline
Member
 
Join Date: Jan 2010
Location: Kyoto Japan
Posts: 79
Default

Thanks for all the help.

One more question. i notice that in the microchip example, there is no

Code:
#pragma config FOSC = INTOSCIO_EC //Internal oscillator, port function on RA6, EC used by USB 
#pragma config WDT = OFF //Disable watchdog timer
Select All
and things like that. In other programs if you dont set the oscillator the micro doesnt do anything,

How come the example doesnt have these settings?
Reply With Quote
  #7  
Old 07-07-2010, 01:09 PM
AlexR's Avatar
AlexR AlexR is offline
Senior Member
 
Join Date: Jan 2008
Location: Australia (UTC+10)
Posts: 735
Default

If you are referring to the example snippets of code on the micro-examples web site then obviously all they give you is the code needed to set the various registers needed for PWM operation. Anything else is up to you and they assume that you will add all the code that is needed to make the PIC work.
__________________
Alex

"A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools."-Douglas Adams
Reply With Quote
The Following User Says Thank You to AlexR For This Useful Post:
KansaiRobot (07-08-2010)
  #8  
Old 07-08-2010, 09:31 AM
KansaiRobot KansaiRobot is offline
Member
 
Join Date: Jan 2010
Location: Kyoto Japan
Posts: 79
Default

thanks for the reply.

it seems to be working now but I got still questions..

please take a look at the new thread I am posting in a few minutes

kansai
Reply With Quote
Reply   Post New Thread

Tags
,


Similar Threads
Thread Thread Starter Forum Replies Last Post
Solution for 5 Volts PWM to 12 Volts PWM? CircuitNewbie General Electronics Chat 22 01-21-2010 06:55 AM
Strange effect with H-Bridge + PWM + Peltier -- is an LC filter needed? j_c General Electronics Chat 2 01-07-2010 12:05 PM
Help Variable PWM using A/D PIC18F2431 trr85 Embedded Systems and Microcontrollers 5 06-12-2009 04:36 AM
current limit a PWM circuit Jim_2.0 General Electronics Chat 13 11-17-2008 05:35 PM
Learning - project pwm MrWoo The Projects Forum 5 06-19-2007 07:47 PM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:41 PM.


User-posted content, unless source quoted, is licensed under a Creative Commons Public Domain License.
Powered by vBulletin
Copyright ©2000 - 2014, vBulletin Solutions, Inc.