電池駆動のデータロガーを作ってみる(2) — RTC周りをmicroPythonで動作確認

部品一式揃ったので、まずはRTC周りの動作確認

パーツだけは載せてみた、配線はまだ終わってないけどRTC動作確認はできる状態

VScodeでのmicroPython使い方としては以下を参考

https://kousaku-prog.com/vscode-micropython/

・拡張機能(MicroPico:VScodeの拡張機能として最も一般的)をインスト、実際はインスト済みであった

・プロジェクト初期化(メニューから以下の画像の機能を選択)して、以下から持ってきたmicroPythonのサンプルプロジェクトを置いとく

% wget -P ~/pico https://files.waveshare.com/upload/5/5a/Pico-rtc-ds3231_code.zip

スクリーンショット 2025-09-12 17.06.15.png

・microPythonのファーム(2025.8.9版)をダウンロードしてきて、ディスクモードでラズピコに書き込むと接続状態になる(VScode下端のステータス表示)

スクリーンショット 2025-09-12 17.11.50.png

・VScodeのターミナルでrunをクリックすると実行される

スクリーンショット 2025-09-12 17.14.36.png

日時はPythonソースで設定されているだけ

・割り込み処理の受け側がないのでそれを追加、同時に割り込みが配線されてないからハードも修正(汚いけどR5相当をはんだブリッジした、これでGPIO3番につながる)

R5相当をジャンパーしたところ

jumper.jpg

コード(set_alarm_time())も元のコードでは割り込み待ち処理ないし、割り込み要因も適切じゃないので修正必要で、それは生成A.Iで修正、

#!/usr/bin/python
# -*- coding: utf-8 -*-
from machine import Pin, I2C
import time
import binascii

#    the first version use i2c1
#I2C_PORT = 1
#I2C_SDA = 6
#I2C_SCL = 7

#    the new version use i2c0,if it dont work,try to uncomment the line 14 and comment line 17
#    it should solder the R3 with 0R resistor if want to use alarm function,please refer to the Sch file on waveshare Pico-RTC-DS3231 wiki
#    https://www.waveshare.net/w/upload/0/08/Pico-RTC-DS3231_Sch.pdf
I2C_PORT = 0
I2C_SDA = 20
I2C_SCL = 21

ALARM_PIN = 3


class ds3231(object):
#            13:45:00 Mon 24 May 2021
#  the register value is the binary-coded decimal (BCD) format
#               sec min hour week day month year
    NowTime = b'\x00\x45\x13\x02\x24\x05\x21'
    w  = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    address = 0x68
    start_reg = 0x00
    alarm1_reg = 0x07
    control_reg = 0x0e
    status_reg = 0x0f
    
    def __init__(self,i2c_port,i2c_scl,i2c_sda):
        self.bus = I2C(i2c_port,scl=Pin(i2c_scl),sda=Pin(i2c_sda))

    def set_time(self,new_time):
        hour = new_time[0] + new_time[1]
        minute = new_time[3] + new_time[4]
        second = new_time[6] + new_time[7]
        week = "0" + str(self.w.index(new_time.split(",",2)[1])+1)
        year = new_time.split(",",2)[2][2] + new_time.split(",",2)[2][3]
        month = new_time.split(",",2)[2][5] + new_time.split(",",2)[2][6]
        day = new_time.split(",",2)[2][8] + new_time.split(",",2)[2][9]
        now_time = binascii.unhexlify((second + " " + minute + " " + hour + " " + week + " " + day + " " + month + " " + year).replace(' ',''))
        #print(binascii.unhexlify((second + " " + minute + " " + hour + " " + week + " " + day + " " + month + " " + year).replace(' ','')))
        #print(self.NowTime)
        self.bus.writeto_mem(int(self.address),int(self.start_reg),now_time)
    
    def read_time(self):
        t = self.bus.readfrom_mem(int(self.address),int(self.start_reg),7)
        a = t[0]&0x7F  #second
        b = t[1]&0x7F  #minute
        c = t[2]&0x3F  #hour
        d = t[3]&0x07  #week
        e = t[4]&0x3F  #day
        f = t[5]&0x1F  #month
        print("20%x/%02x/%02x %02x:%02x:%02x %s" %(t[6],t[5],t[4],t[2],t[1],t[0],self.w[t[3]-1]))

    def set_alarm_time(self, alarm_time):
        self.alarm_pin = Pin(ALARM_PIN, Pin.IN, Pin.PULL_UP)
        self.alarm_pin.irq(lambda pin: print("alarm1 time is up"), Pin.IRQ_FALLING)

    # ステータスフラグクリア
        status = self.bus.readfrom_mem(self.address, self.status_reg, 1)
        self.bus.writeto_mem(self.address, self.status_reg, bytes([status[0] & 0xFE]))

    # コントロールレジスタ設定 (INTCN=1, A1IE=1)
        self.bus.writeto_mem(self.address, self.control_reg, b'\x07')

    # アラーム時刻設定
        hour = alarm_time[0] + alarm_time[1]
        minute = alarm_time[3] + alarm_time[4]
        second = alarm_time[6] + alarm_time[7]
        date = alarm_time.split(",", 2)[2][8] + alarm_time.split(",", 2)[2][9]
        now_time = binascii.unhexlify((second + minute + hour + date).replace(' ', ''))
        self.bus.writeto_mem(self.address, self.alarm1_reg, now_time)
        
if __name__ == '__main__':
    rtc = ds3231(I2C_PORT,I2C_SCL,I2C_SDA)
    rtc.set_time('17:18:00,Friday,2025-09-12')
    rtc.read_time()
    rtc.set_alarm_time('17:18:10,Friday,2025-09-12')
    
    try:
        while True:
            time.sleep(1)  
    except KeyboardInterrupt:
        print("terminated")

割り込み処理が実行できました、

 

<今の回路図>

前回から手直し、特にRTC周りをオプションの回路図から書き写し

 

microPythonではdeepsleepとかちゃんと動かないらしいので、microPythonはプロトタイピングで本番はpico-sdkになるかな

 

admin

コメントを残す