Sub-routine
fma38
Posts: 42
Hello,
I would like to make a sub-routine to send a command to a panohead beacause there are several things to do (pull a CS line low, send command, readback answer...).
1) I guess there are no parameters I can give to that sub-routine, so I have to use global variables. Do you have any design advice to do that in a clean way?
2) I sometime have to send a pre-defined command (a string). How can I load a Byte array with that string? Do I have to index each char (s(0)=":", s(1)="j", s(2)="2", s(3)=CR...)?
Thanks,
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Fr
I would like to make a sub-routine to send a command to a panohead beacause there are several things to do (pull a CS line low, send command, readback answer...).
1) I guess there are no parameters I can give to that sub-routine, so I have to use global variables. Do you have any design advice to do that in a clean way?
2) I sometime have to send a pre-defined command (a string). How can I load a Byte array with that string? Do I have to index each char (s(0)=":", s(1)="j", s(2)="2", s(3)=CR...)?
Thanks,
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Fr
Comments
2) The best way to do predefined strings is to use a DATA statement to load them into the EEPROM along with the program. You use the READ statement to access them one byte at a time, usually use a zero byte to terminate them. You can define a label with the address of the first byte of the string (put the label on the DATA statement) and that's what you use to send it. Usually, you write a subroutine that accepts one parameter (the address of the first byte) and does a SEROUT loop transmitting one byte at a time and stopping with the zero terminator.
You are correct when you surmise that there is no CALL-EXIT_PGM sequence of instruction, nor is that any facility for parameter passing. The closest thing to it is the RUN command which permits thhe selection of the next active program slot on Stamps that permit multiple banks of program memory.
In any/all cases global variables are indeed used. Using meaningful names in one case (for GOSUB-RETURN) and keeping the variable size and order the same (for using RUN) is of the greatest importance.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
' {$PBASIC 2.5}
' I think (but I'm not sure) the BS2 supports the following syntax:
MyStr1 DATA "Hello",0
MyStr2 DATA "There", 0
MsgAddr VAR Word
MAIN:
· MsgAddr = MyStr1
· GOSUB SendMsg
· MsgAddr = MyStr2
· GOSUB SendMsg
· PAUSE 1000
· GOTO MAIN
SendMsg:
· SEROUT 16, 16468, [noparse][[/noparse]STR MsgAddr, 13]
· RETURN
Note the 'zero' byte after each word is necessary, because that's what the STR modifier uses to know when it's hit the end of the string.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Fr