Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

My First line follower code help??

Status
Not open for further replies.

dons21

New Member
Hi all, I built a basic line follower, It has 4 ir sensors controlled with lm324 op-amp and uses SN754410 motor driver.

I can adjust the sensors with a pot which gives me a HIGH output when the line is detected.

I tested the line follower by connecting two of the sensors (output from op-amp) directly to the motor driver and it worked. The robot followed the line quite well once the sensors had been adjusted.

Now I built this to learn more about pic programming so I connected the sensors and motor driver to 12f629.

I all I wanted was the processor to turn on the motors when a line had been detected so I wrote the following code. but it doesn't work well I am not sure if the motors are causing the pic problems or its my code.

Please could someone have a look, I will post a schematic later

Thanks Don
;******************************************************************************
; *
; Filename: first_line_follower.asm *
; Date: 13-6-10 *
; File Version: 0.1v *
; *
; Author: don *
; Company: *
; *
; *
;******************************************************************************
; *
; Files required: P12F629.INC *
;******************************************************************************
;
; Vdd IIIII Vss
;Left Sensor GP5 I I GP0
;Right Sensor GP4 I I GP1 Left Motor
; GP3 IIIII GP2 Right Motor
;
;******************************************************************************

;------------------------------------------------------------------------------
; PROCESSOR DECLARATION
;------------------------------------------------------------------------------

LIST P=12F629 ; list directive to define processor
#INCLUDE <P12F629.INC> ; processor specific variable definitions

;------------------------------------------------------------------------------
;
; CONFIGURATION WORD SETUP
;
; The 'CONFIG' directive is used to embed the configuration word within the
; .asm file. The lables following the directive are located in the respective
; .inc file. See the data sheet for additional information on configuration
; word settings.
;
;------------------------------------------------------------------------------

__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT

;------------------------------------------------------------------------------
; OSCCAL RESTORE (not required if internal OSC is not used)
;------------------------------------------------------------------------------
RESET_VECTOR CODE 0x0000 ; processor reset vector

errorlevel -302
BSF STATUS,RP0 ; set file register bank to 1
CALL 0x3FF ; retrieve factory calibration value
MOVWF OSCCAL ; update register with factory cal value
BCF STATUS,RP0 ; set file register bank to 0


; set up inputs and outputs


;****** Turn Off Comparator
movlw b'00000111' ; Turn off Comparator 0x07
movwf CMCON ; Turn off Comparator 0x07


BSF STATUS,RP0 ; SELECT BANK 1
MOVLW B'111001' ;GP5,GP4,GP3,GP0 INPUTS GP1,GP2 OUPUTS
MOVWF TRISIO
BCF STATUS,RP0 ;BANK0

clrf GPIO ; Clear GPIO

start
BTFSS GPIO,5 ; Test Left Sensor (skip if high)
BCF GPIO,2 ; Turn off Right Motor
BTFSC GPIO,5 ; Test Left Sensor (skip if low)
BSF GPIO,2 ; Turn on right motor
BTFSS GPIO,4 ; Test right sensor (skip if high)
BCF GPIO,1 ; Turn off left motor
BTFSC GPIO,4 ; Test right sensor (skip if low)
BSF GPIO,1 ; Turn on left motor

GOTO start



END ; directive 'end of program'
 
You are running the program with the PIC full speed, you may need to have a time delay among your code.


EDIT: Sorry, I'm stuck, This is true if you are using Stepper motor.

Check your config. word for MCLRE and PWRTE, I think they have to be OFF.
 
Last edited:
I can't see anything wrong with your code but I would remove the call to 0x3ff and the subsequent store to OSCCAL as sometimes the retlw at that address gets written over and this results in a continuous loop.

BTW, when posting code place
Code:
 before it and
after it so that it keeps it's formating like this,
Code:
		__config _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 

;------------------------------------------------------------------------------  
; OSCCAL RESTORE (not required if internal OSC is not used)  
;------------------------------------------------------------------------------  
RESET_VECTOR	code	0x0000		; processor reset vector

		errorlevel -302
		bsf	STATUS,RP0	; set file register bank to 1 
	;	call	0x3FF		; retrieve factory calibration value
	;	movwf	OSCCAL		; update register with factory cal value 
		bcf	STATUS,RP0	; set file register bank to 0

; set up inputs and outputs  
;****** Turn Off Comparator  
		movlw	b'00000111'	; Turn off Comparator 0x07 
		movwf	CMCON		; Turn off Comparator 0x07


		bsf	STATUS,RP0	; SELECT BANK 1
		movlw	B'111001'	;GP5,GP4,GP3,GP0 INPUTS GP1,GP2 OUPUTS
		movwf	TRISIO
		bcf	STATUS,RP0	;BANK0

		clrf	GPIO		; Clear GPIO 
start			 
		btfss	GPIO,5		; Test Left Sensor (skip if high)
		bcf	GPIO,2		; Turn off Right Motor
		btfsc	GPIO,5		; Test Left Sensor (skip if low)
		bsf	GPIO,2		; Turn on right motor 
		btfss	GPIO,4		; Test right sensor (skip if high)
		bcf	GPIO,1		; Turn off left motor
		btfsc	GPIO,4		; Test right sensor (skip if low)
		bsf	GPIO,1		; Turn on left motor

		goto	start
Mike.
 
Well Thanks for the replies, is there anything wrong with my schematic, like I said before. The line follower works when I connect the op-amp to the motor controller, but when connected to the pic i think the motors cause the pic problems. Anyway here is my schematic.
Schematic
 
Hi, they go nowhere at the moment. I have 4 sensors to detect the line, and at the moment I am only using the two center sensors. Once I have mastered playing/programing the 12f629 I will use a 16F628 chip and connect the extra sensors to the 16F chip.
 
Well, I fixed the problem, I needed to have resistor on the MCLR pin. When the motors were running it was resetting the pic.

Now to my next problem, I have added a push button to start and stop the code but the button is very sporadic. Here is my code;


Code:
;******************************************************************************
;                                                                             *
;    Filename:      first_line_follower.asm                                   *
;    Date:          13-6-10                                                   *
;    File Version:    0.1v                                                    *
;                                                                             *
;    Author:          don                                                     *
;    Company:                                                                 *
;                                                                             *
;                                                                             *
;******************************************************************************
;                                                                             
;    Files required: P12F629.INC                                              
;******************************************************************************
;                
;		        Vdd IIIII Vss
;	Left motor	GP5 I   I GP0 Start Stop
;  Right motor	GP4 I   I GP1 Left  sensor
;    			GP3 IIIII GP2 Right sensor                               
;                                                                             
;******************************************************************************

;------------------------------------------------------------------------------
; PROCESSOR DECLARATION
;------------------------------------------------------------------------------

     LIST      P=12F629              ; list directive to define processor
     #INCLUDE <P12F629.INC>          ; processor specific variable definitions

;------------------------------------------------------------------------------
;
; CONFIGURATION WORD SETUP
;
; The 'CONFIG' directive is used to embed the configuration word within the 
; .asm file. The lables following the directive are located in the respective 
; .inc file.  See the data sheet for additional information on configuration 
; word settings.
;
;------------------------------------------------------------------------------

    __CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 


;------------------------------------------------------------------------------
; OSCCAL RESTORE (not required if internal OSC is not used)
;------------------------------------------------------------------------------

RESET_VECTOR  CODE      0x0000 			; processor reset vector
       
		 errorlevel -302
        BSF     STATUS,RP0    			; set file register bank to 1 
        CALL    0x3FF         			; retrieve factory calibration value
       	MOVWF   OSCCAL        			; update register with factory cal value 
       	BCF     STATUS,RP0   			; set file register bank to 0
        
    
; set up inputs and outputs


;****** Turn Off Comparator
		movlw	b'00000111'		; Turn off Comparator 0x07 
		movwf	CMCON			; Turn off Comparator 0x07


		BSF		STATUS,RP0				; SELECT BANK 1
		MOVLW	B'001111'				        ;GP3,GP2,GP1,GP0 INPUTS GP4,GP5 OUPUTS
		MOVWF	TRISIO
	
		BCF		STATUS,RP0				;BANK0

		clrf	GPIO					                ; Clear GPIO 

		nop
		nop

on		BTFSC	GPIO,0					; Test start/stop (skip if low)		
		GOTO	on						; loop till button press		

loop	
		BTFSS	GPIO,1					; Test Left Sensor (skip if high)
		BCF		GPIO,4					; Turn off Right Motor
		BTFSC	GPIO,1					; Test Left Sensor (skip if low)
		BSF		GPIO,4					; Turn on right motor	
		BTFSS	GPIO,2					; Test right sensor (skip if high)
		BCF		GPIO,5					; Turn off left motor
		BTFSC	GPIO,2					; Test right sensor (skip if low)
		BSF		GPIO,5					; Turn on left motor
		
		
		BTFSS	GPIO,0					; Test start/stop (skip if high)
		GOTO	off
		GOTO 	loop	

off		
		clrf	GPIO
		GOTO	on



        END                     		  ; directive 'end of program'



Any ideas are welcome.

Thanks
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top