2020年8月1日 星期六

I2C串列通訊實作Part3 – HY2613 LCD顯示器驅動控制器


  • HY2613_Light.py

from micropython import const

from machine import KEY  #獲取按鍵KEY

from machine import ADC

from machine import I2C, PIN

import utime

import LcdTable

import HY2613_Reg

#import framebuf

 

light = ADC(4)

i2c_1 = None

addrHY2613 = const(0x3E)   # HY2613 Device ID address for slave target

temp = []

DisplayBuffer = bytearray(9)

NUM_DIGITS          = 6

 

def Init_Display():

  # Inital the LCD Drive

  temp.clear()

  temp.append(0xEA) #(ICSET|SWRst|OscModeInt)

  temp.append(0xBF) #(DISCTL|PoMode3|FrInv|PoHigh)

  temp.append(0x00) #(ADSET)

  temp.append(0x00) #(ADSET)

  i2c_1.write(1,addrHY2613,temp,4,1)

 

  # Open the LCD Drive

  temp.clear()

  temp.append(0xBF) #DISCTL|PoMode3|FrInv|PoHigh

  temp.append(0xF0) #BLKCTL

  temp.append(0xFC) #PIXCTL

  temp.append(0xC8) #MODE_SET|Dis_ON

  i2c_1.write(1,addrHY2613,temp,4,1)

 

# RAM Data Send to LCD

def RAM2LCD(Buffer, length):

  temp.clear()

  temp.append(0xBF)   #DISCTL|PoMode3|FrInv|PoHigh

  temp.append(0xF0)   #BLKCTL

  temp.append(0xFC)   #PIXCTL

  temp.append(0xC8)   #MODE_SET|Dis_ON

  temp.append(0x00)   #ADSET

  for i in range(0,length) :

    data = Buffer[i]

    temp.append(data)

  i2c_1.write(1,addrHY2613,temp,length+5,1)

 

def FullLCDframe(data):

  temp.clear()

  temp.append(0xBF)   #DISCTL|PoMode3|FrInv|PoHigh

  temp.append(0xF0)   #BLKCTL

  temp.append(0xFC)   #PIXCTL

  temp.append(0xC8)   #MODE_SET|Dis_ON

  temp.append(0x00)   #ADSET

  for i in range(0,18) :

    temp.append(data)

  i2c_1.write(1,addrHY2613,temp,23,1)

 

def DisplayHYcon():

  DisplayBuffer[0]=0x00

  DisplayBuffer[1]=LcdTable.char_map.get('h')

  DisplayBuffer[2]=LcdTable.char_map.get('y')

  DisplayBuffer[3]=LcdTable.char_map.get('c')

  DisplayBuffer[4]=LcdTable.char_map.get('o')

  DisplayBuffer[5]=LcdTable.char_map.get('n')

  DisplayBuffer[6]=0x00

  DisplayBuffer[7]=0x00

  DisplayBuffer[8]=0x00

  RAM2LCD(DisplayBuffer,9)

 

def DisplayEASYPY():

  DisplayBuffer[0]=LcdTable.char_map.get('e')

  DisplayBuffer[1]=LcdTable.char_map.get('a')

  DisplayBuffer[2]=LcdTable.char_map.get('s')

  DisplayBuffer[3]=LcdTable.char_map.get('y')

  DisplayBuffer[4]=LcdTable.char_map.get('p')

  DisplayBuffer[5]=LcdTable.char_map.get('y')

  DisplayBuffer[6]=0x00

  DisplayBuffer[7]=0x00

  DisplayBuffer[8]=0x00

  RAM2LCD(DisplayBuffer,9)

 

def DisplayData(inValue):

  if len(str(inValue)) > NUM_DIGITS:

    raise OverflowError('{0} too large for display'.format(inValue))

 

  for i in range(5,-1,-1) :

    outValue=inValue%10

    inValue=inValue//10

    value = LcdTable.char_map.get(str(outValue))

    DisplayBuffer[i]=value

 

  DisplayBuffer[6]=0x00

  DisplayBuffer[7]=0x00

  DisplayBuffer[8]=0x00

  RAM2LCD(DisplayBuffer,9)

 

# Start Function

if __name__ == '__main__':

  #temp = bytearray(5)

  # Declaration I2C

  i2c_1 = I2C(1,100000)      #創建頻率為100kHzI2C外設,選擇要使用的外設I2C 1

 

  Init_Display()

  FullLCDframe(0xFF)

  utime.sleep_ms(1000)

  FullLCDframe(0x00)

 

  DisplayEASYPY()

  utime.sleep_ms(2000)

 

  C = KEY(KEY.KEYC)    #創建按鍵C

  try:

    while True:

      L = light.read()

      DisplayData(L)

      utime.sleep_ms(400)

      if C.value() == 0:      #如果按鍵C被按下

        break

 

  finally:

    light.deinit()

 

  i2c_1.deinit()

 

  • HY2613_Reg.py

#******************************************************************************/

#* Filename:HY2613_Reg.py        */

#* HY2613 Register Definitions   */

#******************************************************************************/

 

from micropython import const

 

#define HY2613C

addrHY2613 = const(0x3E)   # HY2613 Device ID address for slave target

 

#Register List

MODE_SET = const(0xC0) #Mode Set

Dis_ON = const(0x08) #Display ON

Bias3  = const(0x00) #1/3 Bias

Bias2  = const(0x04) #1/2 Bias

 

ADSET = const(0x00)    #Address set

 

DISCTL = const(0xA0)   #Display control

#Power save mode FR

PoNormalF = const(0x00) #Normal mode

PoMode1   = const(0x08) #Power save mode1

PoMode2   = const(0x10) #Power save mode2

PoMode3   = const(0x18) #Power save mode3

 

LiInv    = const(0x00) #LINE Inversion

FrInv    = const(0x04) #FRAME Inversion

 

#Power save mode SR

PoSvMode1 = const(0x00) #Power save mode 1

PoSvMode2 = const(0x01) #Power save mode 2

PoNormalS = const(0x02) #Normal

PoHigh    = const(0x03) #High Power Mode

 

ICSET = const(0xE8)   #Set IC Operation

SWRst = const(0x02) #Software Reset

 

 

OscModeInt = const(0x00) #Internal Oscillator

OscModeExt = const(0x01) #External Oscillator

 

BLKCTL = const(0xF0)  #Blink control

#Flash Mode Setting

FlashOff = const(0x00) #Off Flash Mode

Flash0p5 = const(0x01) #Flash 0.5HZ

Flash1   = const(0x02) #Flash 1HZ

Flash2   = const(0x03) #Flash 2HZ

 

PIXCTL = const(0xFC)  #ALL pixel control

PixNormal   = const(0x00)

PixAllOff   = const(0x01)  #All pixels OFF:取消與DDRAM 的內容無關的全屏顯示。

PixAllOn    = const(0x02)  #All pixels ON:點亮與DDRAM 的內容無關的全屏顯示。

#PixAllOff   = const(0x03

 

EXCTL = const(0xE0)  #Extend Control

LightOff = const(0x00)  #LED背光 OFF:不啟用LED背光功能

LightOn  = const(0x04)  #LED背光 ON:啟用LED背光功能,此時SEG32~SEG35 自動切換成LED驅動電路

  

RTCOff   = const(0x00)  #RTC OFF:外部32KHz時鐘輸入。

RTCOn    = const(0x02)  #RTC ON:使用外接震盪器32.768KHz

  

SROff    = const(0x00)  #SROFF:不啟用特殊暫存器功能

SROn     = const(0x01)  #SRON:啟用特殊暫存器功能

 

#Extend Control Register

VLCD_CTVR = const(0x00)  #

#VLCD[3:0] Internal Charge pump output control register can configure VLCD output voltage.

VLCD4p5  = const(0x00)  #0000 4.5V

VLCD4p4  = const(0x10)  #0001 4.4V

VLCD4p3  = const(0x20)  #0010 4.3V

VLCD4p2  = const(0x30)  #0011 4.2V

VLCD4p1  = const(0x40)  #0100 4.1V

VLCD4p0  = const(0x50)  #0101 4.0V

VLCD3p9  = const(0x60)  #0110 3.9V

VLCD3p8  = const(0x70)  #0111 3.8V

VLCD3p7  = const(0x80)  #1000 3.7V

VLCD3p6  = const(0x90)  #1001 3.6V

VLCD3p5  = const(0xA0)  #1010 3.5V

VLCD3p4  = const(0xB0)  #1011 3.4V

VLCD3p3  = const(0xC0)  #1100 3.3V

VLCD3p2  = const(0xD0)  #1101 3.2V

VLCD3p1  = const(0xE0)  #1110 3.1V

VLCD3p0  = const(0xF0)  #1111 3.0V

 

#CTVR[3:0] Internal constrast control register can adjust LCD display contrast for the best display effect.

CTVR00  = const(0x00)  #0000 1*VLCD

CTVR01  = const(0x01)  #0001 0.957*VLCD

CTVR02  = const(0x02)  #0010 0.918*VLCD

CTVR03  = const(0x03)  #0011 0.882*VLCD

CTVR04  = const(0x04)  #0100 0.849*VLCD

CTVR05  = const(0x05)  #0101 0.818*VLCD

CTVR06  = const(0x06)  #0110 0.789*VLCD

CTVR07  = const(0x07)  #0111 0.763*VLCD

CTVR08  = const(0x08)  #1000 0.738*VLCD

CTVR09  = const(0x09)  #1001 0.714*VLCD

CTVR10  = const(0x0A)  #1010 0.692*VLCD

CTVR11  = const(0x0B)  #1011 0.672*VLCD

CTVR12  = const(0x0C)  #1100 0.652*VLCD

CTVR13  = const(0x0D)  #1101 0.634*VLCD

CTVR14  = const(0x0E)  #1110 0.616*VLCD

CTVR15  = const(0x0F)  #1111 0.600*VLCD

 

LCDM = const(0x02)  #

ENBUF     = const(0x80)  #VLCD Buffer enable bit

 

#VLCDEN[1:0] VLCD power drive controller can select VLCD disposition to satisfy different applications.

VLCDEN00  = const(0x00)  #00 VLCD連接至VSS,採用Low Side連接方式

VLCDEN01  = const(0x01)  #01 VLCD連接至VDD,採用High Side連接方式

VLCDEN02  = const(0x02)  #10 VLCD電壓由內部倍壓電路提供,使用內部倍壓電路,即採用High Side連接方式

VLCDEN03  = const(0x03)  #11 VLCD電壓由內部倍壓電路提供,使用內部倍壓電路,即採用High Side連接方式

 

TM = const(0x04)  #

IRQM  = const(0x80)

 

#TMD[2:0] Counter frequency divider control register, which can configure counter output frequency.

TMD_OSCd2   = const(0x00)  #000  OSC_CK÷2

TMD_OSCd4   = const(0x10)  #001  OSC_CK÷4

TMD_OSCd8   = const(0x20)  #010  OSC_CK÷8

TMD_OSCd16  = const(0x30)  #011  OSC_CK÷16

TMD_OSCd32  = const(0x40)  #100  OSC_CK÷32

TMD_OSCd64  = const(0x50)  #101  OSC_CK÷64

TMD_OSCd128 = const(0x60)  #110  OSC_CK÷128

TMD_OSCd256 = const(0x70)  #111  OSC_CK÷256

 

#TMEN[1:0] Counter enable and mode control register

TMEN0 = const(0x00)  #00  禁用計數器

TMEN1 = const(0x02)  #01  致能計數器並設置為Timer Base輸出

TMEN2 = const(0x04)  #10  致能計數器並設置為NMI 脈衝高電平輸出

TMEN3 = const(0x06)  #11  致能計數器並設置為NMI 脈衝低電平輸出

 

#TMCR:Counter zero control register.

TMCR = const(0x00)  #0:Normal counting status

  #1:Counter Zero

 

  • LcdTable.py

#from micropython import const

 

LCD_COM3H = 0x01

LCD_COM2H = 0x02

LCD_COM1H = 0x04

LCD_COM0H = 0x08

LCD_COM3L = 0x10

LCD_COM2L = 0x20

LCD_COM1L = 0x40

LCD_COM0L = 0x80

 

seg_h = LCD_COM3H

seg_c = LCD_COM2H

seg_b = LCD_COM1H

seg_a = LCD_COM0H

seg_d = LCD_COM3L

seg_e = LCD_COM2L

seg_g = LCD_COM1L

seg_f = LCD_COM0L

 

char_map = {

    '0': seg_a+seg_b+seg_c+seg_d+seg_e+seg_f, # char "0"  0x00

    '1': seg_b+seg_c,                         # char "1"  0x01

    '2': seg_a+seg_b+seg_d+seg_e+seg_g,       # char "2"  0x02

    '3': seg_a+seg_b+seg_c+seg_d+seg_g,       # char "3"  0x03

    '4': seg_b+seg_c+seg_f+seg_g,             # char "4"  0x04

    '5': seg_a+seg_c+seg_d+seg_f+seg_g,       # char "5"  0x05

    '6': seg_a+seg_c+seg_d+seg_e+seg_f+seg_g, # char "6"  0x06

    '7': seg_a+seg_b+seg_c+seg_f,             # char "7"  0x07

    '8': seg_a+seg_b+seg_c+seg_d+seg_e+seg_f+seg_g,    # char "8"  0x08

    '9': seg_a+seg_b+seg_c+seg_d+seg_f+seg_g, # char "9"  0x09

    'a': seg_a+seg_b+seg_c+seg_e+seg_f+seg_g, # char "A"  0x0a

    'b': seg_c+seg_d+seg_e+seg_f+seg_g,       # char "b"  0x0b

    'c': seg_a+seg_d+seg_e+seg_f,             # char "C"  0x0c

    'd': seg_b+seg_c+seg_d+seg_e+seg_g,       # char "d"  0x0d

    'e': seg_a+seg_d+seg_e+seg_f+seg_g,       # char "E"  0x0e

    'f': seg_a+seg_e+seg_f+seg_g,             # char "F"  0x0f

    'g': 0x00,

    'h': seg_b+seg_c+seg_e+seg_f+seg_g,       # char "H"  0x10

    'i': seg_c,                               # char "i"  0x11

    'j': seg_b+seg_c+seg_d+seg_g,             # char "J"  0x12

    'k': 0x00,

    'l': seg_d+seg_e+seg_f,                   # char "L"  0x13

    'm': 0x00,

    'n': seg_c+seg_e+seg_g,                   # char "n"  0x14

    'o': seg_c+seg_d+seg_e+seg_g,             # char "o"  0x15

    'p': seg_a+seg_b+seg_e+seg_f+seg_g,       # char "P"  0x16

    'q': seg_a+seg_b+seg_c+seg_f+seg_g,       # char "q"  0x17

    'r': seg_e+seg_g,                         # char "r"  0x18

    's': seg_a+seg_c+seg_d+seg_f+seg_g,       # char "S"  0x05

    't': seg_d+seg_e+seg_f+seg_g,             # char "t"  0x19

    'u': seg_c+seg_e+seg_d,                   # char "u"  0x1a

    'v': 0x00,

    'w': 0x00,

    'x': 0x00,

    'y': seg_b+seg_c+seg_d+seg_f+seg_g,        # char "y"  0x1b

    'z': 0x00

}

沒有留言:

張貼留言