8 years, 7 months ago.

ADC uint16_t in LPC1768

Hey guys!.

I've made the code that calculate ADC value and voltage using addValue.

And I'd like to know that whether I can use uint16_t code at LPC1768 and how to get 12bit value. (I know if I AnalogIn function, that data come out as the 0 ~ 1 values and it is much easier way than what I'm doing now)

The problem is.. if I used the uint16_t function and .read_u16() function, I've got the right value(maximum adcValue is 65535 and minimum is 0). However, when I changed the code from {.read_u16()} to {.read_u16()&0xFFF}, I got the wrong answer.

I was so confused because LPC1768 has the 12-bit ADC. How is this possible?. If you know about .read_u16() function, please let me know.

my code is like this.

  1. include "mbed.h"

AnalogIn adc(p20); Serial pc(USBTX,USBRX);tx,rx

int main() {

uint16_t adcValue; float voltage;

while(1) {

adcValue = adc.read_u16(); &0xFFF voltage = adcValue * 3.3 /65535; 2^16 = 65536 pc.printf("ADC Value : %i, %.3f volt \r\n", adcValue,voltage);

wait(0.1);

} }

Thanks!

1 Answer

8 years, 7 months ago.

To get 12-bit, shift the 16-bit result four positions to the right. The AnalogIn code generates 16-bit from 12-bit by shifting it four the the left, and then filling the four new, empty, LSBs with the four MSBs. This allows code to go from 0x0000 to 0xFFFF while only having a 12-bit ADC.

Accepted Answer

thank you for your answer! Now I can figure out some part of it. But I wonder I can use

adcValue = ((adcValue << 4) & (uint16_t)0xFFF0) | ((adcValue >> 8) & (uint16_t)0x000F);

above code in order to shift the position from 16-bit to 12-bit.

I also tried this code... but it doesn't work...

thanks!

posted by Gucheol Jeong 22 Oct 2015

To go from 16-bit to 12-bit just shift 4 to the right, and don't do anything else:

adcValue = adcValue >> 4;
posted by Erik - 22 Oct 2015

wow!!!! it works!!! haha I really appreciate your help Erik!

posted by Gucheol Jeong 23 Oct 2015