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
TRISIO EQU 85H
ANSEL EQU 9FH

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
CALL DELAY ; Acquisition time
WAIT BTFSC ADCON0,1 ; See if conversion is complete, if not
GOTO WAIT ; continue
MOVLW D’128’ ; Load a value and subtract it from the
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
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
TRISIO EQU 85H
ANSEL EQU 9FH

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
CALL DELAY ; Acquisition time
WAIT BTFSC ADCON0,1 ; See if conversion is complete, if not
GOTO WAIT ; continue
MOVLW D’128’ ; Load a value and subtract it from the
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
TRISIO 		EQU 	85H
ANSEL 		EQU 	9FH
;
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
;
CLRF	GPIO		; TURN OFF ALL OUTPUTS
LOOP		CALL 	DELAY 		; Acquisition time
WAIT 		BTFSC 	ADCON0,1 	; See if conversion is complete, if not
GOTO 	WAIT 		; continue
MOVLW 	D’128’ 		; Load a value and subtract it from the
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

#### Nigel Goodwin

##### Super Moderator

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.