module:micropython:esp32lcdisplayi2c:esp32lcdisplayi2c
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
module:micropython:esp32lcdisplayi2c:esp32lcdisplayi2c [2021/12/16 11:16] – [Version] omdevelop | module:micropython:esp32lcdisplayi2c:esp32lcdisplayi2c [2022/09/13 11:58] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 9: | Line 9: | ||
===== Übersicht ===== | ===== Übersicht ===== | ||
+ | * Esp32-Ansteuerung eines LCDisplays mit I2C-Interface(PCF8574) | ||
+ | * das LCDisplay besitzt 4 Zeilen und 20 Spalten | ||
==== Links ==== | ==== Links ==== | ||
* [[https:// | * [[https:// | ||
- | ==== ==== | + | ==== Quell-Code==== |
+ | **Main-Program: | ||
+ | <code python> | ||
+ | # | ||
+ | import time | ||
+ | import machine | ||
+ | import LCDisplayI2C | ||
+ | # | ||
+ | I2CADDRESS_LCDISPLAY_T | ||
+ | I2CADDRESS_LCDISPLAY_A | ||
+ | # | ||
+ | LCDISPLAY_COUNT_ROWS | ||
+ | LCDISPLAY_COUNT_COLUMNS = 20 | ||
+ | # | ||
+ | def TestLoop(): | ||
+ | for S in range(0, 10): | ||
+ | LCDisplay.MoveTo(0, | ||
+ | LCDisplay.PutText(' | ||
+ | LCDisplay.MoveTo(1, | ||
+ | LCDisplay.PutText(' | ||
+ | LCDisplay.MoveTo(2, | ||
+ | LCDisplay.PutText(' | ||
+ | LCDisplay.MoveTo(3, | ||
+ | LCDisplay.PutText(' | ||
+ | time.sleep_ms(2000) | ||
+ | LCDisplay.Clear() | ||
+ | for P in range(0, 30): | ||
+ | LCDisplay.MoveTo(0, | ||
+ | LCDisplay.PutText(' | ||
+ | LCDisplay.MoveTo(1, | ||
+ | LCDisplay.PutText(' | ||
+ | LCDisplay.MoveTo(2, | ||
+ | LCDisplay.PutText(' | ||
+ | LCDisplay.MoveTo(3, | ||
+ | LCDisplay.PutText(' | ||
+ | # | ||
+ | # | ||
+ | if ' | ||
+ | print(' | ||
+ | # | ||
+ | I2CDisplay = machine.SoftI2C(scl=machine.Pin(22), | ||
+ | LCDisplay = LCDisplayI2C.CLCDisplayI2C(I2CDisplay, | ||
+ | | ||
+ | # | ||
+ | TestLoop() | ||
+ | # | ||
+ | print(' | ||
+ | # | ||
+ | # | ||
+ | </ | ||
+ | |||
+ | **Library-Module: | ||
+ | <code python> | ||
+ | # | ||
+ | """ | ||
+ | # | ||
+ | from LCDisplay import CLCDisplay | ||
+ | from time import sleep_ms | ||
+ | # | ||
+ | # The PCF8574 has a jumper selectable address: 0x20 - 0x27 | ||
+ | DEFAULT_I2C_ADDR = 0x27 | ||
+ | # | ||
+ | MASK_RS = 0x01 | ||
+ | MASK_RW = 0x02 | ||
+ | MASK_E = 0x04 | ||
+ | SHIFT_BACKLIGHT = 3 | ||
+ | SHIFT_DATA = 4 | ||
+ | # | ||
+ | class CLCDisplayI2C(CLCDisplay): | ||
+ | # | ||
+ | def __init__(self, | ||
+ | self.I2C = i2c | ||
+ | self.I2CAddress = i2caddress | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | sleep_ms(20) | ||
+ | # Send reset 3 times | ||
+ | self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) | ||
+ | sleep_ms(5) | ||
+ | self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) | ||
+ | sleep_ms(1) | ||
+ | self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) | ||
+ | sleep_ms(1) | ||
+ | # Put LCD into 4 bit mode | ||
+ | self.hal_write_init_nibble(self.LCD_FUNCTION) | ||
+ | sleep_ms(1) | ||
+ | CLCDisplay.__init__(self, | ||
+ | cmd = self.LCD_FUNCTION | ||
+ | if countrows > 1: | ||
+ | cmd |= self.LCD_FUNCTION_2LINES | ||
+ | self.hal_write_command(cmd) | ||
+ | # | ||
+ | def hal_write_init_nibble(self, | ||
+ | byte = ((nibble >> 4) & 0x0f) << SHIFT_DATA | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | # | ||
+ | def hal_backlight_on(self): | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | # | ||
+ | def hal_backlight_off(self): | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | # | ||
+ | def hal_write_command(self, | ||
+ | """ | ||
+ | byte = ((self.Backlight << SHIFT_BACKLIGHT) | (((cmd >> 4) & 0x0f) << SHIFT_DATA)) | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | byte = ((self.Backlight << SHIFT_BACKLIGHT) | ((cmd & 0x0f) << SHIFT_DATA)) | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | if cmd <= 3: # The home and clear commands require a worst case delay of 4.1 msec | ||
+ | sleep_ms(5) | ||
+ | # | ||
+ | def hal_write_data(self, | ||
+ | byte = (MASK_RS | (self.Backlight << SHIFT_BACKLIGHT) | (((data >> 4) & 0x0f) << SHIFT_DATA)) | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | byte = (MASK_RS | (self.Backlight << SHIFT_BACKLIGHT) | ((data & 0x0f) << SHIFT_DATA)) | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | self.I2C.writeto(self.I2CAddress, | ||
+ | # | ||
+ | # | ||
+ | </ | ||
+ | |||
+ | **Library-Module: | ||
+ | <code python> | ||
+ | """ | ||
+ | # | ||
+ | import time | ||
+ | # | ||
+ | class CLCDisplay: | ||
+ | # HD44780 LCD controller command set | ||
+ | LCD_CLR = 0x01 # DB0: clear display | ||
+ | LCD_HOME = 0x02 # DB1: return to home position | ||
+ | # | ||
+ | LCD_ENTRY_MODE = 0x04 # DB2: set entry mode | ||
+ | LCD_ENTRY_INC = 0x02 # --DB1: increment | ||
+ | LCD_ENTRY_SHIFT = 0x01 # --DB0: shift | ||
+ | # | ||
+ | LCD_ON_CTRL = 0x08 # DB3: turn lcd/cursor on | ||
+ | LCD_ON_DISPLAY = 0x04 # --DB2: turn display on | ||
+ | LCD_ON_CURSOR = 0x02 # --DB1: turn cursor on | ||
+ | LCD_ON_BLINK = 0x01 # --DB0: blinking cursor | ||
+ | # | ||
+ | LCD_MOVE = 0x10 # DB4: move cursor/ | ||
+ | LCD_MOVE_DISP = 0x08 # --DB3: move display (0-> move cursor) | ||
+ | LCD_MOVE_RIGHT = 0x04 # --DB2: move right (0-> left) | ||
+ | # | ||
+ | LCD_FUNCTION = 0x20 # DB5: function set | ||
+ | LCD_FUNCTION_8BIT = 0x10 # --DB4: set 8BIT mode (0->4BIT mode) | ||
+ | LCD_FUNCTION_2LINES = 0x08 # --DB3: two lines (0->one line) | ||
+ | LCD_FUNCTION_10DOTS = 0x04 # --DB2: 5x10 font (0->5x7 font) | ||
+ | LCD_FUNCTION_RESET = 0x30 # See " | ||
+ | # | ||
+ | LCD_CGRAM = 0x40 # DB6: set CG RAM address | ||
+ | LCD_DDRAM = 0x80 # DB7: set DD RAM address | ||
+ | # | ||
+ | LCD_RS_CMD = 0 | ||
+ | LCD_RS_DATA = 1 | ||
+ | # | ||
+ | LCD_RW_WRITE = 0 | ||
+ | LCD_RW_READ = 1 | ||
+ | # | ||
+ | def __init__(self, | ||
+ | self.num_lines = num_lines | ||
+ | if self.num_lines > 4: | ||
+ | self.num_lines = 4 | ||
+ | self.num_columns = num_columns | ||
+ | if self.num_columns > 40: | ||
+ | self.num_columns = 40 | ||
+ | self.CursorY = 0 | ||
+ | self.CursorX = 0 | ||
+ | self.ImpliedNewline = False | ||
+ | self.Backlight = True | ||
+ | self.DisplayOff() | ||
+ | self.BacklightOn() | ||
+ | self.Clear() | ||
+ | self.hal_write_command(self.LCD_ENTRY_MODE | self.LCD_ENTRY_INC) | ||
+ | self.HideCursor() | ||
+ | self.DisplayOn() | ||
+ | # | ||
+ | def Clear(self): | ||
+ | self.hal_write_command(self.LCD_CLR) | ||
+ | self.hal_write_command(self.LCD_HOME) | ||
+ | self.CursorY = 0 | ||
+ | self.CursorX = 0 | ||
+ | # | ||
+ | def ShowCursor(self): | ||
+ | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | | ||
+ | | ||
+ | # | ||
+ | def HideCursor(self): | ||
+ | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY) | ||
+ | # | ||
+ | def BlinkCursorOn(self): | ||
+ | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | | ||
+ | | ||
+ | # | ||
+ | def BlinkCursorOff(self): | ||
+ | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | | ||
+ | | ||
+ | # | ||
+ | def DisplayOn(self): | ||
+ | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY) | ||
+ | # | ||
+ | def DisplayOff(self): | ||
+ | self.hal_write_command(self.LCD_ON_CTRL) | ||
+ | # | ||
+ | def BacklightOn(self): | ||
+ | self.backlight = True | ||
+ | self.hal_backlight_on() | ||
+ | # | ||
+ | def BacklightOff(self): | ||
+ | self.backlight = False | ||
+ | self.hal_backlight_off() | ||
+ | # | ||
+ | def MoveTo(self, | ||
+ | self.CursorY = cursory | ||
+ | self.CursorX = cursorx | ||
+ | Address = cursorx & 0x3f | ||
+ | if cursory & 1: | ||
+ | Address += 0x40 # Lines 1 & 3 add 0x40 | ||
+ | if cursory & 2: # Lines 2 & 3 add number of columns | ||
+ | Address += self.num_columns | ||
+ | self.hal_write_command(self.LCD_DDRAM | Address) | ||
+ | # | ||
+ | def PutCharacter(self, | ||
+ | if character == ' | ||
+ | if self.implied_newline: | ||
+ | self.implied_newline = False | ||
+ | else: | ||
+ | self.CursorX = self.num_columns | ||
+ | else: | ||
+ | self.hal_write_data(ord(character)) | ||
+ | self.CursorX += 1 | ||
+ | if self.CursorX >= self.num_columns: | ||
+ | self.CursorX = 0 | ||
+ | self.CursorY += 1 | ||
+ | self.implied_newline = (character != ' | ||
+ | if self.CursorY >= self.num_lines: | ||
+ | self.CursorY = 0 | ||
+ | self.MoveTo(self.CursorY, | ||
+ | # | ||
+ | def PutText(self, | ||
+ | for C in text: | ||
+ | self.PutCharacter(C) | ||
+ | # | ||
+ | def custom_char(self, | ||
+ | location &= 0x7 | ||
+ | self.hal_write_command(self.LCD_CGRAM | (location << 3)) | ||
+ | self.hal_sleep_us(40) | ||
+ | for i in range(8): | ||
+ | self.hal_write_data(charmap[i]) | ||
+ | self.hal_sleep_us(40) | ||
+ | self.MoveTo(self.cursor_y, | ||
+ | # | ||
+ | def hal_backlight_on(self): | ||
+ | pass | ||
+ | # | ||
+ | def hal_backlight_off(self): | ||
+ | pass | ||
+ | # | ||
+ | def hal_write_command(self, | ||
+ | raise NotImplementedError | ||
+ | # | ||
+ | def hal_write_data(self, | ||
+ | raise NotImplementedError | ||
+ | # | ||
+ | def hal_sleep_us(self, | ||
+ | time.sleep_us(usecs) | ||
+ | # | ||
+ | # | ||
+ | </ | ||
+ | |||
+ | |||
===== Entwicklung ===== | ===== Entwicklung ===== | ||
+ | ==== 211216 : Esp32LCDisplayI2C 01V01 ==== | ||
+ | * Minimierung abgeschlossen | ||
+ | * Download: {{: | ||
+ | |||
+ | |||
+ | ** VSCode: Testprogramm Esp32LCDisplayI2C 01V01** \\ | ||
+ | {{: | ||
+ | |||
+ | ** Funktionsweise Ansteuerung LCDisplayI2C** \\ | ||
+ | {{: | ||
+ | |||
+ | |||
+ | ==== 211216 : DokuWiki: | ||
+ | * WICHTIG: **LCDisplayI2C(PCF8574T) 4row x 20columns muss mit 5V betrieben werden!** | ||
+ | * Versuch, die Github-Original-Files zu minimieren und optimieren | ||
==== 211215 : Erster Versuch Esp32-LCDisplayI2C ==== | ==== 211215 : Erster Versuch Esp32-LCDisplayI2C ==== | ||
* Download [[https:// | * Download [[https:// |
module/micropython/esp32lcdisplayi2c/esp32lcdisplayi2c.1639649788.txt.gz · Last modified: 2021/12/16 12:16 (external edit)