U8G2软件库在OLED、lcd等模块中使用非常的方便,支持的型号也比较多,不用自己每次写自己的显示屏驱动程序,下面介绍下载stm32上使用U8G2库进行一些图形和文字的显示。
首先要下载好U8G2的库,此处在附件中就可以下载到。下载到附件后,我们主要在u8g_arm.c中根据模板写出跟我们硬件相关的接口即可,下面以SSD1306,使用4线方式的OLED接口为例。
我们把下载的库放到我们的工程目录下,然后在keil中添加到工程,然后我们在u8g_arm.c文件中编写如下代码:
[C] 纯文本查看 复制代码 uint8_t u8g2_gpio_and_delay_stm32(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
{
uint8_t delay_cnt;
switch(msg){
//Function which implements a delay, arg_int contains the amount of ms
case U8X8_MSG_GPIO_AND_DELAY_INIT:
//oled_gpio_init();
oled_io_config();
break;
//Function which implements a delay, arg_int contains the amount of ms
case U8X8_MSG_DELAY_MILLI:
delay_ms(arg_int);
break;
//Function which delays 10us
case U8X8_MSG_DELAY_10MICRO:
delay_us(10);
break;
//Function which delays 100ns
case U8X8_MSG_DELAY_100NANO:
//__NOP();
{
delay_cnt = 0;
while(delay_cnt++<10);
}
break;
//Function to define the logic level of the clockline
case U8X8_MSG_GPIO_SPI_CLOCK:
if (arg_int) OLED_SCL = 1;
else OLED_SCL = 0;
break;
//Function to define the logic level of the data line to the display
case U8X8_MSG_GPIO_SPI_DATA:
if (arg_int) OLED_SDA = 1;
else OLED_SDA = 0;
break;
// Function to define the logic level of the CS line
case U8X8_MSG_GPIO_CS1:
break;
//Function to define the logic level of the Data/ Command line
case U8X8_MSG_GPIO_DC:
if(arg_int) OLED_DC = 1;
else OLED_DC = 0;
break;
//Function to define the logic level of the RESET line
case U8X8_MSG_GPIO_RESET:
if (arg_int) OLED_RS = 1;
else OLED_RS = 0;
break;
default:
return 0; //A message was received which is not implemented, return 0 to indicate an error
}
return 1; // command processed successfully.
}
这个就是定义好硬件上连接gpio和延时的操作接口 。此处我们使用的是模拟spi方式,没有使用硬件SPI接口,因此使用自带的操作接口u8x8_byte_4wire_sw_spi就可以了,不用自己实现。然后我们在main中就可以调用跟我们相关的型号就可以操作OLED了,我这里调用u8g2_Setup_ssd1306_128x64_noname_f这个接口,实际情况要根据我们的显示屏的型号来选择了。在main函数中编写如下代码:
然后烧写后,我们就可以看到如下的显示效果,非常的方便:
|