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?

TouchGFXを使おう9~I2C編~

Posted at

TouchGFXを使おう8~ADC編~の続きです。

I2Cの温湿度センサーから温度データを表示してみましょう。
TouchGFXを使おう8~ADC編~とSTM32CubeIDEを使ってみよう How To STM32CubeIDE 日本語版 (9) I2Cを使ってみよう2 si7020編を参考に進めます。

画面の作成

新規プロジェクトを作成し、画面上に温度を表示するテキストエリアを作成します。
TranstationTemp ° にし クリック します。
image.png

wildcardの設定

Wildcard1クリック します。
クリック します。
image.png
Initial Value00.0 とします。
Buffer size12 にしました。
image.png
左の Textsクリック し、文字設定を行います。
Typographies を選択し、LargeWildcard Ranges0-9 にします。
image.png

pin設定

I2CはPF14,PF15を使用します。
image.png
image.png
image.png
Connectivity⇒I2C4⇔ModeI2C にします。
黄色の〇が緑色になります。
image.png
image.png
image.png
Yesクリック します。
image.png

プログラム

TouchGFXではプロジェクトが変更されたことが表示されます。
Yesクリック します。
image.png

Screen1View.cpp

ヘッダーにはI2Cのアドレス、インクルードするヘッダーファイルを追加します。

#ifndef SIMULATOR
#include "stm32h7xx_hal.h"
#include "stm32h7xx_hal_i2c.h"

#include "main.h"

static const uint16_t si7020_ADDR = 0x40<< 1;
#endif
uint8_t reg[2];
uint8_t tmp[2];

float temp;
uint8_t utemp[12];
float tempd;

image.png

プログラムはtickCounterが300毎にI2Cのデータを読み出し、textArea1Bufferを書き換え
textArea1.invalidateで書き換えます。
void Screen1View::handleTickEvent()
{
    tickCounter++;

	if(tickCounter >= 300)
	{
		tickCounter = 0;
		#ifndef SIMULATOR
	    uint8_t rego =0xE3;
	    HAL_I2C_Master_Transmit(&hi2c4, si7020_ADDR,&rego,1, 1000);
	    HAL_I2C_Master_Receive(&hi2c4, si7020_ADDR,tmp,2,1000 );
	    temp = ((((tmp[0]  * 256 + tmp[1])* 175.72) / 65536.0) - 46.85);
	    Unicode::snprintfFloat(textArea1Buffer, TEXTAREA1_SIZE, "%.1f", temp);
	    #endif
	    textArea1.invalidate();
	}
}

image.png

Screen1View.hpp

使用する関数を宣言します。

public:
    virtual void handleTickEvent();
protected:
    uint16_t tickCounter;

image.png

main.h

main.hにexternを追加します。

/* USER CODE BEGIN Includes */
extern I2C_HandleTypeDef hi2c4;
/* USER CODE END Includes */

image.png

以上でコンパイル&書込みすることで、温度データを画面に表示するようになります。

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?