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 ADC on 16f690 demo board

Status
Not open for further replies.

c36041254

Member
Hi there,
I'm trying to make ADC on the PICKit2 low pin count demo board, it has a pot on AN0 and 4 LEDs on PORTC here is the code I'm trying it just lights an LED on pin 5 of PORTC and keeps it alight. Don't know what's the problem :(
HTML:
List p=16f690
	#include<p16f690.inc>
	__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	ERRORLEVEL - 302
;**********************************************************************
	bsf STATUS,RP0
	bsf TRISA,TRISA0
	clrf TRISB
	clrf TRISC
	bcf STATUS,RP0
;**********************************************************************
	main:
	nop
	nop
	nop
	nop
	nop
	nop			; Delay for capacitor to discharge
	banksel ADCON0
	btfss ADCON0,0x01
	goto Loop1
	banksel ANSEL
	bsf ANSEL,ANS0
	banksel ADCON0
	movlw b'00000001'  ; ADFM = 0 (left justified so just read ADRESH and no ADRESL), VCFG = 1 = VDD, CHS = AN0, GO/DONE = 0,ADON = 1
	movwf ADCON0
	bsf ADCON0,0x01		; start ADC
	banksel ADCON1
	movlw b'001'
	movwf ADCON1
	banksel PIR1
	btfsc PIR1,ADIF
	goto Loop1
	goto main
	Loop1:
	banksel ADRESH
	movf ADRESH,w
	movwf PORTC
	banksel PIR1
	bcf PIR1, ADIF
	retlw 00h
	end
 
Your code is rather strange. You appear to be trying to do an ADC conversion before you set up the ADC module. You're also doing a return without doing a call. I posted code in this thread that demonstrated how to read the ADC.

Mike.
 
O.K. agree, that was really a mess, sorry:eek: for that. How about this I think it has at lest some sought of "STANDARD FORM " , but still not working now pin 4 and 5 of port c goes high and remains there.::confused:

HTML:
	List p=16f690
	#include<p16f690.inc>
	__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	ERRORLEVEL - 302
;**********************************************************************
	cblock 0x20
		temp
		resulthi
		resultlo
		d1
		d2
	endc
;**************************SETUP THE CONSTANTS***************************
	bsf STATUS,RP0
	bsf TRISA,TRISA0
	clrf TRISB
	clrf TRISC
	bcf STATUS,RP0
	banksel ANSEL
	bsf ANSEL,ANS0
;**********************************************************************
	main:
	configure:
	banksel ADCON1	; select clock 4Mhz/8
	movlw b'001'
	movwf ADCON1
	banksel ADCON0
	movwf b'0000001' ; see step 2 of ADC procedure in 16f690 datasheet in section 9
	Wait:			; no need to change bank as ADCON0 is also in BANK 0 as d1 and d2 blocks
	decfsz d1
	goto Wait
	decfsz d2
	goto Wait
	bsf ADCON0,GO
	btfsc ADCON0,GO
	goto $-1
	banksel ADRESL
	movf ADRESL,w	; read ADC result, lower 8 bits
	movwf resultlo
	banksel PORTC
	movf resultlo,w
	movwf PORTC
	goto Wait
	end
 
Last edited:
Please Help Me

CAN ANYONE SOLVE THAT ? PLEASE I really need some help with that what's wrong with the code ?:(:(:confused::confused::confused::confused: (SEE ABOVE POST)
 
I've had a quick look at your code and I notice that you are selecting the bank for ADCON0 and loading W with the value to go into it but you aren't writing to ADCON0. I'm guessing the movfw was a typo.
Code:
		banksel	ADCON0
		mov[COLOR="Red"]lw[/COLOR]	b'0000001'	;change this
		[COLOR="Red"]movwf	ADCON0	;<<-----  add this[/COLOR]
Wait					; no need to change bank as ADCON0 is also in BANK 0 as d1 and d2 blocks

HTH

Mike.
 
Sorry I forget to Edit the post I already have added that part and still doesn't work, Have any one not tried this Low Count Demo Board stuff ? !!
 
I've had another quick look at your code and I see that you are writing to resultlo in bank 1 and reading from it in bank 0. Just get rid of the "movf resultlo,w" as W already contains ADRESL.

Edit, if it still doesn't work then post your latest code and put
Code:
 before it and
after it. You seem to be using html tags above.

Mike.
 
Last edited:
I just noticed as well that you have setup ADCON1 wrong.

Code:
		banksel	ADCON1		; select clock 4Mhz/8
		movlw	b'001[COLOR="Red"]0000[/COLOR]'      ;<-- add these zeros
		movwf	ADCON1

Mike.
 
Still doesn't work :(, Here is the code.

Code:
	List p=16f690
	#include<p16f690.inc>
	__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	ERRORLEVEL - 302
;**********************************************************************
	cblock 0x20
		temp
		resulthi
		resultlo
		d1
		d2
		d3
	endc
;**************************SETUP THE CONSTANTS***************************
	bsf STATUS,RP0
	bsf TRISA,TRISA0
	clrf TRISB
	clrf TRISC
	bcf STATUS,RP0
	banksel ANSEL
	bsf ANSEL,ANS0
;**********************************************************************
	main:
	configure:
	banksel ADCON1	; select clock 4Mhz/8
	movlw b'00100000'
	movwf ADCON1
	banksel ADCON0
	movlw b'00000001' ; see step 2 of ADC procedure in 16f690 datasheet in section 9
	movwf ADCON0
	Wait:			; no need to change bank as ADCON0 is also in BANK 0 as d1 and d2 blocks
	decfsz d1
	goto Wait
	decfsz d2
	goto Wait
	bsf ADCON0,GO
	btfss ADCON0,GO
	goto $-1
	banksel ADRESL	
	movwf resultlo			; read ADC result, lower 8 bits
	banksel PORTC
	movf resultlo,w
	movwf PORTC
	goto Wait
	end
BTW in ADCON1 only bit 6-4 are implemented so shouldn't it be


Code:
movlw b'00010000'
	 movwf ADCON1
than
Code:
movlw b'00100000'
	 movwf ADCON1
I tried that too but didn't work. Thanks for paying attention, I thought no one has ever tried something like this before and even did, then my problem is so basic and silly that they don't want to answer it, anyway hope you'll come up with some solution, meanwhile I try to search for it. Thanks !
 
I only put 4 zeros after the 1 not 5.

You have now changed various bits, you have changed the test on the GO bit and you don't read ADRESL.

Try this,
Code:
		list	p=16f690
		#include<p16f690.inc> 
		__config _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
		errorlevel - 302
;**********************************************************************  
		cblock	0x20
temp
resulthi
resultlo
d1
d2
d3
		endc
;**************************SETUP THE CONSTANTS***************************  
		bsf	STATUS,RP0
		bsf	TRISA,TRISA0
		clrf	TRISB
		clrf	TRISC
		bcf	STATUS,RP0
		banksel	ANSEL
		bsf	ANSEL,ANS0
;**********************************************************************  
main
configure
		banksel	ADCON1		; select clock 4Mhz/8
		movlw	b'00010000'	;fosc/8 and right justify
		movwf	ADCON1
		banksel	ADCON0
		movlw	b'10000001'	;ADC on, channel 0, right justify
		movwf	ADCON0
Wait					; no need to change bank as ADCON0 is also in BANK 0 as d1 and d2 blocks 
		decfsz	d1
		goto	Wait
		decfsz	d2
		goto	Wait
		bsf	ADCON0,GO
		btfsc	ADCON0,GO
		goto	$-1
		banksel	ADRESL
		movfw	ADRESL		; read ADC result, lower 8 bits
		banksel	PORTC
		movwf	PORTC
		goto	Wait
		end

Mike.
 
Hey it works thanks!!! btw I think this
Code:
      bsf	ADCON0,GO
		btfsc	ADCON0,GO
		goto	$-1
means set bit on GO (will start ADC ) and then check if GO is cleared, if so then SET it again otherwise move ahead.
But, why we need to check right after just SETTING the bit, we just set it in very previous instruction :confused: Why it is not
Code:
       bsf     ADCON0,GO
                 btfss  ADCON0,GO
                 goto    $-1
Check it if SET, if not, then SET it. In fact I don't get why we need to check the bit at all right after SETTING it?
Anyway, very sorry for that ADC disabled mistake:eek::eek:, I'm working on this since last two days and have done nothing else just this so I was very frustrated but that was my fault. Thanks :)
 
Good to hear it's working.

The reason we test the GO bit is because the chip clears this bit when it has finished the conversion. The goto $-1 is really bad programming and confuses people.

A better way to write it is,
Code:
		bsf	ADCON0,GO
WaitUntilDone	btfsc	ADCON0,GO
		goto	WaitUntilDone

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top