Do I need a serial buffer or MAX232?

I have just started a little project which goes like this.

A Gas sensor module outputs data at 9600 baud, 8 bit, no parity, 1 stop bit, No hardware flow control (only RX TX and GND) Inverted output.

The format of the data stream is as follows:

A 12345 B 12345 C 12345 D 12345 <CR.<LF>

The output is updated about 4 times a second.

What I want to do: read D 12345 part of the data once a second and display it on LCD

So I came up with...

Clock a 18x1 chip to 8mhz to get 9600


main:
setfreq m8 ‘ set freq to 8MHz
serin 1,N9600,(“D”),b1 ‘ receive serial value
write b0,b1 ‘ write value into b1
serout 7,N2400,(b1) ‘ transmit value to serial LCD
pause 20000 ‘ wait 1 second
goto main

Questions:

Will the 18x ok to do this or do i need a 28x?
Will the chip need a buffer or serial driver rsb509B/MAX232 for the incoming data?
Will I need to clear b1 before the next loop?

Any comments would be welcome before i start to solder bits to boards.

Not had much experience with serial data, so i don't really know if the picaxe can handle this type of thing. I did something like this years ago on an old Nascom - but even that was a Z80. Still use them in my CNC Mill
 

womai

Senior Member
If I understand correctly, your incoming string is quite long. It will exceed the number of variables in the Picaxe, so you can't read the whole string at once. Thus I'd recommend using a 28X1 instead, which can do background receive (hserin) into the scratchpad and will not have any issues with the length of the data stream.

There is no need to call setfreq repeatedly. Just pull it outside the loop, so it gets called once at the beginning.

Watch out, the rest of the program does not know that you have the Picaxe running at twice the speed. So you need to adjust the bitrate constants accordingly. Instead of N9600 use N4800, instead of N2400 use N1200.

If you use standard serin/serout, there is no need for a MAX232 tranceiver, just use the standard download circuit (you can omit the 10 kOhm to ground, that leaves you with just a 22 kOhm resistors in front of the receive pin, and an optional 180 - 220 Ohm resistor in front of the transmit pin). But if you use hardware serial (hserin), you must use a MAX232 because hsering expects inverted data.

A general recommendation at the end (but a very important one): DO NOT start out with soldering your circuit together. I can almost guarantee you'll find things that won't work or things that you will want to do differently and having everything soldered makes changes and troubleshooting difficult or impossible. Instead, build up everything on a breadboard and work with that until you have it up and running and everything is the way you want it. Only then think of moving it over to e.g. a stripboard.

Wolfgang
 

BeanieBots

Moderator
It is 'unlikely' that you will need any sort of buffer but that depends on your device. If it happy to receive 5v RS232, then it should work OK.
If it puts out +/-12v signals, then you should interface it to the PICAXE the same way as the download circuit.

Your code might need a little work.
Reduce your pause by factor of 10 to get 1S.
serin 1,N9600_8,(“D”),b1,b2,b3,b4,b5 will be required to read in the 5 digits.
You could then send each digit to the display.
If you want the actual 'number' then there's a bit more work to do.

Also, no such thing as 18X1. Only 18X, unfortunately.
 
Thanks for the solder tip - i do in most cases use breadboard. I just don't want to buy chips that i don't need, hence the questions to clear the wood from the trees.

The data i want is only 'D 12345' part of the string. The rest is scrap. "D" qualifier used

Will I need to look for the "<CR" to breakout the loop!!!??? Is a <CR a terminator?

The baud rates. Missed that one

setfreq m8 ‘ set freq to 8MHz ' Time is halved/baud is doubled
main:
serin 1,N4800,(“D”),b1 ‘ receive serial value
write b0,b1 ‘ write value into b1
serout 7,N2400,(b1) ‘ transmit value to serial LCD
pause 20000 ‘ wait 1 second
goto main
 
Thanks Beaniebots

5V RS232 is the output.

Time = I must slow down when i get brain storms and type better. pause 20000 is a little off.

So awaking. b1 is not a word space so FIFO - umm.

18x 18x1 they all just blur together to me.

setfreq m8 ' Time is halved/baud is doubled
main:
serin 1,N4800_8,(“D”),b1,b2,b3,b4,b5 'freq = 8 so 4800 x 2 = 9600??
write b0,b1 ‘ write value into b1
serout 7,N2400,(b5,b4,b3,b2,b1) ‘ transmit value to serial LCD
pause 2000 ‘ wait 1 second
goto main

or can i use

setfreq m8 ' set freq to 8MHz
main:
serin 1,N4800_8,(“D”),#b1 'put string into variable?
write b0,b1 ‘ write value into b1
serout 7,N2400,(#b1) ‘ FIFI or LIFO
pause 2000 ‘ wait 1 second
goto main


from the manual2: Page 147. Optional #’s are for inputting ascii
decimal numbers into variables, rather than raw characters. Or have i just got it wrong again

I know this is a bit basic on the code but it really has not got much else to do. Its a very basic gas monitor.
 

hippy

Ex-Staff (retired)
5V RS232 is the output

We could have a long technical debate over the specifics but as long as it's 5V coming out of the module's serial it will connect directly to a PICAXE without any additional interfacing needed.

write b0,b1 &#8216; write value into b1

Not sure where this is coming from or why it's needed; that's for writing to Data Eeprom. To take what you receive and send to the LCD, this should be good enough ...

serin 1,N4800_8,(&#8220;D&#8221;),b1,b2,b3,b4,b5
serout 7,N2400,(b5,b4,b3,b2,b1)
 

BeanieBots

Moderator
I don't do much serial other than sending to AXE033 so can't be too sure.
Just stick in a "debug" or "sertxd" after your serin and have a look at what you get back.

As for baud rate, you need N9600_8 if you are running at 8Mhz.
It is the same as N4800 or N4800_4.
They are just predefined symbols but suffixed with the frequency you are using for convienience.
 
symbol data0 = b6
symbol data1 = b7
symbol data2 = b8
symbol data3 = b9
symbol data4 = b10

setfreq m8
start: '***************** WRITE THE REDEFINED CHARS ON LCD DISPLAY
let b1 = 1 ; set b1 to ‘clear display’ instruction
gosub wrins ; send instruction to LCD

main:
serin 1,N9600_8,("D"),b6,b7,b8,b9,b10;**"Read in the data" ;
let b1 = 134 ; set b1 to 131 position on LCD
gosub display0 ; display cell 0
pause 2000
gosub start ; wait 1 second

display0: ;** Send data **
serout 7,T2400,(#data0)
serout 7,T2400,(#data1)
serout 7,T2400,(#data2)
serout 7,T2400,(#data3)
serout 7,T2400,(#data4)
gosub main


wrins:
serout 7,N2400,(134,b1) ;** emulate LDC display on serout 7 **
return
 
This code seems to work i tried. I know its not neat but i need to add other routines so this format may allow for expansion

serout 7,N2400,(b5,b4,b3,b2,b1) seemed to do nothing in simulator.

I think i need to clear the screen between cycles or does the new data just write over the top?

Any ideas on how i can make this a real number that can be +-/* with?
 

Jeremy Leach

Senior Member
Assuming leading zeroes to aways make the number 5 digits, and assuming number<=65535... my preference would be to first bung the digits in RAM then manipulate from there...

Code:
Poke RAM_DigitStoreStart, b1,b2,b3,b4,b5

WordVal = 0 
For Address = RAM_DigitStoreStart To RAM_DigitStoreEnd
    Peek Address, ByteVal
    WordVal = WordVal * 10 + ByteVal
Next
Or something like this. You'll need to define the symbols. On exiting the for..next, WordVal holds the number.
 
Last edited:
I love looking at well written and thought out code like yours. It inspires me to greater heights. Well just to get it going really.

So far:

w0 = b1 : b0
w1 = b2 : b3
w2 = b4 : b5
w3 = b6 : b7

setfreq m8
start: '***************** WRITE THE REDEFINED CHARS ON LCD DISPLAY
let b1 = 1 ; set b1 to ‘clear display’ instruction
gosub wrins ; send instruction to LCD

main:
serin 1,N9600_8,("D"),b0,b1,b2,b3,b4;**"Read in the data" ;
poke $50,b0 ;put it into Memory
poke $51,b1
poke $52,b2
poke $53,b3
poke $54,b4
gosub calc ;make number strings
gosub display0 ; display results
pause 2000

calc:

peek $50,b0 ;get contents of memory
peek $51,b1
peek $52,b2
peek $53,b3
peek $54,b4
;make strings W0 - W3 ????????
return



display0: ;** Send data to LCD**

serout 7,T2400,(b5,b4,b3,b2,b1) ; Amend for string output
gosub main


wrins:
serout 7,N2400,(134,1) ;** emulate LDC display on serout 7 **
return


However looking at the

w0 = b1 : b0
w1 = b2 : b3
w2 = b4 : b5
w3 = b6 : b7

I keep getting syntax error - its just like the manual says.

I am assuming if i understand this right, that this will allow me to combine the contents of the poked valves into a word variable of which i can then do some math on.

Just can't see the error. Any suggestions. I have looked at loads of examples and searched the forum and it don't work.
I have even reload the editor just in case.

Any suggestions. I know i have got it wrong Just can't see the error.
Not much progress in a day - but i have learnt a lot. Some of you guys are so smart.
 

womai

Senior Member
Instead of

w0 = b1 : b0

write

w0 = b1 * 256 + b0

or alternatively

w0 = b1 << 8 + b0.

But actually all this is not really necessary because w0 and (b0, b1) are "mapped" onto the same locations in memory. I.e. if you change b0 or b1, you will automatically also change w0, and vice versa (meaning w0=256*b0+b1 at all times).

Look at the section about variables at the beginning of the Picaxe Basic manual for a more detailed description.

Wolfgang
 
Seems all very confusing, but i understand the application of +/- to it. I - just could not the example. At least i don't get the syntax now - thanks.

I am quite new to picaxe and the coding. Im surprised that on some levels it really is a great system. On the other hand why so difficult to a create string and manipulate it to squirt it to a lcd. I would have thought that would be the dream of the kids that the things are designed for. Oh well just my 2p worth.

I know it will get easier as i go.
 

womai

Senior Member
As to string handling - strings easily use massive amounts of memory, and low-end microcontrollers just don't have much memory. Not a good fit. E.g. the 28X1 has around 368 bytes total RAM, and a portion of that goes into firmware needs. So not to much use spending firmware program space on implementing string routines if in the end they will be barely usable because there isn't much storage space for any strings...

That said, displaying numbers and constant text on an LCD is quite easy (provided you use Rev-Ed's serial LCD which is quite competitively priced). One thing I'd wish for though on the Picaxe side would be built-in support for parrallel-interface LCD - would greatly enhance the selection of displays (and display formats), and reduce cost.

Wolfgang
 

hippy

Ex-Staff (retired)
I don't think it's hard, but I think there's a problem in understanding exactly what you want. I have no idea what you mean about this "+/-"; where it comes from, how is it determined, how is it then used etc, and likewise the "real number" you mentioned.

All this buffering data into RAM seems to be completely confusing things and entirely unnecessary, likewise converting the bytes into words and so on. What are we actually trying to achieve here ?

I also think people may be confusing hardware buffering with software buffering. I know I'm getting more confused the more this is going on.

I go back to my earlier post, a simple SERIN will grab the five digits you want ( or as many as you want, up to 13 ) and a simple SEROUT will put them to the LCD. A few changes to get the timing you desire and LCD formatting and text placement right and that is it.

What more is there to do ?

If there's processing involved we need to know what the raw input stream is and what that means, what you want to display, and an idea of how one would go about converting the input to what you need to display.
 
Sorry a bit vague again. It most likes not help that i have moved the goal post a little. I move thing on once i have cracked one bit. I add another step. Its just a bad way of doing things and I don't really want to bore people with a huge list. So my way is to eat a little at a time and learn. Again sorry for the confusion. If you are confused trust me I'm bewildered.

I have a gas sensor that outs a string 5v RS232. The string is made up of ID letters and 5 digits such as - D 12345 I capture this out of the string. Now what i need to do the is either x 10 for ppm or /100 for %. Then Output the result to LCD.

The +-*/ i use refers to basic math functions that can be applied.

The getting of data is fine. The output and timing is fine, i don't even need long term storage. Its just the bit in the middle I am having a few teething problems getting to grips with.

In the past i have only worked with dos based machines and the code requirements where very very basic. The scary part is that is was nearly thirty years ago and all this pic stuff is amazing. Just getting old and trying to learn
 

BeanieBots

Moderator
If you only want to *10 or /100, then nothing could be easier. You don't even need to do any maths.

You have "12345" held as seperate variables from when it was read in.
So, for *10, output an extra "0" at the end.
For /100, output the first 3 digits, then a "." and finally the last two digits.
 
Top