Program description

What does it do?

This program runs the CHRP robot as a line follower.

CHRP Robot program

This program allows the CHRP robot to follow a black line using a simple digital control scheme. By evaluating the light input as either light or dark, the program can make decisions about which motor to turn on or off. While successful in following the line, this type of control system results in deliberately jerky movements as the robot corrects its course.

What you should know before starting

Make sure that the floor LED (LED10) and the phototransistors (Q1 and Q2) have been tested and are working.

Create the program

The robot program is shown below. Start a new project in MPLAB, copy all of the code into the project, and build the program.


;CHRP20bot2.ASM v2.1	Last modified on January 25, 2011
;===============================================================================
;Description:	Runs the CHRP 2.0 as a digital line following robot.

;Start of MPLAB and processor configuration.

	include	"p16f886.inc"		;Include processor definitions 

	__config _CONFIG1, _DEBUG_OFF & _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTOSCIO
	__config _CONFIG2, _WRT_OFF & _BOR40V

;End of MPLAB and processor configuration.

;Start of hardware equates

LED10		equ	7		;Port A bit position of LED10
S2		equ	0		;Port B pushbutton bit position

;End of hardware equates

	org	00h			;Start of program memory

		clrf	PORTA		;Turn off all port outputs
		clrf	PORTB
		clrf	PORTC
		goto	initPorts	;Jump to initialize subroutine

	org	05h

initPorts	;Set Ports B and C to support CHRP digital circuitry.

		banksel	ANSEL		;Switch register banks
		movlw	01010111b	;Enable Port B pull-ups, TMR0 internal
		movwf	OPTION_REG	;clock, and 256 prescaler
		clrf	ANSEL		;Set all PORTA pins to digital I/O
		clrf	ANSELH		;Set all PORTB pins to digital I/O and
		banksel	TRISA		;Switch register banks
		movlw	01101111b	;Setup LED and beeper outputs, and
		movwf	TRISA		;make all other PORTA pins inputs
		movlw	00000001b	;Make S2 input, and make all other PORTB
		movwf	TRISB		;pins outputs
		movlw	10110000b	;Setup serial input and output pins,
		movwf	TRISC		;and set motor outputs
		banksel	PORTB		;Return to PORTB register bank
		bsf	PORTA,LED10	;Turn floor led on

wait		btfsc	PORTB,S2	;Wait until S2 is pressed
		goto	wait

main		movf	PORTA,W		;Read Port A sensor inputs and
		andlw	00000011b	;keep only floor sensor values
		call	direction	;Determine direction value and
		movwf	PORTC		;move motors
		goto	main		;Do it again...

direction	addwf	PCL,F		;Jump table based on floor sensor data
		retlw	00001001b	;Reverse
		retlw	00000010b	;Right
		retlw	00000100b	;Left
		retlw	00000110b	;Forward

	end