serin and serout with multiple bytes

Jon_

Member
I am trying to sent a pattern of 3 bytes via serial, i have the following code written:

master files:
Code:
#picaxe 40X1

serout 7, N2400, ($01, $80, $01)  'send message
receiving file:

Code:
#picaxe 28X1

setint 0,%00000100	'interrupt on pin2 when low

main:

goto main

interrupt:
	serin 2, N2400, b7, b8, b9
	pause 500


	sertxd("byte b7 = ", #b7, CR, LF)
	sertxd("byte b8 = ", #b8,CR, LF)
	sertxd("byte b9 = ", #b9, CR, LF)

	if  b0 = $01 then	'they are talking to us!
		high portc 3
		pause 500
		low portc 3
		pause 500
		
		
      end if
      
      setint 0,%00000100     
    return
however, the receiver only gets b7 = $00, b8 = $80, b9 = $01 when it should have the transmitter pattern.

The question really is, does the code look right, if so i take it it is the circuit, i never used serout to send multiple bytes so i am concerned its my programming.
 

kevrus

New Member
If I understand correctly, it's the first byte recieved that is missing...perhaps its bieng lost during the time the interrupt is executing...try sending an additional byte prior to the three data bytes and see what happens then
 

InvaderZim

Senior Member
Right now you interrupt when the data line changes. By the time you run serin, you have lost a bit. I think your first byte will always lose the Least Significant Bit (and possibly other bits as well, in practice) in your current scheme.

You might try sending %10101010 as the first byte of every transmission. This will help ensure that the receiver is interrupted properly. On the receiver side, discard the first byte received.

There are more robust ways of communicating, but this can help you get started.
 

westaust55

Moderator
Try adding a qualifier:

Sending:
Code:
#picaxe 40X1

serout 7, N2400, ("ABC", $01, $80, $01)  'send message
and receiving:

Code:
	serin 2, N2400, ("ABC"), b7, b8, b9
[/code

in the SEROUT command you could also try adding several "U" characters (%01010101) as a pre-amble before the "ABC" part
 

hippy

Ex-Staff (retired)
kevrus and InvaderZim are right, that's what normally happens when trying to interrupt on the start of a byte being received; SERIN begins to capture a byte already partly gone by the time the interrupt is entered so it doesn't work.

However ... you are interrupting on the idle state of an Nxxxx polarity line so the program should be continually stuck in the interrupt and should be ready and catching all bytes sent. That suggests the transmitter is sending before the receiver is ready. Put a "PAUSE 1000" at the start of the 40X1 program and that may fix things.
 

huca2010

New Member
Serin 3, N2400, W0

Hi ,
I'm new to this picaxe forum as well as the a newbie picaxe user, just need your help on this.

How to serial IN the Char " AAAA" into ASCII..
example:

serin 3, N2400, w0

" i need to enter "AAAA" as serial data and then send which can accepted as ASCII $AAAA in w0

w0= $AAAA ' w0 should have a value of AAAA as ASCII

thanks for the help.
huca
 

hippy

Ex-Staff (retired)
Welcome to the PICAXE Forum, and don't be afraid of starting a new thread for a new question.

There's some confusion over ASCII and a 16-bit hexadecimal value but if you want to read four separate ASCII characters and form a 16-bit value you need to read four character bytes ...

SerIn pin, baud, b0, b1, b2, b3

Then you need to convert those four ASCII bytes to a single 16-bit number ...

If b0 >= "A" Then : b0 = b0 - "A" + $A : End If : b0 = b0 & $F
If b1 >= "A" Then : b1 = b1 - "A" + $A : End If : b1 = b1 & $F
If b2 >= "A" Then : b2 = b2 - "A" + $A : End If : b2 = b2 & $F
If b3 >= "A" Then : b3 = b3 - "A" + $A : End If : b3 = b3 & $F
w0 = b0 * 16 | b1 * 16 | b2 * 16 | b3
 
Last edited:

huca2010

New Member
serin data ("1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0) ignoring comma

thanks Hippy...it's getting more interesting...
i have another ? for you...what if i have a serial data which as below..
"1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0" - comma separated as serial data which i want to serin on picaxe 8M as a 16 bit binary for example ignoring the comma character.


'serial data "1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0"

serin pin, baudrate, w0?

w0 should contain = %1010101010101010 'ignoring comma

thanks again very much, i really appreciate your support.

this would be my last question with regards to SERIN command hopefully ;) :) then i could proceed exploring the rest of the manual.
 

westaust55

Moderator
I think available time between characters/values to manipulate the incoming data stream, particularly if the baud rate is high will be a problem and the 08M does not have many variables available to handle directly.

Why can you not strip out the comma’s prior to sending the data?
What is the source of the data stream?


Untested, but something like this just may work:

Code:
; first receive all 31 characters
FOR b0 = 80 TO 111
SERIN pin, baudrate, b1
Poke b0, b1
NEXT b0
; then transfer into variable w1
w1 = 0
FOR b0 = 80 TO 111 STEP 2
PEEK, b0 , b1
w1 = w0 * 2 + b1
NEXT b0

The above may not be fast enough to catch every character/byte coming in to the 08M

Certainly consider running the 08M at 8MHz
Hippy could likely come up with a more elegant and functioning solution
 

hippy

Ex-Staff (retired)
The following ( untested ) should work ..

SerIn pin, baudrate, #bit15, #bit14, #bit13, #bit12, #bit11, #bit10, #bit9, #bit8, #bit7, #bit6, #bit5, #bit4, #bit3, #bit2, #bit1, bit0

Note it's intentional that the last 'bit0' is not '#bit0'.

If there are no commas between the 16 digits received then remove all '#'.
 

huca2010

New Member
Excellent support! thank you very much..

Hi Hippy and westaust55,

I'm really happy and very satisfied for your technical support....
thanks a lot and more power to you all!
HUCA:)
 

hippy

Ex-Staff (retired)
I'm paid to support but westaust55 and other members ( inlcuding moderators ) do it out of the kindness of their hearts and for no reward other than to help people and Rev-Ed are pleased to acknowledge that and their help. It's members who make this forum community what it is so welcome aboard and thanks for your thanks.
 

womai

Senior Member
Even with a qualifier I suspect you'll need to send every message twice - once to trigger the interrupt, then again to actually get qualifier and data received. A better way would be to trigger the interrupt, the wait a short while (to give the receiver time to react and jump to the interrupt routine), and then send the actual data (including qualifier if you like):

transmitter:

#picaxe 40X1

' trigger receiver interrupt
low 7
pause 5
high 7

' send actual data
serout 7, N2400, ($01, $80, $01) 'send message


receiver:

#picaxe 28X1

setint 0,%00000100 'interrupt on pin2 when low

main:
goto main

interrupt:
serin 2, N2400, b7, b8, b9
setint 0,%00000100 'interrupt on pin2 when low
return
 
Top