Thunderhorse312
Member level 1
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.:
}
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!
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!