• 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.

ADC problem in 12F675

Status
Not open for further replies.

swapan

Member
Hi friends,
I am purely new in the world of microcontroller. I am trying to assemble a program on ADC and test the practical effect. I have used the PIC12F675. AN0 has been used as analogue input i.e. Vdd of 5v through 4.7k Pot. Internal oscillator is used. After conversion of an analogue input I like to subtract any value within the 8 bit range from the digital value and see the effect. If the subtraction sets the C flag of STATUS register i.e. input is greater than predetermined value, the AD conversion will continue and in the opposite case a LED connected to GP5 will glow.

After programming the device when used in practical, the LED glows continuously. Turning the Pot forward and backward has no effect on the LED. The code is given. Advice from any of my friends solicited.

Variables
STATUS EQU 03H
GPIO EQU 05H
ADRESH EQU 1EH
TRISIO EQU 85H
ANSEL EQU 9FH
ADVALUE EQU 20H ; Register to load AD value.

Main Program
BSF STATUS,5 ; Select Bank1
MOVLW B’00000001’ ; set AN0 as input and rest output.
MOVWF TRISIO ;
MOVLW B’00010001’ ; set Fosc/8 and select AN0 as analogue bit.
MOVWF ANSEL ;
BCF STATUS,5 ; Back to Bank 0
BEGIN MOVLW B’00000001’ ; Set ADFM- Left justified,
VCFG – Vdd
Analogue channel select bit – AN0
MOVWF ADCON0
CALL DELAY ; Acquisition time
BSF ADCON0,1 ; Start conversion
WAIT BTFSC ADCON0,1 ; See if conversion is complete, if not
GOTO WAIT ; continue
MOVF ADRESH,0 ; Load the result (only MSB) into W
MOVWF ADVALUE ; and finally in ADVALUE register.
MOVLW D’128’ ; Load a value and subtract it from the
SUBWF ADVALUE,0 ; ADVALUE
BTFSC STATUS,0 ; See if less than predetermined value,
GOTO BEGIN ; If not, continue conversion.
BSF GPIO,5 ; if yes, turn on LED




DELAY NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
RETURN

END
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Hi friends,
I am purely new in the world of microcontroller. I am trying to assemble a program on ADC and test the practical effect. I have used the PIC12F675. AN0 has been used as analogue input i.e. Vdd of 5v through 4.7k Pot. Internal oscillator is used. After conversion of an analogue input I like to subtract any value within the 8 bit range from the digital value and see the effect. If the subtraction sets the C flag of STATUS register i.e. input is greater than predetermined value, the AD conversion will continue and in the opposite case a LED connected to GP5 will glow.

After programming the device when used in practical, the LED glows continuously. Turning the Pot forward and backward has no effect on the LED. The code is given. Advice from any of my friends solicited.

Variables
STATUS EQU 03H
GPIO EQU 05H
ADRESH EQU 1EH
TRISIO EQU 85H
ANSEL EQU 9FH
ADVALUE EQU 20H ; Register to load AD value.

Main Program
BSF STATUS,5 ; Select Bank1
MOVLW B’00000001’ ; set AN0 as input and rest output.
MOVWF TRISIO ;
MOVLW B’00010001’ ; set Fosc/8 and select AN0 as analogue bit.
MOVWF ANSEL ;
BCF STATUS,5 ; Back to Bank 0
BEGIN MOVLW B’00000001’ ; Set ADFM- Left justified,
VCFG – Vdd
Analogue channel select bit – AN0
MOVWF ADCON0
CALL DELAY ; Acquisition time
BSF ADCON0,1 ; Start conversion
WAIT BTFSC ADCON0,1 ; See if conversion is complete, if not
GOTO WAIT ; continue
MOVF ADRESH,0 ; Load the result (only MSB) into W
MOVWF ADVALUE ; and finally in ADVALUE register.
MOVLW D’128’ ; Load a value and subtract it from the
SUBWF ADVALUE,0 ; ADVALUE
BTFSC STATUS,0 ; See if less than predetermined value,
GOTO BEGIN ; If not, continue conversion.
BSF GPIO,5 ; if yes, turn on LED




DELAY NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
RETURN

END
Very important - use the supplied INCLUDE files, don't define them yourself, and use the names not the numbers like you have - I've highlighted the erros in red. Also, use the Code tags to post code, so it formats correctly.

Your way makes it difficult to read or spot mistakes, although an obvious one is your program falling through into the Delay routine, which will cause it to crash.
 

Gayan Soyza

Active Member
Try this I made some changes.

Code:
STATUS 		EQU 	03H
GPIO 		EQU 	05H
ADRESH 		EQU 	1EH
TRISIO 		EQU 	85H
ANSEL 		EQU 	9FH
ADVALUE 	EQU 	20H 		; Register to load AD value.
		;
MAIN		BSF 	STATUS,5 	; Select Bank1
		CALL	3FFh		; LOAD OSSCAL VALUE FROM LAST PROGRAM MEM
		MOVWF	OSCCAL		; //
		MOVLW 	B’00000001’ 	; set AN0 as input and rest output.
		MOVWF 	TRISIO 		;		
INIT_AD		MOVLW 	B’00010001’ 	; set Fosc/8 and select AN0 as analogue bit.
		MOVWF 	ANSEL 		;
		BCF 	STATUS,5 	; Back to Bank 0
		MOVLW 	B’00000001’ 	; Set ADFM- Left justified,VCFG – Vdd,Analogue channel select bit – AN0
		MOVWF 	ADCON0
		;
		CLRF	GPIO		; TURN OFF ALL OUTPUTS
LOOP		CALL 	DELAY 		; Acquisition time
		BSF 	ADCON0,1 	; Start conversion
WAIT 		BTFSC 	ADCON0,1 	; See if conversion is complete, if not
		GOTO 	WAIT 		; continue
		MOVF 	ADRESH,0 	; Load the result (only MSB) into W
		MOVWF 	ADVALUE 	; and finally in ADVALUE register.
		MOVLW 	D’128’ 		; Load a value and subtract it from the
		SUBWF 	ADVALUE,0 	; ADVALUE
		BTFSC 	STATUS,0 	; See if less than predetermined value,
		GOTO 	LOOP		; If not, continue conversion.
		BSF 	GPIO,5 		; if yes, turn on LED
		GOTO	LOOP

DELAY 		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		RETURN

		END
 

swapan

Member
Thanks Mr. Goodwin for your observations. Please help how to use code tag to post code. I could not follow your comment on delay routine which will cause it to crash. Please through some light.

SWAPAN
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Thanks Mr. Goodwin for your observations. Please help how to use code tag to post code.
Click on 'Go Advanced' this adds the 'Code' tag icon (#)

I could not follow your comment on delay routine which will cause it to crash. Please through some light.
Your program has no 'end' to it, it runs on directly into the Delay subroutine, which then 'returns' to a non-existent address, which could be anywhere.

Check Gayan Soyza's modification.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top