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.

A/D help

Status
Not open for further replies.

homemade24

New Member
I'm trying to covert some code I did in Pic Basic to assembly
and before I get to far off base I'm asking for help
which the part I'm working now is a
if it's day do this
if it's night do something different
the #550 is just what centered the calibration pot on my Basic code
I think I have everything right until you get to the ? marks (I think!!!)
then I'm lost thinks
and yes I'm using a external crystal
thanks
Code:
;*******************************************************************************;
;   16f676  A/D TEST
;  !!! USING 4MHz  EXTERNAL CLOCK !!!!!!
; HARDWARE  10K PHOTO CELL /10 K POT  VOLTAGE DIVIDER POWER FROM PORTA,1 ADIN  PORTA,0
; IS IT DAY OR IS IT NIGHT
;********************************************************************************;
         #include "p16F676.inc"  
       errorlevel  -302  
  __CONFIG  _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _MCLRE_OFF & _CPD_OFF
          cblock    0x20
; **********************************************************************************
       NIGHT                        ;HOLDS A/D RESULTS
       d1                           ;USED BY DELAY
       d2
       d3
      endc
       org 0
  nop
;**********************************************************************************;

Start:
       bcf	STATUS,RP0	            ; register page 0
		bcf	STATUS,RP1	            ; register page 0
		movlw	0X07		        ;SET UP W TO TURN COMPARATOR OFF 
		movwf	CMCON               ;IS NO BANK 0
        
        MOVLW   B'10000100'         ; SELECT  RIGHT JUSTIFIED,VDD,CHANNEL 00(AN0)
        MOVWF   ADCON0              ; IS IN BANK 0
  
        bsf STATUS,RP0              ; GOTO BANK 1
       
        movlw	b'00001101'	        ;CONFIG I/O PORTA
		movwf	TRISA               ; IS IN BANK 1
       
        movlw	b'00000000'	        ;CONFIG I/O PORTC
		movwf	TRISC               ; IS IN BANK 1

        MOVLW   B'00000001'         ; SELECT AN0
        MOVWF   ANSEL               ;IS IN BANK 1

        MOVLW   B'00000101'         ; SELECT 16 TOSC
        MOVWF   ADCON1              ; IS IN BANK 1

        bcf	STATUS,RP0              ; BACK TO BANK 0

        BSF   PORTA,1               ;TURN ON POWER TO PHOTO CELL
      
        movlw	0x03                ; Delay = 0.5 seconds         ;DELAY FOR PHOTO CELL POWER AND STABLIZE
	movwf	d1
	movlw	0x18
	movwf	d2
	movlw	0x02
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0

			;6 cycles
	goto	$+1
	goto	$+1
	goto	$+1

TEST:
        clrf	        NIGHT		        ; clear the NIGHT VAR
        BSF ADCON0, GO               ; GO GET ME A SAMPLE 
Wait_Loop                           ; LOOP UNTIL IT GET IT AND IS DONE
        BTFSC ADCON0, GO            ; WAIT FOR A/D  CONVERSION TO FINISH 
        GOTO Wait_Loop              ; ADC will clear bit ADCON0,GO when done
        MOVF ADRESH, W              ; GET SAMPLED DATA 
        movwf  NIGHT                ; PUT DATA IN  NIGHT VAR ?????????

; ?????????????????????? SCALE 0-1024  ?????????????????????????
         IF NIGHT =<550  THEN  GO DO SOMETHING  ;IT'S NIGHT  
         IF NIGHT  >550  THEN  DO SOMETHING DIFFERENT ;IT'S DAY 
;?????????????????????????????????????????????????????????????????
     GOTO TEST

     END
 
It's never going to get out of the delay. It's would be nice to see how you hooked it up
Code:
;*******************************************************************************;
;   16f676  A/D TEST
;  !!! USING 4MHz  EXTERNAL CLOCK !!!!!!
; HARDWARE  10K PHOTO CELL /10 K POT  VOLTAGE DIVIDER POWER FROM PORTA,1 ADIN  PORTA,0
; IS IT DAY OR IS IT NIGHT
;********************************************************************************;
         #include "p16F676.inc"  
       errorlevel  -302  
  __CONFIG  _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _MCLRE_OFF & _CPD_OFF
          cblock    0x20
; **********************************************************************************
       NIGHT                        ;HOLDS A/D RESULTS
       d1                           ;USED BY DELAY
       d2
       d3
      endc
       org 0
  nop
;**********************************************************************************;

Start:
        bcf	     STATUS,RP0	            ; register page 0
	bcf	     STATUS,RP1	            ; register page 0
	movlw	0X07		        ;SET UP W TO TURN COMPARATOR OFF 
        movwf	CMCON               ;IS NO BANK 0
        
        MOVLW    0x10         ; SELECT  RIGHT JUSTIFIED,VDD,CHANNEL 00(AN0)
        MOVWF   ADCON0              ; IS IN BANK 0
  
        bsf         STATUS,RP0              ; GOTO BANK 1
       
        movlw	b'00001101'	        ;CONFIG I/O PORTA
	movwf	TRISA               ; IS IN BANK 1
       
        movlw	b'00000000'	        ;CONFIG I/O PORTC
	movwf	TRISC               ; IS IN BANK 1

        MOVLW   B'00000001'         ; SELECT AN0
        MOVWF   ANSEL               ;IS IN BANK 1

        MOVLW    0x01         ; SELECT 16 TOSC
        MOVWF   ADCON1              ; IS IN BANK 1

        bcf	STATUS,RP0              ; BACK TO BANK 0

        BSF   PORTA,1               ;TURN ON POWER TO PHOTO CELL
	call  Delay
TEST:
        clrf	        NIGHT		        ; clear the NIGHT VAR
        BSF ADCON0, GO               ; GO GET ME A SAMPLE 
Wait_Loop                           ; LOOP UNTIL IT GET IT AND IS DONE
        BTFSC ADCON0, GO            ; WAIT FOR A/D  CONVERSION TO FINISH 
        GOTO Wait_Loop              ; ADC will clear bit ADCON0,GO when done
        MOVF ADRESH, W              ; GET SAMPLED DATA 
        movwf  NIGHT                ; PUT DATA IN  NIGHT VAR ?????????
        goto    TEST
Delay      
   	        movlw	0x03                ; Delay = 0.5 seconds         ;DELAY FOR PHOTO CELL POWER AND STABLIZE
		movwf	d1
		movlw	0x18
		movwf	d2
		movlw	0x02
		movwf	d3
Delay_0
		decfsz	d1, f
		goto	$+2
		decfsz	d2, f
		goto	$+2
		decfsz	d3, f
		goto	Delay_0

			;6 cycles
		goto	$+1
		goto	$+1
		goto	$+1
		goto	TEST


; ?????????????????????? SCALE 0-1024  ?????????????????????????
;         IF NIGHT =<550  THEN  GO DO SOMETHING  ;IT'S NIGHT  
;        IF NIGHT  >550  THEN  DO SOMETHING DIFFERENT ;IT'S DAY 
;?????????????????????????????????????????????????????????????????
     GOTO TEST

     END

see it this works for you
 
Last edited:
Sorry be80be I blew it on that (cut and paste error)
but this was not work code just using to help with my ?
and
; ?????????????????????? SCALE 0-1024 ?????????????????????????
; IF NIGHT =<550 THEN GO DO SOMETHING ;IT'S NIGHT
; IF NIGHT >550 THEN DO SOMETHING DIFFERENT ;IT'S DAY
;?????????????????????????????????????????????????????????????????
what I need it to do
I never can ask the ?'s right sorry
 
The ADC isn't working because you haven't turned it on and you have selected A1 to convert rather than A0. ADCON0 needs to be 10000001 and ADCON1 needs to be 01010000.

Are you stuck on how to do the comparison with 550?

Mike.
 
The original code got through the delay without any problem. The only thing wrong with it was the values in the ADC registers.

To do the comparison with 550 you can do,
Code:
TEST:
		clrf	NIGHT		; clear the NIGHT VAR
		bsf	ADCON0,GO	; GO GET ME A SAMPLE 
Wait_Loop				; LOOP UNTIL IT GET IT AND IS DONE 
		btfsc	ADCON0,GO	; WAIT FOR A/D  CONVERSION TO FINISH 
		goto	Wait_Loop	; ADC will clear bit ADCON0,GO when done
		movf	ADRESH,W	; GET SAMPLED DATA 
		movwf	NIGHT		; PUT DATA IN  NIGHT VAR ?????????

		sublw	.550/4
		btfsc	STATUS,C
		goto	LessThan550
;is more than 550

		goto	TEST

LessThan550

		goto	TEST

<edit> To get this working you will need the ADC to be left justified by clearing bit 7 of ADCON0.</edit>
Mike.
 
Last edited:
thanks to you both
Pommie I think your understanding what I 'm trying to do
nothing else has to be done to the data that's put in to night
sublw .550/4 will do all that's need?

I think I know have the code closer to being right with your changes

Code:
;*******************************************************************************;
;   16f676  A/D TEST
;  !!! USING 4MHz  EXTERNAL CLOCK !!!!!!
; HARDWARE  10K PHOTO CELL /10 K POT  VOLTAGE DIVIDER POWER FROM PORTA,1 ADIN  PORTA,0
; IS IT DAY OR IS IT NIGHT
;********************************************************************************;
         #include "p16F676.inc"  
       errorlevel  -302  
  __CONFIG  _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _MCLRE_OFF & _CPD_OFF
          cblock    0x20
; **********************************************************************************
       NIGHT                        ;HOLDS A/D RESULTS
       d1                           ;USED BY DELAY
       d2
       d3
      endc
       org 0
  nop
;**********************************************************************************;

Start:
       bcf	STATUS,RP0	            ; register page 0
		bcf	STATUS,RP1	            ; register page 0
		movlw	0X07		        ;SET UP W TO TURN COMPARATOR OFF 
		movwf	CMCON               ;IS NO BANK 0
        
        MOVLW   B'10000001'         ; SELECT  RIGHT JUSTIFIED,VDD,CHANNEL 00(AN0)
        MOVWF   ADCON0              ; IS IN BANK 0
  
        bsf STATUS,RP0              ; GOTO BANK 1
       
        movlw	b'00001101'	        ;CONFIG I/O PORTA
		movwf	TRISA               ; IS IN BANK 1
       
        movlw	b'00000000'	        ;CONFIG I/O PORTC
		movwf	TRISC               ; IS IN BANK 1

        MOVLW   B'00000001'         ; SELECT AN0
        MOVWF   ANSEL               ;IS IN BANK 1

        MOVLW   B'01010000'         ; SELECT 16 TOSC
        MOVWF   ADCON1              ; IS IN BANK 1

        bcf	STATUS,RP0              ; BACK TO BANK 0

        BSF   PORTA,1               ;TURN ON POWER TO PHOTO CELL
      
        movlw	0x03                ; Delay = 0.5 seconds 
	movwf	d1
	movlw	0x18
	movwf	d2
	movlw	0x02
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0

			;6 cycles
	goto	$+1
	goto	$+1
	goto	$+1


        clrf	        NIGHT		        ; clear the NIGHT VAR
        BSF ADCON0, GO               ; GO GET ME A SAMPLE 
Wait_Loop                           ; LOOP UNTIL IT GET IT AND IS DONE
        BTFSC ADCON0, GO            ; WAIT FOR A/D  CONVERSION TO FINISH 
        GOTO Wait_Loop              ; ADC will clear bit ADCON0,GO when done
        MOVF ADRESH, W              ; GET SAMPLED DATA 
        movwf  NIGHT                ; PUT DATA IN  NIGHT VAR ?????????

        sublw	.550/4
	btfsc	STATUS,C
	goto	nighttime             ;because it's Less Than 550
        goto    daytime               ;because it's more than 550
  ; the code will carry on from which sub it goes to
		
     END
 
You just need to left justify the ADC result (by clearing bit 7 of ADCON0) so ADRESH contains 8 bits.

Mike.
 
I'm very sorrow please bare with me but I think I missing what your saying
I did this
The ADC isn't working because you haven't turned it on and you have selected A1 to convert rather than A0. ADCON0 needs to be 10000001 and ADCON1 needs to be 01010000.


You just need to left justify the ADC result (by clearing bit 7 of ADCON0) so ADRESH contains 8 bits.

would that not change what I just did?
 
this Code I wrote in Pic Basic Pro is what I'm trying to dupicate
if helps

Code:
Pic Basic Pro code        
'****************************************************************
   pic16f676
'XT_OSC_, WDT_ON, MCLR_OFF,
'CPD_OFF, BOD_OFF, PWRT_ON,PROTECT_OFF

DEFINE ADC_BITS 10          'ADC 10 BIT #
DEFINE ADC_CLOCK 3          'ADC SAMPLE CLOCK 
DEFINE ADC_SAMPLEUS 50	    'SAMPLE EVERY 50 US.				
Pause 5000                 'DELAY TO LET STABILIZE AFTER POWER UP & MODE 
CMCON = 7                   'COMPARATOR OFF
VRCON = 0                   'VOLTAGE REF. DISABLE	
TRISA = %00001101           'MAKE A.2 & A.3 INPUTS
TRISC = %00000011		    'MAKE .C.0,C.1 PORTC INPUTS
ANSEL = %00000001           'PORTA.0 A/D IN,RST DIGITAL
ADCON0.7 = 1                'RIGHT JUSTIFY FOR 10 BIT 
OPTION_REG=%01000000        'RAISING EDGE/WEAK PULL UP  
WPUA = %00000000            'NO WEAK PULLUPS  

SYMBOL ADC  =PORTA.0         'ADCIN
SYMBOL LDR  =PORTA.1         'PHOTO CELL POWER


NIGHT VAR WORD:NIGHT=0        'LDR/ADCIN VAR FOR LIGHT SENSOR

LDR=1:Pause 250           'TURN LDR/ADCIN POWER ON



NIGHT=0
LED=0
ADCIN 0,NIGHT          'CHECK AN# READ AN0 & STORE IN NIGHT VAR
 
 IF NIGHT=<550  Then goto nighttime
 goto daytime
 
would that not change what I just did?

Yes it would. Let me try and explain why the change is needed. There are two ways to read the ADC, right justify and read all 10 bits or left justify and read just 8. When left justified the value in ADRESH varies from 0 to 255 and so is ¼ of the 10 bit value. Your basic code is using the full 10 bit result whereas the code I suggested uses 8 bits and divides the 550 by 4 to obtain the same outcome. Hope that makes sense.

Sorry for any confusion.

Mike.
 
thanks you for explaining that
just to make sure. I need to change this
MOVLW B'10000001'
MOVWF ADCON0
to this
MOVLW B'00000001'
MOVWF ADCON0
and fingers crossed I'm good to go.
hope to try it tomorrow and let you know
thanks for stay with me on this.
 
Yes, you got it.

FYI, if you wanted to do a full 10 bit comparison in asm you would do,
Code:
Wait_Loop				; LOOP UNTIL IT GET IT AND IS DONE 
		btfsc	ADCON0,GO	; WAIT FOR A/D  CONVERSION TO FINISH 
		goto	Wait_Loop	; ADC will clear bit ADCON0,GO when done
		movf	ADRESH,W	; GET SAMPLED DATA 
		movwf	NightHi		; PUT DATA IN  NIGHT VAR ?????????
		bsf	STATUS,RP0	;bank 1
		movfw	ADRESL		;get low result
		bcf	STATUS,RP0	;bank 0
		movwf	NightLo		;store it
		movlw	High(.550)	;get high byte
		subwf	NightHi,W	;subtract ADCH from it
		bnz	GotResult	;if not equal we have a result
		movlw	low(.550)	;if equal compare the low byte
		subwf	NightLo,W
GotResult	btfss	STATUS,C
		goto	LessThan550
;is greater than or equal 550

		goto	TEST

LessThan550

		goto	TEST

Mike.
 
I could not stand it I had to go try the 8 bit AND IT WORK I'l will play with the 10 bit later
and again you did a great job of explaining.
it took my 6 hr. (digging in the data sheets and look for snips of code on the internet) to write my example you had the problem solved in about 30 sec.
I don't think you get to worry about me taking you job LOL
thanks so much I'm almost finished with the 2 year project.
 
After 25 years of programming I'm pretty fast now but I still get bugs.:eek:

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top