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 PIC 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
 
why don't you use the pic's built in comparators. Comparing two voltages and setting an output when one is greater then the other is what they're ment for.

and it won't give any software overhead apart from setting it up.
 
Exo said:
why don't you use the pic's built in comparators. Comparing two voltages and setting an output when one is greater then the other is what they're ment for.

and it won't give any software overhead apart from setting it up.

The 16F877 doesn't have any!, although the new 16F877A does.
 
Nigel Goodwin said:
Exo said:
why don't you use the pic's built in comparators. Comparing two voltages and setting an output when one is greater then the other is what they're ment for.

and it won't give any software overhead apart from setting it up.

The 16F877 doesn't have any!, although the new 16F877A does.

Then order a 16f877A as a sample first :lol:

didn't know that realy, never used a 16f877... without the A
 
Status
Not open for further replies.

Latest threads

Back
Top