demo
演示
demo
演示
STC32基于u8g2库DMA驱动IIC or SPI OLED屏幕显示
开源项目介绍https://github.com/songzhishuo/STC32_u8g2_drv
| 引脚名 | 引脚编号 |
| ------ | -------- |
| SCL | P1.5 |
| SDA | P1.4 |
| RST | P2.0 |没有此引脚不接
STC32 IIC引脚切换,可以根据个人需求修改:
iic.c
文件中
void HAL_I2C_Init()//包含I2C功能脚选择
{
// I2C功能脚选择,00:P1.5,P1.4; 01:P2.5,P2.4; 11:P3.2,P3.3
I2C_S1 = 0; I2C_S0 = 0; //SCL/P1.5, SDA/P1.4
// I2C_S1 = 0; I2C_S0 = 1; //SCL_2/P2.5, SDA_2/P2.4
// I2C_S1 = 1; I2C_S0 = 0; //SCL_3/P7.7, SDA_3/P7.6
// I2C_S1 = 1; I2C_S0 = 1; //SCL_4/P3.2, SDA_4/P3.3
I2CCFG = 0xe0; //使能I2C主机模式
I2CMSST = 0x00;
#if (IIC_DMA_CFG == 1)
DMA_I2CT_STA = 0x00;
DMA_I2CT_CFG = 0x80; //bit7 1:Enable Interrupt
DMA_I2CT_AMT = 0x1f; //设置传输总字节数(低8位):n+1
DMA_I2CT_AMTH = 0x00; //设置传输总字节数(高8位):n+1
DMA_I2CT_TXAH = (u8)((u16)&DmaTxBuffer >> 8); //I2C发送数据存储地址
DMA_I2CT_TXAL = (u8)((u16)&DmaTxBuffer);
DMA_I2CT_CR = 0x80; //bit7 1:使能 I2CT_DMA, bit6 1:开始 I2CT_DMA
DMA_I2C_ST1 = 0xff; //设置需要传输字节数(低8位):n+1
DMA_I2C_ST2 = 0x00; //设置需要传输字节数(高8位):n+1
#endif
}
| 引脚名 | 引脚编号 |
| ------ | -------- |
| CS | P2.2 |
| CLK | P2.5 |
| MISO | P2.4 |
| MOSI | P2.3 |
| RST | P2.0 |
| DC | P2.1 |
system.h
文件当中:在
35
MHz下也可以运行。
//#define MAIN_Fosc 11059200L //定义主时钟
#define MAIN_Fosc 35000000L //定义主时钟
//#define MAIN_Fosc 22118400L //定义主时钟
代码默认选择的是驱动ssd1306接口的OLED屏幕,如果我们想驱动其他类的屏幕:需在
u8g2.h
的631行-1367行中将其注释解除,并且在u8g2_d_setup.c
中对其定义也解除屏蔽即可。一般一个型号的屏幕对应2个相关的驱动函数。这里以驱动SH1106 屏幕为例:
u8g2_porting.c
void LCD_Init()
{
#if (U8g2_I2C_MODE == 1)
//驱动0.96寸屏幕
// u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8x8_stc32_gpio_and_delay);//u8x8_msg_cb byte_cb
//驱动sh1106屏幕
u8g2_Setup_sh1106_128x64_noname_1(&u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8x8_stc32_gpio_and_delay);
#elif (U8g2_SPI_MODE == 1)
u8g2_Setup_ssd1306_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_4wire_hw_spi, u8x8_stc32_gpio_and_delay);
OLED_DC = 0;
#endif
u8g2_InitDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2, 0);
}
u8g2.h
void u8g2_Setup_sh1106_128x64_noname_1(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb);
void u8g2_Setup_sh1106_i2c_128x64_noname_1(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb);
u8g2_d_setup.c
/* sh1106 f 1.3寸屏幕*/
void u8g2_Setup_sh1106_128x64_noname_1(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb)
{
uint8_t tile_buf_height;
uint8_t *buf;
u8g2_SetupDisplay(u8g2, u8x8_d_sh1106_128x64_noname, u8x8_cad_001, byte_cb, gpio_and_delay_cb);
buf = u8g2_m_16_8_f(&tile_buf_height);
u8g2_SetupBuffer(u8g2, buf, tile_buf_height, u8g2_ll_hvline_vertical_top_lsb, rotation);
}
/* sh1106 f */
void u8g2_Setup_sh1106_i2c_128x64_noname_1(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb)
{
uint8_t tile_buf_height;
uint8_t *buf;
u8g2_SetupDisplay(u8g2, u8x8_d_sh1106_128x64_noname, u8x8_cad_ssd13xx_i2c, byte_cb, gpio_and_delay_cb);
buf = u8g2_m_16_8_f(&tile_buf_height);
u8g2_SetupBuffer(u8g2, buf, tile_buf_height, u8g2_ll_hvline_vertical_top_lsb, rotation);
}
链接:https://pan.baidu.com/s/1B_ePEI6yvbKsMvq1VFHpOw
提取码:drhs