Two PICs Serin/Serout problem

Tony P

Member
I am trying to get an 08M2 to send and receive commands to an 18M2 which has two stepper motors wired.

The test code below works perfectly, turning each motor (1 to 4) a set number of pulses.(25 in this example)

The first few lines of the 18M2 are:
SERIN C.2,N2400,B20,B21
IF B20=1 THEN GOSUB MOTOR_1_CW
IF B20=2 THEN GOSUB MOTOR_1_CCW
IF B20=3 THEN GOSUB MOTOR_2_CW
IF B20=4 THEN GOSUB MOTOR_2_CCW


What i would like to do is replace the "Pause 4000" commands with Serin and have the 18M2 send a "finished moving" command each time the stepper has completed it's move.

I have tried using Serout,pin.baud,("go") on the 18M2 and Serin,pin,baud,("go") on the 08M2 but all I achieve is the 08M2 missing out one instruction and moving on to the next. It is not waiting at the Serin command.


Code:
#PICAXE 08M2
#NO_DATA

INIT:
PAUSE 1000

MAIN:
	SEROUT C.2,N2400,(1,25)
	PAUSE 4000

MOTOR_1_CCW:
	SEROUT C.2,N2400,(2,25)
	PAUSE 500

MOTOR_2_CW:
	SEROUT C.2,N2400,(3,25)
	PAUSE 4000

MOTOR_2_CCW:
	SEROUT C.2,N2400,(4,25)
	PAUSE 4000

GOTO MAIN
 

hippy

Ex-Staff (retired)
I have tried using Serout,pin.baud,("go") on the 18M2 and Serin,pin,baud,("go") on the 08M2 but all I achieve is the 08M2 missing out one instruction and moving on to the next. It is not waiting at the Serin command.
That should work so perhaps post the full code which isn't working for both 18M2 and 08M2 and it may be possible to figure out why it doesn't appear to work as desired.
 

Tony P

Member
EDITED!!

Here is the code Hippy

Code:
#PICAXE 08M2
#NO_DATA

INIT:
PAUSE 1000

MAIN:

MOTOR_1_CW:
	SEROUT C.2,N2400,(1,21)
	SERIN C.3,N2400,(5)
	pause 100
MOTOR_2_CW:
	SEROUT C.2,N2400,(3,23)
	PAUSE 4000
MOTOR_1_CCW:
	SEROUT C.2,N2400,(2,22)
	PAUSE 4000
MOTOR_2_CCW:
	SEROUT C.2,N2400,(4,24)
	PAUSE 4000

GOTO MAIN

Code:
#PICAXE 18M2
#NO_DATA

INIT:
OUTPUT C.1,C.0,C.7,C.6;4021
OUTPUT B.7,B.6,B.5,B.4;BYWR
INPUT C.2
OUTPUT B.0
MAIN:
DEBUG
LOW B.1
LOW B.2
SERIN C.2,N2400,B20,B21
IF B20=1 THEN GOSUB MOTOR_1_CW
IF B20=2 THEN GOSUB MOTOR_1_CCW
IF B20=3 THEN GOSUB MOTOR_2_CW
IF B20=4 THEN GOSUB MOTOR_2_CCW
GOTO MAIN

MOTOR_1_CW:;1
HIGH B.1
	FOR B1= 1 TO B21;STEPS (50 FULL TURN)
		FOR B2 = 1 TO 4;PULSES
			LOOKUP B2,(0,$01,$02,$04,$08),B0
			OUTPINC.1=BIT0;R
			OUTPINC.0=BIT1;G
			OUTPINC.7=BIT2;B
			OUTPINC.6=BIT3;K
			PAUSE 10;SPEED CONTROL
		NEXT B2
NEXT B1
PAUSE 100
SEROUT B.0,N2400,(5)
PAUSE 100
RESET
RETURN
MOTOR_1_CCW:;2
HIGH B.1
	FOR B1= 1 TO B21
		FOR B2 = 1 TO 4
			LOOKUP B2,(0,$01,$02,$04,$08),B0
			OUTPINC.1=BIT3
			OUTPINC.0=BIT2
			OUTPINC.7=BIT1
			OUTPINC.6=BIT0
			PAUSE 10
		NEXT B2
NEXT B1
pause 100
RESET
RETURN
MOTOR_2_CW:;3
HIGH B.2
	FOR B1= 1 TO B21
		FOR B2 = 1 TO 4
			LOOKUP B2,(0,$01,$02,$04,$08),B0
			OUTPINB.7=BIT0
			OUTPINB.6=BIT1
			OUTPINB.5=BIT2
			OUTPINB.4=BIT3
			PAUSE 10
		NEXT B2
NEXT B1
pause 100
RESET
RETURN
MOTOR_2_CCW:
HIGH B.2
	FOR B1= 1 TO B21
		FOR B2 = 1 TO 4
			LOOKUP B2,(0,$01,$02,$04,$08),B0
			OUTPINB.7=BIT3
			OUTPINB.6=BIT2
			OUTPINB.5=BIT1
			OUTPINB.4=BIT0
			PAUSE 10
		NEXT B2
NEXT B1
pause 100
RESET
RETURN
 

hippy

Ex-Staff (retired)
I would guess the problem is with the RESET and DEBUG commands in the 18M2 code. I would suggest restructuring the code along the lines of -
Code:
MAIN:
  LOW B.1
  LOW B.2
  SEROUT B.0, (5)
  SERIN C.2,N2400,B20,B21
  SELECT CASE B20
    CASE 1 : GOSUB MOTOR_1_CW
    CASE 2 : GOSUB MOTOR_1_CCW
    CASE 3 : GOSUB MOTOR_2_CW
    CASE 4 : GOSUB MOTOR_2_CCW
  END SELECT
  GOTO MAIN

MOTOR_1_CW: ;1
  HIGH B.1
  FOR B1= 1 TO B21;STEPS (50 FULL TURN)
    :
  NEXT B1
  PAUSE 100
  RETURN
 

Tony P

Member
I have changed the code as you suggested but now it just keeps on repeating the first command -motor 1 clockwise 25 steps

Also, the stepper is now very jittery
 

Tony P

Member
I have changed the INIT: code on the 08M2 and it would appear that it is resetting after sending the first command.
 

Tony P

Member
Update.

Thank you Hippy.

By using LED's instead of steppers I now have a working system to build on. My reset problem lay with the stepper motors and power rail etc. causing problems

The two working codes are shown below

Code:
#PICAXE 08M2
#NO_DATA

INIT:
PAUSE 1000

MAIN:

MOTOR_1_CW:
	SEROUT C.2,N2400,(1,25,10); MOTOR/DIRECTION , PULSES , SPEED
	SERIN C.3,N2400,(5)

MOTOR_2_CW:
	SEROUT C.2,N2400,(2,25,10)
		SERIN C.3,N2400,(5)

MOTOR_1_CCW:
	SEROUT C.2,N2400,(3,50,10)
		SERIN C.3,N2400,(5)
			SEROUT C.2,N2400,(3,50,2)
		SERIN C.3,N2400,(5)

MOTOR_2_CCW:
	SEROUT C.2,N2400,(4,25,10)
		SERIN C.3,N2400,(5)

GOTO MAIN
Code:
#PICAXE 18M2
#NO_DATA

INIT:
OUTPUT C.1,C.0,C.7,C.6
OUTPUT B.7,B.6,B.5,B.4
INPUT C.2
OUTPUT B.0
OUTPUT B.3

MAIN:
PAUSE 100
  	SEROUT B.0,N2400, (5)
 		SERIN C.2,N2400,B20,B21,B22
  			SELECT CASE B20
				  CASE 1 : GOSUB MOTOR_1_CW
				  CASE 2 : GOSUB MOTOR_1_CCW
				  CASE 3 : GOSUB MOTOR_2_CW
				  CASE 4 : GOSUB MOTOR_2_CCW
			END SELECT
GOTO MAIN

MOTOR_1_CW:;1
HIGH B.1
	FOR B1= 1 TO B21
		FOR B2 = 1 TO 4;PULSES-LEAVE AT 4
			LOOKUP B2,(0,$01,$02,$04,$08),B0
			OUTPINC.1=BIT0;R
			OUTPINC.0=BIT1;G
			OUTPINC.7=BIT2;B
			OUTPINC.6=BIT3;K
			PAUSE B22;SPEED CONTROL
		NEXT B2
	NEXT B1
LOW B.1
RETURN

MOTOR_1_CCW:;2
HIGH B.2
	FOR B1= 1 TO B21
		FOR B2 = 1 TO 4
			LOOKUP B2,(0,$01,$02,$04,$08),B0
			OUTPINC.1=BIT3
			OUTPINC.0=BIT2
			OUTPINC.7=BIT1
			OUTPINC.6=BIT0
			PAUSE B22
		NEXT B2
	NEXT B1
LOW B.2
RETURN

MOTOR_2_CW:;3
HIGH B.1
HIGH B.2
	FOR B1= 1 TO B21
		FOR B2 = 1 TO 4
			LOOKUP B2,(0,$01,$02,$04,$08),B0
			OUTPINB.7=BIT0
			OUTPINB.6=BIT1
			OUTPINB.5=BIT2
			OUTPINB.4=BIT3
			PAUSE B22
		NEXT B2
	NEXT B1
LOW B.1
LOW B.2
RETURN

MOTOR_2_CCW:;4
HIGH B.3
	FOR B1= 1 TO B21
		FOR B2 = 1 TO 4
			LOOKUP B2,(0,$01,$02,$04,$08),B0
			OUTPINB.7=BIT3
			OUTPINB.6=BIT2
			OUTPINB.5=BIT1
			OUTPINB.4=BIT0
			PAUSE B22
		NEXT B2
	NEXT B1
LOW B.3
RETURN
 
Top