stm32F103VB使用uGfx驱动sh1106

前端之家收集整理的这篇文章主要介绍了stm32F103VB使用uGfx驱动sh1106前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

IAR7.4+STM32CUBEMX调试通过。

显示部分,作为麦知智能小车的一部分。显示屏是OLED 1.3寸,控制器是sh1106,但像素是128*64,价格达到惊人的45元/片。


只提供代码,而不同时说明硬件电路图,是导致情景不能复现的主要原因。


这个是委托方提供的原理图和硬件,他每条线都有上拉电阻,显然是打算用模拟SPI。


我在代码里,将cs,dc,res配置为开漏输出,或者推挽输出,都可以正常显示图像。

不知道cs,res在推挽模式下,会不会有问题,所以使用cube配置为OD,低速,而SCLK,SDA默认为PP(配置SPI的默认设置)


与ugfx接口的底层驱动:

  1. </pre><p><pre name="code" class="cpp">/*
  2. * This file is subject to the terms of the GFX License. If a copy of
  3. * the license was not distributed with this file,you can obtain one at:
  4. *
  5. * http://ugfx.org/license.html
  6. * file: board_SSD1306.h
  7. * author: yu
  8. * date: 2015.12.14
  9. * site: www.mazclub.com
  10. */
  11.  
  12. /**
  13. * @file boards/addons/gdisp/board_SSD1306_spi.h
  14. * @brief GDISP Graphic Driver subsystem board interface for the SSD1306 display.
  15. *
  16. * @note This file contains a mix of hardware specific and operating system specific
  17. * code. You will need to change it for your cpu and/or operating system.
  18. */
  19.  
  20. #ifndef _GDISP_LLD_BOARD_H
  21. #define _GDISP_LLD_BOARD_H
  22. #include "stm32f1xx_hal.h"
  23. #include "cmsis_os.h"
  24. // The command byte to put on the front of each page line
  25. //#define SSD1306_PAGE_PREFIX 0x40 // Co = 0,D/C = 1
  26.  
  27. // For a multiple display configuration we would put all this in a structure and then
  28. // set g->board to that structure.
  29.  
  30. #define OLED_DC_Port (GPIOC)
  31. #define OLED_DC_Pin (GPIO_PIN_15)
  32. #define OLED_CS_Port (GPIOA)
  33. #define OLED_CS_Pin (GPIO_PIN_8)
  34. #define OLED_RES_Port (GPIOC)
  35. #define OLED_RES_Pin (GPIO_PIN_14)
  36.  
  37. #define OLED_RES(a) if (a) \
  38. HAL_GPIO_WritePin(OLED_RES_Port,OLED_RES_Pin,GPIO_PIN_SET);\
  39. else \
  40. HAL_GPIO_WritePin(OLED_RES_Port,GPIO_PIN_RESET)
  41.  
  42. #define OLED_DC(a) if (a) \
  43. HAL_GPIO_WritePin(OLED_DC_Port,OLED_DC_Pin,GPIO_PIN_SET);\
  44. else \
  45. HAL_GPIO_WritePin(OLED_DC_Port,GPIO_PIN_RESET)
  46. #define OLED_CS(a) if (a) \
  47. HAL_GPIO_WritePin(OLED_CS_Port,OLED_CS_Pin,GPIO_PIN_SET);\
  48. else \
  49. HAL_GPIO_WritePin(OLED_CS_Port,GPIO_PIN_RESET)
  50.  
  51. #define SET_RST HAL_GPIO_WritePin(OLED_RES_Port,GPIO_PIN_SET);
  52. #define CLR_RST HAL_GPIO_WritePin(OLED_RES_Port,GPIO_PIN_RESET);
  53.  
  54. extern SPI_HandleTypeDef hspi1;
  55.  
  56. static void init_board(GDisplay *g) {
  57. // As we are not using multiple displays we set g->board to NULL as we don't use it.
  58. g->board = 0;
  59. }
  60.  
  61. static void post_init_board(GDisplay *g) {
  62. (void) g;
  63. }
  64.  
  65. static void setpin_reset(GDisplay *g,bool_t state) {
  66. (void) g;
  67. if(state)
  68. CLR_RST
  69. else
  70. SET_RST
  71. }
  72.  
  73. static void acquire_bus(GDisplay *g) {
  74. (void) g;
  75. }
  76.  
  77. static void release_bus(GDisplay *g) {
  78. (void) g;
  79. }
  80.  
  81. static void write_cmd(GDisplay *g,uint8_t cmd) {
  82. (void) g;
  83. OLED_DC(0);
  84. OLED_CS(0);
  85. HAL_SPI_Transmit(&hspi1,&cmd,1,1);
  86.  
  87. OLED_CS(1);
  88. }
  89.  
  90. static void write_data(GDisplay *g,uint8_t* data,uint16_t length) {
  91. (void) g;
  92. OLED_DC(1);
  93. OLED_CS(0);
  94. HAL_StatusTypeDef status = HAL_SPI_Transmit(&hspi1,data,length,20);
  95. if (status != HAL_OK) {
  96. HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET);
  97. HAL_Delay(200);
  98. HAL_GPIO_WritePin(GPIOC,GPIO_PIN_RESET);
  99. }
  100. OLED_CS(1);
  101.  
  102. }

ugfx默认支持chibios,是其一部分,使用IAR编译,会有警告,只能忽略,还没看到什么不良影响。


外设初始化代码,由CUBE生成,在此不需列出。


使用demo里面的代码,画一个矩形。

猜你在找的VB相关文章