Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
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.
- 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.
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 22 Oct 2015