08M2 Serial PWM Motor driver.

Greetings!
Just wandering if I`m on the right track here.
The general idea is to bombard a 08M2 hserin with two bytes containing address for the device and the speed and then let the 08M2 adjust the pwm-frequensy and direction pins(this will be used to drive a currently unknown H-Bridge)
I wrote this snippet of code for the 18M2 and it simulates fine but I`m not sure if i understand the 2byte fifo buffer well enough.

Code:
Symbol Address=b0
Symbol Speed=b1
Symbol duty=b2
Symbol Device_adress=3


setfreq m32
hsersetup b115200_32,%00001000

Main:
Hserin Address
Hserin Speed
If address<>Device_Adress then goto main

If speed=128 then goto main
If speed<128 then goto Backward_
Goto forward_

Forward_:
Low c.0
High b.4				;Should be c.4 for 08m2
Speed=Speed-128
Duty=speed*3
Pwmout pwmdiv4,B.3,99,Duty	;Should be c.2 for 08m2
Goto main

Backward_:
High c.0			
low b.4				;Should be c.4 for 08m2
Speed=128-speed
Duty=speed*3
Pwmout pwmdiv4,B.3,99,Duty	;Should be c.2 for 08m2
Goto main
If i send device address first and then the speed many times a second will they fall into the same fifo space every time or does it depend on where you currently are in the code? When does the new byte replace the old one?
Any help is welcome.
Regards
 

hippy

Ex-Staff (retired)
I wrote this snippet of code for the 18M2 and it simulates fine but I`m not sure if i understand the 2byte fifo buffer well enough.

Main:
Hserin Address
Hserin Speed
If address<>Device_Adress then goto main
On the M2's HSERIN doesn't work quite like a SERRXD which is how you are using it. HSERIN updates the contents of the variable when a new byte is received, leaves it unchanged when no data is received ( see the example in Manual 2 ).

As you don't know what receiver commands is executing at the time a byte is received it's entirely possible that an address sent could be received and stored in the speed variable, and a speed stored in address. If unlucky, all bytes could be stored in address or speed, the other never ever updated.

The M2's have double buffering - under ideal circumstances you can be receiving a second byte while still taking the first with HSERIN - but that's very different to the full background receive capabilities of the X2's.

Untested but you'll need something like ...

Code:
Symbol address = w0 ' <-- Note word variable
Symbol speed = w1 ' <-- Note word variable

Do
  Do
    address = $FFFF
    HSerIn address
  Loop Until address <> $FFFF
  Do
    speed = $FFFF
    HSerIn speed
  Loop Until speed <> $FFFF
  If address = ? Then
    Gosub UpdateSpeed
  End If
Loop
 

westaust55

Moderator
Where is there a reference to 08m2+ and/or 08M2LE?
as just mentioned in another thread,
albeit that the Rev Ed website still as at this morning (Aust time 2/7/11) indicates the latest version of the PE as V5.3.6,
PICAXE Programming Editor Full Download
Please uninstall any old version (via Control Panel) before installing this version.
Programming Editor v5.3.6 (full version, approx. 80MB)
if you proceed to download the PE file, the BAS805.exe file is in fact already V5.4.0. :)

EDIT: or flush the browser cache using the [F5] key as hippy has indicated elsewhere to see the latest page :rolleyes:
 
Last edited:

hippy

Ex-Staff (retired)
This code works. Tested 08M2 at 32MHz using PWMOUT driving an LED on output pin 2, an 18M2 fading the brightness up and down by instructing the 08M2 running at 4MHz, comms at 76800 baud ...

Code:
#Picaxe 08M2

'       .----_----.
'      -| +V   0V |-
'      -| SI   O0 |-
'      -| X4   X1 |<-- HSERIN
'      -| I3   X2 |--> PWM
'       `---------'

Symbol ident.word = w0 ' Word variable
Symbol ident      = b0 ' Lsb
Symbol ident.flag = b1 ' Msb

Symbol speed.word = w1 ' Word variable
Symbol speed      = b2 ' Lsb
Symbol speed.flag = b3 ' Msb

SetFreq M32

HSerSetup B76800_32, %000

Do
  ident.flag = 1
  speed.flag = 1
  Do
    HSerIn ident.word
  Loop Until ident.flag = 0
  Do
    HSerIn speed.word
  Loop Until speed.flag = 0
  Select Case ident 
    Case "S" : Gosub UpdateSpeed
  End Select
Loop

UpdateSpeed:
  PwmOut C.2, 63, speed
  Return
Code:
#Picaxe 18M2

'       .-----_-----.
'      -| C.2   C.1 |-
'       :           :
'      -| B.1   B.6 |-
'      -| B.2   B.5 |--> HSEROUT
'      -| B.3   B.4 |-
'       `-----------'

HSerSetup B76800_4, %000

Pause 500

Do
  For b0 = 0 To 126
    HSerOut 0, ( "S" )
    Pause 2
    HSerOut 0, ( b0 )
    Pause 20
  Next
  For b0 = 125 To 1 Step -1
    HSerOut 0, ( "S" )
    Pause 2
    HSerOut 0, ( b0 )
    Pause 20
  Next
Loop
That works nicely and smoothly, however, at higher baud rates or with lower pauses things start to get shaky, the LED starts flickering and jumping in levels.

One problem is that this double-buffering of HSERIN is in the on-chip hardware and only has limited usefulness. It will receive one byte and hold that while another is being received but if the held byte isn't taken by the time the second byte has arrived then the first is overwritten by the second. So you need to be punctual in grabbing the first byte.

The trouble is that even at high speed the PICAXE takes some time to do its thing and if bytes are arriving faster than that it all fall to pieces. At 115200 baud, back to back characters will be received with just 86us between them. The PICAXE won't be able to get round the DO-HSERIN-LOOP in 86us so the second byte will always have overwritten the first.

Likewise you cannot send back-to-back address/ident plus speed bytes while the PICAXE is busy updating the PWMOUT. If you do, the first address/ident is received, then the speed which overwrites the first, leaving that as the byte read by the first HSERIN as address/ident, and then the PICAXE takes the next ( which will be an address/ident byte ) as the speed. Everything gets out of kilter.

It's therefore necessary to have a slow enough baud rate that the PICAXE will have taken the first byte before the on-chip hardware overwrites that with the second or to use a pause to separate those two bytes. It's also necessary to pause long enough between sets of bytes that the PICAXE is always ready to receive the first of the pair.

I didn't investigate what the minimum pauses need to be, nor why 76800 baud works but 115200 did not.
 
Last edited:
Top