• 让Pegasus天马座开发板用上OLED屏


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

    屏幕介绍

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

     硬件连接

    我将天马座开发板通过4线SPI方式与OLED屏模组进行通信。板子与屏幕相关的连接如下代码所示

    1. /****************时钟*********************/
    2. #define OLED_SCL_PORT (GPIOC)
    3. #define OLED_SCL_PINS (GPIO_PIN_3)
    4. /****************数据*********************/
    5. #define OLED_SDA_PORT (GPIOC)
    6. #define OLED_SDA_PINS (GPIO_PIN_4)
    7. /****************复位*********************/
    8. #define OLED_RES_PORT (GPIOC)
    9. #define OLED_RES_PINS (GPIO_PIN_5)
    10. /****************数据/命令*********************/
    11. #define OLED_DC_PORT (GPIOC)
    12. #define OLED_DC_PINS (GPIO_PIN_6)
    13. /****************片选*********************/
    14. #define OLED_CS_PORT (GPIOC)
    15. #define OLED_CS_PINS (GPIO_PIN_7)

    接线图如下: 

     功能定义

    我根据STM8S003P6及OLED硬件资源情况,主要定义了以下几个功能。

    1. 屏幕显示文字及数字
    2. 屏幕显示按钮的状态
    3. 通过按钮对屏幕显示参数进行配置

    功能实现 

    OLED驱动移植

    在购买OLED屏幕时候,卖家已经提供了该屏幕的SSD1306驱动。因此,只需将相关代码移植到Pegasus天马座开发板示例工程代码中即可。此处,移植过程不做介绍。如有需要的小伙伴,可参考我释放出来的工程代码,进行了解。

    功能代码

    实现功能定义的代码如下:

    main.c

    1. #include "config.h"
    2. #include "delay.h"
    3. #include "oled.h"
    4. #define LED_GPIO_PORT GPIOD
    5. #define LED_PIN GPIO_PIN_4
    6. #define BUTTON_GPIO_PORT GPIOD
    7. #define BUTTON_PIN GPIO_PIN_3
    8. uint32_t count = 0;
    9. #define TIM4_PERIOD 124
    10. void tim4_isr() __interrupt(ITC_IRQ_TIM4_OVF)
    11. {
    12. static uint16_t ctr = 0;
    13. if (++ctr >= 100) {
    14. count++;
    15. ctr = 0;
    16. }
    17. /* Cleat Interrupt Pending bit */
    18. TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
    19. }
    20. static void set_timer()
    21. {
    22. /* Time base configuration */
    23. TIM4_TimeBaseInit(TIM4_PRESCALER_128, TIM4_PERIOD);
    24. /* Clear TIM4 update flag */
    25. TIM4_ClearFlag(TIM4_FLAG_UPDATE);
    26. /* Enable update interrupt */
    27. TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
    28. /* enable interrupts */
    29. enableInterrupts();
    30. /* Enable TIM4 */
    31. TIM4_Cmd(ENABLE);
    32. }
    33. static void init_gpio()
    34. {
    35. GPIO_Init(LED_GPIO_PORT, LED_PIN, GPIO_MODE_OUT_PP_LOW_FAST);
    36. GPIO_Init(BUTTON_GPIO_PORT, BUTTON_PIN, GPIO_MODE_IN_PU_NO_IT);
    37. OLED_Init();
    38. }
    39. void set_screen_color_and_display(uint8_t cfg)
    40. {
    41. // BIT0作为屏幕颜色控制 0: 正常显示, 1: 反色显示
    42. OLED_ColorTurn(cfg & 0x01);
    43. // BIT1作为屏幕颜色控制 0: 正常显示 1: 屏幕翻转显示
    44. OLED_DisplayTurn((cfg >> 1) & 0x01);
    45. }
    46. void main(void)
    47. {
    48. static uint8_t testBuffer[32] = {0};
    49. static uint8_t k = 0;
    50. bool isKeyDown = FALSE;
    51. uint8_t screenCfg = 0;
    52. uint8_t t = ' ';
    53. disable_interrupts();
    54. for (uint16_t i = 0; i < sizeof(testBuffer); i++)
    55. {
    56. testBuffer[i] = i;
    57. }
    58. enable_interrupts();
    59. LOG("Startup...\r\n");
    60. DUMP(testBuffer,sizeof(testBuffer));
    61. init_gpio();
    62. set_timer();
    63. OLED_ColorTurn(0);//0正常显示,1 反色显示
    64. OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
    65. while (1)
    66. {
    67. GPIO_WriteReverse(LED_GPIO_PORT,LED_PIN);
    68. isKeyDown = GPIO_ReadInputPin(BUTTON_GPIO_PORT,BUTTON_PIN) == RESET;
    69. LOG(" app_sdcc == %u -- %lu, key %s\r\n",k++,count, isKeyDown ? "down" : "up");
    70. if (isKeyDown == TRUE)
    71. {
    72. if (++screenCfg > 3)
    73. {
    74. screenCfg = 0;
    75. }
    76. set_screen_color_and_display(screenCfg);
    77. OLED_Clear();
    78. }
    79. OLED_ShowString(25,0,"Pegasus Board",8);
    80. OLED_ShowString(35,2,"2023/09/20",8);
    81. OLED_ShowString(20,4,isKeyDown ? "Button Down" : "Button Up ",8);
    82. OLED_ShowNum(103,4,k,3,8);
    83. OLED_ShowString(0,6,"ASCII:",8);
    84. OLED_ShowString(63,6,"CODE:",8);
    85. OLED_ShowChar(48,6,t,8);
    86. t++;
    87. if(t>'~')t=' ';
    88. OLED_ShowNum(103,6,t,3,8);
    89. delay_ms(500);
    90. }
    91. }
    92. #ifdef USE_FULL_ASSERT
    93. /**
    94. * @brief Reports the name of the source file and the source line number
    95. * where the assert_param error has occurred.
    96. * @param file: pointer to the source file name
    97. * @param line: assert_param error line source number
    98. * @retval None
    99. */
    100. void assert_failed(uint8_t* file, uint32_t line)
    101. {
    102. /* User can add his own implementation to report the file name and line number,
    103. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    104. LOG("Wrong parameters value: file %s on line %d\r\n", file, line);
    105. /* Infinite loop */
    106. while (1)
    107. {
    108. }
    109. }
    110. #endif

    运行效果

    让Pegasus天马座开发板用上OLED屏

    工程分享

    工程下载路径

  • 相关阅读:
    Oracle Primavera P6V7 SQL异常案例
    VIT(Vision Transformer)学习-模型理解(一)
    git常用的几个命令
    【毕业设计】基于Django和协同过滤的电影推荐系统
    关于SQL优化
    Go项目目录结构介绍
    『无为则无心』Python面向对象 — 53、对Python中封装的介绍
    就是比某老师详细(反PPT系列)系列———自定义类型(下)
    设计模式之解释器模式
    Win11一键重装系统后如何使用自带的故障检测修复功能
  • 原文地址:https://blog.csdn.net/lan120576664/article/details/133085146