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.
11 years ago.
Trying to print ADC values to LCD
HI guys, I'm bit of noob to mbed, just picked it up a few days ago. Keep the good work up.
I have an LPC1768 working with an application board.
I'm trying to get a reading from Port1 and Port2 and display the reading on the lcd while using it to change the multi coloured led by pwm.
the code i've come up with, is changing the colour of the leds fine when i change the position of either port1 or port2 but the display is only showing either 1 or 0 for the for either port but not the actual ACD reading, which what i want.
many thanks for your help
- include "mbed.h"
- include "C12832_lcd.h"
- include "Arial_9.h"
- include "Small_7.h"
- include "MMA7660.h"
MMA7660 MMA(p28, p27); C12832_LCD lcd;
AnalogIn port1(p19); AnalogIn port2(p20); PwmOut red(p23); PwmOut blue(p25);
int main() { lcd.set_font((unsigned char*)Arial_9); while(1) { red.write(port1); blue.write(port2); int a; int b; a = port1.read(); b = port2.read(); lcd.locate(5,5); lcd.printf("Port1 = %d", a); lcd.locate(5,20); lcd.printf("Port2 = %d", b);
wait(0.3); lcd.cls(); } }
2 Answers
11 years ago.
A simple question, I like simple questions :)
The ADC read function returns a float between 0 and 1. So you shouldn't use ints, but floats, and then in your printf statement use %f. Now it rounds it to 0 or 1.
If you want the 'raw' data, use read_u16. Then you do get an interger value back (16-bit). It isn't actually the raw value, since it is left alligned and a 12-bit ADC, with the lower 4 bits filled with same value as top 4 bits. That way read_u16 behaves consistently independent of the number of bits of the ADC. But if you use it, and shift them 4 placed to the right, you got the 12-bit result.