View previous topic :: View next topic |
Author |
Message |
abx
Joined: 08 Jun 2007 Posts: 10
|
A/D with ref on AIN2 & AIN... |
Posted: Mon Jun 11, 2007 1:18 pm |
|
|
Hi,
I configure the A/D converter on my µP (i.e. PIC18F458) with external refs on AIN2 & AIN3 respectively at 2V & 3V.
After a reset, when I test the voltage on each ref pin I get the right voltages. When the program pass the line "setup_adc(0x88)", the ref- fall to 0.1V and ref+ to 1.7V. It seems like if the program drive AIN2 to "0".
Then the other AINs are like burned...
Any idea ?
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 11, 2007 1:30 pm |
|
|
Quote: | When the program pass the line "setup_adc(0x88)", |
Don't use "Magic Numbers" when programming. It's considered to be
a bad programming technique. Especially, don't use them with CCS
functions. Always use the constants that are defined in the 18F458.H
file. |
|
|
abx
Joined: 08 Jun 2007 Posts: 10
|
|
Posted: Mon Jun 11, 2007 2:50 pm |
|
|
I've tried the magic number to test. I have the same results with the constants that are defined in the 18F458.H |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 11, 2007 2:59 pm |
|
|
Post a test program, similar to the one in the link below. Send the
output to RS-232 or an LCD. Test it in hardware before you post it.
http://www.ccsinfo.com/forum/viewtopic.php?t=28842&start=7
Also post your compiler version. It's given at the top of the .LST file.
It's a 4-digit number such as 3.249, or 4.041, etc. |
|
|
abx
Joined: 08 Jun 2007 Posts: 10
|
|
Posted: Mon Jun 11, 2007 3:05 pm |
|
|
Ok, I'll do it tomorow. All the hardware is at the office.
Thanks. |
|
|
abx
Joined: 08 Jun 2007 Posts: 10
|
|
Posted: Tue Jun 12, 2007 12:28 am |
|
|
Here is the code :
#include <18F258.h>
#device ICD=TRUE
#DEVICE ADC=10
#use delay(clock=20000000)
#fuses NOWDT, HS, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use rs232(baud=2400,xmit=PIN_B5,stream=DEBUG)
#include <LCD.C>
int16 Can1, Can2;
#int_TIMER1
TIMER1_isr()
{
set_adc_channel( 5 );
delay_us(10);
Can1 = Read_ADC();
set_adc_channel( 0 );
delay_us(10);
Can2 = Read_ADC();
}
void main()
{
float Accel_X, Accel_Y, Accel_X_min, Accel_X_max, Accel_Y_min, Accel_Y_max;
setup_adc_ports(A_ANALOG_RA3_RA2_REF);
setup_adc( ADC_CLOCK_DIV_32 );
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
enable_interrupts(INT_TIMER1);
lcd_init();
delay_ms(6);
enable_interrupts(global);
Accel_X_min = 0;
Accel_X_max = 0;
Accel_Y_min = 0;
Accel_Y_max = 0;
while(TRUE)
{
Accel_X = Can1;
Accel_Y = Can2;
Accel_X = (Accel_X - 511) * 9.81 * 5000 / (312 * 1024);
Accel_Y = (Accel_Y - 511) * 9.81 * 5000 / (312 * 1024);
if ( Accel_X < Accel_X_min)
Accel_X_min = Accel_X;
if ( Accel_X > Accel_X_max)
Accel_X_max = Accel_X;
if ( Accel_Y < Accel_Y_min)
Accel_Y_min = Accel_Y;
if ( Accel_Y > Accel_Y_max)
Accel_Y_max = Accel_Y;
printf(lcd_putc,"\fX : %ld Y : %ld", Can1, Can2); // for test
delay_ms(500);
}
} |
|
|
abx
Joined: 08 Jun 2007 Posts: 10
|
|
Posted: Tue Jun 12, 2007 12:37 am |
|
|
Excuse me, I've forgotten the compiler version :
CCS PCH C Compiler, Version 3.182, 21972
Filename: C:\Tempo\test2.LST
ROM used: 3834 (12%)
Largest free fragment is 28098
RAM used: 63 (4%) at main() level
77 (5%) worst case
Stack: 6 worst case (5 in main + 1 for interrupts) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 12, 2007 12:49 am |
|
|
Quote: | #include <18F258.h>
#int_TIMER1
TIMER1_isr()
{
set_adc_channel( 5 );
delay_us(10);
Can1 = Read_ADC();
set_adc_channel( 0 );
delay_us(10);
Can2 = Read_ADC();
}
void main()
{
float Accel_X, Accel_Y, Accel_X_min, Accel_X_max, Accel_Y_min, Accel_Y_max;
setup_adc_ports(A_ANALOG_RA3_RA2_REF);
|
The 18F258 doesn't have an analog channel 5. The constant that
you're using to setup the adc ports is for the 18F458. It's not valid
for the 18F258. |
|
|
abx
Joined: 08 Jun 2007 Posts: 10
|
|
Posted: Tue Jun 12, 2007 1:36 am |
|
|
The comment in the PIC18F258 is a little bit confuse :
#define A_ANALOG_RA3_RA2_REF 0x8C // A0 A1 A5 Ref=A2,A3
Now, it'ok for the A/D conversion with no refs. thanks.
But the problem with the refs is still present. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 12, 2007 2:05 am |
|
|
That comment refers to the pin names. Pin A5 is also Analog input AN4.
Look at the label on Pin 7 of the SOIC package for the 18F258:
So you should make your external connection to pin RA5, and in the
code, you should select A/D channel 4. Example:
Code: | set_adc_channel( 4 ); |
|
|
|
abx
Joined: 08 Jun 2007 Posts: 10
|
|
Posted: Tue Jun 12, 2007 2:14 am |
|
|
That is done yet.
The A/D is working correctly with AN0 & AN4.
The problem is on the refs. When the program pass the command line :
setup_adc_ports(A_ANALOG_RA3_RA2_REF);
the ref AIN2 fall from 2V to 0.1 V.
Do you have a code example using refs on AIN2 & AIN3 ? |
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 12, 2007 3:26 am |
|
|
There is a 'caveat' here though. The input range you have (1v),is well below the minimum for the ADC to give it's specified accuracy. You will only get about 8bits of 'real' resolution with a range this low...
What is driving your Vref- pin?. I have used both pins fine in the past, but the source needs to be able to deliver 150uA. Also, if any of the analog inputs go _below_ the Vref- value, it'll tend to be pulled down via the internal diode in the multiplexer FET...
Best Wishes |
|
|
abx
Joined: 08 Jun 2007 Posts: 10
|
|
Posted: Tue Jun 12, 2007 3:39 am |
|
|
I have a resistor bridge :
0V -> 10K -> AIN2 -> 5K -> AIN3 -> 10K -> +5V.
I've tried with 1K, 0.5K and 1K, with the same result.
What is the minimun ref value ?[/img] |
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 12, 2007 5:30 am |
|
|
With the 'resistor bridge', the accuracy, will be appalling. The load current changes by a factor of over 100*, at different points in the sample cycle...
You ned to be able to give a stable voltage with a 1mA change in the signal load.
Data sheet. Electrical characteristics, VrefH-VrefL. 3v for 10bit resolution on the chip you mention....
Best Wishes |
|
|
abx
Joined: 08 Jun 2007 Posts: 10
|
|
Posted: Tue Jun 12, 2007 5:38 am |
|
|
We have provided zener diode for the final board.
The bridge is for prototype board to se if we can adjust the A/D ref between the analog input on AN0 & AN4.
The input signal is 2.5V +/- 0.4. |
|
|
|