You are here

Typecasting in PSoC 3 | Cypress Semiconductor

Typecasting in PSoC 3

Summary: 2 Replies, Latest post by U2 on 02 Nov 2011 02:10 AM PDT
Verified Answers: 0
Last post
Log in to post new comments.
AlexB's picture
18 posts

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

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?



user_1377889's picture
9284 posts


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


DAC_Value = ADC_Value >> 4;



uday's picture
Cypress Employee
569 posts
  • 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.


Log in to post new comments.