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

16bit <--> 8Bit Conversion

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







16bit <--> 8Bit Conversion
PostPosted: Tue Nov 16, 2004 2:33 pm     Reply with quote

Is there an easy way to traslate from 8-bits to 16-bits and vise versa?

An example would be a 8 bit DAC and a 12-bit ADC. Have the data from the ADC translate into the 8-bit DAC...


Thanks
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Nov 16, 2004 3:10 pm     Reply with quote

Look up the "casting" operator in a C book. Also you may have to shift the data a few bits depending on where the data is located in the word. For example if you have 12 bits of data in a 16 bit word you have to know which end of the word has the extra four bits. Also CCS has the make8 and make16 functions that can make things more explicit.
_________________
The search for better is endless. Instead simply find very good and get the job done.
16bit 8Bit Conversio
Guest







16bit <--> 8Bit Conversion
PostPosted: Tue Nov 16, 2004 3:36 pm     Reply with quote

So it would look something like this.....


dac_value = byte(ADC_value)


dac_value is an 8-bit#
ADC_value is a 12-bit#
Ttelmah
Guest







Re: 16bit <--> 8Bit Conversion
PostPosted: Tue Nov 16, 2004 3:47 pm     Reply with quote

16bit 8Bit Conversio wrote:
So it would look something like this.....


dac_value = byte(ADC_value)


dac_value is an 8-bit#
ADC_value is a 12-bit#

This (laid out correctly - look at the example below), will convert the data type, but do nothing for the 'contents'. Hence you would end up 'losing' the upper 4 bits. Fine if you want to use 1/16th the range of the ADC, but a problem otherwise. Something like:

dac_value = (int8)((ADC_value+8)>>4);

will give a good conversion from 12bit to 8bit. So a value of 4096, will convert to 256. You can choose the amount 'added', to make the switch points where you want (but beware that as shown, anything over 4072, will potentially 'wrap', and return 0).

Best Wishes
Guest








Re: 16bit <--> 8Bit Conversion
PostPosted: Tue Nov 16, 2004 4:47 pm     Reply with quote

Thanks!!!!!


Ttelmah wrote:
16bit 8Bit Conversio wrote:
So it would look something like this.....


dac_value = byte(ADC_value)


dac_value is an 8-bit#
ADC_value is a 12-bit#

This (laid out correctly - look at the example below), will convert the data type, but do nothing for the 'contents'. Hence you would end up 'losing' the upper 4 bits. Fine if you want to use 1/16th the range of the ADC, but a problem otherwise. Something like:

dac_value = (int8)((ADC_value+8)>>4);

will give a good conversion from 12bit to 8bit. So a value of 4096, will convert to 256. You can choose the amount 'added', to make the switch points where you want (but beware that as shown, anything over 4072, will potentially 'wrap', and return 0).

Best Wishes
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