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 CCS Technical Support

MCP3208 problem

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







MCP3208 problem
PostPosted: Tue Dec 02, 2003 11:39 am     Reply with quote

convert_to_voltage() works, the mcp3208 also works right, but y the hell isnt the result in the range fron 0 to 4096 (mcp3208 has 12bit) ...


thx!






#include <16F877.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 <MCP3208.C>

void display_data(unsigned long data ) {


printf(" (%ld)",data);
}


void main() {
unsigned long value=0;

adc_init();

printf("Sampling:\r\n");

do {


value = read_analog(0); //channel 0 single mode (ADXL_X)
printf("\n\rCh0: ");
printf(" %ld",value);

value = read_analog(1); //channel 0 single mode (ADXL_Y)
printf(" Ch1: ");
printf(" %ld",value);

} while (TRUE);

}


the result looks like this:


Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ttelmah
Guest







Re: MCP3208 problem
PostPosted: Tue Dec 02, 2003 11:51 am     Reply with quote

pfo wrote:
convert_to_voltage() works, the mcp3208 also works right, but y the hell isnt the result in the range fron 0 to 4096 (mcp3208 has 12bit) ...


thx!






#include <16F877.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 <MCP3208.C>

void display_data(unsigned long data ) {


printf(" (%ld)",data);
}


void main() {
unsigned long value=0;

adc_init();

printf("Sampling:\r\n");

do {


value = read_analog(0); //channel 0 single mode (ADXL_X)
printf("\n\rCh0: ");
printf(" %ld",value);

value = read_analog(1); //channel 0 single mode (ADXL_Y)
printf(" Ch1: ");
printf(" %ld",value);

} while (TRUE);

}


the result looks like this:


Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28256
Ch0: 32240 Ch1: -28240
Ch0: 32240 Ch1: -28240


It looks as though the result is being 'left justified' (shifted to the upper end of the 16 bit value), rather than right justified.
I think you will see that the read function, actually shifts the data left four bits.
Try:
value=read_analog(0)>>4;

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Dec 02, 2003 2:08 pm     Reply with quote

The functions in MCP3208.C return unsigned 16-bit numbers.
(All CCS integer data types are unsigned by default -- unlike normal C).

So in your printf statements, you need to use %lu instead of %ld.
--------------------

I'm looking at the CCS manual.

In the Jan 2003 manual, right below the Type Specifier chart, they
had this statement:
All types, except float, by default are unsigned; however, [they]
may be preceded by unsigned or signed.


But they don't have that statement in the current manual (May 2003).

This is going to screw up newbies big time.

They haven't changed the compiler. It still has an int or "long int"
as unsigned by default.
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