0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

STM32G071と超音波距離センサーHC-SR04でOLED SSD1306(128x32)

Last updated at Posted at 2023-03-17

STM32G071と超音波距離センサーHC-SR04でOLED SSD1306

x 過去ログをみよ
x ソースは、長め

目的
反転表示
デカ文字
センター寄せ

o_cop419.jpg

o_cop417.jpg

o_cop418.jpg






//OLED_SSD1306_BITMAP32x320_HC_SR04_071


//ヘッダーファイル
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//定義
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// ビットマップデータ
// 'soiya1', 128x320px
static const unsigned char PROGMEM databytes[] = {




  0x00, 0x0f, 0xf0, 0x00, 0x00, 0x1e, 0x78, 0x00, 0x00, 0x30, 0x0c, 0x00, 0x00, 0x60, 0x06, 0x00, 
  0x00, 0xc0, 0x03, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 
  0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 
  0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 
  0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 
  0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 
  0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00, 0xc0, 0x01, 0x80, 
  0x00, 0xc0, 0x03, 0x00, 0x00, 0x70, 0x07, 0x00, 0x00, 0x3c, 0x1e, 0x00, 0x00, 0x1f, 0xfc, 0x00, 
  0x00, 0x0f, 0xc0, 0x00, 0x00, 0x1f, 0xc0, 0x00, 0x00, 0x31, 0x80, 0x00, 0x00, 0x61, 0x80, 0x00, 
  0x00, 0x41, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 
  0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 
  0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 
  0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 
  0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 
  0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 
  0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0x00, 0x3f, 0xfc, 0x00, 
  0x00, 0x0f, 0xf0, 0x00, 0x00, 0x1e, 0x78, 0x00, 0x00, 0x30, 0x0c, 0x00, 0x00, 0x60, 0x06, 0x00, 
  0x00, 0xc0, 0x03, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 
  0x00, 0x80, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x00, 
  0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x07, 0xf8, 0x00, 
  0x00, 0x0f, 0xf0, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 
  0x00, 0xc0, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 
  0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 
  0x01, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xe0, 0x1f, 0x80, 0x00, 0x7f, 0xff, 0x80, 
  0x00, 0x0f, 0xf0, 0x00, 0x00, 0x1e, 0x78, 0x00, 0x00, 0x30, 0x0c, 0x00, 0x00, 0x60, 0x06, 0x00, 
  0x00, 0xc0, 0x03, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 
  0x01, 0x80, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x00, 
  0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x0f, 0xf8, 0x00, 
  0x00, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 
  0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xc0, 
  0x00, 0x00, 0x00, 0xc0, 0x01, 0x80, 0x00, 0xc0, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x80, 
  0x00, 0xc0, 0x01, 0x80, 0x00, 0x70, 0x03, 0x00, 0x00, 0x3c, 0x1e, 0x00, 0x00, 0x1f, 0xfc, 0x00, 
  0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x70, 0x00, 
  0x00, 0x00, 0x60, 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x03, 0x00, 0x00, 
  0x00, 0x0f, 0x00, 0x00, 0x00, 0x1e, 0x60, 0x00, 0x00, 0x3c, 0x60, 0x00, 0x00, 0x78, 0x60, 0x00, 
  0x00, 0xe0, 0x60, 0x00, 0x00, 0xc0, 0x60, 0x00, 0x01, 0xc0, 0x60, 0x00, 0x01, 0x80, 0x60, 0x00, 
  0x01, 0x80, 0xe0, 0x80, 0x01, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0x80, 0x00, 0x00, 0x60, 0x00, 
  0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 
  0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 
  0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 
  0x00, 0xff, 0xff, 0x80, 0x00, 0xff, 0xff, 0x80, 0x01, 0x80, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 
  0x01, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 
  0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 
  0x01, 0xc0, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x00, 
  0x00, 0x0d, 0xf8, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 
  0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xc0, 
  0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x80, 
  0x00, 0xc0, 0x01, 0x80, 0x00, 0x70, 0x03, 0x00, 0x00, 0x3c, 0x1e, 0x00, 0x00, 0x1f, 0xfc, 0x00, 
  0x00, 0x0f, 0xf0, 0x00, 0x00, 0x1e, 0x78, 0x00, 0x00, 0x30, 0x0c, 0x00, 0x00, 0x60, 0x06, 0x00, 
  0x00, 0xc0, 0x03, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 
  0x01, 0x80, 0x01, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 
  0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x01, 0xff, 0xf8, 0x00, 
  0x01, 0xff, 0xfc, 0x00, 0x01, 0xe0, 0x0c, 0x00, 0x01, 0xc0, 0x06, 0x00, 0x01, 0x80, 0x03, 0x00, 
  0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00, 0xc0, 0x01, 0x80, 0x00, 0xc0, 
  0x01, 0x80, 0x00, 0xc0, 0x01, 0x80, 0x00, 0xc0, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x80, 
  0x00, 0xc0, 0x01, 0x80, 0x00, 0x70, 0x03, 0x00, 0x00, 0x3c, 0x1e, 0x00, 0x00, 0x1f, 0xfc, 0x00, 
  0x03, 0xff, 0xff, 0x00, 0x03, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x03, 0x80, 
  0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x1c, 0x00, 
  0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0xc0, 0x00, 
  0x00, 0x01, 0xc0, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 
  0x00, 0x1e, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 
  0x00, 0xf0, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 
  0x01, 0xe0, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 
  0x01, 0xc0, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 
  0x00, 0x0f, 0xf0, 0x00, 0x00, 0x1e, 0x78, 0x00, 0x00, 0x30, 0x0c, 0x00, 0x00, 0x60, 0x06, 0x00, 
  0x00, 0xc0, 0x03, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 
  0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00, 0xc0, 0x01, 0x00, 
  0x00, 0xc0, 0x03, 0x00, 0x00, 0x60, 0x06, 0x00, 0x00, 0x30, 0x0c, 0x00, 0x00, 0x1f, 0xf8, 0x00, 
  0x00, 0x0f, 0xf8, 0x00, 0x00, 0x1c, 0x1c, 0x00, 0x00, 0x30, 0x06, 0x00, 0x00, 0x60, 0x03, 0x00, 
  0x00, 0xc0, 0x01, 0x80, 0x00, 0xc0, 0x01, 0x80, 0x01, 0x80, 0x00, 0xc0, 0x01, 0x80, 0x00, 0xc0, 
  0x01, 0x80, 0x00, 0xc0, 0x01, 0x80, 0x00, 0xc0, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x80, 
  0x00, 0xc0, 0x01, 0x80, 0x00, 0x70, 0x03, 0x00, 0x00, 0x3c, 0x1e, 0x00, 0x00, 0x1f, 0xfc, 0x00, 
  0x00, 0x0f, 0xf0, 0x00, 0x00, 0x1e, 0x78, 0x00, 0x00, 0x30, 0x0c, 0x00, 0x00, 0x60, 0x06, 0x00, 
  0x00, 0xc0, 0x03, 0x00, 0x00, 0xc0, 0x03, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 
  0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00, 0xc0, 0x01, 0x80, 
  0x00, 0xc0, 0x01, 0x80, 0x00, 0x60, 0x01, 0x80, 0x00, 0x30, 0x0f, 0x80, 0x00, 0x1f, 0xff, 0x80, 
  0x00, 0x0f, 0xf9, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 
  0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 
  0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x03, 0x80, 
  0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x01, 0xe0, 0x00





};


#define TRIG 6
#define ECHO 7
unsigned long times; //測定時間
int distance;        //長さ 単位は、mm



//初期化
void setup() {

  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);


  // I2Cアドレスは使用するディスプレイに合わせて変更する
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

}//setup


//メインループ
void loop() {

  int c1000 = 1; //千の桁
  int c100  = 2; //百の桁
  int c10   = 3; //十の桁
  int c1    = 4; //一の桁

  int x = 46; //数値

  // 超音波を発生させる
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);

  // 超音波を受け取る
  times = pulseIn(ECHO, HIGH,2000000);


  // times = (unsigned long)7700/2; //debug
  // times = 1177; // debug 1ms 20.0cm
  // times = 942;//debug 1ms 16.0cm

  //時間を距離に変換する 音は、1秒間に約340メートル進む
  //速さx時間は距離
  //まず往復なので1/2にする
  // times/2
  // 340 x (times / 1000) 単位が秒なのでそろえてあげる
  // distance = (int)(times * 0.017);
  distance = (int)((times * ((unsigned long)(17))) / 100);

  x = distance;

  int uuj = x;


//10の割り算 0から9999までは、正しい。 stm32
#define DIV10(n) (((n*409)+((n*154)>>8))>>12)

  c1    =  (  x - (DIV10(x) * 10)  );  // '0'+(d%10)
  x = DIV10(x);
  c10   =  (  x - (DIV10(x) * 10)  );  // '0'+(d%10)
  x = DIV10(x);
  c100  =  (  x - (DIV10(x) * 10)  );  // '0'+(d%10)
  c1000 =   DIV10(x);                  // '0'+(d/10)



  // 画面表示をクリア
  display.fillScreen(BLACK);



  //ビットマップの表示
  if(uuj >= 0 && uuj <=9) {
    display.drawBitmap(32+16, 0, databytes + ( c1 * (32 / 8) * 32), 32, 32,  WHITE);
  }//if


  //ビットマップの表示
  if(uuj >= 10 && uuj <=99) {
    display.drawBitmap(32, 0, databytes + ( c10 * (32 / 8) * 32), 32, 32,  WHITE);
    display.drawBitmap(64, 0, databytes + (  c1 * (32 / 8) * 32), 32, 32,  WHITE);
  }//if


  //ビットマップの表示
  if(uuj >= 100 && uuj <=999) {
    display.drawBitmap( 0+16, 0, databytes + (c100 * (32 / 8) * 32), 32, 32,  WHITE);
    display.drawBitmap(32+16, 0, databytes + ( c10 * (32 / 8) * 32), 32, 32,  WHITE);
    display.drawBitmap(64+16, 0, databytes + (  c1 * (32 / 8) * 32), 32, 32,  WHITE);
  }//if



  //ビットマップの表示
  if(uuj >= 1000 && uuj <=9999) {
    display.drawBitmap( 0, 0, databytes + (c1000 * (32 / 8) * 32), 32, 32,  WHITE);
    display.drawBitmap(32, 0, databytes + ( c100 * (32 / 8) * 32), 32, 32,  WHITE);
    display.drawBitmap(64, 0, databytes + (  c10 * (32 / 8) * 32), 32, 32,  WHITE);
    display.drawBitmap(96, 0, databytes + (   c1 * (32 / 8) * 32), 32, 32,  WHITE);
  }//if




  // 描画バッファの内容を画面に表示
  display.display();

  delay(1000); //1秒待つ

}//loop




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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?