37,722
社区成员
发帖
与我相关
我的任务
分享
# SPI Control 0.96 OLED未完
import sensor, image, time
from pyb import Pin, SPI
gram = [ [0] * 8 for i in range(128)]
cs = Pin("P3", Pin.OUT_OD)
rst = Pin("P7", Pin.OUT_PP)
rs = Pin("P8", Pin.OUT_PP)
# The hardware SPI bus for your OpenMV Cam is always SPI bus 2.
spi = SPI(2, SPI.MASTER, baudrate=int(1000000000/66), polarity=0, phase=0)
def write_command_byte(c):
cs.low()
rs.low()
spi.send(c)
cs.high()
def write_data_byte(c):
cs.low()
rs.high()
spi.send(c)
cs.high()
def write_command(c, *data):
write_command_byte(c)
if data:
for d in data: write_data_byte(d)
def write_image(img):
cs.low()
rs.high()
spi.send(img)
cs.high()
def write_string(*s):
if s:
for d in s: write_data_byte(d)
# Reset the OLED.
def reset_oled():
rst.low()
time.sleep(100)
rst.high()
time.sleep(100)
# 开启OLED显示
def display_on():
write_command_byte(0x8D)
time.sleep(120)
write_command_byte(0x14)
time.sleep(120)
write_command_byte(0xAF) # Sleep Exit
time.sleep(120)
# 关闭OLED显示
def display_off():
write_command_byte(0x8D)
time.sleep(120)
write_command_byte(0x10)
time.sleep(120)
write_command_byte(0xAE) # Sleep
time.sleep(120)
#更新显存到LCD
def OLED_Refresh_Gram():
for i in range(0,8):
write_command_byte (0xb0+i) #设置页地址(0~7)
write_command_byte (0x00) #设置显示位置—列低地址
write_command_byte (0x10) #设置显示位置—列高地址
for n in range(0,128):
write_string(gram[n][i])
# 画点函数
def draw_point(x,y,t):
if(x>127|y>63):return;
temp = 1<<(7-(y%8))
if(t):gram[x][7-y//8]|=temp
else:gram[x][7-y//8]&=~temp
# 填充函数x1<=x2;y1<=y2 0<=x1<=127 0<=y1<=63
def OLED_Fill(x1,y1,x2,y2,dot) :
for x in range(x1,x2):
for y in range(y1,y2):
draw_point(x,y,dot)
OLED_Refresh_Gram()
reset_oled()
time.sleep(120)
display_on()
time.sleep(120)
while(True):
OLED_Fill(20,5,108,58,1)
time.sleep(120)