Shop OBEX P1 Docs P2 Docs Learn Events
PWM object - confused — Parallax Forums

PWM object - confused

geokonstgeokonst Posts: 48
edited 2008-10-12 02:50 in Propeller 1
Dear all,
I can't understand why the following code doesn't work
CON

  _clkmode      = xtal1 + pll16x
  _xinfreq      = 5_000_000

  base_pin      = 16

OBJ

  pwm: "PWMx8"

PUB start | j, duty

  pwm.start(base_pin, %1111_1111, 30000)        

 repeat

   repeat j from 0 to 64
     repeat 500
        pwm.duty(base_pin, j)                      
        waitcnt(8000 +cnt)
        pwm.duty(base_pin, 0)                      
        waitcnt(8000 +cnt)



I am using this code for producing analogue audio signals. I've reduced the resolution in PXMx8 so I can have higher frequency. The loudspeaker is connected to output pin. Since the speaker can't respond to 30KHz I don't really need a low pass filter (do I?).

Problem is that although the code is expected to create a sound of constant frequency but varying amplitude the opposite happens. In particular the pitch changes with every increase by 4 of the duty cycle. Any clues?


Also is there an easy way -like an object- to use the sigma-delta modulation from spin. My assembly skills are really bad. I've already searched around and couldn't find anything. I guess I could modify the Microphone to Headphone code, but as I mentioned I lack the skills.

Thanks everybody

Post Edited (geokonst) : 10/8/2008 9:18:29 AM GMT

Comments

  • geokonstgeokonst Posts: 48
    edited 2008-10-08 12:34
    Sorry for the long post. Briefly: is there a way to produce an amplitude varying signal using Spin and no extra DACs ?
    Thank you for your patience
  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-08 15:21
    It can certainly be done, but I don't think there's a ready-made example. You will need a simple low pass filter on the I/O pin. The values used on the Demo Board or in the application note on the counters (AN001) should work fine. You'll need some kind of sine table to provide the amplitudes for the Sigma-Delta DAC and the rate you step through the sine table will determine the frequency being produced.
  • geokonstgeokonst Posts: 48
    edited 2008-10-08 15:45
    Thank you mike. Yes I am using the low pass filter and I do get sound using the demo programs but I am not at all good in ASM and thus I do not know how to use the counters for S-D. I understand the concepts though. So I was wondering if there was an "analogout" object out there although I can't find it. I could use pwm like I've done with other uC but doesn't seem to work right.
    Thank you for your help [noparse]:)[/noparse]
  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-08 16:04
    The counters can be used from Spin as well although their response is slower and has some jitter because of the delays in Spin compared to assembly language. The application note I mentioned has sample programs as well as a detailed description of the use of the counters.

    You could use PWM. I suspect your problems had more to do with timing interactions between the PWM generation and the routine continually calling the PWM duty cycle set routine. Maybe there was a beat frequency or something like that.

    There was a sinewave synthesis program that was pasted here quite some time ago. I don't have a link to it. If you have the time and patience, you could search for it on this Propeller forum.
  • kelvin jameskelvin james Posts: 531
    edited 2008-10-12 02:50
    I think this might be the program Mike Green was referring to?, but not sure.
    Look for Tracy Allens post with sinewave programs.

    http://forums.parallax.com/forums/default.aspx?f=25&p=8&m=113109#m146258
Sign In or Register to comment.