3 years, 10 months ago.

How to output the byte from the ADC ?

Hi everyone, I'm new on the mbed world and I have a little problem. I would like to have the real byte from the ADC and send it through the serial port. So when the voltage value is 3.3V have "11111111". I have already use the ADC, but I can only send float value who is sent in ASCII with the printf. I hope I was clear and I will get hepl :)

Have a nice day

Antoine

/media/uploads/abostem/capture.png

2 Answers

3 years, 10 months ago.

You could use the read_u16 member function of the AnalogIn Class

so in your example

      //Assign a named variable in memory
      unsigned int adcData;

      // Read the ADC and return a 16bit Unsigned short Integer
      adcData=ain.read_u16();

      //send the data as a unsigned integer number
      printf("%#u\n",adcData);
      //or convert to a binary series of 1's and 0's here plenty of howto's on the web for this

A comment on the unsigned number here. This is a 'Normalised' 16 bit number regardless of what the actual number of bits used for any MBED based controller you have selected. The MBED API will know which board you have selected and perform as follows:- e.g. For a 12 Bit ADC to 16 bit conversion. Shift Left by 4 bits and then add the upper 4 bit nibble to the lower (now zero) 4 bit nibble. The result is a 16 bit short Integer. (General case 16-numberofbitsoftheADC to give bit shift) If you know the ACTUAL number of bits your particular ADC uses then all you have to do is RIGHT shift by "16-umberofbitsoftheADC" to give your ADC value.

Accepted Answer

Thanks a lot Martin, I got hope for a moment, but the adc value is still sent with the ascii code... I have an LPC1768 with a 12bit ADC but I only need 8 bit so I shifted by 8. It works because I have 255 for 3.3V, but it is sent in ascii code and I just want the binary value. I don't know if it is possible to send in another way?

posted by Antoine Bostem 07 Nov 2016

Ok, I think I got it ! I just change the printf with putc and it works ! Thanks a lot guys :)

posted by Antoine Bostem 07 Nov 2016
3 years, 10 months ago.

Search for 'float to char' on the developer.mbed.org site and you will find several answers.

Thanks for your answer. I tried, but the result is the same, it sends 3.3 instead of "11111111". Here is my code:

DigitalOut myled(LED1);

AnalogIn ain(p20);

Serial device(p9, p10); tx, rx

float ADCDATA;

char buf[8];

int main() {

while(1) {

myled = 1;

wait(0.2);

myled = 0;

wait(0.2);

device.baud(19200);

ADCDATA=ain.read();

sprintf(buf, "%0.1f", ADCDATA);

device.printf("%s", buf);

}

}

posted by Antoine Bostem 07 Nov 2016