找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7761|回复: 5
打印 上一主题 下一主题
收起左侧

51单片机驱动0.96OLED_C51_SPI源代码

  [复制链接]
跳转到指定楼层
楼主
ID:145511 发表于 2018-10-26 22:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
收藏了个好用的,OLED 0.91的代码,给需要的人

单片机源程序如下:
  1. //////////////////////////////////////////////////////////////////////////////////         
  2. //本程序只供学习使用,未经作者许可,不得用于其它任何用途
  3. //中景园电子
  4. //
  5. //  文 件 名   : main.c
  6. //  版 本 号   : v2.0
  7. //  作    者   : HuangKai
  8. //  生成日期   : 2014-0101
  9. //  最近修改   :
  10. //  功能描述   : OLED 4接口演示例程(51系列)
  11. //              说明:
  12. //              ----------------------------------------------------------------
  13. //              GND    电源地
  14. //              VCC  接5V或3.3v电源
  15. //              D0   P1^0(SCL)
  16. //              D1   P1^1(SDA)
  17. //              RES  接P12
  18. //              DC   接P13
  19. //              CS   接P14               
  20. //              ----------------------------------------------------------------
  21. // 修改历史   :
  22. // 日    期   :
  23. // 作    者   : HuangKai
  24. // 修改内容   : 创建文件
  25. //版权所有,盗版必究。
  26. //Copyright(C) 中景园电子2014/3/16
  27. //All rights reserved
  28. //******************************************************************************/。
  29. #include "oled.h"
  30. //#include "stdlib.h"
  31. #include "oledfont.h"           
  32. //#include "delay.h"
  33. //OLED的显存
  34. //存放格式如下.
  35. //[0]0 1 2 3 ... 127       
  36. //[1]0 1 2 3 ... 127       
  37. //[2]0 1 2 3 ... 127       
  38. //[3]0 1 2 3 ... 127       
  39. //[4]0 1 2 3 ... 127       
  40. //[5]0 1 2 3 ... 127       
  41. //[6]0 1 2 3 ... 127       
  42. //[7]0 1 2 3 ... 127                           
  43. void delay_ms(unsigned int ms)
  44. {                        
  45.         unsigned int a;
  46.         while(ms)
  47.         {
  48.                 a=1800;
  49.                 while(a--);
  50.                 ms--;
  51.         }
  52.         return;
  53. }
  54. #if OLED_MODE==1
  55. //向SSD1106写入一个字节。
  56. //dat:要写入的数据/命令
  57. //cmd:数据/命令标志 0,表示命令;1,表示数据;
  58. void OLED_WR_Byte(u8 dat,u8 cmd)
  59. {
  60.         DATAOUT(dat);            
  61.         if(cmd)
  62.           OLED_DC_Set();
  63.         else
  64.           OLED_DC_Clr();                  
  65.         OLED_CS_Clr();
  66.         OLED_WR_Clr();         
  67.         OLED_WR_Set();
  68.         OLED_CS_Set();          
  69.         OLED_DC_Set();         
  70. }                         
  71. #else
  72. //向SSD1306写入一个字节。
  73. //dat:要写入的数据/命令
  74. //cmd:数据/命令标志 0,表示命令;1,表示数据;
  75. void OLED_WR_Byte(u8 dat,u8 cmd)
  76. {       
  77.         u8 i;                          
  78.         if(cmd)
  79.           OLED_DC_Set();
  80.         else
  81.           OLED_DC_Clr();                  
  82.         OLED_CS_Clr();
  83.         for(i=0;i<8;i++)
  84.         {                          
  85.                 OLED_SCLK_Clr();
  86.                 if(dat&0x80)
  87.                         {
  88.                    OLED_SDIN_Set();
  89.                         }
  90. else
  91.                    OLED_SDIN_Clr();
  92.                                 OLED_SCLK_Set();
  93.                 dat<<=1;   
  94.         }                                                   
  95.         OLED_CS_Set();
  96.         OLED_DC_Set();             
  97. }
  98. #endif
  99.         void OLED_Set_Pos(unsigned char x, unsigned char y)
  100. {
  101.         OLED_WR_Byte(0xb0+y,OLED_CMD);
  102.         OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
  103.         OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
  104. }             
  105. //开启OLED显示   
  106. void OLED_Display_On(void)
  107. {
  108.         OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
  109.         OLED_WR_Byte(0X14,OLED_CMD);  //DCDC ON
  110.         OLED_WR_Byte(0XAF,OLED_CMD);  //DISPLAY ON
  111. }
  112. //关闭OLED显示     
  113. void OLED_Display_Off(void)
  114. {
  115.         OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
  116.         OLED_WR_Byte(0X10,OLED_CMD);  //DCDC OFF
  117.         OLED_WR_Byte(0XAE,OLED_CMD);  //DISPLAY OFF
  118. }                                            
  119. //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!          
  120. void OLED_Clear(void)  
  121. {  
  122.         u8 i,n;                    
  123.         for(i=0;i<8;i++)  
  124.         {  
  125.                 OLED_WR_Byte (0xb0+i,OLED_CMD);    //设置页地址(0~7)
  126.                 OLED_WR_Byte (0x00,OLED_CMD);      //设置显示位置—列低地址
  127.                 OLED_WR_Byte (0x10,OLED_CMD);      //设置显示位置—列高地址   
  128.                 for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
  129.         } //更新显示
  130. }


  131. //在指定位置显示一个字符,包括部分字符
  132. //x:0~127
  133. //y:0~63
  134. //mode:0,反白显示;1,正常显示                                 
  135. //size:选择字体 16/12
  136. void OLED_ShowChar(u8 x,u8 y,u8 chr)
  137. {             
  138.         unsigned char c=0,i=0;       
  139.                 c=chr-' ';//得到偏移后的值                       
  140.                 if(x>Max_Column-1){x=0;y=y+2;}
  141.                 if(SIZE ==16)
  142.                         {
  143.                         OLED_Set_Pos(x,y);       
  144.                         for(i=0;i<8;i++)
  145.                         OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
  146.                         OLED_Set_Pos(x,y+1);
  147.                         for(i=0;i<8;i++)
  148.                         OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
  149.                         }
  150.                         else {       
  151.                                 OLED_Set_Pos(x,y+1);
  152.                                 for(i=0;i<6;i++)
  153.                                 OLED_WR_Byte(F6x8[c][i],OLED_DATA);
  154.                                
  155.                         }
  156. }
  157. //m^n函数
  158. u32 oled_pow(u8 m,u8 n)
  159. {
  160.         u32 result=1;         
  161.         while(n--)result*=m;   
  162.         return result;
  163. }                                  
  164. //显示2个数字
  165. //x,y :起点坐标         
  166. //len :数字的位数
  167. //size:字体大小
  168. //mode:模式        0,填充模式;1,叠加模式
  169. //num:数值(0~4294967295);                           
  170. void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2)
  171. {                
  172.         u8 t,temp;
  173.         u8 enshow=0;                                                  
  174.         for(t=0;t<len;t++)
  175.         {
  176.                 temp=(num/oled_pow(10,len-t-1))%10;
  177.                 if(enshow==0&&t<(len-1))
  178.                 {
  179.                         if(temp==0)
  180.                         {
  181.                                 OLED_ShowChar(x+(size2/2)*t,y,' ');
  182.                                 continue;
  183.                         }else enshow=1;
  184.                           
  185.                 }
  186.                  OLED_ShowChar(x+(size2/2)*t,y,temp+'0');
  187.         }
  188. }
  189. //显示一个字符号串
  190. void OLED_ShowString(u8 x,u8 y,u8 *chr)
  191. {
  192.         unsigned char j=0;
  193.         while (chr[j]!='\0')
  194.         {                OLED_ShowChar(x,y,chr[j]);
  195.                         x+=8;
  196.                 if(x>120){x=0;y+=2;}
  197.                         j++;
  198.         }
  199. }
  200. //显示汉字
  201. void OLED_ShowCHinese(u8 x,u8 y,u8 no)
  202. {                                  
  203.         u8 t,adder=0;
  204.         OLED_Set_Pos(x,y);       
  205.     for(t=0;t<16;t++)
  206.                 {
  207.                                 OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
  208.                                 adder+=1;
  209.      }       
  210.                 OLED_Set_Pos(x,y+1);       
  211.     for(t=0;t<16;t++)
  212.                         {       
  213.                                 OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
  214.                                 adder+=1;
  215.       }                                       
  216. }
  217. /***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
  218. void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
  219. {        
  220. unsigned int j=0;
  221. unsigned char x,y;
  222.   
  223.   if(y1%8==0) y=y1/8;      
  224.   else y=y1/8+1;
  225.         for(y=y0;y<y1;y++)
  226.         {
  227.                 OLED_Set_Pos(x0,y);
  228.     for(x=x0;x<x1;x++)
  229.             {      
  230.                     OLED_WR_Byte(BMP[j++],OLED_DATA);                   
  231.             }
  232.         }
  233. }


  234. //初始化SSD1306                                            
  235. void OLED_Init(void)
  236. {



  237.   OLED_RST_Set();
  238.         delay_ms(100);
  239.         OLED_RST_Clr();
  240.         delay_ms(100);
  241.         OLED_RST_Set();
  242.         /*                                  
  243.         OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
  244.         OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
  245.         OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
  246.         OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  247.         OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
  248.         OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
  249.         OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  250.         OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  251.         OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
  252.         OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
  253.         OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
  254.         OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  255.         OLED_WR_Byte(0x00,OLED_CMD);//-not offset
  256.         OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
  257.         OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
  258.         OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
  259.         OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  260.         OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
  261.         OLED_WR_Byte(0x12,OLED_CMD);
  262.         OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
  263.         OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
  264.         OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
  265.         OLED_WR_Byte(0x02,OLED_CMD);//
  266.         OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
  267.         OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
  268.         OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
  269.         OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
  270.         OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
  271.         */

  272.         OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
  273.         OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
  274.         OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
  275.         OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  276.         OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
  277.         OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
  278.         OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  279.         OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  280.         OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
  281.         OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
  282.         OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
  283.         OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  284.         OLED_WR_Byte(0x00,OLED_CMD);//-not offset
  285.         OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
  286.         OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
  287.         OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
  288.         OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  289.         OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
  290.         OLED_WR_Byte(0x12,OLED_CMD);
  291.         OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
  292.         OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
  293.         OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
  294.         OLED_WR_Byte(0x02,OLED_CMD);//
  295.         OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
  296.         OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
  297.         OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
  298.         OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
  299.         OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
  300.        
  301.         OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
  302.         OLED_Clear();
  303.         OLED_Set_Pos(0,0);        
  304. }  
复制代码

所有资料51hei提供下载:
0.96OLED_C51_SPI.rar (128.77 KB, 下载次数: 253)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏10 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:557850 发表于 2019-6-8 19:25 | 只看该作者
多谢分享
回复

使用道具 举报

板凳
ID:585455 发表于 2019-7-22 15:52 | 只看该作者
正想學SPI通信,感謝分享。
回复

使用道具 举报

地板
ID:776844 发表于 2020-6-12 05:01 | 只看该作者

多谢分享
回复

使用道具 举报

5#
ID:754694 发表于 2020-6-24 22:38 来自手机 | 只看该作者
谢谢楼主分享,刚着~
回复

使用道具 举报

6#
ID:151348 发表于 2020-10-11 14:42 | 只看该作者
主控:STC15W408AS,测试可用
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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