LoginSignup
0
0

More than 1 year has passed since last update.

STM32G031とS-8120で温度計(秋月シリアル7セグに出力(SPI))(mbed-os)

Last updated at Posted at 2022-08-22

x 超超重要 なぜか秋月STM32G031J6M6がうり切れていた。注意!!(2022/8/27現在)

x SWDをいじるので上級者向け

x なぜか3秒ルールが適用できず1回書き込むとシリアル書き込みしか受け付けない

x 過去ログを見よ!!

x OBでGPIOとブートのレガシーピンが有効がわかる人よう

目的
SPIのテスト

o_con700.jpg




//SPI_7SEG_S-8120C_float_031_1


#include "mbed.h"

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


//アナログ入力の設定
//AnalogIn adc_vbat(D0); //PB_7 ADC1/11
AnalogIn adc_vbat(PB_7); //PB_7 ADC1/11


SPI spi( PB_5 , PB_4 , PA_1); // mosi, miso, sclk //031
DigitalOut en( PA_13 ); //031


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 _
};


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

int comma1=0; // コンマ(小数点)の有効フラグ 1で有効 0無効

SPI *_spi1;
DigitalOut *_en1;

//7segの一文字出力 nana_seg
int ns_putc(char ch) {

    if(ch == ' ') {
        ch = '@';
    } else if(ch == '.') {
        return(0);
    } else if (ch >= '0' && ch <= '9' ) {
        ch = nn[ch-'0'];
    }

    if( comma1 != 0 ) { comma1 = (0x80); }
    _spi1->write(seg[ch-64] | comma1  );*_en1=1;*_en1=0;
    comma1 = 0;

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


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

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

        //コンマの処理
        comma1=0;
        if( (*(str1+1) ) == '.' ) { comma1=1; }

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

    } //while

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


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

    // not delete  3s wait    
    wait_us(1000*1000); //1秒待つ
    wait_us(1000*1000); //1秒待つ
    wait_us(1000*1000); //1秒待つ

    SPI spi( PB_5 , PB_4 , PA_1); // mosi, miso, sclk //031
    DigitalOut en( PA_13 ); //031

    _spi1 = &spi;
    _en1  = &en;
    
    spi.format(8,0);
    spi.frequency(1000000);
    en=0;

    char data_read[256];  //バッファー

    //無限ループ
    while(1) {
 
        //センサーのADC値を電圧に変換
        float v = adc_vbat.read() * 3300;  
        
        //センサーの電圧を温度に変換
        // v = -8.2 * t + 1705  =>  t = (v - 1705.0) / -8.2;
        float t = (v - 1705.0) / -8.2;

        //整数と小数に分離
        int t_i = (int)t;
        int t_s = (int)(t*100);
        t_s = t_s - (t_i * 100);

        //数値の表示
        sprintf( data_read, "%2d.%02d", t_i, t_s);
        ns_printf(data_read);

        //1秒待つ
        wait_us(1000*1000);

    } //while

} //main



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