查看: 1818|回复: 4

【赚周年币】技术贴 在STM32F413开发板上实现OLED屏显示

[复制链接]
  • TA的每日心情
    奋斗
    2023-5-10 20:09
  • 签到天数: 1742 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2016-12-18 10:39:16 | 显示全部楼层 |阅读模式
    分享到:
    对于STM32F413开发板来说,它就像一张可供你实现各种可能的白纸,你总是需要为它配上些什么才好使用。这里我们就为它配上一个使用IIC接口的OLED屏来进行人机交互,
    其使用效果如图所示。

    图片1.png
    OLED屏显示效果图

    OLED屏与STM32F413的连接关系为SCKPA5SDAPA6,故程序的初始定义如下:
    1. //   STM32F413+IIC_OLED
    2. #include "main.h"
    3. #include "oledfont.h"
    4. #include "bmp.h"
    5. #define OLED_MODE 0
    6. #define SIZE 8
    7. #define XLevelL                0x00
    8. #define XLevelH                0x10
    9. #define Max_Column        128
    10. #define Max_Row                64
    11. #define        Brightness        0xFF
    12. #define X_WIDTH             128
    13. #define Y_WIDTH           64

    14. #define OLED_SCLK_Clr() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET)  
    15. #define OLED_SCLK_Set() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET)   

    16. #define OLED_SDIN_Clr() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET)  
    17. #define OLED_SDIN_Set() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET)   

    18. #define OLED_CMD  0       
    19. #define OLED_DATA 1
    复制代码
    为了使OLED屏工作,其初始化的函数为:
    1. //  SSD1306                                            
    2. void OLED_Init(void)
    3. {        
    4.         GPIO_InitTypeDef  GPIO_InitStruct;
    5.         __HAL_RCC_GPIOA_CLK_ENABLE();
    6.         GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;          
    7.         GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
    8.     GPIO_InitStruct.Pull  = GPIO_PULLUP;
    9.         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    10.         HAL_GPIO_Init(GPIOA,   &GPIO_InitStruct);
    11.     OLED_SCLK_Set();
    12.     OLED_SDIN_Set();       
    13.     Delay_1ms(800);             //delay_ms(800);
    14.     OLED_WR_Byte(0xAE,OLED_CMD);//--display off
    15.         OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
    16.         OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
    17.         OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  
    18.         OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
    19.         OLED_WR_Byte(0x81,OLED_CMD); // contract control
    20.         OLED_WR_Byte(0xFF,OLED_CMD);//--128   
    21.         OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap
    22.         OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
    23.         OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
    24.         OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty
    25.         OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction
    26.         OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset
    27.         OLED_WR_Byte(0x00,OLED_CMD);//
    28.        
    29.         OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
    30.         OLED_WR_Byte(0x80,OLED_CMD);//
    31.        
    32.         OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
    33.         OLED_WR_Byte(0x05,OLED_CMD);//
    34.        
    35.         OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
    36.         OLED_WR_Byte(0xF1,OLED_CMD);//
    37.        
    38.         OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
    39.         OLED_WR_Byte(0x12,OLED_CMD);//
    40.        
    41.         OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
    42.         OLED_WR_Byte(0x30,OLED_CMD);//
    43.        
    44.         OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
    45.         OLED_WR_Byte(0x14,OLED_CMD);//
    46.        
    47.         OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
    48. }
    复制代码
    制作上图的显示效果,其主函数的内容如下:
    1. int main(void)
    2. {
    3.     HAL_Init();  
    4.     /* Configure the system clock to 84 MHz */
    5.     SystemClock_Config();
    6.     /* -1- Enable GPIOA Clock (to be able to program the configuration registers) */
    7.         __HAL_RCC_GPIOB_CLK_ENABLE();
    8.     /* -2- Configure PA05 IO in output push-pull mode to drive external LED */
    9.         GPIO_InitStruct.Pin = GPIO_PIN_7;  //  LD2  PB7
    10.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    11.     GPIO_InitStruct.Pull = GPIO_PULLUP;
    12.     GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
    13.     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    14.         OLED_Init();                         
    15.         OLED_Clear();
    16.         OLED_ShowString(2,0,"STM32F413",16);
    17.         OLED_ShowString(2,2,"Nucleo-144",16);
    18.         OLED_ShowString(2,4,"1.5MB Flash",16);
    19.         OLED_ShowString(2,6,"320-KB SRAM",16);

    20.   /* -3- Toggle PA05 IO in an infinite loop */  
    21.   while (1)
    22.   {
    23.     HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);
    24.     /* Insert delay 100 ms */
    25.     HAL_Delay(100);
    26.   }
    27. }
    复制代码
    回复

    使用道具 举报

  • TA的每日心情
    擦汗
    2017-10-15 13:21
  • 签到天数: 183 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2016-12-18 10:58:24 | 显示全部楼层
    谢谢分享                     
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2023-5-10 20:09
  • 签到天数: 1742 天

    连续签到: 1 天

    [LV.Master]伴坛终老

     楼主| 发表于 2016-12-18 11:18:57 | 显示全部楼层
    jackten 发表于 2016-12-18 10:58
    谢谢分享

    感谢支持。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2023-5-10 20:09
  • 签到天数: 1742 天

    连续签到: 1 天

    [LV.Master]伴坛终老

     楼主| 发表于 2016-12-19 09:07:55 | 显示全部楼层
    感谢支持!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-3 21:23
  • 签到天数: 2449 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2016-12-19 09:37:37 | 显示全部楼层
    谢谢分享      
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-26 17:04 , Processed in 0.139886 second(s), 22 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.