你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32 8080接口实际使用——OLED SH1108

[复制链接]
STMCU小助手 发布时间:2023-2-1 15:45
之前有项目使用维信诺一款OLED 核心控制器为SH1108,一直埋头搞项目,没有实际做点记录查看SH1108数据手册,感觉这个图很好,把SH1108描述的很形象,这里贴出来

硬件部分

e885e84f425c4da89b58ff015070df9e.png

项目选择是用8080接口,采用IO模拟的方式进行SH1108通信

2cea8b99078641338626dc3f3d18a2b6.png

3ebecca2f7fe4707a2fb6698bfaf9e8b.png

项目使用的OLED屏屏为128 COM× 160 SEG,该屏幕有128 个column与20 个Page,每Page有8 Seg即一共160 Seg。

OLED和STM32的硬件连接,STM32使用外部晶振25Mhz,倍频到72Mhz。

cb5e9e7cbf6c4d2395120f2cb4d553aa.png


软件部分
关于stm32和OLED的管脚定义如下所列
  1. //------------OLED ------------------------------------------------------------------
  2. #define OLED_SH1108_CS_PIN              GPIO_Pin_1                  /* PB.1 -------> OLED_CS */
  3. #define OLED_SH1108_CS_GPIO_PORT        GPIOB                       /* GPIOB */
  4. #define OLED_SH1108_CS_GPIO_CLK         RCC_APB2Periph_GPIOB

  5. #define OLED_SH1108_DC_PIN              GPIO_Pin_13                  /* PB.13 -------> OLED_A0 */
  6. #define OLED_SH1108_DC_GPIO_PORT        GPIOB                       /* GPIOB */
  7. #define OLED_SH1108_DC_GPIO_CLK         RCC_APB2Periph_GPIOB

  8. #define OLED_SH1108_WR_PIN              GPIO_Pin_14                  /* PB.14 -------> OLED_WR */
  9. #define OLED_SH1108_WR_GPIO_PORT        GPIOB                       /* GPIOB */
  10. #define OLED_SH1108_WR_GPIO_CLK         RCC_APB2Periph_GPIOB

  11. #define OLED_SH1108_RD_PIN              GPIO_Pin_15                 /* PB.15 -------> OLED_RD */
  12. #define OLED_SH1108_RD_GPIO_PORT        GPIOB                       /* GPIOB */
  13. #define OLED_SH1108_RD_GPIO_CLK         RCC_APB2Periph_GPIOB

  14. #define OLED_SH1108_RES_PIN              GPIO_Pin_12                /* PB.12 -------> OLED_RES */
  15. #define OLED_SH1108_RES_GPIO_PORT        GPIOB                      /* GPIOB */
  16. #define OLED_SH1108_RES_GPIO_CLK         RCC_APB2Periph_GPIOB

  17. #define OLED_SH1108_D0_PIN              GPIO_Pin_0                 /* PC.0 -------> OLED_D0 */
  18. #define OLED_SH1108_D0_GPIO_PORT        GPIOC                      /* GPIOC */
  19. #define OLED_SH1108_D0_GPIO_CLK         RCC_APB2Periph_GPIOC

  20. #define OLED_SH1108_D1_PIN              GPIO_Pin_1                 /* PC.1 -------> OLED_D1 */
  21. #define OLED_SH1108_D1_GPIO_PORT        GPIOC                      /* GPIOC */
  22. #define OLED_SH1108_D1_GPIO_CLK         RCC_APB2Periph_GPIOC

  23. #define OLED_SH1108_D2_PIN              GPIO_Pin_2                 /* PC.2 -------> OLED_D2 */

  24. #define OLED_SH1108_D3_PIN              GPIO_Pin_3                 /* PC.3 -------> OLED_D3 */

  25. #define OLED_SH1108_D4_PIN              GPIO_Pin_4                 /* PC.4 -------> OLED_D4 */

  26. #define OLED_SH1108_D5_PIN              GPIO_Pin_5                 /* PC.5 -------> OLED_D5 */

  27. #define OLED_SH1108_D6_PIN              GPIO_Pin_6                 /* PC.6 -------> OLED_D6 */

  28. #define OLED_SH1108_D7_PIN              GPIO_Pin_7                 /* PC.7 -------> OLED_D7 */

  29. #define OLED_SH1108_XDATA_GPIO_PORT     GPIOC                       /* GPIOC */
  30. #define OLED_SH1108_XDATA_GPIO_CLK      RCC_APB2Periph_GPIOC

  31. #define OLED_SH1108_RES_LOW()            GPIO_ResetBits(OLED_SH1108_RES_GPIO_PORT, OLED_SH1108_RES_PIN)         
  32. #define OLED_SH1108_RES_HIGH()           GPIO_SetBits(OLED_SH1108_RES_GPIO_PORT, OLED_SH1108_RES_PIN)

  33. #define OLED_SH1108_CS_LOW()            GPIO_ResetBits(OLED_SH1108_CS_GPIO_PORT, OLED_SH1108_CS_PIN)         
  34. #define OLED_SH1108_CS_HIGH()           GPIO_SetBits(OLED_SH1108_CS_GPIO_PORT, OLED_SH1108_CS_PIN)

  35. #define OLED_SH1108_DC_LOW()            GPIO_ResetBits(OLED_SH1108_DC_GPIO_PORT, OLED_SH1108_DC_PIN)         
  36. #define OLED_SH1108_DC_HIGH()           GPIO_SetBits(OLED_SH1108_DC_GPIO_PORT, OLED_SH1108_DC_PIN)

  37. #define OLED_SH1108_WR_LOW()            GPIO_ResetBits(OLED_SH1108_WR_GPIO_PORT, OLED_SH1108_WR_PIN)         
  38. #define OLED_SH1108_WR_HIGH()           GPIO_SetBits(OLED_SH1108_WR_GPIO_PORT, OLED_SH1108_WR_PIN)

  39. #define OLED_SH1108_RD_LOW()            GPIO_ResetBits(OLED_SH1108_RD_GPIO_PORT, OLED_SH1108_RD_PIN)         
  40. #define OLED_SH1108_RD_HIGH()           GPIO_SetBits(OLED_SH1108_RD_GPIO_PORT, OLED_SH1108_RD_PIN)

  41. #define OLED_SH1108_XDATA(x)            GPIO_Write(OLED_SH1108_XDATA_GPIO_PORT, x)
  42. //#define OLED_SH1108_XDATA(x)            GPIOC->ODR=(GPIOC->ODR & 0xff00) | (x & 0x00FF)

  43. #define OLED_SH1108_SCL_LOW()           GPIO_ResetBits(OLED_SH1108_XDATA_GPIO_PORT, OLED_SH1108_D0_PIN)         
  44. #define OLED_SH1108_SCL_HIGH()          GPIO_SetBits(OLED_SH1108_XDATA_GPIO_PORT, OLED_SH1108_D0_PIN)
  45. #define OLED_SH1108_SI_LOW()            GPIO_ResetBits(OLED_SH1108_XDATA_GPIO_PORT, OLED_SH1108_D1_PIN)         
  46. #define OLED_SH1108_SI_HIGH()           GPIO_SetBits(OLED_SH1108_XDATA_GPIO_PORT, OLED_SH1108_D1_PIN)

  47. #define OLED_SH1108_D2_LOW()            GPIO_ResetBits(OLED_SH1108_XDATA_GPIO_PORT, GPIO_Pin_2)         
  48. #define OLED_SH1108_D2_HIGH()           GPIO_SetBits(OLED_SH1108_XDATA_GPIO_PORT, GPIO_Pin_2)

  49. #define OLED_SH1108_D3_LOW()            GPIO_ResetBits(OLED_SH1108_XDATA_GPIO_PORT, GPIO_Pin_3)         
  50. #define OLED_SH1108_D3_HIGH()           GPIO_SetBits(OLED_SH1108_XDATA_GPIO_PORT, GPIO_Pin_3)

  51. #define OLED_SH1108_D4_LOW()            GPIO_ResetBits(OLED_SH1108_XDATA_GPIO_PORT, GPIO_Pin_4)         
  52. #define OLED_SH1108_D4_HIGH()           GPIO_SetBits(OLED_SH1108_XDATA_GPIO_PORT, GPIO_Pin_4)

  53. #define OLED_SH1108_D5_LOW()            GPIO_ResetBits(OLED_SH1108_XDATA_GPIO_PORT, GPIO_Pin_5)         
  54. #define OLED_SH1108_D5_HIGH()           GPIO_SetBits(OLED_SH1108_XDATA_GPIO_PORT, GPIO_Pin_5)

  55. #define OLED_SH1108_D6_LOW()            GPIO_ResetBits(OLED_SH1108_XDATA_GPIO_PORT, GPIO_Pin_6)         
  56. #define OLED_SH1108_D6_HIGH()           GPIO_SetBits(OLED_SH1108_XDATA_GPIO_PORT, GPIO_Pin_6)

  57. #define OLED_SH1108_D7_LOW()            GPIO_ResetBits(OLED_SH1108_XDATA_GPIO_PORT, GPIO_Pin_7)         
  58. #define OLED_SH1108_D7_HIGH()           GPIO_SetBits(OLED_SH1108_XDATA_GPIO_PORT, GPIO_Pin_7)
复制代码

这里为了速度将PC口单独连接到OLED的数据线上,这样可以加快操作OLED的读写速度。

测试main函数,实现了OLED显示汉字和字符串,部分图案显示没有测试
  1. /******************************************************************************
  2. * 函数名称: main()
  3. * 功能描述: OLED测试
  4. * 输入参数: 无
  5. * 输出参数: 无
  6. * 返 回 值: 无
  7. * 其它说明:
  8. * 修改日期      版本号      修改人     修改内容
  9. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  10. * 2013/02/22    V1.0.0.0      
  11. ******************************************************************************/
  12. INT32S main (void)
  13. {
  14.         BSP_Init();        // 系统初始化       
  15.         oled_sh1108_init_program();
  16.         OLED_ShowChinese(1, 20, 4, 1);//主
  17.         OLED_ShowChinese(93, 20, 5, 1);//副
  18.         while(1)
  19.         {               
  20.                 sprintf((char*)oled_display_buf,"    ");
  21.                 oled_sh1108_show_string(61, 5, oled_display_buf, 12);                               
  22.                 sprintf((char*)oled_display_buf,"%4.4d-%2.2d-%2.2d", 2022, 1, 1);
  23.                 oled_sh1108_show_string(1, 5, oled_display_buf, 12);
  24.                 sprintf((char*)oled_display_buf,"%2.2d:%2.2d:%2.2d", 10, 10, 10);
  25.                 oled_sh1108_show_string(105, 5, oled_display_buf, 12);       
  26.                 oled_sh1108_refresh_gram();
  27.         }

  28.         return (0);
  29. }
复制代码


STM32内部缓存定义,每次更改该数据,一定时间,或者及时将该数据刷新​​​​​

  1. /*                  OLED的显存
  2. *                                        存放格式如下.
  3. *                                        [0]0 1 2 3 ... 127       
  4. *                                        [1]0 1 2 3 ... 127       
  5. *                                        [2]0 1 2 3 ... 127       
  6. *
  7. *                                        [19]0 1 2 3 ... 127                   
  8. */
  9. static unsigned char OLED_GRAM[128][20];       
复制代码

刷新缓存到OLED,该函数实现将缓存数据刷新到OLED
  1. /******************************************************************************
  2. * 函数名称: oled_sh1108_all_screen()
  3. * 功能描述: 更新显存到LCD               
  4. * 输入参数: 无
  5. * 输出参数: 无
  6. * 返 回 值: 无
  7. * 其它说明:
  8. * 修改日期      版本号      修改人     修改内容
  9. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  10. * 2013/02/22    V1.0.0.0      
  11. ******************************************************************************/
  12. void oled_sh1108_refresh_gram(void)
  13. {
  14.         unsigned char i, j;                    
  15. //        for(i = 0 ; i < 8; i++)  
  16. //        {  
  17. //                OLED_WR_Byte (0xb0+i,OLED_CMD);    //设置页地址(0~7)
  18. //                OLED_WR_Byte (0x00,OLED_CMD);      //设置显示位置—列低地址
  19. //                OLED_WR_Byte (0x10,OLED_CMD);      //设置显示位置—列高地址   
  20. //                for(n=0;n<128;n++)
  21. //                        OLED_WR_Byte(OLED_GRAM[n][i],OLED_DATA);
  22. //        }
  23.         for(i = 0; i < 20; i++)
  24.         {
  25.                 oled_sh1108_write_c(0xb0);
  26.                 oled_sh1108_write_c(0x00 + i);

  27.                 oled_sh1108_write_c(0x00);
  28.                 oled_sh1108_write_c(0x11);
  29.                 for(j = 0; j < 128; j++)
  30.                 {
  31.                         oled_sh1108_write_d(OLED_GRAM[j][i]);
  32.                 }
  33.         }       
  34. }               
复制代码

————————————————
版权声明:大牛攻城狮


收藏 评论0 发布时间:2023-2-1 15:45

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版