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を使おう8~ADC編~

Posted at

TouchGFXを使おう7~ハードウェアボタン編~の続きです。

今回はTouchGFXでADCを使ってみましょう。

TouchGFX覚書

@airpocketさんのまとめが参考になります。

画面の作成

新規プロジェクト を作成し画面上に textArea を追加します。
image.png
TranstationADC Value とし クリック します。
image.png

wildcardの設定

Wildcard1クリック します。
image.png
クリック します。
image.png
Initial Value00000 とします。
0000 だと、桁が見切れてしまうので要注意です。
Use wildcard buffer のチェックを入れます。
Buffer sizeairpocketさん を参考に 16 にします。
image.png
左の Textsクリック し、文字設定を行います。
Typographies を選択し、DefalultWildcard Ranges0-9 にします。
これをしないと0以外の文字は?と表示されます。
image.png
コード生成 します。
スクリーンショット 2025-02-02 143136.png

pin設定

コード生成後、STM32CubeIDEプロジェクト を開きます。
image.png
ADCとしてアナログ入力を行うためのピンを決めます。
PC0,PH2,PA0,PA1,PC2_C,PC3_C が使えるようです。
今回は、PC0使用 します。
image.png
PC0クリック して ADC1_INP10クリック します。
image.png
ADC1クリック し、 IN10 をプルダウンから IN10 Single-ended選択 します。
image.png
image.png
Parameter SettingsResolution を今回は、10bit にします。
image.png
NVIC Interrupt Tableチェック を入れて コード生成 します。
image.png
クロック等でエラーが多発しますが、このまま Yes で進めることができます。
image.png

クロック修正をする

クロックの修正をする場合は PLL2PをPLL3R等にして回避します。
image.png
image.png
コード生成してYesで進みます。
image.png

プログラム

Screen1View.cpp

Screen1View.cppに使用するプログラムを追加します。
最初にシミュレータエラーを吐かない為のコード

#ifndef SIMULATOR
#include "main.h"

を追加します。

adcValueを宣言し、cntr20回毎にreadADCを行います。

TouchGFX\gui\src\screen1_screen\Screen1View.cpp

#ifndef SIMULATOR
#include "stm32h7xx_hal.h"
#include "main.h"
#endif
Screen1View::Screen1View()
{
	adcValue = 0;
}
void Screen1View::handleTickEvent()
{
	cntr++;
	if(cntr >= 20)
	{
		readADC();
		cntr = 0;
	}
}
void Screen1View::readADC()
{
	#ifndef SIMULATOR
	HAL_ADC_Start(&hadc1);
	HAL_ADC_PollForConversion(&hadc1, 100);
	HAL_ADC_Stop(&hadc1);
	adcValue=HAL_ADC_GetValue(&hadc1);
	#endif

	Unicode::snprintf(textArea1Buffer, TEXTAREA1_SIZE, "%5d", adcValue);
	textArea1.invalidate();
}

image.png

Screen1View.hpp

Screen1View.hppに関数の宣言と初期値を設定します。
TouchGFX\gui\src\screen1_screen\Screen1View.hpp

public:
    Screen1View();
    virtual ~Screen1View() {}
    virtual void setupScreen();
    virtual void tearDownScreen();
    virtual void handleTickEvent();
    void readADC();

protected:
    uint16_t cntr = 0;
    uint16_t adcValue;

image.png

main.h

main.hにextern文を追記します。

/* USER CODE BEGIN Includes */
extern ADC_HandleTypeDef hadc1;
/* USER CODE END Includes */

image.png
以上でコンパイル&書込みすることでPC0に電圧を入力すると0~1023の間の値が表示されます。

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?