U8G2軟件庫在OLED、lcd等模塊中使用非常的方便,支持的型號也比較多,不用自己每次寫自己的顯示屏驅動程序,下面介紹下載stm32上使用U8G2庫進行一些圖形和文字的顯示。
首先要下載好U8G2的庫,此處在附件中就可以下載到(庫和完整工程代碼請到零知原貼中下載)。下載到附件后,我們主要在u8g_arm.c中根據模板寫出跟我們硬件相關的接口即可,下面以SSD1306,使用4線方式的OLED接口為例。
我們把下載的庫放到我們的工程目錄下,然后在keil中添加到工程,然后我們在u8g_arm.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這個接口,實際情況要根據我們的顯示屏的型號來選擇了。我們就可以看到如下的顯示效果,非常的方便:
2.jpg (194.17 KB, 下載次數: 76)
下載附件
2018-8-1 19:08 上傳
3.jpg (1.95 MB, 下載次數: 82)
下載附件
2018-8-1 19:08 上傳
1.jpg (217.12 KB, 下載次數: 81)
下載附件
2018-8-1 19:08 上傳
|