ESP32-32基础教程
本教程基于ESP32-32开发板,ESP32-32是一款基于ESP32芯片的Wi-Fi和蓝牙双模开发板,具有丰富的外设接口和强大的处理能力。本教程将介绍ESP32-32的基本使用方法,包括如何连接开发板、如何烧录固件、如何使用串口通信、如何使用GPIO口、如何使用ADC、如何使用PWM等。
目录👈
环境搭建🎉
1. 连接开发板
CAUTION
开发板的驱动芯片是CH340
首先,将ESP32-32开发板连接到电脑上,可以使用USB线将开发板与电脑连接。
2. 下载固件
https://www.123pan.com/s/i5h0Vv-Mxltv.html提取码:4vFn
将下载的固件包解压
下载ESP32驱动
双击安装,两个你随便选择一个
https://www.wch.cn/download/CH341SER_EXE.html
https://www.123pan.com/s/i5h0Vv-dpltv.html提取码:gOSI
下载&安装Thonny
烧录固件
运行-》编辑解释器
这里选择ESP32,点击安装或更新 点击第一个,会让你选择文件
点击安装就可以了
这样就算可以了
驱动SPI彩色屏幕 🐸
查看效果
CAUTION
购买屏幕的时候需要注意以下参数 硬件接口: SPI 分辨率:240*240 驱动器:ST7735
接线说明
SPI屏幕 | ESP32 |
---|---|
VCC | 3.3V |
GND | GND |
SCL(sck) | 18 |
SDA(mosi) | 23 |
CS | 16 |
DC | 21 |
RES(rst) | 22 |
上传库文件
必须保持这样
在main.py里写入以下代码
python
from st7735 import ST7735
from machine import Pin,SPI
import time
# 初始化SPI
spi=SPI(2, baudrate=20000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23))
# 初始化LCD rot 是显示方向,bgr是默认显示的颜色
lcd= ST7735 (128, 160, spi,dc=Pin(21),cs=Pin(16),rst=Pin(22),rot=2,bgr=0)
lcd.font_load('./GB2312-12.fon') # 加载字体
lcd.text("欢迎访问青柠博客",2,5,0x07e0)
lcd.text("https://www.qingningz.cn/",2,19,0xf800)
lcd.show() # 显示出来
st7735.py文件
python
from time import sleep_ms
from ustruct import pack
from machine import SPI,Pin
from micropython import const
import framebuf
# ST7735V registers definitions
NOP = const(0x00) # No Operation
SWRESET = const(0x01) # Software reset
SLPIN = const(0x10) # Sleep in & booster off
SLPOUT = const(0x11) # Sleep out & booster on
PTLON = const(0x12) # Partial mode on
NORON = const(0x13) # Partial off (Normal)
INVOFF = const(0x20) # Display inversion off
INVON = const(0x21) # Display inversion on
DISPOFF = const(0x28) # Display off
DISPON = const(0x29) # Display on
CASET = const(0x2A) # Column address set
RASET = const(0x2B) # Row address set
RAMWR = const(0x2C) # Memory write
RGBSET = const(0x2D) # Display LUT set
PTLAR = const(0x30) # Partial start/end address set
COLMOD = const(0x3A) # Interface pixel format
MADCTL = const(0x36) # Memory data access control
# panel function commands
FRMCTR1 = const(0xB1) # In normal mode (Full colors)
FRMCTR2 = const(0xB2) # In Idle mode (8-colors)
FRMCTR3 = const(0xB3) # In partial mode + Full colors
INVCTR = const(0xB4) # Display inversion control
PWCTR1 = const(0xC0) # Power control settings
PWCTR2 = const(0xC1) # Power control settings
PWCTR3 = const(0xC2) # In normal mode (Full colors)
PWCTR4 = const(0xC3) # In Idle mode (8-colors)
PWCTR5 = const(0xC4) # In partial mode + Full colors
VMCTR1 = const(0xC5) # VCOM control
GMCTRP1 = const(0xE0)
GMCTRN1 = const(0xE1)
class ST7735(framebuf.FrameBuffer):
def __init__(self, width, height, spi, dc, rst, cs, rot=0, bgr=0):
if dc is None:
raise RuntimeError('TFT must be initialized with a dc pin number')
dc.init(dc.OUT, value=0)
if cs is None:
raise RuntimeError('TFT must be initialized with a cs pin number')
cs.init(cs.OUT, value=1)
if rst is not None:
rst.init(rst.OUT, value=1)
else:
self.rst =None
self.spi = spi
self.rot = rot
self.dc = dc
self.rst = rst
self.cs = cs
self.height = height
self.width = width
self.buffer = bytearray(self.height * self.width*2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565SW, self.width)
if (self.rot ==0):
madctl=0x00
elif (self.rot ==1):
madctl=0xa0
elif (self.rot ==2):
madctl=0xc0
else :
madctl=0x60
if bgr==0:
madctl|=0x08
self.madctl = pack('>B', madctl)
self.reset()
self._write(SLPOUT)
sleep_ms(120)
for command, data in (
(COLMOD, b"\x05"),
(MADCTL, pack('>B', madctl)),
):
self._write(command, data)
if self.width==80 or self.height==80:
self._write(INVON, None)
else:
self._write(INVOFF, None)
buf=bytearray(128)
for i in range(32):
buf[i]=i*2
buf[i+96]=i*2
for i in range(64):
buf[i+32]=i
self._write(RGBSET, buf)
#self._write(NORON)
#sleep_ms(10)
self.show()
self._write(DISPON)
#sleep_ms(100)
def reset(self):
if self.rst is None:
self._write(SWRESET)
sleep_ms(50)
return
self.rst.off()
sleep_ms(50)
self.rst.on()
sleep_ms(50)
def _write(self, command, data = None):
self.cs.off()
self.dc.off()
self.spi.write(bytearray([command]))
self.cs.on()
if data is not None:
self.cs.off()
self.dc.on()
self.spi.write(data)
self.cs.on()
def show(self):
if self.width==80 or self.height==80:
if self.rot==0 or self.rot==2:
self._write(CASET,pack(">HH", 26, self.width+26-1))
self._write(RASET,pack(">HH", 1, self.height+1-1))
else:
self._write(CASET,pack(">HH", 1, self.width+1-1))
self._write(RASET,pack(">HH", 26, self.height+26-1))
else:
if self.rot==0 or self.rot==2:
self._write(CASET,pack(">HH", 0, self.width-1))
self._write(RASET,pack(">HH", 0, self.height-1))
else:
self._write(CASET,pack(">HH", 0, self.width-1))
self._write(RASET,pack(">HH", 0, self.height-1))
self._write(RAMWR,self.buffer)
def rgb(self,r,g,b):
return ((r&0xf8)<<8)|((g&0xfc)<<3)|((b&0xf8)>>3)