找回密码
 立即注册

QQ登录

只需一步,快速开始

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

OLED SPI模式 STC51单片机C语言库文件下载

[复制链接]
跳转到指定楼层
楼主
ID:249366 发表于 2018-10-11 20:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
适用于市面上128x64OLED模块,SPI模式

oled_spi.h 单片机源程序如下:

  1. /*OLED控制用函数
  2. void OLED_WR_Byte(u8 dat,u8 cmd);            
  3. void OLED_Display_On(void);
  4. void OLED_Display_Off(void);                                                                                          
  5. void OLED_Init(void);
  6. void OLED_Clear(void);
  7. void OLED_DrawPoint(u8 x,u8 y,u8 t);
  8. void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot);
  9. void OLED_ShowChar(u8 x,u8 y,u8 chr);
  10. void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2);
  11. void OLED_ShowString(u8 x,u8 y, u8 *p);         
  12. void OLED_Set_Pos(unsigned char x, unsigned char y);
  13. void OLED_ShowCHinese(u8 x,u8 y,u8 no);
  14. void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[]);
  15. */
  16. #include "oledfont_spi.h"
  17. #ifndef __OLED_SPI_H_
  18. #define __OLED_SPI_H_                        
  19. #define  u8 unsigned char
  20. #define  u32 unsigned int
  21. #define OLED_CMD  0        //写命令
  22. #define OLED_DATA 1        //写数据
  23. #define OLED_MODE 0


  24. sbit OLED_CS=P4^4; //片选
  25. sbit OLED_RST =P4^2;//复位
  26. sbit OLED_DC =P4^3;//数据/命令控制
  27. sbit OLED_SCL=P4^0;//时钟 D0(SCLK?
  28. sbit OLED_SDIN=P4^1;//D1(MOSI) 数据


  29. #define OLED_CS_Clr()  OLED_CS=0
  30. #define OLED_CS_Set()  OLED_CS=1

  31. #define OLED_RST_Clr() OLED_RST=0
  32. #define OLED_RST_Set() OLED_RST=1

  33. #define OLED_DC_Clr() OLED_DC=0
  34. #define OLED_DC_Set() OLED_DC=1

  35. #define OLED_SCLK_Clr() OLED_SCL=0
  36. #define OLED_SCLK_Set() OLED_SCL=1

  37. #define OLED_SDIN_Clr() OLED_SDIN=0
  38. #define OLED_SDIN_Set() OLED_SDIN=1;





  39. //OLED模式设置
  40. //0:4线串行模式
  41. //1:并行8080模式

  42. #define SIZE 16
  43. #define XLevelL                0x02
  44. #define XLevelH                0x10
  45. #define Max_Column        128
  46. #define Max_Row                64
  47. #define        Brightness        0xFF
  48. #define X_WIDTH         128
  49. #define Y_WIDTH         64         

  50. unsigned char screen_data_array[8][128];
  51. //-----------------OLED端口定义----------------                                             



  52. void delay_oled_spi(unsigned int ms)
  53. {                        
  54.         unsigned int a;
  55.         while(ms)
  56.         {
  57.                 a=1800;
  58.                 while(a--);
  59.                 ms--;
  60.         }
  61.         return;
  62. }
  63. #if OLED_MODE==1
  64. //向SSD1106写入一个字节。
  65. //dat:要写入的数据/命令
  66. //cmd:数据/命令标志 0,表示命令;1,表示数据;
  67. void OLED_WR_Byte(u8 dat,u8 cmd)
  68. {
  69.         DATAOUT(dat);            
  70.         if(cmd)
  71.           OLED_DC_Set();
  72.         else
  73.           OLED_DC_Clr();                  
  74.         OLED_CS_Clr();
  75.         OLED_WR_Clr();         
  76.         OLED_WR_Set();
  77.         OLED_CS_Set();         
  78.         OLED_DC_Set();         
  79. }                        
  80. #else
  81. //向SSD1306写入一个字节。
  82. //dat:要写入的数据/命令
  83. //cmd:数据/命令标志 0,表示命令;1,表示数据;
  84. void OLED_WR_Byte(u8 dat,u8 cmd)
  85. {        
  86.         u8 i;                          
  87.         if(cmd)
  88.           OLED_DC_Set();
  89.         else
  90.           OLED_DC_Clr();                  
  91.         OLED_CS_Clr();
  92.         for(i=0;i<8;i++)
  93.         {                          
  94.                 OLED_SCLK_Clr();
  95.                 if(dat&0x80)
  96.                         {
  97.                    OLED_SDIN_Set();
  98.                         }
  99. else
  100.                    OLED_SDIN_Clr();
  101.                                 OLED_SCLK_Set();
  102.                 dat<<=1;   
  103.         }                                                   
  104.         OLED_CS_Set();
  105.         OLED_DC_Set();            
  106. }
  107. #endif
  108.         void OLED_Set_Pos(unsigned char x, unsigned char y)
  109. {
  110.         OLED_WR_Byte(0xb0+y,OLED_CMD);
  111.         OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
  112.         OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
  113. }            
  114. //开启OLED显示   
  115. void OLED_Display_On(void)
  116. {
  117.         OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
  118.         OLED_WR_Byte(0X14,OLED_CMD);  //DCDC ON
  119.         OLED_WR_Byte(0XAF,OLED_CMD);  //DISPLAY ON
  120. }
  121. //关闭OLED显示     
  122. void OLED_Display_Off(void)
  123. {
  124.         OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
  125.         OLED_WR_Byte(0X10,OLED_CMD);  //DCDC OFF
  126.         OLED_WR_Byte(0XAE,OLED_CMD);  //DISPLAY OFF
  127. }                                            
  128. //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!         
  129. void OLED_Clear(void)  
  130. {  
  131.         u8 i,n;                    
  132.         for(i=0;i<8;i++)  
  133.         {  
  134.                 OLED_WR_Byte (0xb0+i,OLED_CMD);    //设置页地址(0~7)
  135.                 OLED_WR_Byte (0x00,OLED_CMD);      //设置显示位置—列低地址
  136.                 OLED_WR_Byte (0x10,OLED_CMD);      //设置显示位置—列高地址   
  137.                 for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
  138.         } //更新显示
  139. }


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

  243. void clearScreenData(){
  244.         u8 i,n;                    
  245.         for(i=0;i<8;i++)  
  246.         {  
  247.                 for(n=0;n<128;n++) screen_data_array[i][n]=0x00;
  248.         }
  249. }

  250. void refresh_screen(){
  251.         u8 i,n;                    
  252.         for(i=0;i<8;i++)  
  253.         {  
  254.                 OLED_WR_Byte (0xb0+i,OLED_CMD);    //设置页地址(0~7)
  255.                 OLED_WR_Byte (0x00,OLED_CMD);      //设置显示位置—列低地址
  256.                 OLED_WR_Byte (0x10,OLED_CMD);      //设置显示位置—列高地址   
  257.                 for(n=0;n<128;n++)OLED_WR_Byte(screen_data_array[i][n],OLED_DATA);
  258.         } //更新显示
  259. }


  260. //初始化SSD1306                                            
  261. void OLED_Init(void)
  262. {



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

  298.         OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
  299.         OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
  300.         OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
  301.         OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  302.         OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
  303.         OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
  304.         OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  305.         OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  306.         OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
  307.         OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
  308.         OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
  309.         OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  310.         OLED_WR_Byte(0x00,OLED_CMD);//-not offset
  311.         OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
  312.         OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
  313.         OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
  314.         OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  315.         OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
  316.         OLED_WR_Byte(0x12,OLED_CMD);
  317.         OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
  318.         OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
  319.         OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
  320.         OLED_WR_Byte(0x02,OLED_CMD);//
  321.         OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
  322.         OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
  323.         OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
  324.         OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
  325.         OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
  326.         
  327.         OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
  328.         OLED_Clear();
  329.         OLED_Set_Pos(0,0);         
  330. }  

  331. #endif  
  332.          
复制代码

所有资料51hei提供下载:
oled_spi.zip (2.99 KB, 下载次数: 54)


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

使用道具 举报

沙发
ID:437615 发表于 2019-9-9 13:29 | 只看该作者
找不到文件,用不了
回复

使用道具 举报

板凳
ID:198286 发表于 2019-9-11 19:41 | 只看该作者
我用的是SSD1305驱动 128*32,不知道如何改程序
回复

使用道具 举报

地板
ID:65480 发表于 2019-9-17 22:08 | 只看该作者
非常感谢!学习一下。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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