data_value(); input_volt = volt_read_disply(0);
DELAY_ms(100);
volt =(int)input_volt; // double to int casting
LCD_GoToXY(0,7);
LCD_DisplayNumber(10,volt,3);
output_volt = volt_read_disply(1);
DELAY_ms(100);
LCD_GoToXY(1,7);
LCD_DisplayNumber(10,output_volt,3);
double volt_read_disply(int c){ int i,adc_value[40]={0}; int temp =0;
adc_init();
for(i=0; i<40;i++) // samples taking from 41 times 9.4milli second taken about on complte half cycle
{ DELAY_ms(5);
adc_value[i] = read_adc(c); // reading voltage
}
temp = adc_value[0];
for(i=0; i<40; i++)
{
if(temp<adc_value[i])
temp=adc_value[i];
}
return (double)(temp-307)*0.660; // int to double casting
Code:for(i=0; i<40;i++) { DELAY_ms(5); adc_value[i] = read_adc(c); // reading voltage }
Don't seem clear that you're wasting at least 200ms for every A/D conversion doing nothing?
What is the usefulness of the loop, considering that only the last value will be computed?
Hi,
I didn't go through your code.
But you do a time critical control loop. I recommend to avoid float calculations.
Use 16 bit integers (signed or unsigned ...depending what you need) ..I assume for a voltage stabilizer even 8 bit calculations/resolution could work.
Additionally I recommend to learn how to use interrupt driven control loops.
Don't waste 99.9% of your processing power for busy waits like "delay_ms()".
(An AVR has enough processing power to continously process 1600 samples/s on two channels, squaring, averaging, square root on two channels, with an output data rate of 2.5ms in real time - without missing a single conversion - while it is still able to update the display and communicate via UART)
Klaus
That delay doing nothing , but without that delay the volatge showing like 229,135 ,220,110 etc shows low value some time.
May i need to take the avarage value ?
I assume you talk about an AC voltage stabilizer.May i need to take the avarage value ?
I am making an automatic stabilzer by using atmega8 . I am taking AC input and output directly (with very low gain) and connected to ADC and the part of mesuaring and relay section works correctely [not finall]
The original description seemed as an AC stabilzer.
Hi,
As far as I can see there is no averaging in the analog side.
This means all the averaging needs to be done by software.
I assume the voltage at the ADC input is about 1.5V DC when there is no signal input.
With AC at the input you will see the: divided_AC_voltage + this 1.5V DC at the ADC.
--> Use a scope (and a DVM) to verify this. Give us your results.
--> What's your AC frequency range? A rough estimation will do. Is it grid mains?
--> How often per second do you need a calculated value? (Or calculation interval / regulation interval time)
--> what resolution of your calculated values do you need?
--> what precision of your calculated values do you need? (Or: how much noise and ripple can you accept)
********
There are some methods to determine the amplitude*
1) doing a lot of random samples, find the +/- peak values, multiply them by a constant factor. --> simple math but not the output values will vary a lot.
2) doing a lot of random samples. Subtract offset, rectifying, averaging. --> simple math. Much better than the "peak" solution, but still there will be some ripple in the output signal
3) doing a calculated count of perfectly timed samples. Subtract offset, rectifying, averaging. --> simple math. Much better than the "peak" solution. Much better ripple suppression.
4) doing a calculated count of perfectly timed samples. Subtract offset, squaring, averaging, square root (true RMS) --> needs carefully selected variable sizes, square root takes some processing power. Exact RMS value with low ripple.
I don't recommend 1) because of the huge uncertainty
I don't recommend 4) for beginners. (This is how I'd do it, but I'm a perfectionist)
--> you have to choose a your way.
Klaus
Hi,
I didn't go through your code.
But you do a time critical control loop. I recommend to avoid float calculations.
Use 16 bit integers (signed or unsigned ...depending what you need) ..I assume for a voltage stabilizer even 8 bit calculations/resolution could work.
Klaus
int volt_read_disply(int c)
{ int i,adc_value[160]={0}; int temp =0;
adc_init();
for(i=0; i<160;i++) // samples taking from 41 times 9.4milli second taken about on complte half cycle
{
adc_value[i] = read_adc(c); // reading voltage
}
temp = adc_value[0];
for(i=0; i<160; i++)
{
if(temp<adc_value[i])
temp=adc_value[i];
}
Here is i planned to take the 40 ADC value of every 10milli second (ie, the half cycles of the 50 Hz signals) . In that 10 milli second we gets 40 samples And in which the highest value i taken
You may also say "attenuated" --> The real AC input voltage is higher than the AC voltage at the ADC2)what doees mean "divided voltage "? is it the value /2 +1.5dc ?
Then use a piece of paper an a pencil to draw the (expected) ADC input voltage. With magnitude and timing. Then insert lines for every ADConcersion.3)unfortunately i dont have a scope ?
Even mains frequency has a frequency range. If you want to do a reliable measurement then you should be aware of this.4)The Mesuaring AC Frequency is 50 HZ ,230 volt . yes it is grid voltage .
So you´ve chosen the most unreliable solution.Here is i planned to take the 40 ADC value of every 10milli second
Don´t know where 307 and 0.660 come from. Maybe its correct, maybe not.[(that value) - 307] * 0.660.
.. like expectable...changed the float to 16 bit integer uint16_t adc_value1=0; But some time the grabage value comes and hangaup .
I doubt it reduces the fluctuations. It most likey just takes the (most erronenous) value from 160 instead of 40 values....I changed the samples from 40 to 160 approximatly 2 full wave samples and its peak value .It reduce the flcutuations ( see post #4 ).
if(temp temp=adc_value;
This makes me assume you have a lot of noise an uncertainty..like expected.* 0.660 is not correct it is 0.707 ,Vrms
Hi,
--> as already recommended: use interrupts or any hardware driven ADC timing. Don't use software timing.
Klaus
Then use a piece of paper an a pencil to draw the (expected) ADC input voltage. With magnitude and timing. Then insert lines for every ADConcersion.
Post this drawing.
You may also lend a scope.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?