├── README.md └── st7565.py /README.md: -------------------------------------------------------------------------------- 1 | ST7565 LCD driver for micropython 2 | ================================= 3 | 4 | Description 5 | ----------- 6 | This is micropython driver for LCDs based on **ST7565** controller. 7 | Only serial mode sopported. 8 | 9 | Wiring example for ESP8266-based modules 10 | ---------------------------------------- 11 | 12 | |LCD | ESP8266| 13 | |----|--------| 14 | |A0 | GPIO 0 | 15 | |RST | GPIO 16| 16 | |CS | GPIO 15| 17 | |DATA| GPIO 13| 18 | |CLOCK| GPIO 14| 19 | 20 | Usage example 21 | ------------- 22 | ```python 23 | import machine 24 | from st7565 import ST7565 25 | 26 | RST = Pin(16, Pin.OUT) 27 | A0 = Pin(0, Pin.OUT) 28 | CS = Pin(15, Pin.OUT) 29 | spibus = SPI(1, baudrate=1000000, polarity=1, phase=1) 30 | 31 | display = ST7565(spibus, A0, CS, RST) 32 | display.fill(1) 33 | display.show() 34 | ``` 35 | -------------------------------------------------------------------------------- /st7565.py: -------------------------------------------------------------------------------- 1 | # 2 | # MIT License 3 | # 4 | # Permission is hereby granted, free of charge, to any person obtaining 5 | # a copy of this software and associated documentation files (the 6 | # "Software"), to deal in the Software without restriction, including 7 | # without limitation the rights to use, copy, modify, merge, publish, 8 | # distribute, sublicense, and/or sell copies of the Software, and to 9 | # permit persons to whom the Software is furnished to do so, subject to 10 | # the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | from micropython import const 24 | from time import sleep_ms, sleep_us 25 | import framebuf 26 | 27 | # LCD Commands definition 28 | CMD_DISPLAY_ON = const(0xAF) 29 | CMD_DISPLAY_OFF = const(0xAF) 30 | CMD_SET_START_LINE = const(0x40) 31 | CMD_SET_PAGE = const(0xB0) 32 | CMD_COLUMN_UPPER = const(0x10) 33 | CMD_COLUMN_LOWER = const(0x00) 34 | CMD_SET_ADC_NORMAL = const(0xA0) 35 | CMD_SET_ADC_REVERSE = const(0xA1) 36 | CMD_SET_COL_NORMAL = const(0xC0) 37 | CMD_SET_COL_REVERSE = const(0xC8) 38 | CMD_SET_DISPLAY_NORMAL = const(0xA6) 39 | CMD_SET_DISPLAY_REVERSE = const(0xA7) 40 | CMD_SET_ALLPX_ON = const(0xA5) 41 | CMD_SET_ALLPX_NORMAL = const(0xA4) 42 | CMD_SET_BIAS_9 = const(0xA2) 43 | CMD_SET_BIAS_7 = const(0xA3) 44 | CMD_DISPLAY_RESET = const(0xE2) 45 | CMD_NOP = const(0xE3) 46 | CMD_TEST = const(0xF0) # Exit this mode with CMD_NOP 47 | CMD_SET_POWER = const(0x28) 48 | CMD_SET_RESISTOR_RATIO = const(0x20) 49 | CMD_SET_VOLUME = const(0x81) 50 | 51 | # Display parameters 52 | DISPLAY_W = const(128) 53 | DISPLAY_H = const(64) 54 | DISPLAY_CONTRAST = const(0x1B) 55 | DISPLAY_RESISTOR_RATIO = const(5) 56 | DISPLAY_POWER_MODE = 7 57 | 58 | 59 | class ST7565(framebuf.FrameBuffer): 60 | """ST7565 Display controller driver""" 61 | def __init__(self, spi, a0, cs, rst): 62 | self.spi = spi 63 | self.rst = rst 64 | self.a0 = a0 65 | self.cs = cs 66 | self.width = DISPLAY_W 67 | self.height = DISPLAY_H 68 | self.buffer = bytearray(1024) 69 | super().__init__( 70 | self.buffer, 71 | self.width, 72 | self.height, 73 | framebuf.MONO_VLSB) 74 | self.display_init() 75 | 76 | def display_init(self): 77 | self.reset() 78 | sleep_ms(1) 79 | for cmd in ( 80 | CMD_DISPLAY_OFF, # Display off 81 | CMD_SET_BIAS_9, # Display drive voltage 1/9 bias 82 | CMD_SET_ADC_REVERSE, # Reverse SEG 83 | CMD_SET_COL_NORMAL, # Commmon mode normal direction 84 | CMD_SET_RESISTOR_RATIO + DISPLAY_RESISTOR_RATIO, # V5 R ratio 85 | CMD_SET_VOLUME, # Contrast 86 | DISPLAY_CONTRAST, # Contrast value 87 | CMD_SET_POWER + DISPLAY_POWER_MODE): 88 | self.write_cmd(cmd) 89 | self.show() 90 | self.write_cmd(CMD_DISPLAY_ON) 91 | 92 | def write_cmd(self, cmd): 93 | self.a0(0) 94 | self.cs(0) 95 | self.spi.write(bytearray([cmd])) 96 | self.cs(1) 97 | 98 | def write_data(self, buf): 99 | self.a0(1) 100 | self.cs(0) 101 | self.spi.write(buf) 102 | self.cs(1) 103 | 104 | def set_contrast(self, value): 105 | if 0x1 <= value <= 0x3f: 106 | for cmd in ( 107 | CMD_SET_VOLUME, 108 | value): 109 | self.write_cmd(cmd) 110 | 111 | def reset(self): 112 | self.rst(0) 113 | sleep_us(1) 114 | self.rst(1) 115 | 116 | def show(self): 117 | for i in range(8): 118 | for cmd in ( 119 | CMD_SET_START_LINE, 120 | CMD_SET_PAGE + i, 121 | CMD_COLUMN_UPPER, 122 | CMD_COLUMN_LOWER): 123 | self.write_cmd(cmd) 124 | self.write_data(self.buffer[i*128:(i+1)*128]) 125 | --------------------------------------------------------------------------------