PWM

Discuss the Firewing language

PWM

Postby AndrewB » Thu Aug 07, 2014 5:42 pm

Is it possible to.
Output a pulse train that is spread out evenly in one cycle and is equal to the duration of a PWM cycle.
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: PWM

Postby David John Barker » Fri Aug 08, 2014 7:13 am

Can you give a little more detail?
User avatar
David John Barker
 
Posts: 491
Joined: Thu Nov 08, 2012 12:21 pm

Re: PWM

Postby AndrewB » Fri Aug 08, 2014 7:20 am

As you know PWM is split into a hard duty cycle.
On and off.
for example the PWM signal at one point in time is 50/50.
50% on 50% off in the cycle.

I would like to spread that 50% of the on section in fine pulses over the whole cycle.
So the percentage on is the same but spread out of the whole cycle.

the frequency I'm looking at using 240Hz .
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: PWM

Postby David John Barker » Fri Aug 08, 2014 2:10 pm

The PIC's hardware on / off times are based on frequency. So if you have a duty cycle of 50%, there are typically many on /off pulses a second and not just a simple on for 50%, then off for 50%. So you should get a spread of pulses in the whole cycle. I may have misunderstood you though. If hardware PWM isn't going to work for you then you can use a hardware timer based approach, which may be more suitable to your needs (that is, you can tweak to meet your specific needs). You can get pretty good results this way at relatively low frequencies (with lowish bit resolution) and you don't require too many channels. Something like:
Code: Select all
' the pwm frequency (in Hz)
' high value -> fast refresh, more CPU cycles
' low value  -> slow refresh, less CPU cycles (may flicker)
private const _pwm_frequency = 120                                       

' now calculate the timer reload value, given the pwm frequency
private const _fosc as single = _clock * 1000000 / 2                   ' instructions per second               
private const _update_frequency = _pwm_frequency * 256                 ' actual frequency (pwm * steps)
private const _reloadTimerValue as ushort = _fosc / _update_frequency  ' reload value

' this event handler is triggered when a timer interval reaches zero...
dim pwmDutyCycle as byte = 0
dim pwmPin as D3
dim pwmCounter as byte = 0
private Interrupt OnTimer(Pic.T1Interrupt)
   if pwmCounter = 0 then
      high(pwmPin)
   elseif pwmCounter = pwmDutyCycle then
      low(pwmPin)
   end if         
   pwmCounter += 1
   IFS0.3 = 0
End Interrupt

to set the duty:
Code: Select all
sub SetDuty(pValue as pwmDutyCycle)
   T1CON = 0                   ' clear Timer 1 - no presccale
   PR1 = _reloadTimerValue     ' set timer period
   IFS0.3 = 0                  ' clear timer interrupt flag
   T1CON.15 = 1                ' switch timer on   
   enable(OnTimer)             ' enable the interrupt handler
end sub
User avatar
David John Barker
 
Posts: 491
Joined: Thu Nov 08, 2012 12:21 pm

Re: PWM

Postby AndrewB » Fri Aug 08, 2014 5:46 pm

Tried this and it gives a normally PWM.

Am I correct in just adding

Code: Select all
Sub Main()
     setduty(50)
End Sub


What I was looking for in one cycle is shown in the attached png.
top is a normal PWM cycle below is what I would like.
I have also emailed you a pdf from Cypress.
Attachments
pwm.PNG
picture of waveform
pwm.PNG (1.27 KiB) Viewed 11606 times
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: PWM

Postby David John Barker » Fri Aug 08, 2014 5:57 pm

I don't think I can be much more help, the two methods I have previously described (hardware or software through timer) have always met my PWM needs. If a PIC isn't up to the job then perhaps take a look at a third party chip, like the one you mentioned - please do report back with your results though...
User avatar
David John Barker
 
Posts: 491
Joined: Thu Nov 08, 2012 12:21 pm

Re: PWM

Postby AndrewB » Sat Aug 09, 2014 3:12 pm

Ok Thanks David. I will see what this Cypress cpu can do.
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: PWM

Postby Jerry Messina » Sat Aug 09, 2014 6:06 pm

If the top waveform in the picture is 240Hz, then the bottom one is 6x-7x that... about 1.4KHz.
Also, the bottom trace isn't a constant PWM frequency.

Are you trying to dither/modulate the PWM signal, or is it just poorly drawn?
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: PWM

Postby AndrewB » Sun Aug 10, 2014 5:42 pm

Bad sketch.
Yes dithering the PWM.
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: PWM

Postby David John Barker » Mon Jan 19, 2015 6:38 pm

How did you get on with your dithering PWM? Did you make any inroads?
User avatar
David John Barker
 
Posts: 491
Joined: Thu Nov 08, 2012 12:21 pm


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x