Simple serial question

tmack

Member
One last project I am working on is simple serial communication between 2 picaxes. I have been working on it on and off for a while . I guess Im not too smart cuz i just cant wrap my mind around it. Could anyone show me what the code would look like for one picaxe to send serial data when a push button switch is pressed that would go to a second picaxe that would recieve the data and use it to make a pin go high. I would love to see an example of the code that would be used by the sending unit and the coded that would be used by the recieving unit. I really need anudge in the right direction on this. I will REALLY appreciate the help on this. I wont bug you again for a while.
 

womai

Senior Member
Here's what you asked for (code is untested):

Sender:

Push button is connected to input(!) 1.
Serial line is connected to output(!) 2.
Sender sends a 1 to turn LED on and a 0 to turn it off.

Code:
low_loop: ' wait until button pressed
    while pin1 = 0 goto low_loop
    serout 2, N2400, (1)
high_loop: ' wait until button released
    while pin1 = 1 goto high_loop
    serout 2, N2400, (0)
goto low_loop
Receiver:

Serial line is connected to input(!) 1.
LED is connected to output(!) 2.

Code:
my_loop:
    serin 1, N2400, b0
    if b0 = 1 then
        high 2
    else ' b0 = 0
        low 2
    endif
goto my_loop
The LED on the receive side should turn on whenever the pushbutton is pressed. (Don't forget to add a pulldown resistor so the sender's pushbutton pin isn't floating but is pulled low whenever the pushbutton - connected between input and Vcc=5V) is released.

Wolfgang
 
Last edited:

tmack

Member
Great thanks

You guys on this board are awesome.It doesn't seem to want to accept "while" on the simulator. If I wanted to change the baud rate to 2400 do I just change the spots where it has 2400 to 9600? How do I change it to use 2 buttons to make 2 pins go high on the reciever? Thanks again.
 
Last edited:

hippy

Technical Support
Staff member
@ tmack : If you want to use 4800 baud you would change all N2400 to N4800. Not all PICAXE's support 4800 ( but we'll come back to that ).

Going up to 9600 is a bit harder because none support an N9600 option for SEROUT and SERIN. The trick there is to make the PICAXE run twice as quick with 'SETFREQ M8'. Use that and N4800 and SEROUT and SERIN will be running at 9600 baud.

The same trick can be applied to those PICAXE's which don't have N4800. Use 'SETFREQ M8' and N2400 and the SEROUT and SERIN will work at 4800. That's the best you can get for those.

If the PICAXE doesn't support SETFREQ then 2400 is the maximum SERIN you can use.

SERTXD works using an implied N4800 so the SETFREQ trick will take that up to 9600 baud. Great for talking quickly to a PC, but not so good inter-PICAXE if the SERIN cannot handle 9600 buad.
 

hippy

Technical Support
Staff member
How do I change it to use 2 buttons to make 2 pins go high on the reciever?
You'd need to send more than just two numbers, and you'd have to have the receiver recognise and respond to more than two numbers.
 

SD2100

New Member
Code sample for using 2 pins, outputs 1 & 4 on the receiver will follow
inputs 1 & 4 on the transmitter.

Code:
'Transmitter
'Inputs used are pins 1 & 4
#Picaxe 08m
do
    b0=pins & %00010010  'Get inputs 1 & 4 only, mask other pins
        if b0<>b1 then        'Check if pin state has changed
        serout 2,n2400,(b0) 'if pin state changed then send
        b1=b0                    'Set b1 to new pin state
    end if	
loop


'Receiver
'Outputs used are pins 1 & 4
#Picaxe 08m
low 1,4
do
    serin 3,n2400,b0 
    pins = b0	'Set outputs according to b0 value
loop
 

tmack

Member
While?

I am using the simulator portion of the picaxe programmer to test the code and mess around with it so I can better understand how it works.The recieving portion of the code seams to be working great . However the transmit side keeps coming up with an error at the "while" when I try and run it on simulation. Is it the case that the simulator doesnt recognize the "while" but the picaxe will when I progam it in? Is there another way I can write it that the simulator will recognize? I am trying to use the if/then but cant quite seem to get it right. Thanks for all the help .
 

womai

Senior Member
There is no "while" in the code I wrote so I assume you are talking about different code. It would help if you could post your code on this forum. Otherwise it's impossible to tell what is wrong.

Wolfgang
 

tmack

Member
Sorry Im not clear.

IN lines 2 and 5 of the above code " while pin1 = 0 goto low_loop" and "while pin1 = 1 goto high_loop" the simulator says "syntax error in this line" my other simulator it says the same thing but with the word (While) in parathesis.Thats why I thought it was that word was the problem. Im sure it is just something Im misunderstanding.
 

tmack

Member
would this work ?

low_loop: ' wait until button pressed
if pin1 = 0 then goto low_loop
serout 2, N2400, (1)
high_loop: ' wait until button released
if pin1 = 1 then goto high_loop
serout 2, N2400, (0)
goto low_loop


If I change it from While to if /then the simulator will take it but Im not sure if it will work right this way or not. Sorry I sound kinda dumb but I really want to undestand how to send / recieve serial data like this. Thanks alot for the help.
 

SD2100

New Member
tmack that will work with IF/THEN.
You can use WHILE but it has to be used in a DO/LOOP structure

Code:
low_loop: 
    do while pin1 = 0 :loop  'wait until button pressed
    serout 2, N2400, (1)
    do while pin1 = 1 :loop  'wait until button released
    serout 2, N2400, (0)
goto low_loop
Also what version of the program editor are you using, early versions didn't support
DO/LOOP, WHILE etc, you should be using version 5.1.5 and enhanced option should be selected in the view/options/editor menu.
 
Last edited:

tmack

Member
I see,sorta

Do I have it right then that the program will wait on the line "do while pin1 = 0 :loop" until the status is changed then it will send out a "1" from pin2 at 2400 baud. The program with then sit on line "do while pin1 = 1 :loop " until pin1 goes low again then it sends a "0" through pin2 at 2400 and again sits on the firstline waiting for it to change again? Thats pretty neat.
I think its finally begining to make sense to me. But now I am confused as to how I would go about expanding it. Say if I wanted to have 4 leds or 8 turn on as switches were thrown in the transmitter. If Im sending 1 and 0 over output 2 how do I get more control signals sent over output 2 ? Can I send numbers other than 1 and 0 like say 3,5 11 etc? I was thinking it was 1 and 0 like logic low logic high but is it just a byte that can be sent and understood by the reciever. IM not sure If Im asking it clearly. Can I send a "3" out on pin2 in the same way as the above code sends 1,0 and then put 3 into the recievers code as well? I think Im confusing myself ........

Thanks Ill check and make sure Im using the correct version.
 
Last edited:

hippy

Technical Support
Staff member
@ Phil75 : It's interesting to compare the two functionally similar commands-

-- Do While pin1 = 0 : Loop

-- Do : Loop While pin1 = 0

The code generated for the second is far more efficient; check the program sizes after a Syntax Check -

-- ' Do While pin1 = 0 : Loop
--
-- L1:
-- IF pin1<>0 THEN L2
-- GOTO L1
-- L2:

-- ' Do : Loop While pin1 = 0
--
-- L1:
-- IF pin1=0 THEN L1
 

SD2100

New Member
hippy: yeh i've played around with all these before in the past when trying to sqeeze
the last byte out of an 08 when I'm running out of room, as you know every byte
counts. I like using SELECT/CASE but it's very hungry. If i'm not pushing for space
I don't worry about it much.
 

tmack

Member
Will this work correctly?

transmitter:

main: if pin0 = 1 then zero
if pin0 = 0 then serout 2, N2400,(0)
goto nex1
endif
nex1: if pin1 = 1 then one
if pin1 = 0 then serout 2, N2400,(2)
goto nex2
endif
nex2: if pin2 = 1 then two
if pin2 = 0 then serout 2, N2400,(4)
goto nex3
endif
nex3: if pin6 = 1 then three
if pin6 = 0 then serout 2, N2400,(6)
goto nex4
endif
nex4: if pin7 = 1 then four
if pin7 = 0 then serout 2, N2400,(8)
goto main
endif

Zero: serout 2, N2400, (1)
goto main

One: serout 2, N2400, (3)
goto main

Two: serout 2, N2400, (5)
goto main

Three: serout 2, N2400, (7)
goto main

Four: serout 2, N2400, (9)
goto main



Reciever:

my_loop:
serin 1, N2400, b0
if b0 = 1 then
high 0
else ' b0 = 0
low 0
endif

if b0 = 3 then
high 1
else ' b0 = 2
low 1
endif
if b0 = 5 then
high 2
else ' b0 = 4
low 2
endif
if b0 = 7 then
high 3
else ' b0 = 6
low 3
endif
if b0 = 9 then
high 4
else ' b0 = 8
low 4
endif
if b0 = 11 then
high 5
else ' b0 = 10
low 5
endif
if b0 = 13 then
high 6
else ' b0 = 12
low 6
endif
if b0 = 15 then
high 7
else ' b0 = 14
low 7
endif






goto my_loop

I think the reciever code may work right but Im pretty sure the transmitter side needs tweaking
 

tmack

Member
Serial sending?

I pretty much have worked out a couple of options for recieving the serial data But Im still confused about sending data. Could anyone help me with the code to convert the 8 input pins (of an 18x)into serial output . I should be able to adjust my reciever code to whatever numbers are sent. I appreciate any help you can give me, thanks.
 

hippy

Technical Support
Staff member
The easiest way to send out the value of the 8 input pins is ...

-- SEROUT 2,N2400,(pins)

To receive them at the other side ...

-- SERIN 1,N2400,b0

Conveniently, for some applications, storing the sent pins value in 'b0' also sets 'bit0' through 'bit7' to reflect what were 'pin0' to 'pin7'.
 

SD2100

New Member
Dosn't the 18x have 5 inputs and 8 outputs, if there were 8 inputs and 8 outputs as on a 28x1 then you could just send the input PINS value to the receiver. With only 5 inputs you'll have to do some trickery to control the 8 outputs on the receiver. Someone on here made a multi button input that used only 1 input pin, maybe a search might turn up something.
 

tmack

Member
08m

I ended up using a pair of 08ms for now. I was finally able to get 3 leds to turn on and off using one 08m with the code you gave me. I send the data to the other 08m to recieve it. After that I was able to quickly write a little Visual Basic program to replace the 08m I was sending the data with . Now I can hopefully add more pins using 18xs on the sending and recieving end and expand the code that has been working to get it up to about 15-16 i/o pins. Then I can use it to control a robot I have been making . I can then change between a hand controller to send tell it what I want it to do and my computor screen with VB. I have a pair of wireless rs232 adapters I have been using for a premade rs232 project I was using before (xtreme serial controller from rentron) on a robot. Thanks SOOOOO much for your help. I would not have been able to do it without you guys. Thanks again..
 
Top