Change PWM frequency on the fly
Posted: 11 July 2011, 13:06 PM
According to the Library Reference, I should be able to change the PWM frequency with calls to OpenPWM() with Mode = 1:
Debug output:
ZBasic v3.3.1
2000.0 True
1000.0 False
1000.0 True
I'm using an oscilloscope connected to pin 26 to verify the result.
However, when I try it, the call to OpenPWM fails and no change occurs. Adding a Call PWM() line makes no difference. A call to ClosePWM first works but that's not what I want to do:The Phase/Frequency Correct PWM mode has a maximum frequency of one-quarter of the CPU clock frequency and may be used when the PWM frequency will be changed in the midst of PWM signal generation. Frequency changes are effected by making additional calls to OpenPWM() and the change is synchronized so that it takes effect at the beginning of a cycle.
Code: Select all
Option TargetDevice ZX24x
Sub Main()
dim pwmval as integer
dim pwmchn as byte
dim pwmfrq as single
dim stat as Boolean
pwmchn = 1
'output on Pin 26
pwmval = 50
pwmfrq = 2000.0
' this call succeeds
Call OpenPWM(pwmchn,pwmfrq,1,stat)
Debug.Print pwmfrq;" ";stat
Call PWM(pwmchn,pwmval)
Call sleep(1.0)
pwmfrq = 1000.0
' this call fails
Call OpenPWM(pwmchn,pwmfrq,1,stat)
Debug.Print pwmfrq;" ";stat
Call sleep(1.0)
Call ClosePWM(pwmchn)
Call sleep(1.0)
' this call succeeds
Call OpenPWM(pwmchn,pwmfrq,1,stat)
Debug.Print pwmfrq;" ";stat
Call PWM(pwmchn,pwmval)
End Sub
ZBasic v3.3.1
2000.0 True
1000.0 False
1000.0 True
I'm using an oscilloscope connected to pin 26 to verify the result.