8 years, 6 months ago.

question about uint16_t and read_u16()

Hi guys,

I wanted to make the ADC voltage code by using LPC1768 and I just wonder my code is adequate to get the potentiometer voltage. I checked the data sheet and LPC1768 has ADC 12_bit. In this case, can I use the uint16_t and read_u16()? I heard only 16_bit ADC micro controller can use those one.

  1. include "mbed.h"

AnalogIn adc(p20); float analogValue = adc; 0 <=adc <=1, where 0 means 0V and 1 means 3.3V Serial pc(USBTX,USBRX);

int main() {

uint16_t adcValue; float voltage;

while(1) {

adcValue = adc.read_u16();

voltage = adcValue *3.3 / 65535;

pc.printf("ADC Value : %i, %.3f volt \r\n", adcValue, voltage);

wait(0.1); } }

posted by Martin Simpson 19 Oct 2015

1 Answer

8 years, 6 months ago.

The MBED API for the adc.read_16() is a 'Normalized' number to account for different platforms you could have an 8Bit 10Bit or 12Bit ADC depending on the type of processor used.

To do this the API left shifts by 4 then takes the upper 4bits and puts this in the now Lower 4 bits.

for example in a base 10 system try converting from 0 to 99, to 0 to 999. you would left shift by 1 decimal place (multiply by 10) then take the upper most digit and place in the units column.

If you know your particular chip (12 BIT ADC) you could just read as above and Right Shift by 4 to give your actual 12 Bit value to develop your voltage reading or leave as is.

Hope this Helps

Accepted Answer

Thanks dude!

posted by Gucheol Jeong 20 Oct 2015