View previous topic :: View next topic |
Author |
Message |
16bit 8Bit Guest
|
16bit <--> 8Bit Conversion |
Posted: Tue Nov 16, 2004 2:33 pm |
|
|
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
|
|
Posted: Tue Nov 16, 2004 3:10 pm |
|
|
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 |
Posted: Tue Nov 16, 2004 3:36 pm |
|
|
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 |
Posted: Tue Nov 16, 2004 3:47 pm |
|
|
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 |
Posted: Tue Nov 16, 2004 4:47 pm |
|
|
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 |
|
|
|
|