JDRichards


Home


Engineering and Programming Services


Order On Line.


Link that may be of interest.

RS485 to Basic Stamp Board
This sample source code runs on a Basic Stamp microcontroller (see http://www.parallaxinc.com). Used with the RS485 to Basic Stamp Converter board.
' Copyright 2001 by J.D.Richards
' www.jdrichards.com
' You have the right to use this code
' any way you feel necessary.
'
' This code has been written to be as
' simple as possible.
' The first serin command waits for a
' $A1 string. The the bytes are stored
' up till the ";", then are parsed and
' acted upon. $A1M; prints out the
' command menu.
' For a system with more than 1 module,
' change the $A1 in the first serin command
' to a $A2, the next to $A3, etc.
' ''''''''''''''''''''''''''''''''''''''''''''''
instring VAR byte(8)

''''''''''''''''''''''''''''''''''''
' Define the output pins directions ''''''''''''''''''''''''''''''''''''
output 0 '0 = Read enable, 1 = Write Enable output 1
'Command sets high or low.
output 2
output 3
output 4
output 5
output 6
output 7

'''''''''''''
' Main loop.
'''''''''''''
again:
pause 10
low 0 'Set to read

''''''''''''''''''
'Read in a command
''''''''''''''''''
read1:
serin 1,84+$4000,-1,read1,[WAIT("$A1"),STR instring\7\";"]

''''''''''''''''''''''''
'Command Received, do it
''''''''''''''''''''''''

'* Print help
if instring(0)<>"M" then next100
gosub print_help
goto again

'* Set a pin high
next100:
if instring(0)<>"H" then next200
if instring(1)<>"2" then next101
high 2
goto again
next101:
if instring(1)<>"3" then next102
high 3
goto again
next102:
if instring(1)<>"4" then next103
high 4
goto again
next103:

if instring(1)<>"5" then next104
high 5
goto again
next104:

if instring(1)<>"6" then next105
high 6
goto again
next105:

if instring(1)<>"7" then next200
high 7
goto again

' Set a pin low
next200:
if instring(0)<>"L" then next300
if instring(1)<>"2" then next201
low 2
goto again
next201:

if instring(1)<>"3" then next202
low 3
goto again
next202:

if instring(1)<>"4" then next203
low 4
goto again
next203:

if instring(1)<>"5" then next204
low 5
goto again
next204:

if instring(1)<>"6" then next205
low 6
goto again
next205:

if instring(1)<>"7" then next300
low7
goto again

'* Blink the lights
next300:

if instring(0)<>"B" then next400
gosub blink_lights
high 0 'Set to Write
pause 5
serout 1,84+$4000,2,[cr,"$A2B;",cr]
goto again

'* Find the pin states of 8 and 9
next400:
if instring(0)<>"R" then next500
if in8<>1 then next401
high 0 'Set to Write
pause 5
serout 1,84+$4000,2,["Pin 8 HIGH, "]
goto next402
next401:

high 0 'Set to Write
pause 5
serout 1,84+$4000,2,["Pin 8 Low, "]
next402:

if in9<>1 then next403
high 0 'Set to Write
pause 5
serout 1,84+$4000,2,["Pin 9 HIGH",cr]
goto next500

next403:
high 0 'Set to Write
pause 5
serout 1,84+$4000,2,["Pin 9 Low",cr]

'* Go to beginning.
next500:
goto again

'*******************
' End of main loop.
'*******************

'''''''''''' 'Subroutines ''''''''''''

'*************************
' print_help
' prints a help nessage.
'*************************
print_help:
high 0 'Set to Write
pause 10
serout 1,84+$4000,2,[cr,"Stamp2 RS485 Module 1 Commands: ",cr]
serout 1,84+$4000,2,[" $A1M - Prints this message.",cr]
serout 1,84+$4000,2,[" $A1Hx - Set pin x (2 thru 7) HIGH",cr]
serout 1,84+$4000,2,[" $A1Lx - Set pin x (2 thru 7) LOW",cr]
serout 1,84+$4000,2,[" $A1R - Reads state of pins 8 and 9",cr]
serout 1,84+$4000,2,[" $A1B - Blinks all the lights",cr]
return

'***************************************
' blink_lights
' Blinks the lights in a row one by one.
'***************************************
blink_lights:
OUTS=0
high 2
pause 100
low 2
high 3
pause 100
low 3
high 4
pause 100
low 4
high 5
pause 100
low 5
high 6
pause 100
low 6
high 7
pause 100
low 7

return
To order an RS485 to Basic Stamp Converter board select the Shop button on the left.
.jon@jdrichards.com 530-878-7599
 
JDRichards, Inc.
530-878-7599

jon@jdrichards.com
eBox Internet controllerMondoMini Web Server

Home Services Online Store Useful Links

This document maintained by jon@jdrichards.com.
Copyright © 2001 JDRichards