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.

Some ideals on adc

Status
Not open for further replies.
Nigel
In your PIC ADC tutorial your bit is set ADFM: A/D Result Formed Select bit 1 = Right justified . That is right ? My code is reading for the left justified
I need to make sure this is set right
Code:
     movlw     0x01
     movwf     ADCON0         ; configure A2D for Channel 0
I should use this
Code:
    movlw     B’10000001’     ;Right, Vdd Vref, AN0
    movwf    ADCON0
and change ADRESH,w to ADRESL,W
 
Last edited:
Almost have it I think what it is doing is as reads the volts it lights the led
starting at 1 and going to 5 volts But when I get at 5 volts and go back down
the last led stays on
Code:
list		p=16f684		; list directive to define processor
	#include	<P16F684.inc>		; processor specific variable definitions
	errorlevel -302 ; Turn off banking message
                            ; known tested (good) code

	__CONFIG    _CP_OFF & _CPD_OFF & _BOD_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _FCMEN_OFF & _IESO_OFF
	


	CBLOCK 		0x020
	endc
	#define _1VOLT 0x33
	#define _2VOLT 0x66
	#define _2_5VOLT 0x7F
	#define _3VOLT 0x99
	#define _4VOLT 0xCC
	#define _5VOLT 0xff




	ORG			0x000		; processor reset vector
  	goto		init		; go to beginning of program


	ORG			0x004	    ; interrupt vector location

init:
     bsf       STATUS,RP0     ; select Register Page 1
     movlw     0xFF
     movwf     TRISA          ; Make PortA all input
     clrf      TRISC          ; Make PortC all output
     movlw     0x10           ; A2D Clock Fosc/8
     movwf     ADCON1
     bcf       STATUS,RP0     ; back to Register Page 0

     bcf       STATUS,RP0     ; address Register Page 2
     bsf       STATUS,RP1     
     movlw     0xFF           ; we want all Port A pins Analog
     movwf     ANSEL
     bcf       STATUS,RP0     ; address Register Page 0
     bcf       STATUS,RP1
     clrf	   ADCON0
     movlw     0x01
     movwf     ADCON0         ; configure A2D for Channel 0 
MainLoop:
     call	adcdelay		  ;delay to charge cap
     bsf       ADCON0,GO      ; start conversion
     btfss     ADCON0,GO      ; this bit will change to zero when the conversion is complete
     goto      $-1
	
    movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_1VOLT
    subwf	 ADRESH, w
	BNC 	LessThan1V                ; Branch if less than 1V

	movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_2VOLT
    subwf	 ADRESH, w
	BNC 	LessThan2V                ; Branch if Between 1V and 2V
	
	movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_2_5VOLT
    subwf 	ADRESH, w
	BNC 	LessThan2_5V             ; Branch if Between 2V and 2.5V

	movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_3VOLT
    subwf	 ADRESH, w
	BNC 	LessThan3V                ; Branch if Between 2.5V and  3V

	movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_4VOLT
    subwf 	ADRESH, w
	BNC 	LessThan4V                ; Branch if Between 3V and 4V
	movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_5VOLT
    subwf 	ADRESH, w
	BNC 	LessThan5V                ; Branch if Between 3V and 4V
    goto      MainLoop
LessThan1V:
	bsf		PORTC,0
	return
LessThan2V
	bsf		PORTC,1					; lights led thats in volt range 
	return
LessThan2_5V 
	bsf		PORTC,2 
	return
LessThan3V
    bsf		PORTC,3	  
	return	
LessThan4V
	bsf		PORTC,4
	return
LessThan5V
	clrf	PORTC
	return 

   
adcdelay:
	 nop					  ;1 cycle

	 return					  ;4 cycles (including call)
	
     end
 
Last edited:
If you want them to go off you need to clear the other bits. Also you have returns without calls.

Try changing it to,
Code:
LessThan1V
	movlw	b"00000001"
	movwf	PORTC
	goto	MainLoop
LessThan2V
	movlw	b"0000001[COLOR="Red"]1[/COLOR]"
	movwf	PORTC
	goto	MainLoop

The red one should be zero if you only want one LED on at any one time.

Mike.
 
Did a little changing plus add what you said Mike and this works flawless.
Code:
    list		p=16f684		; list directive to define processor
	#include	<P16F684.inc>		; processor specific variable definitions
	errorlevel -302 ; Turn off banking message
                            ; known tested (good) code

	__CONFIG    _CP_OFF & _CPD_OFF & _BOD_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _FCMEN_OFF & _IESO_OFF
	


	CBLOCK 		0x020
	endc
	#define _1VOLT 0x33
	#define _2VOLT 0x66
	#define _2_5VOLT 0x7F
	#define _3VOLT 0x99
	#define _4VOLT 0xCC
	#define _5VOLT 0xff




	ORG			0x000		; processor reset vector
  	goto		init		; go to beginning of program


	ORG			0x004	    ; interrupt vector location

init:
     bsf       STATUS,RP0     ; select Register Page 1
     movlw     0xFF
     movwf     TRISA          ; Make PortA all input
     clrf      TRISC          ; Make PortC all output
     movlw     0x10           ; A2D Clock Fosc/8
     movwf     ADCON1
     bcf       STATUS,RP0     ; back to Register Page 0

     bcf       STATUS,RP0     ; address Register Page 2
     bsf       STATUS,RP1     
     movlw     0xFF           ; we want all Port A pins Analog
     movwf     ANSEL
     bcf       STATUS,RP0     ; address Register Page 0
     bcf       STATUS,RP1
     clrf	   ADCON0
     movlw     0x01
     movwf     ADCON0         ; configure A2D for Channel 0 
MainLoop:
     call	adcdelay		  ;delay to charge cap
     bsf       ADCON0,GO      ; start conversion
     btfss     ADCON0,GO      ; this bit will change to zero when the conversion is complete
     goto      $-1
	
    movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_1VOLT
    subwf	 ADRESH, w
	BNC 	LessThan1V                ; Branch if less than 1V


    movlw 	_2VOLT
    subwf	 ADRESH, w
	BNC 	LessThan2V                ; Branch if Between 1V and 2V
	

    movlw 	_2_5VOLT
    subwf 	ADRESH, w
	BNC 	LessThan2_5V             ; Branch if Between 2V and 2.5V


    movlw 	_3VOLT
    subwf	 ADRESH, w
	BNC 	LessThan3V                ; Branch if Between 2.5V and  3V


    movlw 	_4VOLT
    subwf 	ADRESH, w
	BNC 	LessThan4V                ; Branch if Between 3V and 4V


    movlw 	_5VOLT
    subwf 	ADRESH, w
	BNC 	LessThan5V                ; Branch if Between 3V and 4V
    goto      MainLoop
LessThan1V:
	movlw	b'00000001'
	movwf	PORTC
	goto	MainLoop

LessThan2V
	movlw	b'00000010'
	movwf	PORTC
	goto	MainLoop

LessThan2_5V 
	movlw	b'00000100'
	movwf	PORTC
	goto	MainLoop

LessThan3V
    movlw	b'00001000'
	movwf	PORTC
	goto	MainLoop

LessThan4V
	movlw	b'00010000'
	movwf	PORTC
	goto	MainLoop

LessThan5V
	movlw	b'00100000'
	movwf	PORTC
	goto	MainLoop

adcdelay:
	 nop					  ;1 cycle

	 return					  ;4 cycles (including call)
	
     end
I was sending it back to the same place it came from lol thanks Mike and Kchriste I have learned a lot here. Wait till you see what I'm making. now it's solder time.
 
Last edited:
QUICK grab your wives n childeren!!!!

And run like hell!!!!
Ol be80be is going to solder something
It's Alive, Its alive can be herd from the basement of the be80be's compound
the lights are flickering, the weather has turned bad, lightening everywhere!!
If you care for your loved ones,
RUN
the be80be laboratory is up and running. just as soon as that soldering iron gets hot!!
and this time he has been playing with ADC!! PIC code
 
I can't say the same

I cut the one battery off my set up just to prevent popping another PIC
In fact the SFHTOP paid me a visit.
I have been put on probation that if I destroy another PIC then off to the big house



Society For Humain Treatment Of Pics
 
Here what I was making the GPIO Play ground I like play with 12f683
so i made this board to go with the one that has the pic and 74hc164 on it. It gives
me 10 switches and 8 leds and two adc toys a pot and a LDR to mess with.
 

Attachments

  • GEDC0474.JPG
    GEDC0474.JPG
    82.5 KB · Views: 164
  • GPIO Play ground.PNG
    GPIO Play ground.PNG
    13 KB · Views: 166
Last edited:
I kind of added it up the data sheet tell you it takes 5 cycles for the cap to settle that's the same as a nop and the return. Oh and the call takes 2 so you have a 5 cycle delay more then good enough.
 
Last edited:
This is the sample code from the data sheet
Code:
      CALL SampleTime     ;Acquisiton delay
      BSF ADCON0,GO       ;Start conversion
      BTFSC ADCON0,GO   ;Is conversion done?
      GOTO $-1                  ;No, test again
      BANKSEL ADRESH      ;
      MOVF ADRESH,W      ;Read upper 2 bits
      MOVWF RESULTHI    ;store in GPR space
      BANKSEL ADRESL     ;
      MOVF ADRESL,W      ;Read lower 8 bits
      MOVWF RESULTLO   ;Store in GPR space
This is the part that your taking about
Code:
 CALL SampleTime     ;Acquisiton delay
I read the adc apnote and the example they gave for the SampleTime delay was
Code:
SampleTime:
           nop   ;1 cycle
           return  ;4 cycles
 
Your code is fine Burt, I shouldn't pay any attention to him - you only need to check if the ADC is busy after initiating a read - unless you've initiated a read, it can't possibly be busy.
 
This is from microchip sample code
Code:
MainLoop:
     nop                      ; wait 5uS for A2D amp to settle and capacitor to charge.
     nop                      ; wait 1uS
     nop                      ; wait 1uS
     nop                      ; wait 1uS
     nop                      ; wait 1uS
     bsf       ADCON0,GO      ; start conversion
     btfss     ADCON0,GO      ; this bit will change to zero when the conversion is complete
what I did is the same thing
A call =2 nop =1 return = 1 that's five for A2D amp to settle and capacitor to charge
 
Bad language post deleted - ElectroMaster.

Next time will be a official warning.
 
Last edited by a moderator:
So, which port of the 16F844 has the analogue input?
Perhaps you should also ask, "Where do I find this mysterious PIC? It doesn't appear on the MicroChip site at all." :D
 
Mrdeb here the schematic for the test board I made
 

Attachments

  • play.PNG
    play.PNG
    13.1 KB · Views: 187
Last edited:
Sorry about that I didn't remove it all it was a 74hc164 but it's not on that board it was on the schematic I thought i remove it all my kids was trying to get me to take them skating and i didn't look it over good I fixed it should make since now.
 
here all the files mrdeb
 

Attachments

  • test.zip
    19.3 KB · Views: 109
  • play.PNG
    play.PNG
    13.1 KB · Views: 140
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top