x 原因は、わからないが液晶がちらつく場合は、ウェートを調整してね!!
目的
I2C液晶のテスト
STM32G031とI2Cスレーブでマスターから送信された文字を液晶に表示(ACM1602K-NLW-BBW)
STM32L010とI2Cスレーブでマスターから送信された文字を液晶に表示(Mbed)(ACM1602K-NLW-BBW)
メインプログラム
//I2C_LCD_NS_010_1
#include "mbed.h"
#include "I2C_LCD_010_1.h"
//メイン関数
int main()
{
lcd.begin(16, 2);
lcd.clear();
//カウンター
unsigned char LL=0;
//無限ループ
while(1) {
//lcd.clear();
//文字列の表示
lcd.setCursor(0, 0);
lcd.print("HELLO WORLD");
//4桁の値の表示
lcd.setCursor(0, 1);
lcd.print( (int)(LL++) );
lcd.print( " " ); //ごみを消す
//char buf_o[16];
//sprintf(buf_o,"%d",12345);
//lcd.print( buf_o );
//1秒の待ち
wait_ms(1000);
} //while
} //main
//容量削減
void error(const char* format, ...) {}
I2C_LCD_010_1.hのプログラム
#include "mbed.h"
#define ADDR (0x80) //address
I2C i2c(PA_10, PA_9); //010
//10の割り算 0から1028までは、正しい。主に0から999
#define DIV10(n) ((n*205)>>11)
char ch_hex_a_b[16];
char *ch_hex_a(int x)
{
ch_hex_a_b[4] = 0;
if ( x >= 9000 ) {x = x - 9000; ch_hex_a_b[0] = '9';
} else if( x >= 8000 ) {x = x - 8000; ch_hex_a_b[0] = '8';
} else if( x >= 7000 ) {x = x - 7000; ch_hex_a_b[0] = '7';
} else if( x >= 6000 ) {x = x - 6000; ch_hex_a_b[0] = '6';
} else if( x >= 5000 ) {x = x - 5000; ch_hex_a_b[0] = '5';
} else if( x >= 4000 ) {x = x - 4000; ch_hex_a_b[0] = '4';
} else if( x >= 3000 ) {x = x - 3000; ch_hex_a_b[0] = '3';
} else if( x >= 2000 ) {x = x - 2000; ch_hex_a_b[0] = '2';
} else if( x >= 1000 ) {x = x - 1000; ch_hex_a_b[0] = '1';
} else { ch_hex_a_b[0] = '0';
}//if
ch_hex_a_b[3] = '0' + ( x - (DIV10(x) * 10) ); // 3 <- 120 - 123
x = DIV10(x); // 12 <= 123 / 10
ch_hex_a_b[2] = '0' + ( x - (DIV10(x) * 10) ); // 2 <- 12 - 10
ch_hex_a_b[1] = '0' + DIV10(x); // 1 <- 12 / 10
return(ch_hex_a_b);
} //ch_hex_a
//クラスの定義
struct _lcd {
void begin(int col, int rows); //メソッドの宣言
void clear(); //メソッドの宣言
void setCursor(int col, int rows); //メソッドの宣言
int write(char ch); //メソッドの宣言
int print(char *str1); //メソッドの宣言
int print(int num); //メソッドの宣言 オーバーロード
};
//ポートをhiにする 初期化
//メソッドの定義
void _lcd::begin(int col, int rows)
{
wait_ms(3000);
}//begin
//ポートをhiにする 初期化
//メソッドの定義
void _lcd::clear()
{
i2c.write(ADDR, "\000", 1);
wait_ms(1);//1mS待つ 連続送信防止用 受信落ち対策
for(int ii=0; ii<16; ii++) {
i2c.write(ADDR, " ", 1);
wait_ms(1);//1mS待つ 連続送信防止用 受信落ち対策
}//for
//char ch = 0x10;
i2c.write(ADDR, "\020", 1);
wait_ms(1);//1mS待つ 連続送信防止用 受信落ち対策
for(int ii=0; ii<16; ii++) {
i2c.write(ADDR, " ", 1);
wait_ms(1);//1mS待つ 連続送信防止用 受信落ち対策
}//for
i2c.write(ADDR, "\000", 1);
wait_ms(1);//1mS待つ 連続送信防止用 受信落ち対策
}//begin
//ポートをhiにする 初期化
//メソッドの定義
void _lcd::setCursor(int col, int rows)
{
write((rows * 0x10)+col);
wait_ms(1);
}//begin
//液晶への一文字出力
//メソッドの定義
int _lcd::write(char ch)
{
i2c.write(ADDR, &ch, 1);
wait_ms(50);//50mS待つ 連続送信防止用 受信落ち対策
return(0);
}//write
//文字列の表示
//メソッドの定義
int _lcd::print(char *str1)
{
//文字の中身がゼロか
while(*str1) {
//一文字出力
write(*str1 ++);
} //while
//戻り値
return(0);
}//print
//文字列の表示 オーバーロード 4桁
//メソッドの定義
int _lcd::print(int num)
{
if(num < 0) {
write('-');
num = 0 - num;
}
// 0123
// [1234]
char *str1;
if ( num >= 1000 ) {
str1 = ch_hex_a(num);
} else if ( num >= 100 ) {
str1 = ch_hex_a(num) + 1;
} else if ( num >= 10 ) {
str1 = ch_hex_a(num) + 2;
} else {
str1 = ch_hex_a(num) + 3;
}
//文字の中身がゼロか
while(*str1) {
//一文字出力
write(*str1 ++);
} //while
//戻り値
return(0);
}//print
//実体の作成
_lcd lcd;