Understanding PWM Better -help needed

hamtech

Member
In another thread i am trying to light leds in accordance with the output from a MSGEQ7 audio chip.
while I await the arrival of these chips I have been experimenting with pwmout to drive an led.

Using a pot on adc channel three to provide a variable input level i am using the readadc output multiplied by 4 to give me a value between zero and 1023. I am using one of the pwmouts on c.5 of my 20x2 chip.using the 0- 1025 as the duty cycle value

The result i get is that i am able to vary the brightness but the led flickers all the time except when the duty cycle is zero when it goes out. Is it possible to choose a period which means that the output does not flicker but maintains a steady brightness level depending on the pot setting. This what i am trying to achieve , in my project i will have a loop which varies the duty cycle depnding on the output from the msg. but i don.t what the flicker undless determined by the input and not by the basic command.

Is what i seek to do possible and could somehelp with the command i need. Thanks for your help .
 

nick12ab

Senior Member
You haven't posted your code, not even in the previous thread.

One thing you can try without modifying the code is put a small capacitor (100nF) on the ADC input to which the pot is connected - that will stabilize the ADC reading.

I am using one of the pwmouts on c.5 of my 20x2 chip.using the 0- 1025 as the duty cycle value
It's 0 to 1023. Maybe there's some mathematical error in your code - this could be determined if you posted your code.
 

hippy

Ex-Staff (retired)
You shouldn't be seeing any flickering. This is flicker-free for me -

Code:
#Picaxe 20X2
Do
  ReadAdc10 C.7, w0
  PwmOut    C.5, 255, w0
Loop
And arguably better ...

Code:
#Picaxe 20X2
PwmOut C.5, 255, 0
Do
  ReadAdc10 C.7, w0
  PwmDuty   C.5, w0
Loop
 

hamtech

Member
Thanks. I'll try that.
I will post the couple of lines of code that i was using later
Hippy used your code and all ok. Thank you! I had inserted a pwm off instruction at the end.......

Here is what I have now which works

Code:
let adcsetup = %0000000000001000; set adc 3

main:

readadc c.7, b5  ; wiper of pot which is across suppy outputs a value of b5= 0-255
let w0 = 4 * b5  ; multiply  by 4 to get a vaule of 0-1020 for modulation width
pwmout  C.5, 255, w0  ;output pulse of maximum period and pulse width determineb by 4x value of b5 

pause 50
goto main

and also

Code:
hpwm 0, 0, %1111,500,1000  ;   4 leds (chan a-d)  made active high

let adcsetup = %0000000000001000; set adc 3

main:

readadc c.7, b5
let w0 = 4 * b5  ; give me a value from pot of 0 - 1020

hpwmduty w0 ;  set pwm duty cycle = pot value on c.7

goto main
I can light four LEDS using HPWM or HPMduty all OK now . Also with PWMOUT on the one pin it is available on my 20x2.
However I would ask Is it possible to control HPWM chans a-d independently as I can't see how to do that but would like to. I can see how to make them active or not but not to control each channel indepnedntly of the others. There is no pin setting with this?? Querulous frown on forehead....

Sorry if I am being thick.
 

hippy

Ex-Staff (retired)
However I would ask Is it possible to control HPWM chans a-d independently as I can't see how to do that but would like to.
The four outputs for HPWM cannot be controlled individually; they will all have the same frequency and duty.

At best you could invert one or more HPWM outputs which could make those LED's dim as the rest get brighter but that's not really individual control.
 

hippy

Ex-Staff (retired)
Actually I should have paid more attention.

You can't control four LED's independently and simultaneously using HPWM but you can control four LED's individually and sequentially to make it appear they are controlled simultaneously. By multiplexing you can also add additional 'independent' sets of four LED's.

This code for the 20X2 uses HPWM to control four LED+R's; anodes to HPWMA/B/C/D, cathodes to 0V. Each LED varies in brightness independently of the others without any flicker.

Code:
#Picaxe 20X2
#No_Data
#No_Table

SetFreq M32
Do
  HPwm PWMSINGLE, PWMHHHH, %1000, 255, w1 : Pause 8
  HPwm PWMSINGLE, PWMHHHH, %0100, 255, w2 : Pause 8
  HPwm PWMSINGLE, PWMHHHH, %0010, 255, w3 : Pause 8
  HPwm PWMSINGLE, PWMHHHH, %0001, 255, w4 : Pause 8
  Gosub UpdateLevels
Loop

UpdateLevels:

  If w11 = 0 Then
    w1 = w1 + $01
    If w1 >= $03FF Then
       w1 = $03FF
       w11 = 1
    End If
  Else
    w1 = w1 - $01
    If w1 >= $03FF Then
       w1 = $0000
       w11 = 0
    End If
  End If

  If w12 = 0 Then
    w2 = w2 + $03
    If w2 >= $03FF Then
       w2 = $03FF
       w12 = 1
    End If
  Else
    w2 = w2 - $03
    If w2 >= $03FF Then
       w2 = $0000
       w12 = 0
    End If
  End If

  If w13 = 0 Then
    w3 = w3 + $07
    If w3 >= $03FF Then
       w3 = $03FF
       w13 = 1
    End If
  Else
    w3 = w3 - $07
    If w3 >= $03FF Then
       w3 = $0000
       w13 = 0
    End If
  End If

  If w14 = 0 Then
    w4 = w4 + $0F
    If w4 >= $03FF Then
       w4 = $03FF
       w14 = 1
    End If
  Else
    w4 = w4 - $0F
    If w4 >= $03FF Then
       w4 = $0000
       w14 = 0
    End If
  End If

  Return
The following is untested, but disconnect the cathodes from 0V and connect them all to C.0, run the program and it should be pretty much as above.

Now connect another set of four LED+R's to HPWMA/B/C/D but with their cathodes to C.1. If all's gone well you should have eight LED's appearing to dim independently.

Code:
SetFreq M32
High C.0
High C.1
Do
  Toggle C.0
  HPwm PWMSINGLE, PWMHHHH, %1000, 255, w1 : Pause 4
  HPwm PWMSINGLE, PWMHHHH, %0100, 255, w2 : Pause 4
  HPwm PWMSINGLE, PWMHHHH, %0010, 255, w3 : Pause 4
  HPwm PWMSINGLE, PWMHHHH, %0001, 255, w4 : Pause 4
  Toggle C.0
  Toggle C.1
  HPwm PWMSINGLE, PWMHHHH, %1000, 255, w5 : Pause 4
  HPwm PWMSINGLE, PWMHHHH, %0100, 255, w6 : Pause 4
  HPwm PWMSINGLE, PWMHHHH, %0010, 255, w7 : Pause 4
  HPwm PWMSINGLE, PWMHHHH, %0001, 255, w8 : Pause 4
  Toggle C.1
  Gosub UpdateLevels
Loop

UpdateLevels:

  If w11 = 0 Then
    w1 = w1 + $01
    If w1 >= $03FF Then
       w1 = $03FF
       w11 = 1
    End If
  Else
    w1 = w1 - $01
    If w1 >= $03FF Then
       w1 = $0000
       w11 = 0
    End If
  End If

  If w12 = 0 Then
    w2 = w2 + $03
    If w2 >= $03FF Then
       w2 = $03FF
       w12 = 1
    End If
  Else
    w2 = w2 - $03
    If w2 >= $03FF Then
       w2 = $0000
       w12 = 0
    End If
  End If

  If w13 = 0 Then
    w3 = w3 + $07
    If w3 >= $03FF Then
       w3 = $03FF
       w13 = 1
    End If
  Else
    w3 = w3 - $07
    If w3 >= $03FF Then
       w3 = $0000
       w13 = 0
    End If
  End If

  If w14 = 0 Then
    w4 = w4 + $0F
    If w4 >= $03FF Then
       w4 = $03FF
       w14 = 1
    End If
  Else
    w4 = w4 - $0F
    If w4 >= $03FF Then
       w4 = $0000
       w14 = 0
    End If
  End If

  If w15 = 0 Then
    w5 = w5 + $04
    If w5 >= $03FF Then
       w5 = $03FF
       w15 = 1
    End If
  Else
    w5 = w5 - $0F
    If w5 >= $03FF Then
       w5 = $0000
       w15 = 0
    End If
  End If

  If w16 = 0 Then
    w6 = w6 + $05
    If w6 >= $03FF Then
       w6 = $03FF
       w16 = 1
    End If
  Else
    w6 = w6 - $0F
    If w6 >= $03FF Then
       w6 = $0000
       w16 = 0
    End If
  End If

  If w17 = 0 Then
    w7 = w7 + $09
    If w7 >= $03FF Then
       w7 = $03FF
       w17 = 1
    End If
  Else
    w7 = w7 - $0F
    If w7 >= $03FF Then
       w7 = $0000
       w17 = 0
    End If
  End If

  If w18 = 0 Then
    w8 = w8 + $0B
    If w8 >= $03FF Then
       w8 = $03FF
       w18 = 1
    End If
  Else
    w8 = w8 - $0F
    If w8 >= $03FF Then
       w8 = $0000
       w18 = 0
    End If
  End If

  Return
 
Top