View previous topic :: View next topic |
Author |
Message |
dsaari
Joined: 05 May 2006 Posts: 25
|
Load Cell A/D Issue |
Posted: Thu Dec 04, 2008 12:02 am |
|
|
Hello,
I have successfully amplified a 10mV/V load cell bridge using an IAN122
instrumentation amplifier to provide roughly 0 - 5V analog input to a pic.
I am using 10bit A/D conversion in the pic and it works great with one
exception. As I printf to verify the conversion from the A/D it sporadically
outputs 512 (1024/2) to the monitor regardless if I am applying force to
the load cell or not. Sometimes the majority of the values appear good,
other times, every other conversion is a 512. I've tried a lot of different
things and nothing seems to make a difference. Does anyone see
anything I could be doing wrong?
PCWH 4.012
Code: |
#include <18F2331.h>
#device adc=10
#DEVICE CCSICD=TRUE //Use the ICD debugger
#fuses NOWDT, HS, NOPUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, DEBUG
#use delay(clock=20000000) //20MHz clock
#use rs232(debugger)
void main()
{
setup_adc_ports(sAN1|VSS_VDD); //Make these I/O pins A/D inputs
setup_adc(ADC_CLOCK_INTERNAL);
While (TRUE)
{
int16 adc_value = 0;
set_adc_channel(1);
while(TRUE)
{
adc_value = read_adc();
printf("%lu\n\r", adc_value);
delay_ms(100);
}
}
} |
Any help is much appreciated. thx |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Dec 04, 2008 12:26 am |
|
|
I think, there should be a short (e. g. 20 us) delay after set_adc_channel(). |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 04, 2008 12:59 am |
|
|
Quote: | #use delay(clock=20000000) // 20MHz clock
setup_adc(ADC_CLOCK_INTERNAL); |
Look at this table in the 18F2331 data sheet:
Quote: | TABLE 20-2: TAD vs. DEVICE OPERATING FREQUENCIES |
It's on page 257 (page 259 in the Acrobat reader). It shows the
correct divisor value to use with the ADC, for your oscillator frequency.
Data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/39616C.pdf |
|
|
Ttelmah Guest
|
|
Posted: Thu Dec 04, 2008 3:08 am |
|
|
Both comments already made apply.
Also, avoid declaring variables inside code blocks. Historically, C did not allow this, and CCS, still occassionaly has problems with this. It is just 'not worth' doing.
Second comment. Why two whiles?. The code will never leave the second, so the first is 'pointless'.
Best Wishes |
|
|
dsaari
Joined: 05 May 2006 Posts: 25
|
Load Cell A/D Issue |
Posted: Thu Dec 04, 2008 7:51 am |
|
|
I've made the changes recommended by modifying the TAD divisor, adding a delay after setting the channel, and removing the delarations for the code block. The reason I had two "while's" is because I had stripped out other things as I've been troubleshooting. It looks pointless but had purpose, regardless I don't think that's it. I'm still having the issue.
I've made a characterization of this behavior since my original post that may help point me in the right direction. Each time I program the PIC and begin the debugger or reset it; this behavior consistently occurs:
1. It begins displaying seemingly good values.
2. Every 5-10 seconds it spits out a 512.
3. This frequency gradually increases until basically I am getting nothing but 512's.
4. After about 2 minutes BAM! works perfectly, and never hiccups again unless I reset/reprogram it.
It's as if something's building until it discharges. I want to say it's circuit related but the 512 coincidence is strange. If in fact noise or some discharge where causing this, I'd expect to see variance, not the consistent 512's I'm getting. I'm going to put a scope on it and see if there's something I can detect. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 04, 2008 11:44 am |
|
|
Disconnect the load cell circuit from the PIC. Temporarily add a 5K
trimpot instead. Set it for various values. See if you still get the problem. |
|
|
dsaari
Joined: 05 May 2006 Posts: 25
|
Load Cell A/D Issues |
Posted: Thu Dec 04, 2008 5:49 pm |
|
|
Ok, I'm an idiot. I can't bring myself to leave you kind folks without closure so I'm eating a little crow. I changed the TAD divisor to ADC_CLOCK_DIV_32 and then ADC_CLOCK_DIV_64 and both those settings seem to work fine. PCM programmer you were spot on. I can't hardly read those damn microchip datasheets and make much sense from them. They say keep the time as slow as possible, so in my head dividing a clock makes things faster so I didn't try the settings above DIV8. Who knew? In anycase, I think it's going for now, I've settled on the DIV32 because in my mind it's slower than DIV64. Thanks for all your help. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 04, 2008 6:20 pm |
|
|
The recommended divisor value based on the table in the data sheet
for a 20 MHz crystal would be ADC_CLOCK_DIV_16.
It says you can use "div 8" up to 19.2 MHz, and then "div 16" up to 38.4.
Since you're using 20 MHz, "div 16" is the answer. |
|
|
|