ADC processing
AnalogIn provides the following interfaces:
- read() - This gives a floating point value of range 0.0 to 0.1 with 6 decimal precision. But in order to calculate the actual value we need to multiply with 3.3V which is the Voltage Reference for the Chip. This would increase the overhead on the processor requiring it to process more and each floating point operation becomes costly. So it would not be optimal to be used.
- read_u16() - This gives a Unsigned short value in the range 0xF000 to
0xFFFF. Here is a catch in this the upper most nibble needs to chopped off. So the
actual value would be given by
(read_u16()&0x0FFF)
using a bitwise and to get the 12 bit value. As per the datasheet we know that ADC of LPC1768 is 12-bit so the max decimal value you can expect is 4095 and the Reference Voltage 3.3 so in order to get the actual voltage we need the following Calculation:
(read_u16()&0x0FFF) * 3.3/4096
Thus even if we don't want to have the Exact value of the voltage we can still make use of the digital Value which would not need the heavy floating point operation. This makes the use of the read_u16() more economic in terms of code size and performance. <<code>> //////////
Include Files
- include "mbed.h"
//////////
Pin Defines
DigitalOut sysled(LED1); System Indicator
DigitalOut ind1(LED2);
DigitalOut ind2(LED3);
DigitalOut ind3(LED4);
AnalogIn Sense1(p15); Sensor 1
Configure the PC Serial Port for CDC USB
Serial pc(USBTX, USBRX); tx, rx
//////////
main Program
int main() {
Configure the Fastest Baud Rate
pc.baud(115200);
printf("Aum Sri Ganeshay Namh !!\n");
Say the System is Ready to Run
sysled = 1;
Final Infinite Loop
while (1) {
ind3 = !ind3;
wait(0.2);
printf("\n ADC Val : %f",(Sense1.read_u16()&0xFFF)*3.3/4095);
}
} <</code>>
3 comments on ADC processing :
Please log in to post comments.
Surely the range for the ADC value is 0-1.0 ?