找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6387|回复: 8
收起左侧

SPI OLED的stm32驱动程序源码

  [复制链接]
ID:306762 发表于 2018-9-2 10:15 | 显示全部楼层 |阅读模式
该实验是用stm32f103c8t6驱动spi方式的OLED屏幕,可以显示数字,符号,英文字符串,中文字符串(需要自己做字符库)以及BMP图片(需要自己符库)  (如果对大家有帮助的话,麻烦在论坛里按一下支持给我点点鼓励,谢谢)

单片机源程序如下:
  1. //////////////////////////////////////////////////////////////////////////////////         
  2. //
  3. //  文 件 名   : main.c
  4. //  功能描述   : OLED 接口演示例程
  5. //              说明:
  6. //              ----------------------------------------------------------------
  7. //              GND    电源地
  8. //              VCC  接5V或3.3v电源
  9. //              D0   接PB13(SCL)
  10. //              D1   接PB15(SDA)
  11. //              RES  接系统复位
  12. //              DC   接PB1
  13. //              CS   接PB12            
  14. //              ----------------------------------------------------------------
  15. //******************************************************************************/

  16. #include "delay.h"
  17. #include "sys.h"
  18. #include "oled.h"
  19. #include "bmp.h"
  20. int main(void)
  21. {       
  22.          
  23.   u8 t;
  24.         delay_init();                     //延时函数初始化          
  25.         NVIC_Configuration();          //设置NVIC中断分组2:2位抢占优先级,2位响应优先级         LED_Init();                             //LED端口初始化
  26.         OLED_Init();                        //初始化OLED  
  27.         OLED_Clear()          ;
  28.         t=' ';
  29.         while(1)
  30.         {               
  31.                 OLED_Clear();
  32.                 OLED_ShowCHinese(0,0,0);//你
  33.                 OLED_ShowCHinese(18,0,1);//好
  34.                 OLED_ShowCHinese(36,0,2);//改
  35.                 OLED_ShowCHinese(54,0,3);//变
  36.                 OLED_ShowCHinese(72,0,4);//世
  37.                 OLED_ShowCHinese(90,0,5);//界
  38.     OLED_ShowChar(108,0,'!');//!
  39.                 OLED_ShowString(0,3,"096' OLED TEST");
  40.                 OLED_ShowString(0,6,"ASCII:");  
  41.                 OLED_ShowString(63,6,"CODE:");  
  42.                 OLED_ShowChar(48,6,t);//显示ASCII字符          
  43.                 t++;
  44.                 if(t>'~')t=' ';
  45.                 OLED_ShowNum(103,6,t,3,16);//显示ASCII字符的码值        
  46.                 delay_ms(8000);       
  47.                 delay_ms(8000);
  48.                 delay_ms(8000);
  49.                 delay_ms(8000);
  50.                 delay_ms(8000);
  51.                 delay_ms(8000);
  52.                 OLED_Clear();
  53.                 delay_ms(8000);
  54.                 OLED_DrawBMP(0,0,128,8,BMP1);  //图片显示(图片显示慎用,生成的字表较大,会占用较多空间,FLASH空间8K以下慎用)
  55.                 delay_ms(8000);       
  56.                 delay_ms(8000);
  57.                 delay_ms(8000);
  58.                 delay_ms(8000);
  59.                 delay_ms(8000);
  60.                 delay_ms(8000);
  61.                 OLED_DrawBMP(0,0,128,8,BMP2);
  62.                 delay_ms(8000);       
  63.                 delay_ms(8000);
  64.                 delay_ms(8000);
  65.                 delay_ms(8000);
  66.                 delay_ms(8000);
  67.                 delay_ms(8000);
  68.                 OLED_DrawBMP(0,0,128,8,BMP3);
  69.                 delay_ms(8000);       
  70.                 delay_ms(8000);
  71.                 delay_ms(8000);
  72.                 delay_ms(8000);
  73.                 delay_ms(8000);
  74.                 delay_ms(8000);
  75.                 OLED_DrawBMP(0,0,128,8,BMP4);
  76.                 delay_ms(8000);       
  77.                 delay_ms(8000);
  78.                 delay_ms(8000);
  79.                 delay_ms(8000);
  80.                 delay_ms(8000);
  81.                 delay_ms(8000);
  82.                 OLED_DrawBMP(0,0,128,8,BMP5);
  83.                 delay_ms(8000);       
  84.                 delay_ms(8000);
  85.                 delay_ms(8000);
  86.                 delay_ms(8000);
  87.                 delay_ms(8000);
  88.                 delay_ms(8000);
  89.                 OLED_DrawBMP(0,0,128,8,BMP6);
  90.                 delay_ms(8000);       
  91.                 delay_ms(8000);
  92.                 delay_ms(8000);
  93.                 delay_ms(8000);
  94.                 delay_ms(8000);
  95.                 delay_ms(8000);
  96.         }          
  97.        
  98. }
复制代码
  1. //////////////////////////////////////////////////////////////////////////////////         
  2. //******************************************************************************/

  3. #include "oled.h"
  4. #include "stdlib.h"
  5. #include "oledfont.h"           
  6. #include "delay.h"
  7. //OLED的显存
  8. //存放格式如下.
  9. //[0]0 1 2 3 ... 127       
  10. //[1]0 1 2 3 ... 127       
  11. //[2]0 1 2 3 ... 127       
  12.               
  13. //[3]0 1 2 3 ... 127       
  14. //[4]0 1 2 3 ... 127       
  15. //[5]0 1 2 3 ... 127       
  16. //[6]0 1 2 3 ... 127       
  17. //[7]0 1 2 3 ... 127                           


  18. //向SSD1306写入一个字节。
  19. //dat:要写入的数据/命令
  20. //cmd:数据/命令标志 0,表示命令;1,表示数据;
  21. void OLED_WR_Byte(u8 dat,u8 cmd)
  22. {       
  23.         u8 i;                          
  24.         if(cmd)
  25.           OLED_DC_Set();
  26.         else
  27.           OLED_DC_Clr();                  
  28.         OLED_CS_Clr();
  29.         for(i=0;i<8;i++)
  30.         {                          
  31.                 OLED_SCLK_Clr();
  32.                 if(dat&0x80)
  33.                    OLED_SDIN_Set();
  34.                 else
  35.                    OLED_SDIN_Clr();
  36.                 OLED_SCLK_Set();
  37.                 dat<<=1;   
  38.         }                                                   
  39.         OLED_CS_Set();
  40.         OLED_DC_Set();             
  41. }
  42. void OLED_Set_Pos(unsigned char x, unsigned char y)
  43. {
  44.         OLED_WR_Byte(0xb0+y,OLED_CMD);
  45.         OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
  46.         OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
  47. }             
  48. //开启OLED显示   
  49. void OLED_Display_On(void)
  50. {
  51.         OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
  52.         OLED_WR_Byte(0X14,OLED_CMD);  //DCDC ON
  53.         OLED_WR_Byte(0XAF,OLED_CMD);  //DISPLAY ON
  54. }
  55. //关闭OLED显示     
  56. void OLED_Display_Off(void)
  57. {
  58.         OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
  59.         OLED_WR_Byte(0X10,OLED_CMD);  //DCDC OFF
  60.         OLED_WR_Byte(0XAE,OLED_CMD);  //DISPLAY OFF
  61. }                                            
  62. //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!          
  63. void OLED_Clear(void)  
  64. {  
  65.         u8 i,n;                    
  66.         for(i=0;i<8;i++)  
  67.         {  
  68.                 OLED_WR_Byte (0xb0+i,OLED_CMD);    //设置页地址(0~7)
  69.                 OLED_WR_Byte (0x00,OLED_CMD);      //设置显示位置—列低地址
  70.                 OLED_WR_Byte (0x10,OLED_CMD);      //设置显示位置—列高地址   
  71.                 for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
  72.         } //更新显示
  73. }


  74. //在指定位置显示一个字符,包括部分字符
  75. //x:0~127
  76. //y:0~63
  77. //mode:0,反白显示;1,正常显示                                 
  78. //size:选择字体 16/12
  79. void OLED_ShowChar(u8 x,u8 y,u8 chr)
  80. {             
  81.         unsigned char c=0,i=0;       
  82.                 c=chr-' ';//得到偏移后的值                       
  83.                 if(x>Max_Column-1){x=0;y=y+2;}
  84.                 if(SIZE ==16)
  85.                         {
  86.                         OLED_Set_Pos(x,y);       
  87.                         for(i=0;i<8;i++)
  88.                         OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
  89.                         OLED_Set_Pos(x,y+1);
  90.                         for(i=0;i<8;i++)
  91.                         OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
  92.                         }
  93.                         else {       
  94.                                 OLED_Set_Pos(x,y+1);
  95.                                 for(i=0;i<6;i++)
  96.                                 OLED_WR_Byte(F6x8[c][i],OLED_DATA);
  97.                                
  98.                         }
  99. }
  100. //m^n函数
  101. u32 oled_pow(u8 m,u8 n)
  102. {
  103.         u32 result=1;         
  104.         while(n--)result*=m;   
  105.         return result;
  106. }                                  
  107. //显示2个数字
  108. //x,y :起点坐标         
  109. //len :数字的位数
  110. //size:字体大小
  111. //mode:模式        0,填充模式;1,叠加模式
  112. //num:数值(0~4294967295);                           
  113. void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size)
  114. {                
  115.         u8 t,temp;
  116.         u8 enshow=0;                                                  
  117.         for(t=0;t<len;t++)
  118.         {
  119.                 temp=(num/oled_pow(10,len-t-1))%10;
  120.                 if(enshow==0&&t<(len-1))
  121.                 {
  122.                         if(temp==0)
  123.                         {
  124.                                 OLED_ShowChar(x+(size/2)*t,y,' ');
  125.                                 continue;
  126.                         }else enshow=1;
  127.                           
  128.                 }
  129.                  OLED_ShowChar(x+(size/2)*t,y,temp+'0');
  130.         }
  131. }
  132. //显示一个字符号串
  133. void OLED_ShowString(u8 x,u8 y,u8 *chr)
  134. {
  135.         unsigned char j=0;
  136.         while (chr[j]!='\0')
  137.         {                OLED_ShowChar(x,y,chr[j]);
  138.                         x+=8;
  139.                 if(x>120){x=0;y+=2;}
  140.                         j++;
  141.         }
  142. }
  143. //显示汉字
  144. void OLED_ShowCHinese(u8 x,u8 y,u8 no)
  145. {                                  
  146.         u8 t,adder=0;
  147.         OLED_Set_Pos(x,y);       
  148.     for(t=0;t<16;t++)
  149.                 {
  150.                                 OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
  151.                                 adder+=1;
  152.      }       
  153.                 OLED_Set_Pos(x,y+1);       
  154.     for(t=0;t<16;t++)
  155.                         {       
  156.                                 OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
  157.                                 adder+=1;
  158.       }                                       
  159. }
  160. /***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
  161. void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
  162. {        
  163. unsigned int j=0;
  164. unsigned char x,y;
  165.   
  166.   if(y1%8==0) y=y1/8;      
  167.   else y=y1/8+1;
  168.         for(y=y0;y<y1;y++)
  169.         {
  170.                 OLED_Set_Pos(x0,y);
  171.     for(x=x0;x<x1;x++)
  172.             {      
  173.                     OLED_WR_Byte(BMP[j++],OLED_DATA);                   
  174.             }
  175.         }
  176. }


  177. //初始化SSD1306                                            
  178. void OLED_Init(void)
  179. {        

  180.          
  181.         GPIO_InitTypeDef  GPIO_InitStructure;
  182.        
  183.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);         //使能A端口时钟

  184.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;         
  185.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  //推挽输出
  186.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
  187.         GPIO_Init(GPIOB, &GPIO_InitStructure);          //初始化GPIOD3,6
  188.         GPIO_SetBits(GPIOB,GPIO_Pin_1|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);       




  189. //  OLED_RST_Set();//复位使用系统复位,所有此处不需要再复位
  190. //        delay_ms(100);
  191. //        OLED_RST_Clr();
  192. //        delay_ms(200);
  193. //        OLED_RST_Set();
  194.                                           
  195.         OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
  196.         OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
  197.         OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
  198.         OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  199.         OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
  200.         OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
  201.         OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  202.         OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  203.         OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
  204.         OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
  205.         OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
  206.         OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  207.         OLED_WR_Byte(0x00,OLED_CMD);//-not offset
  208.         OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
  209.         OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
  210.         OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
  211.         OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  212.         OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
  213.         OLED_WR_Byte(0x12,OLED_CMD);
  214.         OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
  215.         OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
  216.         OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
  217.         OLED_WR_Byte(0x02,OLED_CMD);//
  218.         OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
  219.         OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
  220.         OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
  221.         OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
  222.         OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel       
  223.         OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
  224.         OLED_Clear();
  225.         OLED_Set_Pos(0,0);        
  226. }  
复制代码


所有资料51hei提供下载:
spi oled.rar (323.67 KB, 下载次数: 275)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:365823 发表于 2018-10-5 11:57 | 显示全部楼层
感谢分享嘻嘻嘻
回复

使用道具 举报

ID:199427 发表于 2018-11-5 14:16 | 显示全部楼层
谢谢楼主分享的程序代码!
回复

使用道具 举报

ID:421201 发表于 2018-11-5 21:23 | 显示全部楼层
谢谢楼主分享
回复

使用道具 举报

ID:466510 发表于 2019-1-11 15:39 | 显示全部楼层
感谢分享
回复

使用道具 举报

ID:466510 发表于 2019-1-11 15:40 | 显示全部楼层
感谢分享
回复

使用道具 举报

ID:286293 发表于 2019-6-3 09:07 | 显示全部楼层
感谢分享
回复

使用道具 举报

ID:552592 发表于 2019-6-3 11:41 | 显示全部楼层
代码时怎么复制成这个样子的
回复

使用道具 举报

ID:511461 发表于 2019-6-3 19:23 | 显示全部楼层
谢谢大佬分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表