Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to program ADC using PIC16F877A for more than one channel for inputs

Status
Not open for further replies.

bogusmeister

Junior Member level 2
Joined
Nov 3, 2010
Messages
21
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,283
Activity points
1,452
Hi guys!. i've been using this ADC of pic16f877a with the code that i got. it uses channel0 sample and 1 analog input for the conversion.
Code:
	LIST p=16f877a

; Include file, change directory if needed
	INCLUDE "p16f877A.inc"
	__CONFIG _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

	ERRORLEVEL -302
	
; Start at the reset vector
	ORG 0x000
	NOP
Start 
	BANKSEL PORTD
	CLRF PORTD 				;Clear PORTD
	MOVLW B'01000001' 		;Fosc/8, A/D enabled, Sample Channel 0
	MOVWF ADCON0
	BANKSEL OPTION_REG
	MOVLW B'10000111' 		;TMR0 prescaler, 1:256
	MOVWF OPTION_REG
	CLRF TRISD 				;PORTD all outputs
	MOVLW B'00001110'		;Left justify, 1 analog channel
	MOVWF ADCON1			;VDD and VSS references
	BANKSEL PORTD

Main 
	BCF INTCON,T0IF
Loop 
	BTFSS INTCON,T0IF 	;Wait for Timer0 to timeout
	GOTO Loop
	BSF ADCON0,GO 			;Start A/D conversion for channel 0
Wait 
	BTFSS PIR1,ADIF 	;Wait for conversion to complete
	GOTO Wait
	MOVF ADRESH,W 			;Write A/D result to PORTD
	MOVWF PORTD 			;LEDs
	BCF PIR1,ADIF 			;Clear the completion flag
	GOTO Loop 				;Do it again
	
	END

now my problem is when i want to use 2channels for 2 analog inputs i cant get it working. the LED's just blinking.. what i did is that i repeat this code and change the value of adcon0 for channel1 and adcon1 for 2 analog inputs.

could you please help me how to program the ADC if im gonna use more than one channel for analog inputs? thank you very much
 

Re: ADC using PIC16F877A

I am pretty new to programming and I may be way off base on this one. I have a simular problem, but mine gets stuck in a calculation. The way I figured it out is I commented out one line of code at a time and reloaded it. When everything else in the program worked except that one line I knew where the prob was. My problem ended up being that the calculation was never being completed before an interrupt occured and then the values got changed. If nothing else its a good trouble shooting tip. Hope this helps
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top