Master Slave using serout and serin

wlie

Member
I have now struggled to get the following to work, but the damn things won't do as I suppose they should.
Having a 18M2 to work as a Master and some 08M to work as Slaves. This should be very simple as I only tries to send a character from the Master to the Slave. Afterwards the Slave should return the same character to the Master.
I can send and receive both ways without problem, but when I insert interrupt routines in the slaves, I got wrong charecters. The characters are wrong allready when received by the Slave. Only occasionally the right character shows up.
The diagram here
Master-Slave.jpg
Master program here
Code:
#PICAXE 18M2
#No_Data
#Terminal 4800

	symbol baud = N2400
	symbol outpin = B.4
	symbol inpin = B.1
	low outpin

	b1 = 170
main:
	sertxd ("Master Mainloop",cr,lf)
	gosub sendit
	gosub receiveit
	pause 1000
goto main
	
sendit:
	pulsout outpin, 1
	pause 5
     serout outpin,baud,(b1)
	low outpin
return

receiveit:
	serin [1000,NoData],inpin,baud, b2
	sertxd (#b2," received",cr,lf)
return

NoData:
	sertxd (" No data received",cr,lf)
return
Slave program here
Code:
#PICAXE 08M
#Terminal 4800

	symbol baud = N2400
	symbol outpin = C.2
	symbol inpin = C.4
	setint %00010000,%00010000


MainLoop:
	high C.0
	pause 500
	low C.0
	pause 500
	sertxd ("Mainloop/Slave",cr,lf)
	gosub Interrupt
goto MainLoop


Interrupt:
	serin inpin,baud,b0
	sertxd (#b0," ")
	serout outpin,baud, (b0)	
	b0 = 0
	setint %00010000,%00010000
return
Notice that I until now I only has worked with one slave.

Any help is appreciated.
 

hippy

Ex-Staff (retired)
It's hard to synchronise a pulse and then data with interrupt and receive as the timing has to be right on both sides.

The trick is to not pulse but take the line high, pause, serout with a Txxxx baud rate, then drop the signal low again.

On the receiver, the interrupt then occurs on the rising edge of setting the line high, serin also with a Txxxx baud begins during the pause and is ready to receive the byte when sent.

Also it makes no sense to ' gosub interrupt'.
 

inglewoodpete

Senior Member
I would also change your wiring to have a 330ohm resistor on every input pin. It provides protection for the pin internals should its direction get changed while you're testing your circuit or code.
 

wlie

Member
Hippy
Thanks you for your help.
As your requested I have changed from "pulsout outpin, 1" to "High outpin". Also changed to T2400.
This wasn't enough. The main problem was, that after the slave has transmitted, the outpin was held high, so I need to add a "High outpin" after the serout. That did the final trick.
"Also it makes no sense to ' gosub interrupt'" - agreed, this was just a part of my struggle with the problem where I tried to drive without the interrupt, and I forgot to remove it before writing to the forum.

inglewoodpete
Your are rigth - a resistors are cheaper than a 08M.
 

hippy

Ex-Staff (retired)
The main problem was, that after the slave has transmitted, the outpin was held high, so I need to add a "High outpin" after the serout.
I presume there's some typo there and having only just looked at your circuit diagram you really need to use T2400 baud for Master to slave and N2400 for Slave to Master.
 
Top