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.

PIC 16F877 read 2 analog signal

Status
Not open for further replies.

chilly85

New Member
Could anyone help me to find out what wrong with my code
My program will check value from portB
If portB =11000 PIC will read analog input from RA0 then send digital value to PORTC
If PortB not equal 11000 PIC will read analog input from RA1 then send digital value to PORTC
Here is my source code
LIST P=16F877,W=-302
INCLUDE P16F877.INC
__CONFIG 0x3D32 ; XTAL 20 MHZ. HS GAIN

include "P16F877.inc"
ADCon0 EQU H'001F'
ADCon1 EQU H'009F'
status EQU H'0003'
z EQU H'0002'
PORTB EQU 0x06
PORTD EQU 0x08
TRISB EQU 0x86
TRISD EQU 0x88

; Start at the reset vector
org 0x000
goto Start
org 0x004
Interrupt
retfie
Start
bsf status,RP0 ;bank 1
bcf status,RP1
BANKSEL ADCon1
movlw B'00000000'
movwf TRISC ;portc [7-0] outputs
movlw B'11111111'
movwf TRISB
movlw B'00000000'
movwf TRISD
clrf ADCon1 ;left justified, all inputs a/d
bcf status,RP0 ;bank 0


Main
movlw B'00011000'
xorwf PORTB,w
btfsc status,z
movlw B'01000001'
movlw B'01001001'
movwf ADCon0
call ad_portc

goto Main


ad_portc

bsf ADCon0,GO ;Start A/D conversion
Wait
btfsc ADCon0,GO ;Wait for conversion to complete
goto Wait

movf ADRESH,W ;write A/D result to PORTC
movwf PORTC ;
return




end


When I run this program it will show the output from RA1 only whatever I set for PORTB
 
Last edited:
Hi Chilly,

Welcome to the forum.

Your problem is that btfsc only skips one instruction and so,
Code:
Main
		movlw	B'00011000' 
		xorwf	PORTB,w 
		btfsc	status,z
		movlw	B'01000001'     ;<<<< may get skipped
		movlw	B'01001001'    ;always executed
		movwf	ADCon0
ADCON0 always gets loaded with 01001001 and so A1 is always read.

You can fix it by doing,
Code:
Main
		movlw	B'00011000' 
		xorwf	PORTB,w 
		btfsc	status,z
		movlw	B'01000001' 
		btfss	status,z          ;<<<---- added
		movlw	B'01001001'
		movwf	ADCon0

BTW, when you post code if you type
Code:
 before it and
after it then it will keep it's formating.

Mike.
 
Hi Mike,
I've tried my program with your fixing code. It's worked.
From this I got more understanding about assembly language
Thanks for your support
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top