Typecasting in PSoC 3

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello All,

   

I would like to convert a 12 bit reading from the delta-sigma ADC (range = 1.024V single ended) into an 8 bit DAC value (range = 1.024V).

   

I had originally thought to typecase the int16 adc conversion into a uint8 DAC value:

   

sprintf(buffer,"%d, %d\n\r", adc_val, (uint8)adc_val);//Note the typecast
UART_PutString(buffer);

   

However, when I do this I get the following (using the sprintf function):

   

adc_val = 3491

   

 (uint8)adc_val = - 23773

   

Can anyone suggest how I could better go about this process? Is this error a compilier thing or did I make a silly mistake?

   

Thanks,

   

Alex

0 Likes
2 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Hi,

   

wouldn't it be better to divide the 12-bit ADC_Value by 16 to get the 8-bit DAC_Value?

   

Thus:

   

DAC_Value = ADC_Value >> 4;

   

 

   

Bob

0 Likes
Anonymous
Not applicable
        
  • Bob's suggestion seems to be a better way of doing it. A right shift by 4 positions will produce the result that you need.
  •     
  • If your intention is to view the dat out on the UART, sprintf would be the ideal way to do it beacuse you have the data already in ASCII format that can be printed.
  •     
  • Since your intention is to write the data a DAC directly, you can either typecast the data as uint8 or you could right shift the 12 bit dat by 4 bits.
  •    
0 Likes