Discussion in "PIC Microcontroller Discussion" started by    Luqman    Jul 30, 2007.
Mon Jul 30 2007, 12:12 pm
#1
hi, would any one send me an example PIC C code to take averages of many ADC values, to get a stable one so that a comparator with hysteresis effect may be obtain.
Mon Jul 30 2007, 04:20 pm
#2
its fairly easy... create an array and store the value.. like this...

unsigned long int adc_val;

unsigned int adc_avg; // stores adc average value

main() { //this is just to explain
unsigned char i;
adc_val =0;
for(i=0;i<50;i++){
adc_val+ = adc(); // adc() is function that returns
// converted adc value
}
adc_avg = adc_val/50;
}


This will give you average of 50 value, and i am sure its stable you can take average of averages.. that will give you even more stable value.. but that is needed when you have high fluctuations.. etc
If anymore doubt please ask..


[ Edited Mon Jul 30 2007, 04:22 pm ]
 Luqman like this.
Fri Aug 03 2007, 04:02 pm
#3
/in the following programe
//Vs taking different adc values
//when Vs<=Va and when FG1=1 then LED at pin c4 should be on at Vs=Va+Va_1/2
//and for the same Vs<=Va but FG1=0 LED at pin c4 should be off at Vs=Va-Va_1/2
//i.e. want to get flashing of LED with some delay at a slightly different voltages
//so that trying to achieve a compratr with hystarisis
//how it could be achieve plz suggest a practical logic to cachieve this task

#include "snsr1.h"

#define cutoff 255 //5V
#define Va_1 10 //0.2V
#define Va 51 //1V
#define Vb 102 //2v
#define Vc 179 //3.5v


void main()
{
int Vs;
int1 FG1,FG2,FG3,FG4;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

// TODO: USER CODE!!

while(1)
{
set_adc_channel( 0 );
Vs = read_adc();
if(Vs<=Va)
{
FG1=1;
Vs = Va+Va_1/2;
output_bit(PIN_C4,1);
output_bit(PIN_C5,0);
output_bit(PIN_C6,0);
delay_ms(1000);
FG1=0;
Vs = Va-Va_1/2;
output_bit(PIN_C4,0);
output_bit(PIN_C5,0);
output_bit(PIN_C5,0);
}

else if(Va<Vs<=Vb)
{
FG2=1;
output_bit(PIN_C4,1);
output_bit(PIN_C5,0);
output_bit(PIN_C6,0);
}

else if(Va<Vs<=Vc)
{
FG3=1;
output_bit(PIN_C4,0);
output_bit(PIN_C5,1);
output_bit(PIN_C6,0);
}

else
{
FG4=1;
output_bit(PIN_C4,0);
output_bit(PIN_C5,0);
output_bit(PIN_C6,1);
}
}
}


[ Edited Fri Aug 03 2007, 04:34 pm ]
Fri Aug 03 2007, 04:43 pm
#4
As i gave you an example already.. i think you did not read it carefully.. anyways you need to make just a little change in your program.
Please replace
Vs = read_adc();
in your program with the code below

adc_val =0;
for(i=0;i<50;i++){
     adc_val+ = read_adc();
}
Vs = adc_val/50;



and define adc_val as unsigned long int and define i as unsigned char.. and see what you get..
 Luqman like this.
Fri Aug 03 2007, 07:01 pm
#5

i really appreciate ur concern
there is Flow Chart named FC1.doc file and modified code with ur suggestions, i m trying to achieve the following conditions

1) When Vs<=Va Green LED flashes(on and off) with certain delay
and RLY1 should also be on and off with the above
delay. It was achieved in comparator based circuit
with adding a flasher cct. (a comparator with
hysteresis).This hysteresis effect has to implement in
software.
2) WhenVa<Vs<=Vb
Green LED on constantly
and RLY1 should off
3) When Vb<Vs<=Vc
yellow LED on
and RLY2 should on
4) When Vc<Vs
Red LED on
and RLY3 should on

plz suggest me the practical schematic(16F877 cct.) to meet for these conditions and sugget me the corresponding modifications in the code.
Fri Aug 03 2007, 08:23 pm
#6
well whatever you are telling you can do by yourself.. i ain't giving you any code for that..
but for point 1. can you first tell me what is the result you're getting with the software change i told you?
Sat Aug 04 2007, 10:17 am
#7
actually i have some doubts in my cct. (schematic) as well, that how can i connect an LED and Relay at the same port pin a/c to my desired condition and what voltage should i give at Vref- and Vref+. would u like to suggest me the schematic(cct.) plz.
Sat Aug 04 2007, 05:31 pm
#8
connecting led is simple..

5V |----/\/\/\/\----|>|---Port Pin

and for connecting relays.. i have added a document in the circuit section of the download check it. It has full explanation of the different circuits that can be used for the relays..
In your case i think the Fig D of the document is the best way to connect the relay.
 Luqman like this.
Wed Aug 08 2007, 10:05 am
#9
u say that i just finish things like
while(1) {----}
what does it mean
Wed Aug 08 2007, 02:12 pm
#10
that was a part of my signature.. doesn't matter did u see the circuit? i hope you wont have any problem now

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Astorne
Tue Apr 16 2024, 08:52 pm
Williamjaf
Tue Apr 16 2024, 12:25 pm
best_yyPa
Tue Apr 16 2024, 09:42 am
ErnestoExpop
Tue Apr 16 2024, 02:57 am
Jamesclepe
Mon Apr 15 2024, 11:10 am
Aliciaelora
Mon Apr 15 2024, 07:59 am
btaletvpcu
Mon Apr 15 2024, 04:36 am
UbvpwcTib
Mon Apr 15 2024, 03:13 am