Shop OBEX P1 Docs P2 Docs Learn Events
How do I get the co processors on a BS2PE moboo to run an un-attended PWM signa — Parallax Forums

How do I get the co processors on a BS2PE moboo to run an un-attended PWM signa

Deano1936Deano1936 Posts: 15
edited 2008-12-19 16:16 in BASIC Stamp
confused.gif I'm new at PBASIC and I'm really enjoying this language. I have
> a BS2pe MoBOo and I'm trying to get the co processors to generate a
> constant PWM signal to run an H bridge motor circuit. Can you give me
> a link where I can find a sample program that will help me understand
> this procedure ?

Post Edited (Deano1936) : 12/17/2008 10:10:15 PM GMT

Comments

  • Carl HayesCarl Hayes Posts: 841
    edited 2008-12-17 22:24
    Coprocessors? A BS2PE has only one processor.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-12-17 22:39
    Deano1936,

    Here is the documentation for GPIO3, the firmware that comes preloaded on the MoBoStamp-pe's coprocessors. It details how to set up the AVR's outputs for constantly-running PWM.

    Here is a sample program to get you started:

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    
    owio    PIN   6                    'Change to 10 for socket A.
    
    pwmval  VAR   Byte
    
    PAUSE 10                           'Make sure AVR comes out of reset.
    
    DO
      FOR pwmval = 0 TO 255
        OWOUT owio, 0, [noparse][[/noparse]$26, pwmval]   'Set AVR output 2, to PWM positive at pwmval/256.
        PAUSE 10
      NEXT
      FOR pwmval = 254 TO 1
        OWOUT owio, 0, [noparse][[/noparse]$26, pwmval]   'Set AVR output 2, to PWM positive at pwmval/256.
        PAUSE 10
      NEXT
    LOOP
    
    
    


    If you're using the PWM with an inductive load, such as a motor, you may want to switch to one of the odd-numbered PWM frequencies. The reason is that, at these frequencies, the PWM output maintains the correct phase, rather than aligning with the leading or trailing edge. This is supposed to help get smoother operation from the motors. To change to 18.8KHz (frequency 1) for example, add the following ahead of the DO...LOOP in the above code:

    OWOUT Owio, 0, [noparse][[/noparse]$F1]
    
    
    


    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 12/17/2008 10:53:34 PM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-12-17 22:53
    Carl,

    The MoBoStamp-pe has three processors onboard: a BASIC Stamp 2pe and two AVR ATTiny13s that can be programmed to offload tasks from the Stamp.

    -Phil
  • Deano1936Deano1936 Posts: 15
    edited 2008-12-18 22:00
    Hey Phil, your my HERO!
    The program was just what I needed. It worked great. I wrote the following code to get four separate PWM signals from the A & B co-processors.
    It also worked great. Now I'm going to try and sample an analog voltage on ports 0 & 1 of each AVR processor so I can control the four PWM signals using four pots.. I know I'm re-inventing the wheel sooo--- if you have the time and you know of a sample code that will help me with this task , I would really appreciate you posting it.

    Thank you again

    Dean


    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}

    'This program will set up the two AVR co processors (A&B) so they will generate four separate unattended PWM signals.

    OWOUT 10, 0, [noparse][[/noparse]$F1] 'Set co-processors A and B to18.8khz
    OWOUT 6, 0, [noparse][[/noparse]$F1]

    pwmAP2 VAR Byte
    pwmAP3 VAR Byte
    pwmBP2 VAR Byte
    pwmBP3 VAR Byte

    pwmAP2 = 50 ' 'Set the duty cycles so that each output will look different on the o-scope
    pwmAP3 = 100
    pwmBP2 = 150
    pwmBP3 = 200


    PAUSE 10 'Make sure AVR comes out of reset.

    DO
    OWOUT 6, 0, [noparse][[/noparse]$26, pwmAP2] 'Set A AVR port 2, to PWM positive
    OWOUT 6, 0, [noparse][[/noparse]$36, pwmAP3] 'Set A AVR port 3, to PWM positive
    OWOUT 10, 0, [noparse][[/noparse]$26, pwmBP2] 'Set B AVR port 2, to PWM positive
    OWOUT 10, 0, [noparse][[/noparse]$36, pwmBP3] 'Set B AVR port 3, to PWM positive

    PAUSE 700 'Test the effects of program RUN time. Note: I could see no interruption or any effect on the PWM signals due to this PAUSE 500 command.

    LOOP tongue.giftongue.giftongue.gif

    Post Edited (Deano1936) : 12/18/2008 10:12:35 PM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-12-18 23:18
    To read a pot input, connect it as shown:

    attachment.php?attachmentid=57425

    The wiper resistor is there to ensure no damage to the AVR, should the wiper be at an extreme with the pin accidentally set to output.

    Here's some sample code for reading the voltage at the pin:

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    
    OWIOA   CON   10
    OWIOB   CON   6
    
    pwmval  VAR   Byte
    myow    VAR   Nib
    mypin   VAR   Nib
    adcval  VAR   Byte
    busy    VAR   Bit
    
    PAUSE 10                           'Make sure AVR comes out of reset.
    
    DO
      DEBUG HOME
      FOR myow = OWIOA TO OWIOB STEP 4
        FOR mypin = 0 TO 1
          GOSUB ReadADC
          DEBUG "Owio: ", DEC2 myow, " Inp: ", DEC mypin, " Val: ", DEC3 adcval, CR
        NEXT
      NEXT
    LOOP
    
    ReadADC:
    
      OWOUT myow, 0, [noparse][[/noparse]mypin << 4 | $0A]            'Setup to read bits 9..2 of ADC.
      DO : OWIN myow, 4, [noparse][[/noparse]busy] : LOOP WHILE Busy  'Wait for ADC to finish.
      OWIN myow, 0, [noparse][[/noparse]adcval]                       'Read ADC value.
      RETURN
    
    
    



    -Phil
    274 x 146 - 963B
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-12-19 15:51
    Phil Pilgrim (PhiPi) said...
    Carl,

    The MoBoStamp-pe has three processors onboard: a BASIC Stamp 2pe and two AVR ATTiny13s that can be programmed to offload tasks from the Stamp.

    -Phil
    Ah so.· Never heard of it.· Thanks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
  • sylvie369sylvie369 Posts: 1,622
    edited 2008-12-19 16:16
    Carl Hayes said...
    Ah so. Never heard of it. Thanks.

    Oh, man, you've got to take a look. Very cool little board. One of those with the power input, 3-pin header I/O daughterboard is about $95, and it'll do 10-bit ADC, PWM, frequency counter input and all sorts of other stuff. The 2pe communicates with the AVRs through the one-wire protocol. It's easily my favorite Stamp module.
Sign In or Register to comment.