Background serial receive


I'm a bit confused ;-(

I want to do some background serial in on a 40X1 (16MHz resonator) receiving from a PC, so do I:

1) Use HSERSETUP B_19200_16, %00000001

2) Connect tx on my PC to pin 26 (In c7/Out c7/ser rx)..
- ...via a MAX232 to get the polarity correct

3) Either use SETINTFLANGS %1000000, %10000000 to go to my interrupt routine every time a character is received (and what happens if they arrive really, really fast?)..
-...or check the hserinflag at an appropriate point in my code (and how do I check it?)

Is there any example code around that puts all this together into a simple, but not too simple, snippet from a real application?

TIA, Martin


New Member
I agree, a good general purpose hardware serial interface, full duplex, buffered and interrupt driven software program would sure be a nice learning tool.

How about it you software gurus, care to educate and amaze us ;-)


Edited by - retrolefty on 28/08/2007 01:48:59



...for the clever people to help out on this - I'd really like to progress it and I suppose I could do it by trial and error, but having some exmplar code and an idea about the hardware would make it a whole load easier...

TIA, Martin


Technical Support
Staff member
It could well be that no one has done this so there's no example to be had.

To get decent examples requires someone to set out on that path to deliver it but most people are usually trying to solve a specific application problem which isn't necessarily as useful, although something is invariably better than nothing. Unfortunately most finished projects don't end with public documentation.

Part of any development involves testing things like this to see what does and does not work, and it would be nice to see more feedback on such things. When the PICAXE Showcase is in full-swing we will hopefully see more of that. Some sort of 'design competition' would hopefully bring a lot more practical examples of PICAXE programming out into the open.


Understood - but I guess Technical must have done this to system test it before calling Technical! Are you there?


Technical Support
Staff member
Hopefully this will give you some ideas - its our test code, you will need to change the baudrates etc. It polls the flags, but you could modify to use 'setintflags' to interrupt.

<code><pre><font size=2 face='Courier'>
; This example program shows how to use the serial hardware
; with automatic background serial receives.
; Serial hardware pins (C6 and C7) connected via MAX232 to
; computers serial port. Output 1 connected to an LED.

; In normal use the LED just flashes. When a background serial receive
; occurs the bytes are echoed back out for testing purposes.

symbol last_byte_received = b1
symbol next_byte_to_echo = b2
symbol counter = b3

; set picaxe type
#picaxe 28x1

; open terminal after download
#terminal 9600
; Don't forget to move serial cable to the hardware pins!

; setup serial hardware
; at 9600 with background receive
hsersetup b9600_4,%01

'loop waiting for a byte

' do something - e.g. flash LED
toggle 1
pause 500

' test hserinflag which is set when a
' background receive occurs
if hserinflag = 1 then byte_echo
goto main

' echo back out the received bytes
hserinflag = 0 'reset the flag

' the last scratchpad address written to
' will be the serialpointer - 1
last_byte_received = hserptr - 1

' echo out any bytes we have not yet used
for counter = next_byte_to_echo to last_byte_received
ptr = counter ' set scatchpad pointer to the relevant address
' and then output the data @ that address
hserout 0,(#ptr,&quot;=&quot;,@ptr,cr,lf)
next counter

' set the last byte echoed as the current serialpointer value
next_byte_to_echo = last_byte_received + 1

goto main


So just to confirm my further thoughts...

I could use SETINTFLAGS to get a jump to the interrupt: routine asap after a hardware receive occurs - but that means I can't use SETINT at the same time to detect a change on say pin 1

...or I poll the hserinflag as shown, but that is up to me to find time to do so.

As a small aside, where are all the SETINTFLAGS masks defined - in the manual it says &quot;e.g. timer 0, hi2c write, hserin etc.&quot; Are there any others?