
-----------------------------------
molten_rock01
Wed May 24, 2006 10:01 pm

Help on Assembler Code for Robot
-----------------------------------
Hi. I am currently working on building a robot which follows a wall. For this, I am using two limit switches, which when turned on, make the robot reverse and change direction. So far, I've only gotten the robot to move forward, but the limit switches do not work. The following code is in Assembler and I am using Microchip's P16F684 PIC. 

Could you please check over this code and give me a hint why the limit switches may not be working? Thank you.

title "asmWall"
;
; Robot moves forward until a signal is received from the input switch.
; When a signal from the right input switch is received, the robot reverses direction
; for 2 seconds and then turns left for 2 seconds.
; When a signal from the left input switch is received, the robot reverses direction
; for 2 seconds and then turns right for 2 seconds.
;
; Hardware Notes:
; -P16F684 running at 4MHz
; -RC0:RC1 : right motor
; -RC2:RC3 : left motor
; -RA0 : left input switch
; -RA1 : right input switch
;
; Author
; May 22, 2006
;--------------------------------------------------------------------------
; Setup
	LIST R=DEC
	INCLUDE "p16f684.inc"

 __CONFIG _FCMEN_OFF & _IESO_OFF & _BOD_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTOSCIO

; Variables
 CBLOCK 0x20	;Variable Declaration

DlayValue:2		;Requires 24 Bit Counter (w/ WREG)

 ENDC

Dlay Macro Cycles
 variable CyclesLeft	;Keep Track of Remaining Cycles

 variable LargeNum
CyclesLeft = Cycles
 local LongLoop

 if Cycles > 0x04FFFF0	;Can't Handle > 83 Seconds (@ 4 MHz)
 
 error "Required Delay is longer than 'Dlay' Macro can support"
 endif
 if Cycles > 327681	;Need Large Loop?
LargeNum = CyclesLeft / 327681
	movlw LargeNum
	movwf DlayValue + 2	;Calculate Number of Loops

LongLoop:	;Repeat for Each Loop

	clrf DlayValue + 1	;Do Maximum Possible Loop Count

	clrf DlayValue 
	decf DlayValue, f
	btfsc STATUS, Z
	 decfsz DlayValue + 1, f
	  goto $ - 3
	decfsz DlayValue + 2, f	;Repeat Loop
	 goto LongLoop
CyclesLeft = CyclesLeft - ((LargeNum * 327681) + 1 + (LargeNum * 3))
 endif	;Need Large Loop
 if Cycles > 14	;Will a Loop be Required?

	movlw HIGH (((CyclesLeft - 3)/ 5) + 256)
	movwf DlayValue + 1
	movlw LOW (((CyclesLeft - 3)/ 5) + 256)
	movwf DlayValue 
	decf DlayValue, f	;5 Cycle Constant Delay Loop

	btfsc STATUS, Z
	 decfsz DlayValue + 1, f
      goto $ - 3
CyclesLeft = CyclesLeft - (3 + (5 * ((CyclesLeft - 3)/ 5)))
 endif	;Finished with Loop Code
 while CyclesLeft >= 2 ;Put in 2 Instruction Cycle Delays

  goto $ + 1
CyclesLeft = CyclesLeft - 2
 endw
 if CyclesLeft == 1	;Put in the Last Required Cycle
 
  nop
 endif
 endm

; --------------------------------------------------------------------------
 PAGE
 org	0

	movlw 7	; turn off comparators
	movwf CMCON0

	bsf STATUS, RP0	; all ports are digital
	clrf ANSEL ^ 0x080

	clrf TRISC ^ 0x080	; teach all of PORTC to be outputs
	movlw b'0011'	; teach RA0 and RA1 to be inputs
	movwf TRISA
	bcf STATUS, RP0

loop:	; robot moves forward
	
	; move robot forward
	movlw b'1010'
	movwf PORTC
	
	btfss PORTA, 0
	btfss PORTA, 1
	call SubLSwitch

	btfss PORTA, 1
	goto loop
	call SubRSwitch

	goto loop

goto $

		
; --------------------------------------------------------------------------
 PAGE
;  Subroutines

SubLSwitch:
	
	movlw b'0101'	; reverse for 2 seconds
	movwf PORTC
	Dlay 2000000

	movlw b'1001'	; turn right for 2 seconds
	movwf PORTC
	Dlay 2000000
	return

SubRSwitch:

	movlw b'0101'	; reverse for 2 seconds
	movwf PORTC
	Dlay 2000000	

	movlw b'0110'	; turn left for 2 seconds
	movwf PORTC
	Dlay 2000000
 	return

 end

-----------------------------------
wtd
Wed May 24, 2006 10:56 pm


-----------------------------------
Unfortunately I don't think I can offer much help, but is that PowerPC assembly?

-----------------------------------

Tue May 30, 2006 7:28 pm


-----------------------------------
Try to use code taggings please =)
