Shop OBEX P1 Docs P2 Docs Learn Events
Interesting CTR effects on piezo — Parallax Forums

Interesting CTR effects on piezo

T ChapT Chap Posts: 4,198
edited 2015-10-10 23:32 in Propeller 1
Some years back Phil gave me this little gem of code to make a piezo have volume control, using one CTR and it requires a specific duration. The problem this week is that I need the tone to stay on indefinitely if needed for a different application, without locking up the main program.
PUB beep(pinn, freqq, durr) | period, cycles, frqhi, frqlo, t
  if ((period := clkfreq / (freqq << 1)) < 4000)
    return
  cycles := freqq * durr / 5000
  if (voll)
    frqhi := $10_0000 << (voll #> 0 <# 11) - 1
  else
    frqhi~
  frqlo~
  frqb~
  ctrb := %00110 << 26 | pinn
  'dira[pinn]~~
  t := cnt
  repeat cycles
    waitcnt(t += period)
    frqb := frqhi
    waitcnt(t += period)
    frqb := frqlo
  'dira[pinn]~
  ctrb~

So I tried an experiment using 2 counters and it works perfectly for an indefinite tone as a volume control.
PUB beepON
  If Voll > 0
    dira[14]~~
    frqa := 8000000   ' this higher freq turns the volume down when combined with b
    frqb := 100000     'sounds like 3-4k tone 
    ctrb := %00100 << 26 | 14  'pinn
    ctra := %00100 << 26 | 14  'pinn

ctra @ frqa turns the volume down dramatically, and without ctra the volume is maxed. Adjust frqa from 100000 to 10000000 for a range of full on to full off as a volume control.

I just found this kind of interesting and maybe someone uses Piezo's and finds this of use.


Sign In or Register to comment.