会宁网站建设,恩施网站建设xiduyun,网站建设年度汇报,u网站建设文章目录1. I2C与SPI通信协议对比2. 四脚OLED与六脚OLED3. I2C驱动OLED显示oled.h oled.c#xff1a;汉字取模 oledfont.h#xff1a;main.c 显示示例#xff1a;连线方法#xff1a;4. SPI驱动OLED显示1. I2C与SPI通信协议对比
I2C#xff08;Inter-Integra…
文章目录1. I2C与SPI通信协议对比2. 四脚OLED与六脚OLED3. I2C驱动OLED显示oled.h oled.c汉字取模 oledfont.hmain.c 显示示例连线方法4. SPI驱动OLED显示1. I2C与SPI通信协议对比
I2CInter-Integrated CircuitSPISerial Peripheral Interface传输方式半双工全双工传输速度低速100Kbps----4Mbps高速30Mbps以上几线制4线制VCCGNDSCLSDA6/7线制VCCGNDSCLK(D0)MOSI(D1/SDA)DCCS/SS主从模式多主机总线通过SDA上的地址信息锁定从机只有一个主机主机通过CS片选来确定从机读写方式读写时序比较固定统一设备驱动编写方便根据不同从机datasheet来实现读写相对复杂一些
2. 四脚OLED与六脚OLED
四脚的OLED即使用I2C进行通信的接口六脚的OLED即使用SPI/I2C进行通信的接口也有七脚的OLED多一个复位引脚RES.
3. I2C驱动OLED显示
通过oled.c、myiic.c等库函数的开发方式驱动oled显示中文字符、英文字符串需要先掌握如何在keil中配置stm32工程模板熟悉添加并使用库函数等。
oled.h oled.c
oled.h头文件包含了oled.c中的oled配置、控制函数声明
void OLED_WR_Byte(unsigned dat,unsigned cmd);
void OLED_Display_On(void);
void OLED_Display_Off(void);
void OLED_Init(void);
void OLED_Clear(void);
void OLED_DrawPoint(u8 x,u8 y,u8 t);
void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot);
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size);
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size);
void OLED_ShowString(u8 x,u8 y, u8 *p,u8 Char_Size);
void OLED_Set_Pos(unsigned char x, unsigned char y);
void OLED_ShowCHinese(u8 x,u8 y,u8 no);
void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[]);
void fill_picture(unsigned char fill_Data);
void OLED_Refresh_Gram(void);
void Boot_Animation(void);
void OLED_DrawCircle(u8 x0,u8 y0,u8 r);
void OLED_DrawLine(unsigned int x1, unsigned int y1, unsigned int x2,unsigned int y2);
void OLED_Write_Command(unsigned char IIC_Command);
void OLED_Write_Data(unsigned char IIC_Data);
oled.c包含了以上函数的具体封装。
汉字取模 oledfont.h
Oled显示utf-8字符串可以直接使用OLED_ShowString但要想显示汉字则需要用字体取模软件获取输出数制并保存在oledfont.h中
main.c 显示示例
int main(void)
{delay_init(); //延时函数初始化 USART1_Config();IIC_Init();OLED_Init(); //初始化OLEDOLED_Clear();while(1){OLED_Display_On();//显示16*16汉字, 第一行第8位开始显示第一个中文字符字体模值数据在oledfont.h中OLED_ShowCHinese(8,0,0); OLED_ShowCHinese(24,0,1);OLED_ShowCHinese(40,0,2);OLED_ShowCHinese(56,0,3);OLED_ShowCHinese(72,0,4);OLED_ShowCHinese(88,0,5);OLED_ShowCHinese(104,0,6);OLED_ShowString(0,2,Hello,world!,16); //显示字符串OLED_ShowString(20,4,2023-02-11,16);OLED_ShowString(0,6,STM32 ,16);OLED_ShowString(63,6,OLED,16);}
}连线方法
通过OLED_ShowCHinese函数Go To Defination跳转至函数内部
//显示汉字
void OLED_ShowCHinese(u8 x,u8 y,u8 no)
{u8 t,adder0;OLED_Set_Pos(x,y);for(t0;t16;t){OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);adder1;}OLED_Set_Pos(x,y1);for(t0;t16;t){OLED_WR_Byte(Hzk[2*no1][t],OLED_DATA);adder1;}
}再Go To Defination“OLED_DATA”至myiic.h中可以定位到接口信息SCLK连PB6SDA连PB7也可自行修改
/* 定义I2C总线连接的GPIO端口, 用户只需要修改下面4行代码即可任意改变SCL和SDA的引脚 */#define IIC_SCK_PIN GPIO_Pin_6 /* 连接到SCK时钟线的GPIO */
#define IIC_SCK_PORT GPIOB /* GPIO端口 */
#define IIC_SCK_CLK RCC_APB2Periph_GPIOB /* GPIO端口时钟 */
#define IIC_SCL PBout(6) //SCL#define IIC_SDA_PIN GPIO_Pin_7 /* 连接到SDA时钟线的GPIO */
#define IIC_SDA_PORT GPIOB /* GPIO端口 */
#define IIC_SDA_CLK RCC_APB2Periph_GPIOB /* GPIO端口时钟 */
#define IIC_SDA PBout(7) //SDA
#define READ_SDA PBin(7) //输入SDA4. SPI驱动OLED显示
利用库函数方式开发的main.c写法同I2C方式驱动oled差不多主要是走的协议不同所以接线方式也不一样oled初始化自然也不一样需要了解SPI工作原理、时序图等
//OLED的初始化——From 中景园电子
void OLED_Init(void)
{GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); //使能A端口时钟GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);GPIO_InitStructure.GPIO_Pin GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_15;GPIO_InitStructure.GPIO_Mode GPIO_Mode_Out_PP; //推挽输出GPIO_InitStructure.GPIO_Speed GPIO_Speed_50MHz;//速度50MHzGPIO_Init(GPIOA, GPIO_InitStructure); //初始化GPIOAGPIO_SetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_15);OLED_RES_Clr();delay_ms(200);OLED_RES_Set();OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panelOLED_WR_Byte(0x00,OLED_CMD);//---set low column addressOLED_WR_Byte(0x10,OLED_CMD);//---set high column addressOLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control registerOLED_WR_Byte(0xCF,OLED_CMD);// Set SEG Output Current BrightnessOLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常OLED_WR_Byte(0xA6,OLED_CMD);//--set normal displayOLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 dutyOLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)OLED_WR_Byte(0x00,OLED_CMD);//-not offsetOLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequencyOLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/SecOLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge periodOLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks Discharge as 1 ClockOLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configurationOLED_WR_Byte(0x12,OLED_CMD);OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomhOLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect LevelOLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)OLED_WR_Byte(0x02,OLED_CMD);//OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disableOLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disableOLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7) OLED_Clear();OLED_WR_Byte(0xAF,OLED_CMD);
}