Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
microbit-adc-2/main.cpp, micro:bit adc read, P0
//microbit-adc-2/main.cpp
#include "mbed.h"
#include <stdio.h>
#include <string>
Serial mPc(USBTX, USBRX);
AnalogIn mySensor(P0_3 );
//
long convert_Map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
// reading LM60BIZ
int getTempNum(){
int iRet=0;
//unsigned long reading = 0;
float read=0;
for (int i=0; i<10; i++) {
// reading= P0.getAnalogValue();
read= mySensor.read();
// delay(100);
}
read =read * 1000;
mPc.printf("read=%d\n" , (int)read ) ;
// mPc.printf("read=%f\n" , read ) ;
int voltage=convert_Map((int) read , 0, 1000, 0,3300); // V
mPc.printf("v=%d\n" ,voltage);
int iTemp = (voltage - 424) / 6.25;
iRet= iTemp;
return iRet;
}
//
int main() {
mPc.baud( 115200 );
mPc.printf("#start-microbit-adc-2 \n");
// AnalogIn mySensor(P0_3 );
while (true)
{
// mPc.printf("%f\r\n" ,mySensor.read() );
int iT1 =getTempNum();
mPc.printf( "iT1=%d\n",iT1) ;
wait(1.0 );
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment