Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 3 > Typecasting in PSoC 3

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



Typecasting in PSoC 3
Moderator:
RKRM

Post Reply
Follow this topic



Typecasting in PSoC 3

AlexB posted on 04 Oct 2011 10:49 AM PST
Top Contributor
22 Forum 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
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




Re: Typecasting in PSoC 3

Bob Marlowe posted on 06 Oct 2011 04:21 AM PST
Top Contributor
1768 Forum Posts

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



Re: Typecasting in PSoC 3

U2 posted on 02 Nov 2011 12:10 AM PST
Cypress Employee
589 Forum 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.

 






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: KXP; Secondary Owner: VWA; Sunset Date: 01/01/20