M2 comms using hardware serial

thomasfoltz

New Member
I am trying to use a 14M2 to send two bytes to an 08M2 using the hardware serial commands. I have B.0 of the 14M2 connected to C.1 of the 08M2. My goal is to make the 14M2 a controller for 4-08M2's, so the 14M2 will send an id number, followed by a command to turn an LED on or off, for now. The LED is connect to C.4 of the 08M2. I cannot seem to get it to work. Any help would be appreciated. Here's my code

14M2:
Code:
init:
hsersetup b2400_4,0	'non-inverted data

main:
do
	hserout 0,(1,2)	'command 2 turns on the LED
	pause 100
loop
08M2:
Code:
symbol chipid=1		'we give this chip number 1
				'receiving commands from
				'the main controller
symbol LED = c.4

symbol serid=b0
symbol sercmd=b1

init:
hsersetup b2400_4,0	'non-invert data
low LED

main:
do
	pause 50
	do:hserin b0:loop while b0!=1
	hserin b1
		if b1=2 then
			high LED
		else
			low LED
		endif
	b0=0:b1=0
loop
 
Last edited by a moderator:

lbenson

Senior Member
Do you have the grounds connected between the boards?

If that's not the problem, try to simplify by just using serout and serin to make sure your hardware is connected correctly. Then move to the hser commands.

Note that if you post your code between [ code ] and [ /code ] tags (omit the space characters), your line indentations will be preserved.
 

nick12ab

Senior Member
Since on M2 parts the hserin command is non-blocking and just either copies new data from the buffer to the specified variable or leaves the variable unchanged if there isn't any, the code is probably too fast for the second byte to be received before the next hserin command is received, so the 2 is never written to b0.

Try this code with a delay added before the second hserin.
Code:
#picaxe 08m2
symbol chipid=1		'we give this chip number 1
				'receiving commands from
				'the main controller
symbol LED = c.4

symbol serid=b0
symbol sercmd=b1

init:
hsersetup b2400_4,0	'non-invert data
low LED

main:
do
	pause 50
	do:hserin b0:loop while b0!=1
	pause 100
	hserin b1
		if b1=2 then
			high LED
		else
			low LED
		endif
	b0=0:b1=0
loop
 

thomasfoltz

New Member
Thanks for the assist. I have tried with pause values up to 250, and the LED still blinks. The two picaxes share the same power supply, so they do have a common ground.
 

eclectic

Moderator
Thanks for the assist. I have tried with pause values up to 250, and the LED still blinks. The two picaxes share the same power supply, so they do have a common ground.
It might be best if you posted

1. Your full circuit.

2. A hi-res photo of your setup.

e
 

dmaxben

Member
Ive had some trouble figuring out the hardware serial in on the 20M2 chips as well...I have some programs that use parallel-tasking and wanted to have one of the tasks "background monitor" the hardware serial-in for some simple commands from a slave-processor Picaxe 14M2.

I could never really get it to work correctly, I didnt know if maybe hardware serial in (background receive) feature on the M2 chips doesnt work correctly when in parallel-task mode or.......???

Also, the manual is not quite clear on whether you can use just the hardware serial-in (background receive), while simultaneously keeping the serial out port as a general purpose input/output on the M2 chips. Or does the hardware serial-out port get "tied up" mandatory when the hardware serial-in (background receive) is enabled/configured??

Because I could not get it to work properly, I ended up just setting up a normal serial-in, and what the Picaxe 14M2 slave processor would do is send a pulse to one of the 20M2 inputs right before it needed to send some serial data, then the Picaxe 20M2 master processor would jump to a "serial receive" gosub portion of the program, receive the serial data, and go back to what it was previously doing... (that way the Picaxe 20M2 isnt sitting there tied up when the Picaxe 14M2 slave proc is idle (not sending any data).

Ben
 

thomasfoltz

New Member
I wonder if it's a problem with the FIFO buffer. There's no real good way to tell if it has received new data and which of the two variables will get that new data.
 

Goeytex

Senior Member
The problem is with TX / RX timing related to the cheesy M2 two byte serial buffer. Since the hserin command is non blocking and there is no background receive interrupt available and no way to reset the hserin buffer pointer ...the timing will get off. Hserin as implemented on M2 parts is pretty worthless unless you are looking for only one byte at a time.

You could use an extra input on the 08M2 as an interrupt pin that is tied to the hserin pin. When data is received ( Low) an interrupt is triggered and program flow goes to the interrupt sub routine. Pause long enough to allow both bytes to be received then do the testing ....turn on or off the led, reset the interrupt, return to the main loop and then wait for the next interrupt. But all this is a waste of I/O pins ......

In this application there is no advantage to using hardware serial. It actually makes things worse. I would highly suggest using serout / serin.
 
Last edited:

thomasfoltz

New Member
Thanks for that idea. I'll give it a try. In the end, the 08M2 will only be controlling two servo motors. My goal is to send commands to position these servos and do it in a way that won't affect the servopos timing. I hope the hser commands will accomplish this.
 

Goeytex

Senior Member
I think you will find that the servo will glitch from time to time anytime hserial is active. The servo command wants exclusive use of the timer and if it doesn't get what it wants, it behaves poorly. The same glitching will happen with hspi and hi2c. This glitching can be eliminated by creating a servo signal with pulsout in a timed 20 ms loop, but it's tricky.

Search the active forum for the "Servo Vectoring" thread where sample code is posted. I think I posted code showing how to do this in other threads as well.

Good Luck
 

thomasfoltz

New Member
That looks like it worked. I was able to crank the baud rate up all the way. Now on to servos and this vectoring.

Code:
'Example of using hserout to send a command to 
'another picaxe which will control an LED
#com1
#picaxe 14m2
#no_data
#terminal off

symbol buttin = pinc.3

symbol serid = b0			'which leg gets the command
symbol sercmd = b1		'command to be transmitted

init:
hsersetup b115200_4,0	'non-inverted data
serid=1
sercmd=0

main:
do
	if buttin=on then
		sercmd=1
	else
		sercmd=0
	endif
	hserout 0,(serid,sercmd)	'command 1 turns on the LED
loop


'Example of using hserin to receive a command from
'another picaxe to control an LED connected to C.4
#com1
#picaxe 08m2
#no_data
#terminal off

symbol chipid=1				'we give this chip number 1
											'receiving commands from
											'the main controller
symbol LED = c.4

symbol serid=b0
symbol sercmd=b1

init:
hsersetup b115200_4,0		'non-invert data
low LED
setint 0,%000010

main:
do
	pause 5
loop

interrupt:
hserin serid:hserin sercmd
if serid=1 then
	if sercmd=1 then
		high LED
	else
		low LED
	endif
endif
serid=0
setint 0,%000010
return
Did I do this right? (code,/code)
 
Last edited:

Goeytex

Senior Member
On the 08M2 since it only receives hserial data it may be a good idea to disable hserout by setting bit3. This allows pin C.0 to be used
for other output functions.

hsersetup B2400_4,%001000
 

thomasfoltz

New Member
I have plans for that also. I may need to have the slave 08M2's acknowledge that tasks have been completed so they can receive the next command.
 
Top