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.

Need Help with tracking program

Status
Not open for further replies.

blitzalpha

New Member
I'm having difficulty writing a program for a tracking unit that I am building for a school project. I'm writing this in PIC with the 16F877 chip. I want it to take 2 analog inputs (pin ra0 and ra1), convert them to digital, and compare the voltages with each other. If the voltage at pin ra0 is greater than the voltage at pin ra1, I want rb1 to display a high. If the voltage at pin ra1 is greater than the voltage at pin ra0, rb0 should display a high. The following is my program. Any help would be greatly appreciated. Thanks in advance.

; PIC program for the Automatic Tracking Unit

list p=16F877
#include <P16F877.inc>

D_LEFTL equ 20h
D_LEFTH equ 21h
D_RIGHTL equ 22h
D_RIGHTH equ 23h


org 00h
GOTO MAIN
org 10h

MAIN:
BANKSEL TRISA
movlw 07
movwf TRISA
clrf TRISC

BANKSEL ADCON0
;Init_ADC
; Set ADCON0
movlw b'11000001' ;ra0 = left
movwf ADCON0
; Set ADCON1
BANKSEL ADCON1
movlw b'10000101'
movwf ADCON1
BANKSEL ADCON0



;Read_ADC
bsf ADCON0, GO_DONE ;initiate conversion
btfsc ADCON0, GO_DONE
goto $-1 ;wait for ADC to finish

movf ADRESH,W
andlw 0x03
movwf D_LEFTH
BANKSEL ADRESL
movf ADRESL,W
BANKSEL ADRESH
movwf D_LEFTL

BANKSEL ADCON0
;Init_ADC
; Set ADCON0
movlw b'11001001' ;ra1 = right
movwf ADCON0
; Set ADCON1
BANKSEL ADCON1
movlw b'10000101'
movwf ADCON1

BANKSEL ADCON0



;Read_ADC
bsf ADCON0, GO_DONE ;initiate conversion
btfsc ADCON0, GO_DONE
goto $-1 ;wait for ADC to finish

movf ADRESH,W
andlw 0x03
movwf D_RIGHTH
BANKSEL ADRESL
movf ADRESL,W
BANKSEL ADRESH
movwf D_RIGHTL


; RSSI COMPARISON

MOVF D_LEFTH, W
SUBWF D_RIGHTH, W ; W = D_RIGHTH - D_LEFTH
BTFSC STATUS, Z
GOTO EQUAL ; if D_RIGHTH = D_LEFTH
BTFSS STATUS, C
GOTO RUNLEFT ; if D_RIGHTH > D_LEFTH
GOTO RUNRIGHT ; if D_LEFTH > D_RIGHTH

EQUAL: MOVF D_LEFTL, W
SUBWF D_RIGHTL, W ; W = D_RIGHTL - D_LETL
BTFSS STATUS, C


GOTO RUNLEFT ; if D_RIGHTL > D_LEFTL

GOTO RUNRIGHT ; if D_LEFTL > D_RIGHTL


RUNLEFT:
BANKSEL TRISB
clrf TRISB
MOVLW b'00000001'
MOVWF PORTB



RUNRIGHT:
BANKSEL TRISB
clrf TRISB
MOVLW b'00000010'
MOVWF PORTB

GOTO MAIN

end
 
Well I dont do assemble so cant help you in that department but I can give some insite into what Ive had to do using A2D and pics for tracking.

First off I dont know what your using to get a voltage signal but I can assume its gonna have some noise in it. Currently on a bot Im using I have 2 sharp proximity detectors, even with nothing infront of them they tend to swing wildly from 1 -77 from ambient light(even though they are supose to be modulated) on a 10 bit A2D. My first attempt at object detection didnt work as expected because I didnt take this into consideration. My bot would swing wildly from side to side at times hehe.

The best method I've found sofar is to set a threshold difference value. Basically I wouldnt kick in the stearing unless the reading on one sensor was atleast 100 higher then the other, this worked to an extent but will fail if it would run straight into an object. Both sensors would scale accordinlg with neither passing the threshold. To fix that I did a simple OR check to see if either value exceeded 700(aprox 8 inches infront of my bot), if it did exceed 700 I would then just pypass the threshold check and stear towards the lowest voltage till >700 was achieved.
 
There are PICs with comparator inputs, but they're becoming obsolete. The functionality is usually better with the ADC. More versatility. Best thing is to either use two ADC channels and compare the values. If the values change so fast that the delay between sampling one channel and the other confuses the relationship, you could buffer with a fully differential opamp and send the difference to the ADC.

If your PIC doesn't have a comparator, you could always use the op amp with high enough gain that it becomes a comparator.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top