CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

10bit a/d conversion

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
young



Joined: 24 Jun 2004
Posts: 285

View user's profile Send private message

10bit a/d conversion
PostPosted: Wed Jul 07, 2004 7:35 am     Reply with quote

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







PostPosted: Wed Jul 07, 2004 7:42 am     Reply with quote

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. Very Happy
BT
Guest







10-bit ADC
PostPosted: Wed Jul 07, 2004 7:46 am     Reply with 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 .....

HTH
young



Joined: 24 Jun 2004
Posts: 285

View user's profile Send private message

PostPosted: Wed Jul 07, 2004 7:55 am     Reply with quote

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







PostPosted: Wed Jul 07, 2004 8:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jul 07, 2004 8:07 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jul 07, 2004 8:13 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jul 07, 2004 8:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jul 07, 2004 8:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jul 07, 2004 11:50 am     Reply with quote

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. Wink

Humberto
.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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