|
|
View previous topic :: View next topic |
Author |
Message |
young
Joined: 24 Jun 2004 Posts: 285
|
10bit a/d conversion |
Posted: Wed Jul 07, 2004 7:35 am |
|
|
I have the following program:
#if defined(__PCM__)
#include <16F877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12
#include <lcd420_1.c>
#include <math.h>
void main() {
int i, min, max;
int16 val1,val2,val3;
float Total;
lcd_init();
//lcd has to be cleaned before corectly position data on it
printf(lcd_putc," ");
lcd_gotoxy(1,1);
printf(lcd_putc,"Avitech LLC:");
setup_port_a( ALL_ANALOG );
setup_adc( ADC_CLOCK_INTERNAL );
// set_adc_channel( 2 );
do {
set_adc_channel( 1 );
delay_ms(1);
val1 = Read_ADC();
lcd_gotoxy(1,1);
printf(lcd_putc,"Red:%3u",val1);
set_adc_channel( 2 );
delay_ms(1);
val2 = Read_ADC();
lcd_gotoxy(9,1);
printf(lcd_putc,"Blue:%3u",val2);
set_adc_channel( 3 );
delay_ms(1);
val3 = Read_ADC();
lcd_gotoxy(1,2);
printf(lcd_putc,"Green:%3u",val3);
Total=(float)(val1+val2+val3)*3/255;
lcd_gotoxy(10,2);
printf(lcd_putc," T:%4.2f",Total);
} while (TRUE);
}
I want to get 10 bit resolution conversion, but I just got 8 bit conversion, how to change it and get 10 bit conversion |
|
|
valemike Guest
|
|
Posted: Wed Jul 07, 2004 7:42 am |
|
|
Code: |
#include <16F873.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ADC=10
#device *=16
#use delay(clock=4000000)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
|
The above is a snippet of code in my application that uses 10 bit A/D conversion.
Notice the statement that says #device ADC=10
That's all you need to do. |
|
|
BT Guest
|
10-bit ADC |
Posted: Wed Jul 07, 2004 7:46 am |
|
|
I always use the following in my #DEVICE statements:
#DEVICE ADC=10
But don't know what the default is if this is omitted .....
HTH |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Wed Jul 07, 2004 7:55 am |
|
|
I added #device=10,
but how to use the printf in my case
printf(lcd_putc,"Red:%3u",val1) error occured format type is invalid. |
|
|
valemike Guest
|
|
Posted: Wed Jul 07, 2004 8:05 am |
|
|
I haven't done any LCD-printf() statements, but this is what I do to print my 16 bit values to Hyperterminal:
printf("A/D value = 0x%lx\r\n", ad_value);
similiarly, you can write: "AD = %ld\r\n", ad_value);
where ad_value is a "long" (aka "int16"). The important thing to note is that you need the "l" (lower case L) after the % to denote that it is a "long". |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Wed Jul 07, 2004 8:07 am |
|
|
When I use printf(lcd_putc,"G:%4x",val1);
the out put is 00fc, the high bits are 00 and lower bit are some value, it is not a ten bit value ( it is always lower than 255). what is wrong? |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Wed Jul 07, 2004 8:13 am |
|
|
You need to specify to print a LONG hex value. A simple %4x will tell it to print an 8bit value. Try this:
printf(lcd_putc, "G:%lx", val1);
That should enable you to print a 16bit value
Ronald
Smile, it will make people wonder what you've been up to. |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Wed Jul 07, 2004 8:24 am |
|
|
Thank you, NOw I am able to print out correct readings for val1, val2, and val3, however the calculation for T=(float)(val1+val2+val3)*3/1024 return wrong result as T sametime over 5.0, (should be lower than 3.0) what happened here? |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Wed Jul 07, 2004 8:39 am |
|
|
Sorry, it is my misumderstanding, it is correct. since each of the val1, val2, val3 is less than 1024 than the (val1+val2+val3) less than 3*1024, then divided by 1024 should be less than 3, and 3*3 equals 9. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Jul 07, 2004 11:50 am |
|
|
BT wrote:
Quote: |
I always use the following in my #DEVICE statements:
#DEVICE ADC=10
But don't know what the default is if this is omitted .....
|
The default is ADC=8 , 2 LSB truncated.
Humberto
. |
|
|
|
|
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
|