x MCP9701-E/TO 販売コード 103199
x Mbed2のリビジョンは、125
x あまり正確では、ない
目的
秋月で売っている安価なMCP9701(約25円)を使って温度を出力する。
A0は、PA_0の事
putcなのは、容量削減のため
//SER_MCP9701_float_010_1
//温度
#include "mbed.h"
//10の割り算 0から9999までは、正しい。
#define DIV10(n) (((n*409)+((n*154)>>8))>>12)
//アナログ入力の設定
AnalogIn adc_vbat(A0);
RawSerial pc(PA_2, PA_3); //010
//メイン関数
int main() {
int s; //ADCの値
float v; //電圧
float t; //温度
int t_100; //温度の100倍
char data1[]={0,0,0}; //バッファ
char data2[]={0,0,0}; //バッファ
//無限ループ
while(1) {
//ADCの読み込み 0から4096
s = (adc_vbat.read_u16()>>4);
//センサーのADC値を電圧に変換
v = s * ( 3.3 / 4096.0 );
//センサーの電圧を温度に変換
t = (v - 0.4) * (1.0 / 0.0195);
//温度の表示の為の変換
t_100 = t * 100.0;
data2[1]='0'+ (t_100-(DIV10(t_100)*10)); //t%10
t_100 = DIV10(t_100);
data2[0]='0'+ (t_100-(DIV10(t_100)*10)); //t%10
t_100 = DIV10(t_100);
data1[1]='0'+ (t_100-(DIV10(t_100)*10)); //t%10
data1[0]='0'+ DIV10(t_100); //t/10
//温度の表示
pc.putc(data1[0]);pc.putc(data1[1]); //整数
pc.putc('.'); //小数点
pc.putc(data2[0]);pc.putc(data2[1]); //小数点以下
pc.putc('\r');pc.putc('\n'); //リターン
//1秒の待ち
wait_ms(1000);
} //while
} //main
//容量削減
void error(const char* format, ...){}
テストプログラム
//10の割り算 0から9999までは、正しい。
#define DIV10(n) (((n*409)+((n*154)>>8))>>12)
#include <stdio.h>
int main(void){
for(int s = 497;s<=4096;s++){
printf("[%d]=",s);
//センサーのADC値を電圧に変換
float v = s * ( 3.3 / 4096.0 );
//printf("[%f]",v);
//センサーの電圧を温度に変換
float t = (v - 0.4) * (1.0 / 0.0195);
printf("[%f]",t);
int t_100 = t * 100.0;
printf("_[%d]_",t_100);
char data1[]={0,0,0,0};
char data2[]={0,0,0,0};
data2[1]='0'+ (t_100-(DIV10(t_100)*10)); //t%10
t_100 = DIV10(t_100);
data2[0]='0'+ (t_100-(DIV10(t_100)*10)); //t%10
t_100 = DIV10(t_100);
data1[1]='0'+ (t_100-(DIV10(t_100)*10)); //t%10
data1[0]='0'+ DIV10(t_100); //t/10
printf(data1); //整数
printf("."); //小数点
printf(data2); //小数点以下
printf("\r\n"); //リターン
//printf("\r\n");
}//for
}