Shop OBEX P1 Docs P2 Docs Learn Events
Help with adding signals and PWM output — Parallax Forums

Help with adding signals and PWM output

grivers21grivers21 Posts: 1
edited 2014-06-26 10:00 in General Discussion
Hi everyone, I'm currently trying to add two analog signals (take them from the ADC channels) and then have a digital output with PWM but with a frequency of f = sum_of_signals*10,000, but I can't get it working. Can someone please help me by taking a look at my code and pointing out what's wrong?
I am very new at this and I have no idea what could be wrong.

I appreciate very much any help. =)

PS: i didn't develop the PWM code and the other code I took some of it from the android DEMO.

Here is my codePUB Start

''Start Serial Driver

debug.start(31, 30, 0, 115_200)
waitcnt(clkfreq*5 + cnt) 'Delay to wait for BT or WiFi to connect
debug.rxflush 'Gets information from the androidaq

dira[0] := 1
prop[0] := 4
freq[0] := 0

ADC.start(Vo_p, Vn_p, Vclk_p, Vcs_p, 8, 3, 12, 1, false) 'starts scanning ADC channels


value_0 := COGNEW (GetVolts, @stack2[0])
volts_sum := value_0

value_1 := COGNEW (GetVolts2, @stack3 [0])
volts_sum := volts_sum + value_1

frequency := volts_sum * 10000


PWM.start (00000, 10000000, frequency)



PUB GetVolts: val_0 | reading, volts, conv 'Reads MCP3208 for X time then returns maximum values per channel


conv := 8056 'conversion for 3.3-volts


Time:=cnt
repeat
waitcnt(Time+=clkfreq/40)

reading := ADC.average(0, 10)
volts := conv * reading
volts := volts/1000
val[0] := volts
debug.DecDP(volts, 4)
'debug.tx(CR)
debug.tx(" ")
debug.tx(" ")


val_0 := val[0]


PUB GetVolts2:val_1 | reading, volts, conv 'Reads MCP3208 for X time then returns maximum values per channel


conv := 8056 'conversion for 3.3-volts



Time:=cnt
repeat
waitcnt(Time+=clkfreq/40)

reading := ADC.average(1, 10)
volts := conv * reading
volts := volts/1000
val[1] := volts
debug.DecDP(volts, 4)
'debug.tx(CR)
debug.tx(" ")
debug.tx(" ")


val_1 := val[1]PUB start(base, mask, freq)


' This method is used to setup the PWM driver and start its cog. If a driver had
' already been started, it will be stopped first. The arguments are as follows:
'
' base: The base pin of the PWM output block. Must be 0, 8, 16, or 24.
'
' mask: The enable mask for the eight pins in the block:
'
' bit 0 = basepin + 0
' bit 1 = basepin + 1
' ...
' bit 7 = basepin + 7
'
' Set a bit to 1 to enable the corresponding pin for PWM ouput.
'
' freq: The frequency in Hz for the PWM output.
'
' Returns true on success; false, if no cog is available or parameters exceed
' permissible limits.


if (cogno)
stop
freq *= resolution
if (clkfreq =< 4000000 or freq > 20648881 or clkfreq < freq * 135 / 10 or clkfreq / freq > 40000 or base <> base & %11000 or mask <> mask & $ff or resolution <> resolution & $7ffffffc)
return false
basepin := base
pinmask := mask << base
longfill(@pwmdata, 0, nlongs)
longfill(@previndex, 0, 8)
fcb[0] := nlongs
fcb[1] := freq
fcb[2] := constant(1 << 29 | 1 << 28) | base << 6 | mask
fcb[3] := pinmask
fcb[4] := @pwmdata
if (cogno := cognew(@pwm, @fcb) + 1)
return true
else
return false


PUB stop


' This method is used to stop an already-started PWM driver. It returns true if
' a driver was running; false, otherwise.


if (cogno)
cogstop(cogno - 1)
cogno~
return true
else
return false
Sign In or Register to comment.