Serial Communication

glengui

New Member
Hello Picaxe Community

I've been banging my head against the wall about this one for a while, and it feels like there is an obvious answer. I've got two chips, 18M2 and 40X2, and I want to send data from the 18 to the 40. The 18 reads inputs, and the 40 set outputs, I've heard this is a pretty standard setup for people who need more pins.

My problem is that I can't get the 18M2 to talk to the 40X2 using SEROUT and SERIN, no wrong data, no corrupt data, just nothing is received by the 40X2. I have one OUT pin on the 18 tied directly to an IN pin on the 40, both chips have common ground (and common power). I have tried pull-up and pull-down resistors on the data line between the chips. I'm fairly confident that the 18M2 is sending the data, all I need to send is one variable, not a lot of data.

I feel like I'm pretty experienced when it comes to Picaxe chips, but this time I'm stumped. Can someone please help me?
 

inglewoodpete

Senior Member
Welcome to the PICAXE forum. I've tried banging my head too, over many years. It doesn't work. Posting your problems here probably will work, though!

Could you post the code you have developed for each PICAXE? It will help to post just a "working" version of the serial comms routines at both ends (yes I realise that it doesn't work but I think you know what I mean). That way, forum members can get a better idea of how they can help.
 

glengui

New Member
Good point, thank you inglewoodpete.
My code for calculating B3 in the 18M2 is long and I know it works, so I omitted it. Pin 6 of the 18M2 is pin B.0 and is capable of SRI/Out/In. Pin 21 on the 40X2 is pin D.2 and is capable of In/Out {ADC22/touch}. On the 40X2, I'm just using debug to see if the variable is changing, so far it has never changed.

PICAXE 18M2
Code:
thetop:
;calculate value for B3 based on button pressed
serout 6, N2400, (B3)
goto thetop

PICAXE 40X2
Code:
thetop:
debug  ;check to see if B4 has changed
serin [1000,thetop], 21, N2400, B4
 

hippy

Technical Support
Staff member
PICAXE commands use pin names rather than leg numbers so try ...

serout B.0, N2400, (B3)

serin [1000,thetop], D.2, N2400, B4
 

rossko57

Senior Member
Another consideration, prompted by use of the N2400 parameter. You might not get the baud rate you were expecting if either Picaxe is running at altered clock speed. Using the form like N2400_8 at least reminds us to think about these things!
 

lbenson

Senior Member
As hippy says, use the "d.2" notation. You can actually use a number instead, since the pin.# forms resolve to a number between 0 and 31 (on the 40x2). However, 21 is A.5, not D.2, which would be 26.

#picaxe 40x2
sertxd(#A.5," ",#D.2) ' yields "21 26"

This can be useful if you want to walk through a series of pins in a loop, but needs care.
 

hippy

Technical Support
Staff member
It is best not to use the underlying numbers for port.pin names because they won't mean much to most people. If one wants to step through pin names for some reason just use the range of pins on a particular port. For example -

For pinIndex = D.0 To D.7
High pinIndex
Next
 

glengui

New Member
PICAXE commands use pin names rather than leg numbers so try ...

serout B.0, N2400, (B3)

serin [1000,thetop], D.2, N2400, B4
The devil's in the details. THANK YOU hippy! You are right. Changing from "pin number" to "port.pin" solved everything. So now my only question is, why is the example for "serin" in the manual ... wrong?

Example:
main: for b0 = 0 to 63 ; start a loop
serin 6,N2400,b1 ; receive serial value
write b0,b1 ; write value into b1
next b0 ; next loop


However, 21 is A.5, not D.2, which would be 26.
I triple checked this, pin 21 is D.2 on the 40X2. Everything else you said is correct and very helpful, I may use that in the future.
 

hippy

Technical Support
Staff member
So now my only question is, why is the example for "serin" in the manual ... wrong?
Not so much wrong as outdated. Earlier PICAXE had implicit port references, so "SERIN 6" referred to what would now be called C.6. It is "Input Pin 6" rather than "leg 6".

I triple checked this, pin 21 is D.2 on the 40X2.
What lbenson was saying is the numeric value 21 refers to the A.5 pin if the number were used in a PICAXE program command.
 

lbenson

Senior Member
Pins 0-31 are port B, C, A, D. I always run in the simulator, for example, "sertxd(#A.5)" to check.

But as hippy said, it's almost always best to use the port.pin designation, e.g., A.5.
 

hippy

Technical Support
Staff member
As lbenson notes the numbers 0-31 represent ports B, C, A, D. While that's likely to remain so, there is no guarantee it will always be as such for future PICAXE variants.

For older PICAXE; how numbers are mapped to the port.pin names we now commonly use depends on context; SERIN 1 can reference C.1 for input while SEROUT 1 can reference B.1 for output.

The main reason for not using pin numbers is simply because they will be taken for leg numbers and assumed to be a coding error or misunderstanding.
 

edmunds

Senior Member
As lbenson notes the numbers 0-31 represent ports B, C, A, D. While that's likely to remain so, there is no guarantee it will always be as such for future PICAXE variants.

For older PICAXE; how numbers are mapped to the port.pin names we now commonly use depends on context; SERIN 1 can reference C.1 for input while SEROUT 1 can reference B.1 for output.

The main reason for not using pin numbers is simply because they will be taken for leg numbers and assumed to be a coding error or misunderstanding.
Perfectly understandable. Can I expect that the numbers will always be sequential for a port? If that is the case I do not need to know the actual numbers :).


Thank you for your time,

Edmunds
 
Top