ESP32-Audio-Kit音频开发板为用户提供的扩展资源实在称得上是紧缺,用扩展接口提供的引脚也多是复用的。要是使用个SPI接口啥的,估计能都给整没了!
为了显示的需要,这里就为它配置一个I2C接口OLED屏来显示。
该显示屏是一款0.91寸的单色屏,其显示效果如图1所示。
图1显示效果
显示屏与开发板的连接关系为:
SCL---IO23
SDA---IO18
其初始化函数为:
void OLED_Init(){
//SSD1306
delay(100);
OLED_WR_Byte(0xAE,OLED_CMD);
OLED_WR_Byte(0x40,OLED_CMD);
OLED_WR_Byte(0xB0,OLED_CMD);
OLED_WR_Byte(0xC8,OLED_CMD);
OLED_WR_Byte(0x81,OLED_CMD);
OLED_WR_Byte(0xff,OLED_CMD);
OLED_WR_Byte(0xa1,OLED_CMD);
OLED_WR_Byte(0xa6,OLED_CMD);
OLED_WR_Byte(0xa8,OLED_CMD);
OLED_WR_Byte(0x1f,OLED_CMD);
OLED_WR_Byte(0xd3,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xd5,OLED_CMD);
OLED_WR_Byte(0xf0,OLED_CMD);
OLED_WR_Byte(0xd9,OLED_CMD);
OLED_WR_Byte(0x22,OLED_CMD);
OLED_WR_Byte(0xda,OLED_CMD);
OLED_WR_Byte(0x02,OLED_CMD);
OLED_WR_Byte(0xdb,OLED_CMD);
OLED_WR_Byte(0x49,OLED_CMD);
OLED_WR_Byte(0x8d,OLED_CMD);
OLED_WR_Byte(0x14,OLED_CMD);
OLED_WR_Byte(0xaf,OLED_CMD);
OLED_Clear();
}
显示定位函数为:
void OLED_Set_Pos(unsigned char x, unsigned char y){
OLED_WR_Byte(0xb0+y,OLED_CMD);
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f),OLED_CMD);
}
清屏函数为:
void OLED_Clear(void){
unsigned char i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD);
OLED_WR_Byte (0x00,OLED_CMD);
OLED_WR_Byte (0x10,OLED_CMD);
for(n=0;n<128;n++) OLED_WR_Byte(0,OLED_DATA);
}
}
字符显示函数为:
void OLED_ShowChar(unsigned char x,unsigned char y,unsigned char chr,unsigned char Char_Size)
{
unsigned char c=0,i=0;
c=chr-' ';
if(x>Max_Column-1){x=0;y=y+2;}
if(Char_Size ==16)
{
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
OLED_Set_Pos(x,y+1);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
}
else {
OLED_Set_Pos(x,y);
for(i=0;i<6;i++)
OLED_WR_Byte(F6x8[c][i],OLED_DATA);
}
}
相应的主程序为:
void setup() {
pinMode(SCLK, OUTPUT);
pinMode(SDIN, OUTPUT);
pinMode(ledPin, OUTPUT);
OLED_Init();
OLED_Clear();
OLED_Showstring(0,0,15,16);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
由于ArduinoIDE不支持指针的使用,故在显示字符串时需要借助数组来解决,基于字符显示函数的字符串显示函数为:
char Str[16] = "ESP32-Audio-Kit";
void OLED_Showstring(unsigned char x,unsigned char y,unsigned char n,unsigned char Char_Size)
{
unsigned char i;
for(i=0;i<n;i++)
{
OLED_ShowChar(x,y,Str[i],16);
x=x+8;
}
}
有了OLED屏的显示功能,信息的显示任务就不用完全依赖串口通信了!