Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ADC problem for PIC16F877A.. How to average analog inputs for a more accurate output

Status
Not open for further replies.

Thunderhorse312

Member level 1
Joined
May 7, 2012
Messages
32
Helped
0
Reputation
0
Reaction score
1
Trophy points
1,286
Activity points
1,526
Hi! i am writing a program for a digital weighing scale and i want to know how do i average my input voltages from an analog port (i.e. A0) because my source voltage is not a constant input. Thus, my output values would have a large deviation without first averaging the inputs..

here is my code for the program.:
Code:
#include <htc.h>
#include <pic16f877a.h>
#include "delay.c"
#define _XTAL_FREQ 20000000

unsigned int ADCRead(unsigned char channel)
{
	unsigned char l_byte, h_byte;		//Variables in exchange for ADRESL and ADRESH
	unsigned int ADR;					//stores the Analog to Digital input value

	ADCON0 = 0x81;						//sets what channel to be used for analog input
	DelayUs(50);						//acquistion time delay
	GO_DONE = 1;						//start the A/D conversion

	while(GO_DONE == 1); 				//wait for the bit to be cleared

	l_byte = ADRESL;
	h_byte = ADRESH;

	ADR = (h_byte << 8)|l_byte;			//shift ADRESH 8 places to the left and be ORed with ADRESL

	return ADR;

}

	void main(void)
		{
			#define Weight25 8
			#define Weight30 9
			#define Weight31 10
			#define Weight35 11	
			#define Weight36 12
			#define Weight40 13
			#define Weight41 14
			#define Weight46 15
			#define Weight47 16
			#define Weight52 17
			#define Weight53 18
			#define Weight58 19
			#define Weight59 20
			#define Weight64 21
		    #define Weight65 22
			#define Weight70 23
			#define Weight71 24
			#define Weight76 25
			#define Weight77 26
			#define Weight82 27
			#define Weight83 28
			#define Weight88 29
			#define Weight89 30
			#define Weight94 32
			#define Weight95 33
			#define Weight100 34
			unsigned int Ch0;

			PORTD = 0;
			TRISD = 0;
			PORTB = 0;
			TRISB = 0;

			ADCON0 = 0x81;
			ADCON1 = 0xCD;	
			
		while (1)
				{
					Ch0 = ADCRead(0);		//Read from channel 0
					
					if (Ch0 >= Weight25)
					{
						if (Ch0 < Weight30)
							{		
								RB0 = 1; 
							}
							else
								{
								RB0 = 0;
								}
					
					}
					else
					{
								RB0 = 0;
					}		
					
					

					if (Ch0 > Weight31)				//weight 31 - 35
					{
						if (Ch0 < Weight35)
							{		
								RB1 = 1; 
							}
							
							else
								{
								RB1 = 0;
					}
					else
					{
								RB1 = 0;
					}		
					

					if (Ch0 > Weight36)					//weight 36 - 40
					{
						if (Ch0 < Weight40)
							{		
								RB2 = 1; 
							}
							
							else
								{
								RB2 = 0;
								}
					}
					else
					{
								RB2 = 0;
					}	

					
					if (Ch0 > Weight41)					//weight 41 - 46
					{
						if (Ch0 < Weight46)
							{		
								RB3 = 1; 
							}
							
					
					}
					else
					{
								RB3 = 0;
					}			
						

					if (Ch0 > Weight47)					//weight 47 - 52
					{
						if (Ch0 < Weight52)
							{		
								RB4 = 1; 
							}
							
					
					}
					else
					{
								RB4 = 0;
					}			


						if (Ch0 > Weight53)					//weight 53 - 58
					{
						if (Ch0 < Weight58)
							{		
								RB5 = 1; 
							}
							
					
					}
					else
					{
								RB5 = 0;
					}			


						if (Ch0 > Weight59)					//weight 59 - 64
					{
						if (Ch0 < Weight64)
							{		
								RB6 = 1; 
							}
							
					
					}
					else
					{
								RB6 = 0;
					}			


						if (Ch0 > Weight65)					//weight 59 - 64
					{
						if (Ch0 < Weight70)
							{		
								RD0 = 1; 
							}
							
					
					}
					else
					{
								RD0 = 0;
					}			

				

						if (Ch0 > Weight71)					//weight 59 - 64
					{
						if (Ch0 < Weight76)
							{		
								RD1 = 1; 
							}
							
					
					}
					else
					{
								RD1 = 0;
					}			
					















		
}


}


I'd like to get maybe 5 to 10 samples before getting an output.. how or what code can i use to make that sampling possible.. Also, is it possible for the ADC or is there any way that the ADC could read ADC values having decimal points. All i know is that the ADC for PIC 16F877A can read ADC values ranging form 0 to 1023 and no decimal points. Please help! thanks a lot!
 

Use a For loop to scan the input say 16 times, then say sum the values and finally divide by 16 and place the value in the variable,

Something like:

Code:
for (i=0;i<16;i++)               // ADC Sampling Channel 0
       {
           SelChanConvADC(ADC_CH0);
           while(BusyADC());
           Ch0Result += ReadADC();
       }
       Ch0Result /=16;

A snippet of code from a program i am currently working on,

Obviously this requires that you include the ADC.h library,

Hope this helps
 

Ahhh i see.. thanks! i was also leaning towards using the for loop in implementing sampling.. thanks again!
 

no probs

glad to be of help

---------- Post added at 16:22 ---------- Previous post was at 16:22 ----------

no probs

glad to be of help
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top