ADC programming on PIC16f886

Status
Not open for further replies.

anj

Member level 3
Joined
Aug 16, 2012
Messages
66
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,288
Location
Rajkot, Gujarat, India
Activity points
1,679
Hi... all..

i am working on my college project and currently i am trying to sample the AC signal(0-12V) 50Hz and then from sampled values i want to calculate RMS value of signal.
my
Code:
#include<htc.h>
#include<pic.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include "func.h"

void main()
{	

	int i;
 	long s1[20];

        i=0;
        TRISB = 0;										//Set PORTB to inputs
        TRISC = 0;

ADCON0= 0b10000001;
ADCON1=0b10001000;
init_rs232();
delay_ms(20);
for(i=0;i<20;i=i+2)
{
GO=1;
while(GO==1);
s1[i]=ADRESL;
s1[i+1]=ADRESH;
delay_ms(2);  // 10 samples at 2ms delay so 10*2=20ms i.e. 1 cycle. of 50 hz
}

txdat("adc value:  "); // serial tx of string

for(i=0;i<20;i=i+2)
{

Serial_Hexbyte(s1[i+1]); // hex conversion of samples and display on virtual terminal in proteus to see the samples.
Serial_Hexbyte(s1[i]);
delay_ms(2);
txdat("  ");
}
now after sampling what to do for getting rms value. vref+=12V, vref-=gnd and 10 bit conversion is going on. please help. Regards.
 
Last edited:

You have to scale down your 12 v Ac to 5 V dc and give it to adc pins and vref+/- pins.
 

thanks for reply.
is it necessary.?? i mean sampling of the changing wave (e.g. sine) is possible or not??
 


Thanks Tahmid..:smile:

so, I got some points correct me if i am wrong..:

1) signal must not have maximum voltage above 5V..
2) If input is 230V then i have to use a transformer to scale it down to 5V. i.e. 230/5 = 46/1 so turn ratio of transformer is 46:1 ..
3) I can use the signals directly to ADC those having maximum voltage less than or equal to 5V ..

doubts:
However, the voltage must not exceed the maximum voltage limit of the ADC - 5V.
1) maximum limit of all ADC is 5V?? or it depends??

thank you again.
 


1) Correct
2) Yes. But you may have losses in the transformer causing voltage changes. Maybe some kind of direct measurement is better.
3) Yes.

doubts:

1) maximum limit of all ADC is 5V?? or it depends??

thank you again.

Maximum limit of the ADC of the PIC you are using is 5V (provided ADC VREF+ = 5V). Maximum limit of all ADC depends on the reference voltage and specific ADC used.

Hope this helps.
Tahmid.
 
Reactions: anj

    anj

    Points: 2
    Helpful Answer Positive Rating
ok.. this is helpful again..:smile:
now, please look at my code. I just want to know that if my procedure of sampling is correct or not.?? because I have to do that for which I may change my PIC to get Higher voltage limit in ADC..
thanks..:smile:
 

ok.. this is helpful again..:smile:
now, please look at my code. I just want to know that if my procedure of sampling is correct or not.?? because I have to do that for which I may change my PIC to get Higher voltage limit in ADC..
thanks..:smile:

Before I look at the code, you should understand that 5V is the maximum for most microcontrollers (all that I know of).
 

Ohh..
then what would you suggest to do for 230V AC signal??
I just want to 'read' that signal and then to display on LCD or seven segment.
 

You can test by using a transformer for step down, converting it to DC and then dividing the voltage using a voltage divider to ensure that the signal does not go over 5V.
 

You need a circuit like this. First step down 230 V AC to 12 V AC and then rectify it to get some 12 V DC. Apply that 12 V DC to the potential divider shown in the circuit. Connect to ADC pin as shown.
 

Attachments

  • sc31.jpg
    148.4 KB · Views: 94
Reactions: anj

    anj

    Points: 2
    Helpful Answer Positive Rating
Its even better if you use 2.6k ans 1.3k resistors. Your upper limit will be 15 volts. You can measure upto 15 v.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…