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.

Averaging the adc reading

Status
Not open for further replies.

bee

Member
My project is working well with the adc readings from 12f675
However it can be better
What iam looking for is some help on either just averaging the reading out or a rolling average not sure how to code this in assembly language
I have messed around adding four values together then rotate the bits right but it didnt work something wrong there
Any help or links to tutorials,ect

Thanks
bee
 
My project is working well with the adc readings from 12f675
However it can be better
What iam looking for is some help on either just averaging the reading out or a rolling average not sure how to code this in assembly language
I have messed around adding four values together then rotate the bits right but it didnt work something wrong there
Any help or links to tutorials,ect

Thanks
bee

hi bee,
Are you using 8bit or 10bit values from the adc.?

For maths routines look at the PICList web page.
 
Last edited:
I would use an 8 byte buffer to hold 4 readings, the FSR register to point to the buffer and write 1 value each reading in a circular manner. I would then shift the answer right twice to divide by 4.

Code:
		cblock	h'20'		;start of general purpose registers
Buffer:8
Average:2
		endc

		movlw	Buffer		;get the address of Buffer
		movwf	FSR		;and point FSR at Buffer
Main		call	GetADC		;read the ADC
		bsf	STATUS,RP0	;bank 1
		movwf	ADRESL		;get the low byte
		bcf	STATUS,RP0	;back to bank 0
		movwf	INDF		;store in buffer
		incf	FSR,F		;and point to next location
		movwf	ADRESH		;get high byte
		movwf	INDF		;and store it
		incf	FSR,F		;point to next location
		movfw	FSR		;see if FSR has reached
		xorlw	Buffer+8	;the end of the buffer
		movlw	Buffer		;prepare to reset FSR
		btfsc	STATUS,Z	;if it's at the end
		movwf	FSR		;reset it

		movfw	Buffer		;move first value to average
		movwf	Average
		movfw	Buffer+1	;and high byte
		movwf	Average+1

		movfw	Buffer+2	;add second value
		addwf	Average,f	;first low byte
		btfsc	STATUS,C	;if there was an overflow
		incf	Average+1,f	;increment high byte
		movfw	Buffer+3	;and add in high byte
		addwf	Average+1,f

		movfw	Buffer+4	;add third value
		addwf	Average,f
		btfsc	STATUS,C
		incf	Average+1,f
		movfw	Buffer+5
		addwf	Average+1,f

		movfw	Buffer+6	;add fourth value
		addwf	Average,f
		btfsc	STATUS,C
		incf	Average+1,f
		movfw	Buffer+7
		addwf	Average+1,f

		bcf	STATUS,C	;divide by 2
		rrf	Average+1,f	;by shifting high byte
		rrf	Average,f	;and then low byte
		bcf	STATUS,C	;divide by 4
		rrf	Average+1,f
		rrf	Average,f

		goto	Main		;loop for ever

Mike.
 
Thanks Eric an Pommie

I will get onto this tonight after work cannot wait hehe
I am using 8 bit values from the adc
I will play around with that code snippet mike thanks

cheers
bee
 
I did this before ,,And it worked in mplab debug, but not in circuit
not sure what i am doing here???? :(

Any help appreciated

bee

Code:
ADC 		
		        clrf		ADRESH
		 	bsf	        ADCON0,1	;start adc
                       movlw	        5			;20 us delay for adc
                       addlw		0XFF
       		        btfss		STATUS,Z
                       goto		$-2
	         	btfsc		ADCON0,GO_DONE	;skip when clear
      		        goto		$-1
		 	movf		ADRESH,w	    ;move value into W
			addwf	        num       ; add adc value to num 4x
			bcf		STATUS,C
			decfsz	        add,f      ; value in add is 4 so 4 loops
			goto		ADC
			rrf		num,f              ; divide x 4
			rrf		num,f
			movf		num,w
	        	return
 
You appear to be starting the conversion before the acquisition time.

Mike.
 
I have never noticed that before good one
So do you reckon this should work with the simple averaging calculations at the end
I am stuck at work will have to wait untill tonight to try it out again
I am in engineering(injection moulding) so if there is anything i can do for you let me know
I am in Melbourne

Thanks
bee
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top