Fun With Serial Ports
I'm working on building a BS-2 equivalent of the Matrix Orbital series LCD displays however I've hit a wall and need to know if this is a programming limitation or if it's something I have coded.
Here's the rules of the program:
Commands start with a 0xFE (254) and are followed by the actual command and any optional parameters.
Example:
(all numbers are their Decimal equivalents, actual commands are sent in HEX and are doded as such.)
254,88 'Clears display and homes cursor
254,74 'Turns on LCD underline
254,71,4,12 ' Moves cursor to 4'th row, 12th line.
etc..
I wrote a quick little app that reads a char from the serial port (using port 16 because I can't afford the MAX232)
and prints some basic info on it. I would expect that it would read each character from the serial port but instead it only reads one.
Here's the app:
'{$STAMP BS2}
'{$PBASIC 2.5}
char VAR Byte
DO
SERIN 16, 16468, [noparse][[/noparse]char]
DEBUG "Char received '",char,"'",CR
DEBUG "DEC = ",DEC CHAR,CR
DEBUG "HEX = ",HEX CHAR,CR
DEBUG "BIN = ",BIN8 CHAR,CR
LOOP
END
I would expect that (254)H would show:
Char received ' '
DEC = 254
HEX = FE
BIN = 11111110
Char received 'H'
DEC = 72
HEX = 48
BIN = 01001000
but instead all I get is the info about the first character (char 254)
Any suggestions as to why the BS2 never picks up the "H"? I read in the PBASIC syntax guide in SERIN that there is no serial buffer in the BS2, Is this what is happening and the lack of a buffer is causing the problem? Is there a way to add one? Would using a MAX232 help this problem?
Thanks in advance.
FIRESTORM_v1
Here's the rules of the program:
Commands start with a 0xFE (254) and are followed by the actual command and any optional parameters.
Example:
(all numbers are their Decimal equivalents, actual commands are sent in HEX and are doded as such.)
254,88 'Clears display and homes cursor
254,74 'Turns on LCD underline
254,71,4,12 ' Moves cursor to 4'th row, 12th line.
etc..
I wrote a quick little app that reads a char from the serial port (using port 16 because I can't afford the MAX232)
and prints some basic info on it. I would expect that it would read each character from the serial port but instead it only reads one.
Here's the app:
'{$STAMP BS2}
'{$PBASIC 2.5}
char VAR Byte
DO
SERIN 16, 16468, [noparse][[/noparse]char]
DEBUG "Char received '",char,"'",CR
DEBUG "DEC = ",DEC CHAR,CR
DEBUG "HEX = ",HEX CHAR,CR
DEBUG "BIN = ",BIN8 CHAR,CR
LOOP
END
I would expect that (254)H would show:
Char received ' '
DEC = 254
HEX = FE
BIN = 11111110
Char received 'H'
DEC = 72
HEX = 48
BIN = 01001000
but instead all I get is the info about the first character (char 254)
Any suggestions as to why the BS2 never picks up the "H"? I read in the PBASIC syntax guide in SERIN that there is no serial buffer in the BS2, Is this what is happening and the lack of a buffer is causing the problem? Is there a way to add one? Would using a MAX232 help this problem?
Thanks in advance.
FIRESTORM_v1
Comments
2. If you DO use the "port 16" debug port, anything sent TO the BS2 will be echo'ed back FROM the BS2. You have to allow for this.
3. If you DO use the "port 16" debug port, then you probably shouldn't use the "DEBUG" output at the same time. This port uses the 'RX' line voltage in the 'TX' signal back out from the BS2 (that's why there's the echo).
4. You CAN have the BS2 recieve (SERIN) serial data on any pin, IF you put a 22 Kohm resistor in series with the signal. You may have to tweak the 'Invert' baud mode.
5. If you use "SERIN 16, 16468, [noparse][[/noparse]MyChar]", then you have told the BS2 to read a single character. You need
"SERIN 16, 16468, [noparse][[/noparse]STR MyStr]" to read an entire string. (Where MyStr is declared as "MyStr VAR BYTE(10)")
OR, you could use SERIN 16, 16468, [noparse][[/noparse]DEC Sync, DEC Byte1, DEC Byte2, DEC Byte3, DEC Byte4]
The "DEC" modifier here will tell the BS2 to read a decimal number string, convert it to a 'number', and stuff that 'number' into the following variable.· This saves memory space on your BS2.
·
That should clear up the port 16 issues quite nicely. [noparse]:)[/noparse]
About point #5:
I was thinking that the BS2 would read the first character and then swing about and ring the 2nd character after it prints the lines. Do I need to add a serial buffer to take care of this issue or will I need to just read in blocks of 20 each time?
2. The non-USB versions of the Matrix Orbital modules appear to have a jumper, which will let you select what they call "RS232TTL". This is the RS232 protocol, done with TTL voltages. That's what the BS2 wants, and means you don't have to insert the MAX232 in the circuit.
3. You can use a 'time-out' on your SERIN, with a DEC modifier on your parameters, to get the data. It's not clear at this point if you intend the BS2 to be a 'stand-in' for the display?
-- Reading further, it DOES look like you're trying to use the BS2 as a 'replacement' for the Matrix Orbital module.· So yes, things would be much easier with a 'second' serial port using the MAX232 to talk to the PC as if the BS2 were a Matrix Orbital module.
Oh, and the reason the BS2 only gets the first character is that the BS2 has no serial buffer.· If the BS2 is·not sitting in a SERIN command, waiting for data, any data that arrives gets ignored.· Thus, your "DEBUG" output statements take so much time, that by the time the BS2 gets back to read another character, the whole message has already gone by.
The DEC modifier gets the BS2 SERIN command to 'wait' for a full number before returning.· Even then, if you want 4 numbers, you need to have 4 DEC clauses.
Post Edited (allanlane5) : 8/8/2006 6:12:05 PM GMT
Hmm, that could kill the project. I know that I would have a max of 24 characters at any given time, but I think I would run out of variable space. [noparse]:([/noparse] You wouldn't have any suggestion for conserving variable space, would you?
I should be using Byte, not word, correct?
I'm still excited about the MAX232s and am glad that I get those to play around with.
Thanks for the help, hopefully I can find a solution.. [noparse]:)[/noparse]
It also depends if you can put delays between the bytes being output. If it were possible for the BS2 to get one byte, make a decision, then enter another SERIN statement BEFORE the PC sent the next byte, all kinds of space-saving stuff becomes possible.
I don't think that it would be possible to insert any pauses into the output of the signals, making it even harder.
So, anyone know how to add a serial buffer to the BS2?
Thanks for the help everyone. [noparse]:)[/noparse]
The 'SMT' co-processor chip includes a serial recieve buffer. It's kind of limited -- 8 bytes? But it should fulfill what you're looking for.