2021年1月18日 星期一

SPI串列通訊實作Part3 – MAX7219 7段LED顯示RTC

 SPI串列通訊實作Part3 – MAX7219 7段LED顯示RTC

 

電路圖

 

  

程式列表

from machine import KEY

from machine import SPI, PIN

from machine import RTC

import max7219_8digit

import utime

 

RTC_DONE = None

count  = None

x = None

 

# Connections:

# EPY    SPI0  SPI1     MAX7219

# 3V3                    VCC

# GND                    GND

# MOSI    P15   P26  ->  DIN

# CS      P16   P23  ->  CS

# CLK     P13   P24  ->  CLK

# MISO    P14   P25  ->  DOUT

 

spi_1  = None

spi_1 =SPI(1, mode=SPI.MASTER, baudrate=5000000, polarity=1, phase=0) #For Hardware SPI bus

  # baudrate is the SCK clock rate.

  # polarity can be 0 or 1, and is the level the idle clock line sits at.

  # phase can be 0 or 1 to sample data on the first or second clock edge respectively.

  # bits is the width in bits of each transfer. Only 8 is guaranteed to be supported by all hardware.

  # firstbit can be SPI.MSB or SPI.LSB.

 

def RTCINT(x):

  global RTC_DONE

  RTC_DONE = True

 

def KEYDINT(x):      #Press D Key

  global count

  if count == 0:

    count = 1

  else:

    count = 0

 

# Start Function

if __name__ == '__main__':

  key_c = KEY(KEY.KEYC)

  key_d = KEY(KEY.KEYD)

  key_d.irq(trigger = PIN.IRQ_FALLING, handler = KEYDINT)

  count = 0

 

  display = max7219_8digit.Display7seg(spi_1)

  display.write_to_buffer('EPYTHON')

  display.show()

  utime.sleep_ms(1000)

 

  rtc = RTC()

  rtc.init(datetime=(2021,1,17,12,0,0))

  rtc.alarm(time = 1,repeat = True)

  rtc.irq(trigger = RTC.ALARM0,handler = RTCINT)

 

  while True:

    if RTC_DONE == True :

      (year, month, date, hour, minute, second, none1, none2) = rtc.now()

      if count == 0:

        RTCNUM= hour*10000 + minute*100 + second

      else:

        RTCNUM= year*10000 + month*100 + date

 

      display.write_number(RTCNUM)

      display.show()

      str1 = str(year) + '-' + str(month) + '-' + str(date) + ' ' + str(hour) + ':' + str(minute) + ':' + str(second)

      print(str1)

      RTC_DONE = False

 

    if key_c.value() == 0:      #Press C Key

      break

 

key_c.deinit()

spi_1.deinit()

rtc.deinit()

 

執行結果

 https://youtu.be/LEC44dZABHo

 

沒有留言:

張貼留言