0、前言
关于OLED的详细驱动,可查看一文彻底了解SSD1306驱动0.96寸OLED,本文使用GD32L233硬件I2C驱动。
1、关于GD32L233CCT6的I2C接口
可以看出有两路I2C,分别是I2C0,I2C1。
2、开发板上I2C的引脚
可以看出,I2C1的引脚为PB10和PB11。
3、通用引脚复用为I2C
可以看出,是复用功能4。
4、代码实现
(1)初始化i2c
代码语言:javascript复制void I2cInit(void)
{
rcu_periph_clock_enable(RCU_I2C1);
rcu_periph_clock_enable(RCU_GPIOB);
gpio_af_set(GPIOB, GPIO_AF_4, GPIO_PIN_10);
gpio_af_set(GPIOB, GPIO_AF_4, GPIO_PIN_11);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_11);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
/* configure I2C timing */
i2c_timing_config(I2C1, 0, 0x3, 0);
i2c_master_clock_config(I2C1, 0x13, 0x36);
/* configure I2C address */
i2c_address_config(I2C1, I2C_OWN_ADDRESS7, I2C_ADDFORMAT_7BITS);
/* configure slave address */
i2c_master_addressing(I2C1, I2C_SLAVE_ADDRESS7, I2C_MASTER_TRANSMIT);
/* configure number of bytes to be transferred */
i2c_transfer_byte_number_config(I2C1, 2);
/* enable I2C1 */
i2c_enable(I2C1);
}
(2)写OLED 下面分写命令和数据.
代码语言:javascript复制void SSD1306_WriteCmd(uint8_t var)
{
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C1);
/* wait until the transmit data buffer is empty */
I2C_STAT(I2C1) |= I2C_STAT_TBE;
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
/* data transmission */
i2c_data_transmit(I2C1, 0x00);
/* wait until the TI bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
i2c_data_transmit(I2C1, var);
/* wait until the TI bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
/* wait for transfer complete flag */
while(!i2c_flag_get(I2C1, I2C_FLAG_TC));
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C1);
/* wait until stop condition generate */
while(!i2c_flag_get(I2C1, I2C_FLAG_STPDET));
/* clear the STPDET bit */
i2c_flag_clear(I2C1, I2C_FLAG_STPDET);
}
void SSD1306_WriteData(uint8_t var)
{
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C1);
/* wait until the transmit data buffer is empty */
I2C_STAT(I2C1) |= I2C_STAT_TBE;
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
/* data transmission */
i2c_data_transmit(I2C1, 0x40);
/* wait until the TI bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
i2c_data_transmit(I2C1, var);
/* wait until the TI bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
/* wait for transfer complete flag */
while(!i2c_flag_get(I2C1, I2C_FLAG_TC));
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C1);
/* wait until stop condition generate */
while(!i2c_flag_get(I2C1, I2C_FLAG_STPDET));
/* clear the STPDET bit */
i2c_flag_clear(I2C1, I2C_FLAG_STPDET);
}
(3)OLED相关驱动
代码语言:javascript复制//坐标设置:也就是在哪里显示
void OledSetPos(uint8_t x, uint8_t y)
{
//以下3个寄存器只在页寻址的模式下有效
SSD1306_WriteCmd(0xb0 y); //页地址设置 0xb0~0xb7
SSD1306_WriteCmd(((x&0xf0)>>4)|0x10); //列高位地址设置
SSD1306_WriteCmd((x&0x0f)); //列低位地址设置
}
//开启Oled显示
void OledDisplayOn(void)
{
SSD1306_WriteCmd(0X8D); //SET DCDC命令
SSD1306_WriteCmd(0X14); //DCDC ON
SSD1306_WriteCmd(0XAF); //DISPLAY ON
}
//关闭Oled显示
void OledDisplayOff(void)
{
SSD1306_WriteCmd(0X8D); //SET DCDC命令
SSD1306_WriteCmd(0X10); //DCDC OFF
SSD1306_WriteCmd(0XAE); //DISPLAY OFF
}
//清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样
void OledClear(void)
{
uint8_t i,n;
for(i=0;i<8;i )
{
SSD1306_WriteCmd (0xb0 i); //设置页地址(0~7)
SSD1306_WriteCmd (0x00); //设置显示位置—列低地址
SSD1306_WriteCmd (0x10); //设置显示位置—列高地址
for(n=0;n<128;n )
SSD1306_WriteData(0);
} //更新显示
}
//在指定位置显示一个字符,包括部分字符
//x:0~127,y:0~7
//Char_Size:选择字体 16/12
void OledShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size)
{
uint8_t c=0,i=0;
c=chr-' ';//得到偏移后的值
if(x>MAX_COLUMN-1)
{
x=0;
y=y 2;
}
if(Char_Size ==16)
{
OledSetPos(x,y);
for(i=0;i<8;i )
{
SSD1306_WriteData(F8X16[c*16 i]);//先写上半部分
}
OledSetPos(x,y 1);
for(i=0;i<8;i )
{
SSD1306_WriteData(F8X16[c*16 i 8]);//后写下半部分
}
}
else
{
OledSetPos(x,y);
for(i=0;i<6;i )
{
SSD1306_WriteData(F6x8[c][i]);
}
}
}
//显示一个字符串
void OledShowString(uint8_t x,uint8_t y,char *str,uint8_t Char_Size)
{
unsigned char j=0;
while (str[j]!='