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.

Help to develop ADC code in mikroC

Status
Not open for further replies.

swapan

Full Member level 4
Joined
Feb 20, 2009
Messages
199
Helped
27
Reputation
54
Reaction score
24
Trophy points
1,298
Location
Kolkata
Activity points
2,806
Hi guys,

While trying to make a stabiliser using PIC (circuit diagram collected from this forum) I could not got any way how to take ADC result from a half-wave rectified and pulsating DC source. The circuit diagram is as follows. Mean while, I have thought a way like this. I take sample at certain interval. compare it with previously taken one. If present sample is higher than previous, then save the present, otherwise previous will prevail. Thus taking sample for a considerable period, say more than 10ms, there will be one peak value of the pulsati


Code:
void Get_value () {
unsigned int adv, cnt;
ADS = ADC_Read(0);

for (cnt = 0; cnt < 200; cnt++)
   {
Delay_us(100);
adv = ADC_Read(0);
if (adv > ADS)
ADS = adv;
else
ADS = ADS;
   }
                   }

View attachment steblizer adc.pdf
 

Hi,

What value do you really want to know? RMS? average? peak?
What is the exact input waveform?

else
ADS = ADS;
what is this for?
 

Actually I want to compare input voltage. If input is higher than a predefined value, turn on a relay. It is not a matter if the value is RMS, average or peak. The stepped down AC is rectified in a half wave mode. Thus the exact waveform is pulsating DC having only positive half cycles of AC. Since in the circuit there is no zero crossing detector, would it be possible to get average or RMS value? Hence I have opined that peak value could be got which will correspond to a RMS or average value. I may be wrong.

Since only peak value is to be detected, I have taken 200 samples of input and taken only the highest value. If present value is higher than previous value, then previous value will be replaced by present one, otherwise previous value will be as it is. For this I have attempted as

else
ADS =ADS ;

swapan
 

Hi,

i know many people work with peak values. I personally avoid this.

This is because of noise and other faulty voltage levels may cause a 100% error.
I´d rather use average. With averaging 200 values (as you mentioned) a single spike my cause only 0.5% of it´s error.

For sure you can go the hard way and do the averaging in software,
but on the analog side it simply is done by changing a capacitor´s value.

I like to calculate the values instead of try and error.
If you want the ripple to be less than 1% combined with a good reaction time i´d use a second order low pass filter.

First stage is combined as a voltage divider and filter.
For example a 230V, 50Hz input signal should give a 2.5V DC signal to the ADC.

with half wave rectifier you get a DC that is 0.3183 of the AC peak. that is 230V * 1.414 * 0.3183 = 103.5V (diode error not included)
Voltage divider: 103.5/2.5 = 41.41. this is Vin/Vout

If dissipated power should be less than 0.25W you need the RMS voltage of the half wave rctified sine. = 230V/1.414 = 162.6V.
Since P = U * U / R --> R = ( U * U) / P = (162.6 x 162.6) / 0.25 = 105800 Ohms.

The bottom resistor: = 105800 / (41.41 -1) = 2618.8 Ohms
The upper resistor = 105800 - 2618.8 = 103.181 Ohms
--> use 100k and 2490 Ohms.

Filter frequency:
input ripple is 230V * 1.414 / 41.41 = 7.85V.
desired ripple is 1% of 2.5V = 0.025V
attenuation = 0.025/7.85 = 1/314. for a first order filter
for a second order filter this is the squareroot of it: sqrt(1/314) = 1/17.7
for a good estimation the capacitor´s xc has to be 1/17.7 of the bottom resistor (i know about 2.5% error); xc = 140 ohms
xc = 1/(2 * pi * f *c) --> c = 1/2 * pi * f * xc) = 1 / (2 * pi * 50 Hz * 140) = 22.6 uF. Use 22 uF.

second stage= impedance at least 5 x higher than the first stage: R = 2490 x 5 = 12450 Ohms. use 12.4kOhms
xc * 5 --> C/5 = 22.6 uF / 5 = 4.5uF. use 4.7uF.

finished.. puhh
So try to use
* diode 1N4007
* 100k Ohms (similar to R8 in your schematic)
* 2490 Ohms and
* 22uF in parrallel (RV1)
* 12k4 (R7)
* 4u7 (C1)

see what happens.
(use higher value capacitors for better ripple attenuation, or lower value capacitors for faster reaction)

good luck

Klaus
 

Hi,
Many thanks for your detailed explanation. Actually I am an electronics hobbyist. I have some practical experience in electronics. By profession, I am an employee working in administration. But it gives me immense pleasure to play with electronics. Your detailed post will certainly help me a lot.

I have seen a practical circuit of Automatic Voltage Stabiliser. By reverse engineering I tried to make a code on my own intending to do whatever the Stabiliser does with its original code. The hardware has no filtration in attenuated DC source as shown in earlier schematic. I personally, like to use filtration with suitable capacitor trading off between smoothness and response speed. It is nothing but my sheer curiosity - how this aspect is managed with set up as in the given schematic.

swapan
 

Hi,

the given schematic has a low pass filtering of 600Hz ... 1600Hz depending on pot setup.


For a complete software solution:
I´d use 3000 samples/s to read the input. (exactely 3000!)

with an average over 60 values for 50Hz (or 50 values for 60Hz) you get a very good and fast information of your mains voltage.
About 20 times faster than the analog filtered solution above.

for normal use go to max. 80% of ADC range, to get enough headroom.
even with 10% input range you get very good voltage information because of averaging of continously changing input signal.

Klaus
 

klaustst sir can you post a code sample for it
 

Hi,

the given schematic has a low pass filtering of 600Hz ... 1600Hz depending on pot setup.


For a complete software solution:
I´d use 3000 samples/s to read the input. (exactely 3000!)

with an average over 60 values for 50Hz (or 50 values for 60Hz) you get a very good and fast information of your mains voltage.
About 20 times faster than the analog filtered solution above.

for normal use go to max. 80% of ADC range, to get enough headroom.
even with 10% input range you get very good voltage information because of averaging of continously changing input signal.

Klaus

So far as averaging is concerned, in my sense, taking ADC samples should be started from a certain point and upto a certain point. In the given hardware setup there is no provision of zero crossing detection. So how will you manage this in software? Would you offer a piece of code in this regard?

swapan
 

Hi,

as averaging is concerned
HS or SW averaging?

from a certain point and upto a certain point
do this in an interger multiple of mains period time.

In the given hardware setup there is no provision of zero crossing detection. So how will you manage this in software?
compare the last sample with the new sample. on the first time "new > old" that it it the time of zerocross. (Works only without anlog averaging. )

Would you offer a piece of code in this regard?
klaustst sir can you post a code sample for it
Sorry i don´t have code by hand.
It´s not a big challenge. usually set up a timer to interrupt with sample frequency.
Read out the previous conversion value, start a new conversion, process the conversion value; leave interrupt.

Klaus
 

Thank u KlausST. It appears to me there is a faint light at the end of this tunnel. I am going through your guidance line by line and trying to get it. I think I have grasped your idea of detecting zero crossing. Say the timer is so set that it enables 60 ADC samples throughout a cycle of rectified AC. After dectecting zero crossing point, every sample value to be added. This addition will continue upto the next zero crossing. The sum of the sample values to be devided by number of samples to get average value of a cycle. Isn’t it? But still I have a question. As you stated “new > old” that is the zero crossing point - this will happen throughout the entire rising edge. Then how to decide the actual zero crossing point. Please take some more pain.

swapan
 

Hi,

yes, you are rigth about zero crossing.
this is why i wrote "the first time..."

it is possible with the adc readings, but the timing is not that exact. with 3000 smpl/s you get a timing resolution of about 300us.
Let´s say +/- 150us. this may cause a 6% variation in output power. For a heating application it is OK.
But maybe this causes flicker with a light bulb. I´m not sure.

I´d rather go for a tiny hardware solution. Do you have an analog comparator in your ucontroller?
If not, use an external one. Compare the rectified voltage with a fixed voltage (voltage divider and capacitor) and generate an edge triggered interrupt. In the ISR clear the counter.

This is not much effort, but you get much better timing.


Klaus
 

Yes. There is analogue comparator in the MCU (PIC 16F676). Is it possible to use the same bit of I/O port for CIN (comparator purpose) and analogue input for ADC?

swapan
 

Hi,

Is it possible to use the same bit of I/O port for CIN (comparator purpose) and analogue input for ADC?

Datasheet will tell you. I don´t know PIC16F676


Klaus
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top