Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PWM, simple help needed please ?

Status
Not open for further replies.

max_imum2000

New Member
hello
i was reading pic18F4331 datasheet for using power PWM module
i came across this and i dont really understand is it possible.

they say that period is controlled by PTPERL (8bits) and PTPERH(4bits)
and duty cycle is controlled by PDC0L (8bits) and PDC0H (6bits)
if i set all bits to 1
how come the duty cycle is more than the period ? is it possible

and if i set the period to 255 and the duty cycle to 255 will i get the max pwm duty cycle for that period

thanks
 
how come the duty cycle is more than the period?

The period is the equal to the value of (PTERH:pTERL+1) x 4 x prescale. There is a built-in x4 multiplier. That is why you need 2-bits less than the duty cycle.
 
Thanks
i totaly missed that equation
but i revised the datasheet and the equation looks a bit different

TPWM = (PTPER+1)/Fosc/(PreScaler/4)

anyway i gut it for that part

now i am a bit confused about calculating the Period
lets say i want to place 255 in PTPER
shall this value go to the 8bit LSB, or 4 bits in HSB and 4 in the LSB ?
then when i get the value of TPWM, if i want 50% duty cycle. i just put TPWM/2 in the Duty cycle registers (H:L) as well ??

thanks .
 
has anybody any code for pwm with a 16F88 or can point me to some info as I am illiterate on pics and just starting and no I don't have time to play around with flashing leds first
 
Thunderchild said:
has anybody any code for pwm with a 16F88 or can point me to some info as I am illiterate on pics and just starting and no I don't have time to play around with flashing leds first

If you think people starting with flashing LED is a waste of time, may be you should never go into programming any microcontroller. :(
 
max_imum2000 said:
Thanks
now i am a bit confused about calculating the Period
lets say i want to place 255 in PTPER
shall this value go to the 8bit LSB, or 4 bits in HSB and 4 in the LSB ?
The lower 8-bits will go to PTPERL while the upper bits will go to PTPERH. In your sample, 255 will go to PTPERL while 0 has to be programmed to PTPERH.

then when i get the value of TPWM, if i want 50% duty cycle. i just put TPWM/2 in the Duty cycle registers (H:L) as well ??

thanks .
The maximum value of PWM will be the (PTPER+1)*4. Therfore to get 50% duty, you load 1/2 of this to PDC.
 
thanks a lot , i gut that part too

what about the clock speed (Fosc).
wont that affect the period or the dutycycle in anyway ?
i tried your equation and it works fine , while the equation on the datasheet that contains Fosc in it is not giving me what it should give.
(I use oscilloscope)
 
eblc1388 said:
If you think people starting with flashing LED is a waste of time, may be you should never go into programming any microcontroller. :(


well problem is I have not a lot of time and not much equipment either thru lack of money (osciliscope) or more importantly just can't get hold of them (breadboards and a place to go have PCBs done). what a crap up this place (south italy) is. with all the time I need to get organised I'm so pissed off I can't be bothered anymore - sad isn't it !
 
thuderchild , what are you doing
this is not your thread .
you are really an annoying person


MOTION please

what about the clock speed (Fosc).
wont that affect the period or the dutycycle in anyway ?
i tried your equation and it works fine , while the equation on the datasheet that contains Fosc in it is not giving me what it should give.
(I use oscilloscope)
 
hello, i have been trying hard to figure out how can i use the PWM power module on pics like 18F4431 , mainly 18Fxx31 series.
they have up to 8 pwm module pairs

the data sheet state for Period calculations we use the formula

(PTPER+1)
TPWM = -------------
Fosc (PS/4)

i use 8mhz crystal
so here is what i places

Fosc = 8Mhz
PS = 1:1
TPWM = 20Khz = 0.02Mhz (since i want to run a small motor over h-bridge) so i placed it as 50us

the PRPER regsiter then is equal to 99 or 1100011
to my understanding i load the PTPERL with 1100011 and PTPERH with 0

ok now i need to setup the dutycyle , since i want it to be 50% , the value for duty cycle must be TPWM/2= 25 or 11001 again this is went to PDC0L and zero goes to PDC0H.

then i fire the pwm module

i watch the signel over my cheap usb scope and i find a strage ramp like waves. not the know square wave,
i hook the motor and it only makes a hummmm sound but not turning

i tried to :
increase and decrease the duty cycle, didnt work also

what am i doing wrong ?? can anyone help with these PICs family please ?

thanks
 
max_imum2000 said:
hello, i have been trying hard to figure out how can i use the PWM power module on pics like 18F4431 , mainly 18Fxx31 series.
they have up to 8 pwm module pairs
It only has 4 complementary output pairs plus 2 single ended PWM channels from the CCP modules.

the data sheet state for Period calculations we use the formula

(PTPER+1)
TPWM = -------------
Fosc (PS/4)

i use 8mhz crystal
so here is what i places

Fosc = 8Mhz
PS = 1:1
TPWM = 20Khz = 0.02Mhz (since i want to run a small motor over h-bridge) so i placed it as 50us
Make sure your configuration fuses are setup for OSC = HSPLL to get the maximum speed possible from the 8Mhz crystal (Fosc=32Mhz). All PIC18F4431's are rated 40Mhz @ 4.2-5V supply.

However, since you used an Fosc=8Mhz (no PLL), let us use that to check your calculations.

the PRPER regsiter then is equal to 99 or 1100011
to my understanding i load the PTPERL with 1100011 and PTPERH with 0
The value of the PTPER is correct for a 20Khz PWM frequency, 1:1 prescale and free running mode (PTCON0=0).

ok now i need to setup the dutycyle , since i want it to be 50% , the value for duty cycle must be TPWM/2= 25 or 11001 again this is went to PDC0L and zero goes to PDC0H.

MAX_PWM is 100*4 = 400 (it's less than 9-bits resolution BTW). Therefore, for 50% duty, PWM value is 200. The value of 200 goes to PDC0L and 0 to PDC0H.

then i fire the pwm module

i watch the signel over my cheap usb scope and i find a strage ramp like waves. not the know square wave,
i hook the motor and it only makes a hummmm sound but not turning

i tried to :
increase and decrease the duty cycle, didnt work also

what am i doing wrong ?? can anyone help with these PICs family please ?

thanks

I suspect there is something wrong your software and/or circuit. There are H-bridge circuits that can be configured to run maximum in one direction when the duty is 100%, zero speed at 50% duty, and maximum speed in the opposite direction when the duty is 0%.

Notice the PWM outputs are complementary. If the duty in one output is 0%, it's 100% on the complementary pair. Similarly, 50% on one means 50% on the other.

Your cheap USB scope is also suspect. The output of the PIC's PWM outputs are pure digital (high or low). It should not produce a ramp output.

If you can hear the chopping frequency, there is something modulating the 20Khz PWM frequency. It alone is inaudible to normal humans. Maybe your program is continuously writing to the PDC or PTPER registers somehow?
 
Last edited:
hello motion
thanks for your great help, i have managed to get it working fine now
i just have 2 question i hope you can help me with
1. when i use the internal oscillator at 8mhz the motor get jerky movment. the speed is ok and everything is ok, but the motor looks like it is moving in steps rather than continous, anyway to improve this ?
2. what are the name of the H-bridge circuits that can be configured to run maximum in one direction when the duty is 100%, zero speed at 50% duty, and maximum speed in the opposite direction when the duty is 0% ? does this method of operation has a name ? , example of ic numbers will be appricated.
thanks again
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top