|
|
View previous topic :: View next topic |
Author |
Message |
samyg
Joined: 26 Apr 2013 Posts: 11
|
PIC12F675 - analog I/P not working as expected |
Posted: Thu May 23, 2013 5:49 am |
|
|
Hello,
I'm writing a code which does the following:
1. a small start up delay.
2. Reads the half-wave rectified AC voltage (230V mains is brought down to about 900mV peak and fed to GP4/AN3). In the negative half of the cycle, the signal is at 0. For testing purposes, the signal is read just once and then the program continues. Once this works, i will modify it to take average values over the full cycle etc.
3. Takes a decision if the read value is within two limits. For testing, this only checks if the read value is equal to 0 or not. It never is.
If yes, a relay and LED are switched on.
If not, and if the outputs were already on, they are switched off and the program restarted from step one. Else, the program waits till the input goes back into range, while blinking the LED.
Problems:
1. The ADC result was never coming as zero, even in the negative half.
2. I took 100 samples at 1 ms intervals and stored it in the eeprom. On reading the eeprom, all 100 values showed up as either "46" or "47" (hexadecimal, of course).
3. I analyzed the analog input on an oscilloscope, and without the PIC in the IC base, the proper 900mv sine wave input was on pin 3 (GP4). As soon as I put the PIC in, the waveform became a straight line, looking as if it was switching between two close values at a very high frequency.
I've set the tris properly, and am using fast IO mode.
Pins: GP2 & GP5: outputs.; GP4: used as analog input AN3.
I know for a fact that the circuit board runs properly.
Can someone help me spot possible errors in my code, which is not letting the GP4 pin act as expected when configured as an analog input?
Thanks.
Code: |
#device PIC12F675
#include<12F675.h>
#device ADC=10
#use fast_io(all)
#fuses NOMCLR NOWDT INTRC BROWNOUT NOPROTECT
#use delay(clock=4000000)
#define relay PIN_A5
#define led PIN_A2
#define lovtg 0x0000
#define hivtg 0xffff
#define initdly 5
#define cycles 10
void init()
{
setup_comparator( NC_NC_NC_NC ); // disable comparators
set_tris_a( 0b11011011 );
setup_adc_ports(sAN3, VSS_VDD);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(3);
delay_us(10);
}
void startupdly()
{
int i;
for(i=0;i<initdly;i++)
{
output_low(led);
delay_ms(100);
output_high(led);
delay_ms(500);
output_low(led);
delay_ms(400);
}
// while(1) //used for testing, but relay never turned on even by this. However shorting GP4 to ground made the relay turn on, indicating that adc module was working.
// {
// if(read_adc()<=10)
// output_high(relay);
// }
}
void main()
{
output_a(0x00);
int16 adcavg;
boolean opflag=false;
init();
delay_us(100);
startupdly();
while(1)
{
adcavg=read_adc();
if((adcavg==0))
{
output_high(relay);
output_high(led);
opflag=true;
}
else
{
if(opflag==true)
{
output_low(led);
output_low(relay);
opflag=false;
startupdly();
// continue;
}
else
{
output_high(led);
delay_ms(100);
output_low(led);
delay_ms((900))
// continue;
}
}
delay_ms(533); //for tests, to be removed.
}
}
|
Last edited by samyg on Thu May 23, 2013 7:50 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9240 Location: Greensville,Ontario
|
|
Posted: Thu May 23, 2013 7:08 am |
|
|
Your main problem is not code !!
WHY do you send an AC voltage into the PICs ADC that can only read DC voltages.OUCH!
Please read the ADC section and specs in the datasheet.It is very,very bad to send AC into the PIC.You can easily destroy the ADC,partially damage it(giving wierd readings) or other 'fun' stuff.
As you're talking about 230V mains, I assume that's 50Hz AC.Mike W. will know for sure, but usually you'll rectify the AC and then filter it a bit, as well as current limit the input to the PIC.
I'm unsure as to what you're trying to do as the PIC ADC can only read +ve DC voltage.If you need to read AC you'll have to build a simple 'level convertor' using an OPamp and 4 or 5 resistors.If you want to compare the two levels(+ve side of sine wave vs -ve side of sign wave) , again you'll need some opamp circuit before the ADC.
hth
jay |
|
|
samyg
Joined: 26 Apr 2013 Posts: 11
|
|
Posted: Thu May 23, 2013 7:48 am |
|
|
Thanks for the reply.
I apologize, as i gave slightly wrong information. The input is actually a half-wave rectified signal. What I'm trying is that this continuously (sinusoidal) varying DC input (which i erroneously called AC), be sampled again and again. As I said, the input is half wave rectified, so during the negative half of the cycle, it stays at 0v. Also, the 230V has been brought down to under 1V, so it is very safe for the PIC. As you said, yes, it is a 50Hz signal.
The problem is with the code itself, i believe. Some small thing i've missed must be causing the AN3 input to behave in this abnormal manner. It seems to be acting as a source, at least, not as a normal input, as it is changing the input at the GP4 pin.
Please check the code sir.
Thanks. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Thu May 23, 2013 8:33 am |
|
|
Think about it for a moment. What is going to discharge the PIC pin?.
If you have a rectifier feeding this with positive half cycles, the internal capacitance in the ADC, and the capacitance of the board/pin itself, will make this tend to integrate the signal, and it won't track the 0v, or even dip close to 0v....
The PIC ADC, has significant capacitance.
You need to do significant signal processing before sending this signal to the PIC. First, if you are using a voltage divider off the main, the 'minimum', is _two_ series resistors, both of which are mains rated (this way if one fails 'short circuit', there is not a direct mains path), together with a fuse, and a clamp zener, just above the peak voltage from the signal, so if a resistor does fail, the fuse will be blown by the zener.
Secondly even with this, you then need to have a proper precision rectifier circuit, otherwise the diode forward voltage will degrade the accuracy terribly. Then this should be followed by a buffer amplifier to avoid the capacitance problem.
As a 'comment', why bother?. Why not just integrate the voltage with a slightly larger capacitor, and read this?. That way you don't have to worry about sampling so fast, but you can still read the voltage.
Best Wishes |
|
|
|
|
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
|