2 Replies Latest reply on Nov 2, 2011 2:10 AM by udayanu_

    Typecasting in PSoC 3

      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?





        • 1. Re: Typecasting in PSoC 3



          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;





          • 2. Re: Typecasting in PSoC 3
            • 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.