I cannot get the the PWMOut command to work. I have copied the code directly from the help file and it will not compile. Being an idiot like I am, I can not make something work unless I get shown exactly how it works. Does someone have a working example?
Thanks.
Mitch
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I had tried that and it did let it compile fine but PWM still didn't work.
I am using HPWM on a single port and it works great but I need another PWM channel and my chip only has one. I would really like to find a way to make this work. The following is my code.
Dim InputType as Word
Dim InputTime as Word
Dim M1 as Word
Dim M2 as Word
DIR PWM_Out1 OUT
DIR GPIO.2 OUT
DIR GPIO.5 IN
Startup:
Wait StartUpDelay
Cycle:
InputType = 0
InputTime = 0
Do While Pin5 Off
Loop
Do While Pin5 On
InputType = InputType + 1
Loop
Do While Pin5 Off
Loop
Do While Pin5 On
InputTime = InputTime + 1
Loop
If InputType > 20 And InputType < 60 Then
M1 = InputTime/15
If M1 > 255 then M1 = 255
End If
If InputType > 100 And InputType < 200 Then
M2 = InputTime/15
If M2 > 255 then M2 = 255
End If
PWMOut 1, M2, 100
HPWM 1, 40, M1
Goto Cycle
Thanks.
Mitch
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Unlike the hardware PWM which you can set up and leave to run, the software PWM only works when the routine is being called. It's only really useful for chips that only have to manage the PWM on a single channel and nothing else.
If you've got the update.zip version of GCBASIC, you could try using an interrupt to generate the PWM. You'd need this code to initialise it:
Then, add this subroutine to turn the pin on and off
Sub OutputPWM
CurrentPWMTime += 1
If CurrentPWMTime > PWMPowerLevel Then
Set PWM_Out1 Off
Else
Set PWM_Out1 On
End If
End Sub
With those sections added, you can then set the PWMPowerLevel variable, and it will cause the duty cycle to change. To alter the frequency, change the prescaler used by Timer 1 - see the InitTimer1 help article for more on this.
Another tip, you can replace this code:
Do While Pin5 Off
Loop
With this:
Wait While Pin5 Off
The assembly generated is the same, but it's less to type and a bit neater.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hugh:
First off, thank you for GC Basic. I think I tried every other basic package before I found yours and I think it's the best.
One last question. Will HPWM work on two pins with chips that have ECCP? According to the datasheet for the 12F615, it supports PWM on two pins using ECCP.
Thanks for the coding tip and the above information. I will try that in the next few days.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I cannot get the the PWMOut command to work. I have copied the code directly from the help file and it will not compile. Being an idiot like I am, I can not make something work unless I get shown exactly how it works. Does someone have a working example?
Thanks.
Mitch
The online copy of the help has a typo in the PWMOut example.
I did the same thing doing some testing (Copy and pasted it) As below:
'Select chip model
#chip 16F877A, 20
'Set PWM Port
#define PWM_Out1 PORTB.0
'Set port directions
dir SoftPWM1 out 'PWM <------------ That should be "dir PWM_Out1 out"
dir PORTA.0 in 'Potentiometer
'Main routine
do
PWMOut 1, ReadAD(AN0), 100 '100 cycles is a purely
'arbitrary value here.
loop
Cheers, Chris H.
Thank you for the reply Chris.
I had tried that and it did let it compile fine but PWM still didn't work.
I am using HPWM on a single port and it works great but I need another PWM channel and my chip only has one. I would really like to find a way to make this work. The following is my code.
#CHIP 12F683, 8
#config OSC = INT, MCLR = OFF
#define StartUpDelay 100 ms
#define Pin5 GPIO.5
#define Pin2 GPIO.2
#define PWM_Out1 GPIO.1
Dim InputType as Word
Dim InputTime as Word
Dim M1 as Word
Dim M2 as Word
DIR PWM_Out1 OUT
DIR GPIO.2 OUT
DIR GPIO.5 IN
Startup:
Wait StartUpDelay
Cycle:
InputType = 0
InputTime = 0
Do While Pin5 Off
Loop
Do While Pin5 On
InputType = InputType + 1
Loop
Do While Pin5 Off
Loop
Do While Pin5 On
InputTime = InputTime + 1
Loop
If InputType > 20 And InputType < 60 Then
M1 = InputTime/15
If M1 > 255 then M1 = 255
End If
If InputType > 100 And InputType < 200 Then
M2 = InputTime/15
If M2 > 255 then M2 = 255
End If
PWMOut 1, M2, 100
HPWM 1, 40, M1
Goto Cycle
Thanks.
Mitch
Unlike the hardware PWM which you can set up and leave to run, the software PWM only works when the routine is being called. It's only really useful for chips that only have to manage the PWM on a single channel and nothing else.
If you've got the update.zip version of GCBASIC, you could try using an interrupt to generate the PWM. You'd need this code to initialise it:
InitTimer1 Osc, PS1_1/1
StartTimer 1
CurrentPWMTime = 0
On Interrupt Timer1Overflow Call OutputPWM
Then, add this subroutine to turn the pin on and off
Sub OutputPWM
CurrentPWMTime += 1
If CurrentPWMTime > PWMPowerLevel Then
Set PWM_Out1 Off
Else
Set PWM_Out1 On
End If
End Sub
With those sections added, you can then set the PWMPowerLevel variable, and it will cause the duty cycle to change. To alter the frequency, change the prescaler used by Timer 1 - see the InitTimer1 help article for more on this.
Another tip, you can replace this code:
Do While Pin5 Off
Loop
With this:
Wait While Pin5 Off
The assembly generated is the same, but it's less to type and a bit neater.
Hugh:
First off, thank you for GC Basic. I think I tried every other basic package before I found yours and I think it's the best.
One last question. Will HPWM work on two pins with chips that have ECCP? According to the datasheet for the 12F615, it supports PWM on two pins using ECCP.
Thanks for the coding tip and the above information. I will try that in the next few days.
HPWM won't work with the 2 pins on the ECCP. It only handles multiple pins on chips that have multiple CCP modules, like the 16F877A.