Shop OBEX P1 Docs P2 Docs Learn Events
PWM "chip" — Parallax Forums

PWM "chip"

Jon WilliamsJon Williams Posts: 6,491
edited 2004-12-17 13:58 in General Discussion
A member of the DPRG (Dallas Personal Robotics Group) was looking for a single chip PWM device that would take the duty cycle from an-eight bit bus.· Another member suggested an Atmel controller, of course I suggested the SX.· As an exercise, I wrote the attached program.· I tested it, and it works as expected.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office

Comments

  • pjvpjv Posts: 1,903
    edited 2004-12-17 01:47
    Hello Jon;

    Or how about some simple assembler:·5 bytes for the PWM routine, and 24 bytes for initializing the SX28.

    id 'DACout'···································;Dec 16, 2004
    device·sx28,osc4Mhz,turbo,stackx····;new assembler
    reset·reset_entry··························;JUMP to start label on reset

    roption·······equ·· %1000_1000········;RTCC rollover interrupt
    intconstant·equ·· -40···················· ;10 usec base tick is 40 instructions at 4Mhz

    ··············· org···· 8

    counter····· ds····· 1·······················;duty cycle accumulator

    ············· ··org··· ·0
    interrupt···· mov·· w,RB···················;get external duty cycle value of 1 through 255
    ················add··· counter,w············;add duty value to one byte counter which then sets/clears carry depending on overflow
    ············· ··rl······ RA····················· ;pull carry into PWM output bit RA.0, other bits are irrelevant
    ················mov·· w,#intconstant·····;get rtcc time adjustment
    ············· ··retiw························ ··;exit interrupt with time adjustment


    reset_entry································· ·;Program starts here on power-up and on reset.
    initialize···· mov·· m,#$0d················ ;Set 0 for CMOS levels
    ···············mov· ·!ra,#%0000·········· ;cmos
    ···· ······· ··mov· ·!rb,#%0000_0000···;cmos
    ········· ·····mov· ·!rc,#%0000_0000···;cmos
    ···· ······· ··mov· ·m,#$0e············· ···;Set 0 for pullups
    ········· ·· ··mov· ·!ra,#%0001···········;
    ·············· mov·· !rb,#%0000_0000···;eigth bit parallel input
    ···· ··········mov· ·!rc,#%0000_0000···;port not used
    ········· ·· ··mov· ·m,#$0f············· ···;Set 0 for output
    ·············· mov· ·!ra,#%1110·········· ;bit 0 is PWM output
    ···· ······· ··mov· ·!rb,#%1111_1111···;eigth bit parallel input
    ········· ·· ··mov· ·!rc,#%1111_1111···;port not used
    ······· ······ mov· ·!option,#roption··· ·;1=RTCC, 0=enabl, 0=internl
    main········ jmp·· ·main····················;sit here and wait for a rtcc overflow interrupt

    This works great too, and certainly tighter code.

    Peter
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-17 02:45
    Cool, that works too, but doesn't give a constant frequency output that I was going for (for motor control).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • pjvpjv Posts: 1,903
    edited 2004-12-17 03:37
    Hello Jon,

    If constant frequency output is your goal, then you are correct.

    However, if you intend to use RA.0 as the driver for an RC filter (say 4K7 into 0.1·uF)·to generate the DAC voltage, then this technique is superior as it generally has a higher output frequency more easily filtered by the RC.

    I use this technique a lot, and by·using look-up·fixed increment sine amplitude values from a table for the DUTY value, it makes great looking sine waves.

    In fact, if you run two sine waves of the same frequency simultaneously from the same processor, but shifted by a fixed (phase) period, and add them together, you can alter the amplitude by altering the phase shift. A real neat way to digitally generate modem frequencies with adjustable amplitude for 6 cents' wort of components (over top of the SX ofcourse). This really works!

    If there is interest expressed in this, I'd be happy to post a schematic and (assembler) code for the sine generator.

    Have fun experimenting all!

    Peter
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-17 04:14
    No, my goal was for a motor driver. SXB has a command called PWM that uses the accumulator technique you demoed above.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • James NewtonJames Newton Posts: 329
    edited 2004-12-17 07:46
    PJV, I'm always interested in schematics and code for cool stuff on an SX. Please do post them.

    Jon, That parallel value comming into your program... is that from a PC printer port or some other parallel interface?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james@sxlist.com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-17 13:58
    I don't know -- as I stated, the request came from a DPRG member and I simply wrote my little program as an exercise.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
Sign In or Register to comment.