LoginSignup
0
0

STM32L010とMCP9701で温度を秋月シリアル7セグに出力 (GPIO)(STM32)

Last updated at Posted at 2021-07-02

x MCP9701-E/TO 販売コード 103199

x Mbed2のリビジョンは、125
x あまり正確では、ない

目的
秋月で売っている安価なMCP9701(約25円)を使って温度を出力する。

構成
MCP9701-E/TO I-03199
赤色7セグメントLEDシリアルドライバキット K-10360

説明
MCP9701は、
0℃の時、400mV
1℃あたり19.5mV
精度は、±4℃
電線が引き出しやすい位置のPA0をアナログ入力にする
計算には、容量削減の為に浮動小数点となるべく割り算は、使わない
MCP9700は、ファミリー、オフセット500mV、10mV/1℃ 今回は、使わない

19.5は、1℃あたりの電圧
3300は、ADCの電圧
400は、0℃

3300/19.5=169.2
400/19.5=20.5

(4096*1692)/4096=1692



#include "mbed.h"

//*******   ******   *****  *****  
//     *         *   *      * 
//    *         *    *****  *  ***
//  *       ****     *      *    *
//  *       *        *      *    *
//  *        *****   *****  *****

//10の割り算 0から1028までは、正しい。主に0から999
#define DVI10(n) ((n*205)>>11)

//アナログ入力の設定
//AnalogIn adc_vbat(A3); //PA_4 010
AnalogIn adc_vbat(A0); //PA_0 010
//AnalogIn adc_vbat(A0); //767 303

#define swclk1  PA_5    //A4
#define test01  PA_6
#define swdio1  PA_7    //A6
#define en1     PA_4    //A3

DigitalOut swdclk(swclk1);
DigitalOut swdio(swdio1);
DigitalOut en(en1);

char seg[32] = {
   0x00 , //0 @ -> ' '
   0x77 , //1 A  o
   0x7c , //2 B      combined use "6"
   0x39 , //3 C
   0x5e , //4 D
   0x79 , //5 E  o
   0x71 , //6 F
   0x3d , //7 G
             
   0x76 , //8 H  o
   0x06 , //9 I     combined use "1"
   0x1e , //10 J
   0x75 , //11 K 
   0x38 , //12 L  o
   0x15 , //13 M
   0x37 , //14 N  o
   0x3f , //15 O  o combined use "0"
              
   0x73  , //16 P
   0x67  , //17 Q combined use "9"
   0x50  , //18 R
   0x6d  , //19 S combined use "5"
   0x78  , //20 T
   0x3e  , //21 U
   0x1c  , //22 V
   0x2a  , //23 W  o
   0x64  , //24 X
               
   0x6e  , //25 Y
   0x5b  , //26 Z combined use "2"
   0x4f  , //27 [  --> "3"
   0x66  , //28 \  --> "4"
   0x27  , //29 ]  --> "7"
   0x7f  , //26 ^  --> "8"
   0x08    //31 _
};

//             12345678   12345678   12345678   12345678
char b8[8] = {0b10000000,0b01000000,0b00100000,0b00010000,
              0b00001000,0b00000100,0b00000010,0b00000001 };

void seg1(char v)
{
    v=seg[v-64];
    for(int jj=0;jj<8;jj++){
        if( (v & b8[jj]) == 0 ){
                swdio=0; //ビットが0
        } else {
                swdio=1; //ビットが1 
        }//endif
        swdclk=1;swdclk=0; //clk
    }//for
    en=1;
    en=0;       
}//seg1


char nn[]={'O','I','Z','[','\\','S','B', ']','^','Q'};

//7segの一文字出力 nana_seg
int ns_putc(char ch) {
    
    if(ch == ' ') {
        ch = '@';
    } else if(ch == '.') {
        ch='@';
        seg[0]=0x80;
    } else if (ch >= '0' && ch <= '9' ) {
        ch = nn[ch-'0'];
    }
    
    seg1(ch);
    seg[0]=0x00;

    //戻り値
    return(0);
}//ns_putc


//文字列の表示 nana_seg
int ns_printf(char *str1) {

    //文字の中身がゼロか
    while(*str1){

        //一文字出力
        ns_putc(*str1 ++);

    } //while

    //戻り値
    return(0);
}//ns_printf


//メイン関数
int main() {

    //7セグの初期化
    en=0;
    swdclk=0;

    //ns_printf("STAR");wait_ms(1000); //debug
 
    int s;  //ADCの値
    int n0; //温度 小数点以上
    char data_read[8]; //バッファー

    //無限ループ
    while(1) {

        //adcの読み込み 0から4096
        s = (adc_vbat.read_u16()>>4);

        //電圧を温度に変換 ex 20.0 -> 200 温度の十倍を出力
        s=((s*1692)>>12)-205;

        //s=0;   //0    debug
        //s=200; //20.0 debug

        //小数点以上と小数点以下を分ける
        n0=DVI10(s);    // 小数点以上
        s =(s-(n0*10)); // 小数点以下

        //温度の表示
        data_read[0] = '0' + DVI10(n0);
        data_read[1] = '0' + (  n0-(DVI10(n0)*10)  );
        data_read[2] = '.';
        data_read[3] = '0' + s;
        data_read[4] = 0;
        ns_printf(data_read);
        
        //待つ
        for(s=0;s<65535;s++){for(n0=0;n0<200;n0++){}}

    }//while
 
}//main

//容量削減
void error(const char* format, ...){}




spi_7seg_9701_010_1.jpg

h_con28.jpg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0