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

【经验分享】STM32智能送药小车:0.96寸7针OLED的配置与编程

[复制链接]
STMCU小助手 发布时间:2022-5-11 13:27
一.OLED的简单说明
0.96寸七针OLED:
CS:OLED片选信号;
RST(RES):硬复位OLED;
DC(RS):命令/数据标志(0,读写命令;1,读写数据)。


0fe5a463022c4bf4ac831a75a882c288.png

接线如下:D0,D1分别接SPI_CLK,SPI_MOSI

VROLAE]V}0CCIO](0GP6)OW.png

二.Cubemx的配置

9Y7YA)H2B@O86~2K)N61NQF.png
6~Y`HLR)JW[_`ZQ{H%1JFB9.png


三.根据时序图写驱动程序及自定义显示程序
驱动程序:
  1. void OLED_WR_Byte(uint8_t dat,uint8_t cmd)
  2. {                                 
  3.         if(cmd)
  4.         {
  5.                 OLED_DC_Set();//命令/数据标志位置为1,则表示传送的是命令字节
  6.         }
  7.          
  8.         else
  9.           OLED_DC_Clr();//命令/数据标志位置为0,则表示传送的是数据字节                  
  10.           OLED_CS_Clr();//片选信号为低,表示选中OLED
  11.           HAL_SPI_Transmit_DMA(&hspi2,&dat,1);//oled.c文件唯一修改的地方,这里是利用了hal库提供的SPI传送函数
  12.           OLED_CS_Set();
  13.           OLED_DC_Set();         
  14. }

  15. void OLED_Set_Pos(unsigned char x, unsigned char y)
  16. {
  17.         OLED_WR_Byte(0xb0+y,OLED_CMD);
  18.         OLED_WR_Byte((((x+2)&0xf0)>>4)|0x10,OLED_CMD);
  19.         OLED_WR_Byte(((x+2)&0x0f),OLED_CMD);
  20. }              
  21. //开启OLED显示
  22. void OLED_Display_On(void)
  23. {
  24.         OLED_WR_Byte(0X8D,OLED_CMD);  //设置电荷泵命令字
  25.         OLED_WR_Byte(0X14,OLED_CMD);  //开启电荷泵
  26.         OLED_WR_Byte(0XAF,OLED_CMD);  //DISPLAY ON
  27. }
  28. //关闭OLED显示
  29. void OLED_Display_Off(void)
  30. {
  31.         OLED_WR_Byte(0X8D,OLED_CMD);  //设置电荷泵命令字
  32.         OLED_WR_Byte(0X10,OLED_CMD);  //关闭电荷泵
  33.         OLED_WR_Byte(0XAE,OLED_CMD);  //DISPLAY OFF
  34. }                                            
  35. //清屏函数,清完后整个屏幕都是黑色的,没有一点光亮
  36. void OLED_Clear(void)  
  37. {  
  38.         uint8_t i,n;                    
  39.         for(i=0;i<8;i++)  
  40.         {  
  41.                 OLED_WR_Byte (0xb0+i,OLED_CMD);    //设置页地址
  42.                 OLED_WR_Byte (0x02,OLED_CMD);      //设置起始列低地址
  43.                 OLED_WR_Byte (0x10,OLED_CMD);      //设置起始列高地址   
  44.                 for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
  45.         } //更新显示
  46. }
复制代码


用户自定义:
  1. //在指定位置显示一个字符,包括部分字符
  2. //x:0~127
  3. //y:0~6
  4. //mode:0,反白显示;1,正常显示                                 
  5. //size:选择字体大小 16/12
  6. void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr)
  7. {              
  8.         unsigned char c=0,i=0;        
  9.                 c=chr-' ';//得到偏移后的值                        
  10.                 if(x>Max_Column-1){x=0;y=y+2;}
  11.                 if(SIZE ==16)
  12.                         {
  13.                         OLED_Set_Pos(x,y);        
  14.                         for(i=0;i<8;i++)
  15.                         OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
  16.                         OLED_Set_Pos(x,y+1);
  17.                         for(i=0;i<8;i++)
  18.                         OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
  19.                         }
  20.                         else {        
  21.                                 OLED_Set_Pos(x,y+1);
  22.                                 for(i=0;i<6;i++)
  23.                                 OLED_WR_Byte(F6x8[c]<i>,OLED_DATA);
  24.                                 
  25.                         }
  26. }
  27. //m^n函数
  28. uint32_t oled_pow(uint8_t m,uint8_t n)
  29. {
  30.         uint32_t result=1;         
  31.         while(n--)result*=m;   
  32.         return result;
  33. }                                 
  34. //显示两个数字
  35. //x,y :起点坐标
  36. //len :数字的位数
  37. //size:字体大小
  38. //mode:0:填充模式;1:叠加模式
  39. //num:数值(0~4294967295);                           
  40. void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size)
  41. {                 
  42.         uint8_t t,temp;
  43.         uint8_t enshow=0;                                                   
  44.         for(t=0;t<len;t++)
  45.         {
  46.                 temp=(num/oled_pow(10,len-t-1))%10;
  47.                 if(enshow==0&&t<(len-1))
  48.                 {
  49.                         if(temp==0)
  50.                         {
  51.                                 OLED_ShowChar(x+(size/2)*t,y,' ');
  52.                                 continue;
  53.                         }else enshow=1;
  54.                           
  55.                 }
  56.                  OLED_ShowChar(x+(size/2)*t,y,temp+'0');
  57.         }
  58. }
  59. //显示一个字符串
  60. void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr)
  61. {
  62.         unsigned char j=0;
  63.         while (chr[j]!='\0')
  64.         {                OLED_ShowChar(x,y,chr[j]);
  65.                         x+=8;
  66.                 if(x>120){x=0;y+=2;}
  67.                         j++;
  68.         }
  69. }
  70. //显示汉字
  71. void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no)
  72. {                                 
  73.         uint8_t t,adder=0;
  74.         OLED_Set_Pos(x,y);        
  75.     for(t=0;t<16;t++)
  76.                 {
  77.                                 OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
  78.                                 adder+=1;
  79.      }        
  80.                 OLED_Set_Pos(x,y+1);        
  81.     for(t=0;t<16;t++)
  82.                         {        
  83.                                 OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
  84.                                 adder+=1;
  85.       }                                       
  86. }
  87. /*显示BMP图片。x的范围为0~127,y的页得的范围0~7*/
  88. void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
  89. {         
  90. unsigned int j=0;
  91. unsigned char x,y;

  92.   if(y1%8==0) y=y1/8;      
  93.   else y=y1/8+1;
  94.         for(y=y0;y<y1;y++)
  95.         {
  96.                 OLED_Set_Pos(x0,y);
  97.     for(x=x0;x<x1;x++)
  98.             {      
  99.                     OLED_WR_Byte(BMP[j++],OLED_DATA);                    
  100.             }
  101.         }
  102. }
  103. //-------------------------------------------------------------------------------------------------------------------
  104. //  @brief      输出单个数字
  105. //  @param      void                     
  106. //  @return                                    
  107. //  @since      v1.0
  108. //  Sample usage:               
  109. //-------------------------------------------------------------------------------------------------------------------
  110. void oled_hexascii(uint16_t hex,int8_t * Print)
  111. {
  112.         uint8_t hexcheck ;
  113.         uint8_t TEMP ;
  114.         TEMP = 6 ;
  115.         Print[TEMP ]='\0';
  116.         while(TEMP)
  117.         {
  118.                   TEMP -- ;
  119.                   hexcheck  =  hex%10 ;
  120.                   hex   /=10 ;
  121.                   Print[TEMP ]  = hexcheck + 0x30 ;
  122.         }

  123. }
  124. //-------------------------------------------------------------------------------------------------------------------
  125. //  @brief      OLED显示字符串(6*8字体)
  126. //  @param      x                        x轴坐标设置0-127
  127. //  @param      y           y轴坐标设置0-7
  128. //  @param      ch[]        字符串
  129. //  @return     void
  130. //  @since      v1.0
  131. //  Sample usage:                        
  132. //-------------------------------------------------------------------------------------------------------------------
  133. void oled_p6x8str(uint8_t x,uint8_t y,const int8_t ch[])
  134. {
  135.         uint8_t c=0,i=0,j=0;
  136.         while (ch[j]!='\0')
  137.         {
  138.                   c =ch[j]-32;
  139.                   if(x>126){x=0;y++;}
  140.                   OLED_Set_Pos(x,y);
  141.                   for(i=0;i<6;i++)        OLED_WR_Byte(oled_6x8[c]<i>,OLED_DATA);
  142.                   x+=6;
  143.                   j++;
  144.         }
  145. }
  146. //-------------------------------------------------------------------------------------------------------------------
  147. //  @brief      OLED显示有符号数(6*8字体)
  148. //  @param      x                        x轴坐标设置0-127
  149. //  @param      y           y轴坐标设置0-7
  150. //  @param      num         有符号数
  151. //  @return     void
  152. //  @since      v1.0
  153. //  Sample usage:                        
  154. //-------------------------------------------------------------------------------------------------------------------
  155. void oled_int16(uint8_t x, uint8_t y, int16_t num)
  156. {
  157.         int8_t ch[7];
  158.         if(num<0)   {num = -num;oled_p6x8str(x, y, "-");}
  159.         else         oled_p6x8str(x, y, " ");
  160.         x+=6;      

  161.         oled_hexascii(num,ch);
  162.     oled_p6x8str(x, y, &ch[1]);            //显示数字  6*8字体
  163. }
  164. float fabs(float num)
  165. {
  166.         if (num >= 0)
  167.         {
  168.                 num = num;
  169.         }
  170.         else
  171.         {
  172.                 num = -num;
  173.         }
  174.         return num;
  175. }
  176. //显示9位字符,最高位正负,三位整数,第五位小数点,后四位小数部分
  177. //x,y :起点坐标         
  178. //len :数字的位数
  179. //size:字体大小                           
  180. void OLED_Showdecimal(uint8_t x,uint8_t y,float num,uint8_t len,uint8_t size2)
  181. {                 
  182.         uint8_t t,temp,len1,temp1;
  183.         float temp2;
  184.         uint8_t enshow=0;
  185.     if(num < 0)
  186.         {
  187.                 OLED_ShowChar(x,y,'0'-3);
  188.         // OLED_ShowChar(x,y,'0'- 3,size2);
  189.                 num =fabs(num);
  190.         }
  191.     else
  192.         OLED_ShowChar(x,y,' ');//第一位显示符号
  193.         temp1 = (int)temp;
  194.         temp2 = num - temp1;
  195.         len1 = len - 6;//len1为整数部分位数,若显示数位需要扩展,修改该行
  196.         OLED_ShowChar(x + size2/2*4,y,'0'- 2);//浮点数的第5位显示小数点
  197.         x = x + size2/2;
  198.         for(t=0;t<len1;t++)//整数部分的显示
  199.         {
  200.                 temp=(int)((num/oled_pow(10,len1-t-1)))%10;
  201.                 if(enshow==0&&t<(len1-1))
  202.                 {
  203.                         if(temp==0)
  204.                         {
  205.                                 OLED_ShowChar(x+(size2/2)*t,y,' ');
  206.                                 continue;
  207.                         }else enshow=1;
  208.                           
  209.                 }
  210.                  OLED_ShowChar(x+(size2/2)*t,y,temp+'0');
  211.         }
  212.         OLED_ShowChar(x+(size2/2)*4,y,((int)(temp2*10)%10) + '0'); //小数第一位
  213.         OLED_ShowChar(x+(size2/2)*5,y,((int)(temp2*100)%10) + '0'); //小数第2位
  214.         OLED_ShowChar(x+(size2/2)*6,y,((int)(temp2*1000)%10) + '0'); //小数第3位
  215.         OLED_ShowChar(x+(size2/2)*7,y,((int)(temp2*10000)%10) + '0'); //小数第4位
  216. }
  217. //初始化SSD1306                                            
  218. void OLED_Init(void)
  219. {         
  220.         
  221.         OLED_RST_Clr();
  222.         HAL_Delay(200);
  223.         OLED_RST_Set();
  224.         
  225.         OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
  226.         OLED_WR_Byte(0x02,OLED_CMD);//---set low column address
  227.         OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
  228.         OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  229.         OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
  230.         OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
  231.         OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0Xa0左右反置 0Xa1正常
  232.         OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0Xc0上下反置 0Xc8正常
  233.         OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
  234.         OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
  235.         OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
  236.         OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  237.         OLED_WR_Byte(0x00,OLED_CMD);//-not offset
  238.         OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
  239.         OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
  240.         OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
  241.         OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  242.         OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
  243.         OLED_WR_Byte(0x12,OLED_CMD);
  244.         OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
  245.         OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
  246.         OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
  247.         OLED_WR_Byte(0x02,OLED_CMD);//
  248.         OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
  249.         OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
  250.         OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
  251.         OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
  252.         OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
  253.         
  254.         OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
  255.         OLED_Clear();
  256. }  


  257. </i></i>
复制代码


收藏 评论0 发布时间:2022-5-11 13:27

举报

0个回答

所属标签

相似分享

官网相关资源

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