概述
本章STM32CUBEMX配置STM32F103,并且在GD32F303中进行开发,同时通过开发板内进行验证。 使用IIC进行驱动SSD1306的12864OLED。 最近在弄ST和GD的课程,需要GD样片的可以加群申请:615061293 。
视频教学
https://www.bilibili.com/video/BV1Q8411t7sK/
csdn课程
课程更加详细。 https://download.csdn.net/course/detail/37152
样品申请
https://www.wjx.top/vm/mB2IKus.aspx
生成例程
这里准备了自己绘制的开发板进行验证。查看原理图,PB8和PB9设置为开发板的IIC。
配置时钟树
配置时钟为64M。
IIC配置
本次实验使用的IIC与显示屏通信,配置如下。
OLED
查看文档可以知道地址为b0111100或者b0111101,当R/W位置写0的时候,才能进行写的操作,那么地址默认为0x78(b01111000)或者0x7A(b01111010)
参考程序
这里使用0.96OLED显示屏_STM32F103C8_IIC_V1.0来进行移植查看工程可以知道主要是有3个文件来进行OLED的驱动,移入OLE.h .c 、oledfont.h 、 bmp.h 移入文件之后要注意导入C文件。
删除没有用的头文件 删除sys.h文件
删除delay.h文件
可以查看stdint.h
在oled.h中添加u8 u32定义 也可以直接修改
typedef unsigned char u8;
typedef unsigned int u32;
先把oled.h添加主程序中。
查看oled.h,可以看到IIC定义,同时也是模拟IIC,可以注释掉
查看oled.c,可以看到是模拟iic,这些都可以注释不要,我们使用的是iIC,不是模拟。
HAL_I2C_Mem_Write()
IIC写固定的设备写入数据。 记得导入#include 'i2c.h'
将HAL_I2C_Mem_Write函数放入oled.c中。 例如将Write_IIC_Command函数修改为HAL_I2C_Mem_Write发送方式。
向0x78设备写命令0x00的IIC_Command指令。
/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(unsigned char IIC_Command)
{
// IIC_Start();
// Write_IIC_Byte(0x78); //Slave address,SA0=0
// IIC_Wait_Ack();
// Write_IIC_Byte(0x00); //write command
// IIC_Wait_Ack();
// Write_IIC_Byte(IIC_Command);
// IIC_Wait_Ack();
// IIC_Stop();
HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x00,I2C_MEMADD_SIZE_8BIT,&IIC_Command,1,0x100);
}
IIC写数据 向0x78设备写数据0x40的IIC_Data指令。
/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(unsigned char IIC_Data)
{
// IIC_Start();
// Write_IIC_Byte(0x78); //D/C#=0; R/W#=0
// IIC_Wait_Ack();
// Write_IIC_Byte(0x40); //write data
// IIC_Wait_Ack();
// Write_IIC_Byte(IIC_Data);
// IIC_Wait_Ack();
// IIC_Stop();
HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x40,I2C_MEMADD_SIZE_8BIT,&IIC_Data,1,0x100);
}
在oled.c中,OLED_Init初始化可以注释掉gpio初始化
在oled.c中,OLED_Init初始化中有个延时800ms。 导入
#include 'stm32f1xx_hal.h' 可以使用HAL_Delay(800);
取模
在oledfont.h中放入字库
char Hzk[][32]={
// 记(0) 帖(1) 年(2) 月(3) 日(4)
{0x40,0x40,0x42,0xCC,0x00,0x00,0x00,0x84,0x84,0x84,0x84,0x84,0xFC,0x00,0x00,0x00},
{0x00,0x00,0x00,0x7F,0x20,0x10,0x00,0x3F,0x40,0x40,0x40,0x40,0x41,0x40,0x70,0x00},/*'记',0*/
/* (16 X 16 , 宋体 )*/
{0x00,0xF8,0x08,0xFF,0x08,0xF8,0x00,0x00,0x00,0x00,0xFF,0x10,0x10,0x10,0x10,0x00},
{0x00,0x0F,0x00,0xFF,0x08,0x0F,0x00,0xFF,0x41,0x41,0x41,0x41,0x41,0xFF,0x00,0x00},/*'帖',1*/
/* (16 X 16 , 宋体 )*/
{0x00,0x20,0x18,0xC7,0x44,0x44,0x44,0x44,0xFC,0x44,0x44,0x44,0x44,0x04,0x00,0x00},
{0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x04,0xFF,0x04,0x04,0x04,0x04,0x04,0x04,0x00},/*'年',2*/
/* (16 X 16 , 宋体 )*/
{0x00,0x00,0x00,0xFE,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0xFE,0x00,0x00,0x00},
{0x80,0x40,0x30,0x0F,0x02,0x02,0x02,0x02,0x02,0x02,0x42,0x82,0x7F,0x00,0x00,0x00},/*'月',3*/
/* (16 X 16 , 宋体 )*/
{0x00,0x00,0x00,0xFE,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0xFE,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0xFF,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0xFF,0x00,0x00,0x00,0x00},/*'日',4*/
/* (16 X 16 , 宋体 )*/
};
在main.c中添加显示
OLED_Init(); //初始化OLED
OLED_Clear() ;
OLED_ShowCHinese(0,0,0);//记
OLED_ShowCHinese(16,0,1);//帖
OLED_ShowNum(0,2,2022,4,16);//显示ASCII字符的码值
OLED_ShowCHinese(32,2,2);//年
OLED_ShowNum(48,2,9,1,16);//显示ASCII字符的码值
OLED_ShowCHinese(64,2,3);//月
OLED_ShowNum(80,2,10,2,16);//显示ASCII字符的码值
OLED_ShowCHinese(96,2,4);//日
字体大小只有2个显示字符
OLED_ShowString(40,2,'ZXY',8);
图片放在bmp.h中。
#ifndef __BMP_H
#define __BMP_H
unsigned char BMP1[]={
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x1F,0x1F,0x1F,0x7F,0xFF,0xFF,
0xFF,0xFF,0xFF,0x8F,0x8F,0x8F,0x8F,0x8F,0x8F,0x8F,0x8F,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FF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,/*'D:GD32F303GD32F30x_Firmware_Library_V2.1.4_iic_oled记帖.bmp',0*/
/* (64 X 64 )*/
};
#endif
显示图片
delay_1ms(2000);
OLED_Clear();
OLED_DrawBMP(0,0,64,8,BMP1); //图片显示(图片显示慎用,生成的字表较大,会占用较多空间,FLASH空间8K以下慎用)
OLED.C
//////////////////////////////////////////////////////////////////////////////////
// 功能描述 : 0.69寸OLED 接口演示例程(STM32F103ZE系列IIC)
// 说明:
// ----------------------------------------------------------------
// GND 电源地
// VCC 接5V或3.3v电源
// SCL 接PA5(SCL)
// SDA 接PA7(SDA)
// ----------------------------------------------------------------
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
#include 'oled.h'
#include 'stdlib.h'
#include 'oledfont.h'
//#include 'delay.h'
#include 'stm32f1xx_hal.h'
#include 'i2c.h'
//OLED的显存
//存放格式如下.
//[0]0 1 2 3 ... 127
//[1]0 1 2 3 ... 127
//[2]0 1 2 3 ... 127
//[3]0 1 2 3 ... 127
//[4]0 1 2 3 ... 127
//[5]0 1 2 3 ... 127
//[6]0 1 2 3 ... 127
//[7]0 1 2 3 ... 127
/**********************************************
//IIC Start
**********************************************/
/**********************************************
//IIC Start
**********************************************/
//void IIC_Start()
//{
// OLED_SCLK_Set() ;
// OLED_SDIN_Set();
// OLED_SDIN_Clr();
// OLED_SCLK_Clr();
//}
///**********************************************
////IIC Stop
//**********************************************/
//void IIC_Stop()
//{
//OLED_SCLK_Set() ;
//// OLED_SCLK_Clr();
// OLED_SDIN_Clr();
// OLED_SDIN_Set();
//
//}
//void IIC_Wait_Ack()
//{
// //GPIOB- >CRH &= 0XFFF0FFFF; //设置PB12为上拉输入模式
// //GPIOB- >CRH |= 0x00080000;
//// OLED_SDA = 1;
//// delay_us(1);
// //OLED_SCL = 1;
// //delay_us(50000);
///* while(1)
// {
// if(!OLED_SDA) //判断是否接收到OLED 应答信号
// {
// //GPIOB- >CRH &= 0XFFF0FFFF; //设置PB12为通用推免输出模式