View previous topic :: View next topic |
Author |
Message |
nasbyc
Joined: 27 Apr 2009 Posts: 50
|
dspic30f4013 |
Posted: Wed Jan 23, 2013 6:30 am |
|
|
Hi i want to read analog input from acs712 using dspic30f4013.Is there any diffrence in setup reading the analog input from pic. because the reading I obtained convert to current is different from the multimeter reading. FYI I already tried the same prog for pic18f2480 and it works great
I'm using PCWHD 4.140
Code: |
#include <30f4013.h>
#device adc=10
#use delay(clock=20000000) // A 20 Mhz crystal is used
#fuses hs,noprotect,nowdt
#use rs232(baud=9600,xmit=PIN_F5,rcv=PIN_F4, ERRORS)
#include <stdlib.h>
int main()
{
unsigned int16 value;
unsigned int16 temp;
unsigned int16 Factor = 264;
setup_port_a(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_16);
SETUP_ADC_PORTS(sAN1);
set_adc_channel(1);
delay_us(10);
do
{
value = read_adc();
value = value + read_adc();
value = value + read_adc();
value = value/3;
if (value < 512)
temp = (512 - value);
else
temp = (value - 512);
temp = (temp*Factor) ;
value = temp/10;
printf("Current is %ld mA\n", value);
delay_ms(500);
}while(1);
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Jan 23, 2013 8:10 am |
|
|
hmm.. you don't say what the readings are,which is kinda important!
Are they 'off' by 2-3 bits or 'random'?
some things to consider
1)You've set port A 'all_analog' but only use 1 input. Noise can easily be transferred to the reading.Normally you only 'enable' those ADC pins you really need.This might cause some 'random' nosie.
2) You've setup the adc_clock with /16. Is this correct for THAT PIC at the 20MHz clock speed? Datasheet will tell what is best configuration.
3)You read 3 times then compute average. It is 'better' programming to take 4 readings( or 8,16,etc) then average.The PIC will just right shift x4,x8,etc. It's faster and more accurate.
4)Hardware ? PCB, wirewrap,breadboard? 'floating' wires in the air? Improper grounds? EMI from welders,cellphones, coffee pot?
5) DVMs only sample about 3-4 readings per second, you do it in a few microseconds.
6) source noise. What's an ACS712 and what is it measuring?
The more information you supply the better we can try to figure out what's going on.
hth
jay |
|
|
nasbyc
Joined: 27 Apr 2009 Posts: 50
|
|
Posted: Wed Jan 23, 2013 9:05 am |
|
|
ACS712 is a current sensor which I used to measure the current consume by dspic and several devices. this is the result I get where on multimeter 0.05A or 0.04A. I already change setup_port_a(sAN1) and take 4 values into average but still the same result
Code: |
Current is 73 mA
Current is 54 mA
Current is 34 mA
Current is 80 mA
Current is 93 mA
Current is 100 mA
Current is 60 mA
Current is 80 mA
Current is 60 mA
Current is 67 mA
Current is 60 mA
Current is 40 mA
Current is 54 mA
Current is 73 mA
Current is 47 mA
Current is 67 mA
Current is 100 mA
|
|
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Wed Jan 23, 2013 9:19 am |
|
|
nasbyc wrote: | ACS712 is a current sensor which I used to measure the current consume by dspic and several devices. this is the result I get where on multimeter 0.05A or 0.04A. I already change setup_port_a(sAN1) and take 4 values into average but still the same result
|
Are you kidding? The ACS712 is a 20A full scale device. You're trying to resolve 40 or 50mA.
What you're trying to do is akin to using a 10 ton scale to weigh a housefly. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Jan 23, 2013 8:00 pm |
|
|
I just downloaded the Allegro specs....and it gets worse !!
That device is + and - 20 Amps ! So full range is 40 amps....
There is a +-5 amp device but really ,it's totally wrong for measuring <100ma current draws.
Far better to use a simple resistor and diff opamp or 'instrumentation amplifier' system. Heck even a simple PIC with a resistor between 2 analog pins will work(actually very well...).
Depending on the project a simple $5 DVM will do the job,right out of the box!
hth
jay |
|
|
|