Luqman
Jul 29 2007, 11:42 PM
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.
Ajay
Jul 30 2007, 3:50 AM
its fairly easy... create an array and store the value.. like this...
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..
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;
}
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.. etcIf anymore doubt please ask..
Luqman
Aug 3 2007, 3:32 AM
/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);
}
}
}
//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);
}
}
}
Ajay
Aug 3 2007, 4:13 AM
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
and define adc_val as unsigned long int and define i as unsigned char.. and see what you get..
Please replace
Vs = read_adc();
in your program with the code below
CODE:
adc_val =0;
for(i=0;i<50;i++){
adc_val+ = read_adc();
}
Vs = adc_val/50;
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
Aug 3 2007, 6:31 AM
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.
Ajay
Aug 3 2007, 7:53 AM
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?
but for point 1. can you first tell me what is the result you're getting with the software change i told you?
Luqman
Aug 3 2007, 9:47 PM
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.
Ajay
Aug 4 2007, 5:01 AM
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.
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
Aug 7 2007, 9:35 PM
u say that i just finish things like
while(1) {----}
what does it mean
while(1) {----}
what does it mean
Ajay
Aug 8 2007, 1:42 AM
that was a part of my signature..
doesn't matter
did u see the circuit? i hope you wont have any problem now 
doesn't matter
did u see the circuit? i hope you wont have any problem now 
Luqman
Aug 8 2007, 3:52 AM
Y does 9306 Serial EEPROM is interfaced with controllers? Is it good to interfaced it with PIC controllers?
Ajay
Aug 8 2007, 4:26 AM
well selecting an EEPROM for your project is usually depends on your requirement, and also second point is the compatibility. Its not mandatory that you should use only one type of EEPROM. e.g. your project need a maximum data of 2Kb to store so, there is no point for you to use an EEPROM of 8Kb or something.. i hope you understand my point. So according to need of your project you can select EEPROM.
Luqman
Aug 20 2007, 5:12 AM
hi, ajay
There is a comparator module in PIC 16F877 how can i configure it or program it to achieve system hysteresis i.e. the stability(control) in on/off a device?
There is a comparator module in PIC 16F877 how can i configure it or program it to achieve system hysteresis i.e. the stability(control) in on/off a device?
Ajay
Aug 20 2007, 5:29 AM
you cannot achieve a perfect hysteresis in comparator. because minor fluctuations are always there. But i really don't know any other method to achieve hysteresis except taking averages. I have seen many projects (like digital thermometer etc.) where i have taken averages and its working pretty file. You can also define a particular limit upto which the o/p holds its stability.
e.g. define rage for a constant value.
say you want to have a value 5 for which switching is done.
so define a range. like.. from 3-7 or 4-6 as per your need and amount of fluctuation. This will help you control the switching and minor fluctuations will be neglected.
Also what you can have a high resolution comparator. That also will solve your purpose, but it will be a separate hardware.
e.g. define rage for a constant value.
say you want to have a value 5 for which switching is done.
so define a range. like.. from 3-7 or 4-6 as per your need and amount of fluctuation. This will help you control the switching and minor fluctuations will be neglected.
Also what you can have a high resolution comparator. That also will solve your purpose, but it will be a separate hardware.
Luqman
Aug 21 2007, 12:07 AM
would u brief me an example C code to define a specific range and amount of fluctuation.How can i get C code for digital thermometer from download section at the forum
Ajay
Aug 21 2007, 2:37 AM
I have not posted the thermometer project yet.. but i will do it. soon...
instead of just using one if statement
like
if(dat<5)
switch something
else
switch something
A range can be specified something like this..
if(dat>6)
switch something
else if(dat<4)
switch something
so this way you have specified the switching range.. instead of just switching at 5 u are actually providing +/-1 limit to the switching.
I hope you have got it..
instead of just using one if statement
like
if(dat<5)
switch something
else
switch something
A range can be specified something like this..
if(dat>6)
switch something
else if(dat<4)
switch something
so this way you have specified the switching range.. instead of just switching at 5 u are actually providing +/-1 limit to the switching.
I hope you have got it..
Luqman
Aug 22 2007, 11:21 AM
i've downloaded ur code at download section for 89C4051 as RTC as it is a software based RTC instead of interfacing external RTC IC's ,but i didn't understand it completely so i want to know that from where did u conceived the idea is there any resources(like tutorial) available on the net which help me to understand the software based RTC.
Ajay
Aug 22 2007, 11:23 AM
well.. the only idea i had in mind was...
60 seconds = 1 min
60 min = 1hr
24 hr = 1 day
30/31/28 days = 1 month
12 months = 1 year
So i had this idea
using a for loop!! isn't that simple?
60 seconds = 1 min
60 min = 1hr
24 hr = 1 day
30/31/28 days = 1 month
12 months = 1 year
So i had this idea
using a for loop!! isn't that simple?Luqman
Aug 23 2007, 2:31 AM
i read 89C4051 as RTC and only understand few functions with the help of commenst written in the code the remaining functions i did not understand would u give details of the code in a tutorial manner as u have done it in the LCD tutorial.
Ajay
Aug 23 2007, 2:45 AM
its a biiiiggg.. code.. i cannot comment all of it... will look rubbish.. also it need time.. you can paste the part of code which u didn't understand, i will comment it here..
Luqman
Aug 23 2007, 2:59 AM
how does the following cunction give 1 second delay?
void sec1()//can be fine tuned by changing the for loop values
{
bit_8 i,j;
TMOD=0x11;
for(i=0;i<230;i++)
_nop_();
for(i=0;i<7;i++)
{
TH1=0;
TH0=1;
TL1=0;
TL0=0;
TR1=1;
for(j=0;j<119;j++)
_nop_();
while(TF1!=1)
{
for(j=0;j<255;j++);
_nop_();
}
TF1=0;
TR0=1;
TR1=0;
while(TF0!=1)
{
for(j=0;j<120;j++)
_nop_();
}
TF0=0;
TR0=0;
}
}
void sec1()//can be fine tuned by changing the for loop values
{
bit_8 i,j;
TMOD=0x11;
for(i=0;i<230;i++)
_nop_();
for(i=0;i<7;i++)
{
TH1=0;
TH0=1;
TL1=0;
TL0=0;
TR1=1;
for(j=0;j<119;j++)
_nop_();
while(TF1!=1)
{
for(j=0;j<255;j++);
_nop_();
}
TF1=0;
TR0=1;
TR1=0;
while(TF0!=1)
{
for(j=0;j<120;j++)
_nop_();
}
TF0=0;
TR0=0;
}
}
Ajay
Aug 23 2007, 3:10 AM
well i was noob at that time..
when i wrote that function.. try writing a better 1 sec delay yourself
take care of the call and jumps etc.
try making 1 sec routine with the help of simple for loops.. i think thats much better..
when i wrote that function.. try writing a better 1 sec delay yourself
take care of the call and jumps etc.try making 1 sec routine with the help of simple for loops.. i think thats much better..
Luqman
Sep 5 2007, 11:18 PM
well ajay, i would like to request u plz start PIC projects and coding PIC controllers so that we can get skills to program it easily.
Ajay
Sep 6 2007, 1:00 AM
well Luqman, i really want to start by i don't have PIC controller with me.. and i think there is not much difference in controllers..
PIC AVR 8051 everything is same.. there is no difference.. whatever you make in 8051/AVR you can do the same thing in PIC to.. the only difference is features and some registers thats all..
When i get a PIC IC with me... i will start another step by step tutorial for it...
Thank you for your input...
PIC AVR 8051 everything is same.. there is no difference.. whatever you make in 8051/AVR you can do the same thing in PIC to.. the only difference is features and some registers thats all..
When i get a PIC IC with me... i will start another step by step tutorial for it...
Thank you for your input...Luqman
Sep 24 2007, 7:53 PM
hi, ajay
plz go thru the schematic and code that i've sent earlier.
plz go thru the schematic and code that i've sent earlier.
Ajay
Sep 24 2007, 10:20 PM
I really don't remember which code/schematic.. must be lost somewhere in 1000 mails
Please post the code and schematic here in forum..
Please post the code and schematic here in forum..
buddy
Apr 24 2008, 10:19 AM
