CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Voltage Measurement using PIC16f877a

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ratheeshbr



Joined: 26 Jan 2011
Posts: 31

View user's profile Send private message

Voltage Measurement using PIC16f877a
PostPosted: Tue Mar 06, 2012 5:19 pm     Reply with quote

I am trying to measure AC voltage using micro controller. I am using a 3-0-3 transformer and after converting to DC using diode, I am reading it using ADC. But the output voltage shows around 105-115 volts. I think there is mistake in the multiplication factor. I am using (230v/1023). Is there any mistake? Please reply.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Mar 06, 2012 7:13 pm     Reply with quote

a mistake ?? Could well be ...

but without a schematic and your code??

who can tell?
ratheeshbr



Joined: 26 Jan 2011
Posts: 31

View user's profile Send private message

Details
PostPosted: Tue Mar 06, 2012 7:54 pm     Reply with quote

Sorry. I forget to give the details. The circuit is nothing but a 3-0-3 centre tapped transformer- a rectifier to make it and a 10uF capacitor for smoothening and reading using ADC of PIC16f877a.
Code:
#include<16f877a.h>
# use delay(clock=20000000)
#USE RS232(BAUD=9600,errors,PARITY=N,XMIT=PIN_C6,RCV=PIN_C7,BITS=8)
void main()
{
int x,volt;
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(ALL_ANALOG);
while(1)
{
set_adc_channel(2);
   delay_us(20);
   x=read_adc();
volt=x*230/1023;
printf("%d",volt);
}
}


O/p is around 170v
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Tue Mar 06, 2012 8:11 pm     Reply with quote

Recognize that when you half wave rectify your signal and feed it to a filter cap, you have just introduced a couple of significant errors - the first is you are going to be reading peak voltage while the line voltage spec you are talking about (either 115 or 230 is RMS (.707 * peak for a sine wave). Also, at that low a voltage (3 volts, if you are using a silicon diode, the typical drop across it is 0.7 volts which also is going to screw up your reading. Both of those factors need to be taken into consideration in your calculations. The diode drop will probably be the harder one to deal with because the forward drop across the diode is a non-linear function of the current through the diode (see the spec sheets for your diode of choice).

There are a couple of ways around the diode issue - use a "precision rectifier" usually built with an op-amp and a diode in the feedback. Another way would be to not use a diode. Use a zero-crossing detector then measure the voltage 90 degrees later (granted, this requires you to be working with a known frequency - in this case the line mains for example). Another way to minimize the diode drop issues is to step the voltage down to 100VAC for example (using a transformer for isolation) rectify that then use a resistive voltage divider to drop it down to the level of the ADC in the PIC - at 100v, a 0.7 drop is not very significant - at 3 volts it is.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
temtronic



Joined: 01 Jul 2010
Posts: 9173
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 06, 2012 8:22 pm     Reply with quote

default for ADC is 8 bits.......
ratheeshbr



Joined: 26 Jan 2011
Posts: 31

View user's profile Send private message

PostPosted: Tue Mar 06, 2012 8:26 pm     Reply with quote

ya I changed it to 10.
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Mar 07, 2012 1:03 am     Reply with quote

gpsmikey wrote:

There are a couple of ways around the diode issue - use a "precision rectifier" usually built with an op-amp and a diode in the feedback.


I think with a single diode you want it in series with the output and then feeding back.

for the orignal poster (if they want):

http://en.wikipedia.org/wiki/Precision_rectifier
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
Ttelmah



Joined: 11 Mar 2010
Posts: 19366

View user's profile Send private message

PostPosted: Wed Mar 07, 2012 2:42 am     Reply with quote

Key line:
int x,volt;

How big can an 'int' be?.
What will happen when you multiply an incoming value like '200', by '230'?.....


Best Wishes
temtronic



Joined: 01 Jul 2010
Posts: 9173
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Mar 07, 2012 9:38 am     Reply with quote

also...
x=read_adc();

what does 'x' contain after the read_adc() function ?

hint, it ain't 10 bits of adc data !
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

Measuring ac
PostPosted: Wed Mar 07, 2012 2:55 pm     Reply with quote

If you want to measure ac, you don't HAVE to rectify.

Feed your stepped down voltage into your ADC, with the ADC input biased to Vref/2.

By taking ADC values at regular intervals you can then calculate any parameter of your input voltage.

NB. I am not contradicting anything that has been said by other contributers to this post, simply adding to it.

Mike
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group