继上篇《让Pegasus天马座开发板吃上STM8S标准库》移植完标准库之后,于是我又想为天马座开发板添加一块屏幕。终于在我的零件箱底下找到了沉入箱底多年的0.96OLED屏幕。

这个是128x64像素的屏幕模块,其使用的SSD1306的驱动IC。而目前该模组,只支持3/4线SPI及I2C通信方式。

我将天马座开发板通过4线SPI方式与OLED屏模组进行通信。板子与屏幕相关的连接如下代码所示
- /****************时钟*********************/
- #define OLED_SCL_PORT (GPIOC)
- #define OLED_SCL_PINS (GPIO_PIN_3)
-
- /****************数据*********************/
- #define OLED_SDA_PORT (GPIOC)
- #define OLED_SDA_PINS (GPIO_PIN_4)
-
- /****************复位*********************/
- #define OLED_RES_PORT (GPIOC)
- #define OLED_RES_PINS (GPIO_PIN_5)
-
- /****************数据/命令*********************/
- #define OLED_DC_PORT (GPIOC)
- #define OLED_DC_PINS (GPIO_PIN_6)
-
- /****************片选*********************/
- #define OLED_CS_PORT (GPIOC)
- #define OLED_CS_PINS (GPIO_PIN_7)
接线图如下:

我根据STM8S003P6及OLED硬件资源情况,主要定义了以下几个功能。
在购买OLED屏幕时候,卖家已经提供了该屏幕的SSD1306驱动。因此,只需将相关代码移植到Pegasus天马座开发板示例工程代码中即可。此处,移植过程不做介绍。如有需要的小伙伴,可参考我释放出来的工程代码,进行了解。
实现功能定义的代码如下:
main.c
- #include "config.h"
- #include "delay.h"
- #include "oled.h"
-
- #define LED_GPIO_PORT GPIOD
- #define LED_PIN GPIO_PIN_4
-
- #define BUTTON_GPIO_PORT GPIOD
- #define BUTTON_PIN GPIO_PIN_3
-
- uint32_t count = 0;
- #define TIM4_PERIOD 124
-
- void tim4_isr() __interrupt(ITC_IRQ_TIM4_OVF)
- {
- static uint16_t ctr = 0;
- if (++ctr >= 100) {
- count++;
- ctr = 0;
- }
- /* Cleat Interrupt Pending bit */
- TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
- }
-
- static void set_timer()
- {
- /* Time base configuration */
- TIM4_TimeBaseInit(TIM4_PRESCALER_128, TIM4_PERIOD);
- /* Clear TIM4 update flag */
- TIM4_ClearFlag(TIM4_FLAG_UPDATE);
- /* Enable update interrupt */
- TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
-
- /* enable interrupts */
- enableInterrupts();
-
- /* Enable TIM4 */
- TIM4_Cmd(ENABLE);
- }
-
- static void init_gpio()
- {
- GPIO_Init(LED_GPIO_PORT, LED_PIN, GPIO_MODE_OUT_PP_LOW_FAST);
- GPIO_Init(BUTTON_GPIO_PORT, BUTTON_PIN, GPIO_MODE_IN_PU_NO_IT);
- OLED_Init();
- }
-
-
- void set_screen_color_and_display(uint8_t cfg)
- {
- // BIT0作为屏幕颜色控制 0: 正常显示, 1: 反色显示
- OLED_ColorTurn(cfg & 0x01);
- // BIT1作为屏幕颜色控制 0: 正常显示 1: 屏幕翻转显示
- OLED_DisplayTurn((cfg >> 1) & 0x01);
- }
-
- void main(void)
- {
- static uint8_t testBuffer[32] = {0};
- static uint8_t k = 0;
- bool isKeyDown = FALSE;
- uint8_t screenCfg = 0;
- uint8_t t = ' ';
- disable_interrupts();
- for (uint16_t i = 0; i < sizeof(testBuffer); i++)
- {
- testBuffer[i] = i;
- }
- enable_interrupts();
- LOG("Startup...\r\n");
- DUMP(testBuffer,sizeof(testBuffer));
- init_gpio();
- set_timer();
- OLED_ColorTurn(0);//0正常显示,1 反色显示
- OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
- while (1)
- {
- GPIO_WriteReverse(LED_GPIO_PORT,LED_PIN);
- isKeyDown = GPIO_ReadInputPin(BUTTON_GPIO_PORT,BUTTON_PIN) == RESET;
- LOG("
app_sdcc == %u -- %lu, key %s\r\n" ,k++,count, isKeyDown ? "down" : "up"); - if (isKeyDown == TRUE)
- {
- if (++screenCfg > 3)
- {
- screenCfg = 0;
- }
- set_screen_color_and_display(screenCfg);
- OLED_Clear();
- }
-
- OLED_ShowString(25,0,"Pegasus Board",8);
- OLED_ShowString(35,2,"2023/09/20",8);
- OLED_ShowString(20,4,isKeyDown ? "Button Down" : "Button Up ",8);
- OLED_ShowNum(103,4,k,3,8);
- OLED_ShowString(0,6,"ASCII:",8);
- OLED_ShowString(63,6,"CODE:",8);
- OLED_ShowChar(48,6,t,8);
- t++;
- if(t>'~')t=' ';
- OLED_ShowNum(103,6,t,3,8);
- delay_ms(500);
- }
- }
-
-
- #ifdef USE_FULL_ASSERT
-
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- LOG("Wrong parameters value: file %s on line %d\r\n", file, line);
-
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
让Pegasus天马座开发板用上OLED屏
