【Raspberry Pi Pico w】MicroPython I2Cテスト:LM73温度センサー
MicroPythonを使い、I2C LCD(AMQ1602)にLM73の温度センサーの表示テストです。
Arduinoで表示したC言語プログラムをMicroPythonに直すだけなので、 文法にそってI2Cのプログラムを書く内容です。
〜 from machine import Pin, I2C import time # I2C の設定(GPIO16 端子を SDA, GPIO17 端子を SCL として使用、クロック 100kHz) i2c = I2C(0,freq=100000,scl=Pin(17),sda=Pin(16)) # AQM1602 I2C デバイスのアドレス addr=0x3e buf=bytearray(2) # LM73 I2C デバイスのアドレス LM73_addr=0x4c LM73_buf=bytearray(2) # ************************************************************************/ # AQM1602へコマンド送信 # ************************************************************************/ def write_cmd(cmd): buf[0]=0x00 buf[1]=cmd # 7ビットアドレス 0x3e のペリフェラルに2バイトを書き込みます i2c.writeto(addr,buf) # ************************************************************************/ # AQM1602へデータ書き込み # ************************************************************************/ def write_char(char): buf[0]=0x40 buf[1]=char i2c.writeto(addr,buf) # print 関数の名前を変更 def custom_print(str): for c in str: write_char(ord(c)) def LCD_cursor(x,y): #1段目に書き込み*/ if y==0: write_cmd(0x80+x) #2段目に書き込み*/ if y==1: write_cmd(0xc0+x) def LCD_clear(): buf[0]=0x00 buf[1]=0x01 i2c.writeto(addr,buf) time.sleep(0.001) def LCD_home(): buf[0]=0x00 buf[1]=0x02 i2c.writeto(addr,buf) time.sleep(0.001) # ************************************************************************/ # AQM1602の初期化設定 # ************************************************************************/ def LCD_init(): orders = [b'\x38', b'\x39', b'\x14', b'\x73', b'\x56', b'\x6c',b'\x38', b'\x0c', b'\x01'] time.sleep(0.04) for order in orders: # addr のメモリの、ペリフェラルのメモリアドレス 0で,orderを書き込みます i2c.writeto_mem(addr, 0x00, order) time.sleep(0.001) # ************************************************************************/ # LM73の初期化設定 # ************************************************************************/ def LM73_ini(): LM73_buf[0]=0x04 LM73_buf[1]=0x60 i2c.writeto(LM73_addr,LM73_buf) time.sleep(0.001) #ポイント・レジスタを0にしておく(readするだけで温度が読めるようになる) i2c.writeto(LM73_addr,b'\x00') time.sleep(0.001) LCD_init() LCD_clear() LCD_home() LM73_ini() custom_print('Hello Word') while True: time.sleep(0.1) LCD_cursor(0,1) # スレーブアドレス LM73_addr のデバイスから 2バイトを読み出す LM73_buf = i2c.readfrom(LM73_addr,2) #バイトデータを加工 temp = ((LM73_buf[0] << 6) | (LM73_buf[1] >> 2)); #データを結合して14ビットの温度値にする temp=temp* 0.03125; custom_print(str(temp)) time.sleep(0.5) 〜
【実行結果】