找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机IIC驱动OLED显示的Proteus仿真不亮的问题

[复制链接]
跳转到指定楼层
楼主
今天用中景园的OLED图做了一个仿真,但不知道哪里有问题,麻烦有知道的大佬指点一下

1、仿真软件用的是proteus8
代码如下:
  1. #include "reg52.h"

  2. // ------------------------------------------------------------
  3. // IO口模拟I2C通信
  4. // SCL接P1^3
  5. // SDA接P1^2
  6. // ------------------------------------------------------------
  7. sbit SCL=P2^0; //串行时钟
  8. sbit SDA=P2^1; //串行数据

  9. #define high 1
  10. #define low 0

  11. #define        Brightness        0xCF
  12. #define X_WIDTH         128
  13. #define Y_WIDTH         64
  14. /*********************OLED驱动程序用的延时程序************************************/
  15. void delay(unsigned int z)
  16. {
  17.         unsigned int x,y;
  18.         for(x=z;x>0;x--)
  19.                 for(y=110;y>0;y--);
  20. }

  21. /**********************************************
  22. //IIC Start
  23. **********************************************/
  24. void IIC_Start()
  25. {
  26.    SCL = high;               
  27.    SDA = high;
  28.    SDA = low;
  29.    SCL = low;
  30. }

  31. /**********************************************
  32. //IIC Stop
  33. **********************************************/
  34. void IIC_Stop()
  35. {
  36.    SCL = low;
  37.    SDA = low;
  38.    SCL = high;
  39.    SDA = high;
  40. }

  41. /**********************************************
  42. // 通过I2C总线写一个字节
  43. **********************************************/
  44. void Write_IIC_Byte(unsigned char IIC_Byte)
  45. {
  46.         unsigned char i;
  47.         for(i=0;i<8;i++)
  48.         {
  49.                 if(IIC_Byte & 0x80)
  50.                         SDA=high;
  51.                 else
  52.                         SDA=low;
  53.                 SCL=high;
  54.                 SCL=low;
  55.                 IIC_Byte<<=1;
  56.         }
  57.         SDA=1;
  58.         SCL=1;
  59.         SCL=0;
  60. }

  61. /*********************OLED写数据************************************/
  62. void OLED_WrDat(unsigned char IIC_Data)
  63. {
  64.         IIC_Start();
  65.         Write_IIC_Byte(0x78);
  66.         Write_IIC_Byte(0x40);                        //write data
  67.         Write_IIC_Byte(IIC_Data);
  68.         IIC_Stop();
  69. }
  70. /*********************OLED写命令************************************/
  71. void OLED_WrCmd(unsigned char IIC_Command)
  72. {
  73.         IIC_Start();
  74.         Write_IIC_Byte(0x78);            //Slave address,SA0=0
  75.         Write_IIC_Byte(0x00);                        //write command
  76.         Write_IIC_Byte(IIC_Command);
  77.         IIC_Stop();
  78. }
  79. /*********************OLED 设置坐标************************************/
  80. void OLED_Set_Pos(unsigned char x, unsigned char y)
  81. {
  82.         OLED_WrCmd(0xb0+y);
  83.         OLED_WrCmd(((x&0xf0)>>4)|0x10);
  84.         OLED_WrCmd((x&0x0f)|0x01);
  85. }
  86. /*********************OLED全屏************************************/
  87. void OLED_Fill(unsigned char bmp_dat)
  88. {
  89.         unsigned char y,x;
  90.         for(y=0;y<8;y++)
  91.         {
  92.                 OLED_WrCmd(0xb0+y);
  93.                 OLED_WrCmd(0x01);
  94.                 OLED_WrCmd(0x10);
  95.                 for(x=0;x<X_WIDTH;x++)
  96.                 OLED_WrDat(bmp_dat);
  97.         }
  98. }
  99. /*********************OLED复位************************************/
  100. void OLED_CLS(void)
  101. {
  102.         unsigned char y,x;
  103.         for(y=0;y<8;y++)
  104.         {
  105.                 OLED_WrCmd(0xb0+y);
  106.                 OLED_WrCmd(0x01);
  107.                 OLED_WrCmd(0x10);
  108.                 for(x=0;x<X_WIDTH;x++)
  109.                 OLED_WrDat(0);
  110.         }
  111. }
  112. /*********************OLED初始化************************************/
  113. void OLED_Init(void)
  114. {
  115.         delay(500);//初始化之前的延时很重要!
  116.         OLED_WrCmd(0xae);//--turn off oled panel
  117.         OLED_WrCmd(0x00);//---set low column address
  118.         OLED_WrCmd(0x10);//---set high column address
  119.         OLED_WrCmd(0x40);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  120.         OLED_WrCmd(0x81);//--set contrast control register
  121.         OLED_WrCmd(Brightness); // Set SEG Output Current Brightness
  122.         OLED_WrCmd(0xa1);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  123.         OLED_WrCmd(0xc8);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  124.         OLED_WrCmd(0xa6);//--set normal display
  125.         OLED_WrCmd(0xa8);//--set multiplex ratio(1 to 64)
  126.         OLED_WrCmd(0x3f);//--1/64 duty
  127.         OLED_WrCmd(0xd3);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  128.         OLED_WrCmd(0x00);//-not offset
  129.         OLED_WrCmd(0xd5);//--set display clock divide ratio/oscillator frequency
  130.         OLED_WrCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
  131.         OLED_WrCmd(0xd9);//--set pre-charge period
  132.         OLED_WrCmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  133.         OLED_WrCmd(0xda);//--set com pins hardware configuration
  134.         OLED_WrCmd(0x12);
  135.         OLED_WrCmd(0xdb);//--set vcomh
  136.         OLED_WrCmd(0x40);//Set VCOM Deselect Level
  137.         OLED_WrCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
  138.         OLED_WrCmd(0x02);//
  139.         OLED_WrCmd(0x8d);//--set Charge Pump enable/disable
  140.         OLED_WrCmd(0x14);//--set(0x10) disable
  141.         OLED_WrCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
  142.         OLED_WrCmd(0xa6);// Disable Inverse Display On (0xa6/a7)
  143.         OLED_WrCmd(0xaf);//--turn on oled panel
  144.         OLED_Fill(0x00); //初始清屏
  145.         OLED_Set_Pos(0,0);
  146. }
  147. /***************功能描述:显示6*8一组标准ASCII字符串        显示的坐标(x,y),y为页范围0~7****************/
  148. void OLED_P6x8Str(unsigned char x, y,unsigned char ch[])
  149. {
  150.         unsigned char c=0,i=0,j=0;
  151.         while (ch[j]!='\0')
  152.         {
  153.                 c =ch[j]-32;
  154.                 if(x>126){x=0;y++;}
  155.                 OLED_Set_Pos(x,y);
  156.                 for(i=0;i<6;i++)
  157.                 OLED_WrDat(F6x8[c][i]);
  158.                 x+=6;
  159.                 j++;
  160.         }
  161. }
  162. /*******************功能描述:显示8*16一组标准ASCII字符串         显示的坐标(x,y),y为页范围0~7****************/
  163. void OLED_P8x16Str(unsigned char x, y,unsigned char ch[])
  164. {
  165.         unsigned char c=0,i=0,j=0;
  166.         while (ch[j]!='\0')
  167.         {
  168.                 c =ch[j]-32;
  169.                 if(x>120){x=0;y++;}
  170.                 OLED_Set_Pos(x,y);
  171.                 for(i=0;i<8;i++)
  172.                 OLED_WrDat(F8X16[c*16+i]);
  173.                 OLED_Set_Pos(x,y+1);
  174.                 for(i=0;i<8;i++)
  175.                 OLED_WrDat(F8X16[c*16+i+8]);
  176.                 x+=8;
  177.                 j++;
  178.         }
  179. }

  180. void OLED_ShowChar(unsigned char x,unsigned char y,unsigned char chr,unsigned char Char_Size)
  181. {              
  182.         unsigned char c=0,i=0;        
  183.                 c=chr-' ';//得到偏移后的值                        
  184.                 if(x>X_WIDTH-1){x=0;y=y+2;}
  185.                 if(Char_Size ==16)
  186.                         {
  187.                         OLED_Set_Pos(x,y);        
  188.                         for(i=0;i<8;i++)
  189.                         OLED_WrDat(F8X16[c*16+i]);
  190.                         OLED_Set_Pos(x,y+1);
  191.                         for(i=0;i<8;i++)
  192.                         OLED_WrDat(F8X16[c*16+i+8]);
  193.                         }
  194.                         else {        
  195.                                 OLED_Set_Pos(x,y);
  196.                                 for(i=0;i<6;i++)
  197.                                 OLED_WrDat(F6x8[c][i]);
  198.                                 
  199.                         }
  200. }

  201. void OLED_ShowCharf(unsigned char x,unsigned char y,unsigned char chr,unsigned char Char_Size)
  202. {              
  203.         unsigned char c=0,i=0;        
  204.                 c=chr-' ';//得到偏移后的值                        
  205.                 if(x>X_WIDTH-1){x=0;y=y+2;}
  206.                 if(Char_Size ==16)
  207.                         {
  208.                         OLED_Set_Pos(x,y);        
  209.                         for(i=0;i<8;i++)
  210.                         OLED_WrDat(~F8X16[c*16+i]);
  211.                         OLED_Set_Pos(x,y+1);
  212.                         for(i=0;i<8;i++)
  213.                         OLED_WrDat(~F8X16[c*16+i+8]);
  214.                         }
  215.                         else {        
  216.                                 OLED_Set_Pos(x,y);
  217.                                 for(i=0;i<6;i++)
  218.                                 OLED_WrDat(~F6x8[c][i]);
  219.                                 
  220.                         }
  221. }

  222. /*****************功能描述:显示16*16点阵  显示的坐标(x,y),y为页范围0~7****************************/
  223. void OLED_P16x16Ch(unsigned char x, y, N)
  224. {
  225.         unsigned char wm=0;
  226.         unsigned int adder=32*N;
  227.         OLED_Set_Pos(x , y);
  228.         for(wm = 0;wm < 16;wm++)
  229.         {
  230.                 OLED_WrDat(F16x16[adder]);
  231.                 adder += 1;
  232.         }
  233.         OLED_Set_Pos(x,y + 1);
  234.         for(wm = 0;wm < 16;wm++)
  235.         {
  236.                 OLED_WrDat(F16x16[adder]);
  237.                 adder += 1;
  238.         }                  
  239. }

  240. void OLED_P16x16Chf(unsigned char x, y, N)
  241. {
  242.         unsigned char wm=0;
  243.         unsigned int adder=32*N;
  244.         OLED_Set_Pos(x , y);
  245.         for(wm = 0;wm < 16;wm++)
  246.         {
  247.                 OLED_WrDat(~F16x16[adder]);
  248.                 adder += 1;
  249.         }
  250.         OLED_Set_Pos(x,y + 1);
  251.         for(wm = 0;wm < 16;wm++)
  252.         {
  253.                 OLED_WrDat(~F16x16[adder]);
  254.                 adder += 1;
  255.         }                  
  256. }

  257. //显示32*32的汉字
  258. void OLED_ShowCHinese32(unsigned char x,unsigned char y,unsigned char no)
  259. {                                 
  260.   unsigned char t, i;
  261.   for (i = 0; i < 4; i++)
  262.   {
  263.     OLED_Set_Pos(x, y + i);
  264.     for(t = 0; t < 32; t++)
  265.     {
  266.           OLED_WrDat(Hzk32[no][32 * i + t]);
  267.     }
  268.   }
  269. }

  270. //显示64*64的汉字
  271. void OLED_ShowCHinese64(unsigned char x,unsigned char y,unsigned char no)
  272. {                                 
  273.   unsigned char t, i;
  274.   for (i = 0; i < 8; i++)
  275.   {
  276.     OLED_Set_Pos(x, y + i);
  277.     for(t = 0; t < 64; t++)
  278.     {
  279.           OLED_WrDat(Hzk64[no][64 * i + t]);
  280.     }
  281.   }
  282. }

  283. /***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
  284. void Draw_BMP(unsigned char x0, y0,x1, y1,unsigned char BMP[])
  285. {
  286.         unsigned int j=0;
  287.         unsigned char x,y;

  288.   if(y1%8==0) y=y1/8;      
  289.   else y=y1/8+1;
  290.         for(y=y0;y<y1;y++)
  291.         {
  292.                 OLED_Set_Pos(x0,y);
  293.     for(x=x0;x<x1;x++)
  294.             {      
  295.                     OLED_WrDat(BMP[j++]);
  296.             }
  297.         }
  298. }



  299. //#include "reg52.h"
  300. #include "intrins.h"
  301. #include "stdio.h"
  302. #include "codetab.h"
  303. #include "LQ12864.h"

  304. int  count=51;
  305. float count2=3.14159;

  306. void main(void)
  307. {
  308.         unsigned char i;
  309.         unsigned char s[3];
  310.         float t[5];

  311.         OLED_Init(); //OLED初始化

  312.         //delay(8000);
  313.         while(1)
  314.         {
  315.                 OLED_Fill(0xff); //屏全亮
  316.                 delay(2000);
  317.                 OLED_CLS();//清屏

  318.                 OLED_Fill(0xaa);
  319.                 delay(300);
  320.                 OLED_Fill(0x55);
  321.                 delay(300);
  322.                 OLED_Fill(0xaa);
  323.                 delay(300);
  324.                 OLED_Fill(0x55);
  325.                 delay(300);
  326.                 OLED_Fill(0xaa);
  327.                 delay(300);
  328.                 OLED_Fill(0x55);
  329.                 delay(300);

  330.                 OLED_Fill(0x00);
  331.                 delay(300);
  332.                 OLED_Fill(0x01);
  333.                 delay(300);
  334.                 OLED_Fill(0x03);
  335.                 delay(300);
  336.                 OLED_Fill(0x07);
  337.                 delay(300);
  338.                 OLED_Fill(0x0f);
  339.                 delay(300);
  340.                 OLED_Fill(0x1f);
  341.                 delay(300);
  342.                 OLED_Fill(0x3f);
  343.                 delay(300);
  344.                 OLED_Fill(0x7f);
  345.                 delay(300);
  346.                 OLED_Fill(0xff);
  347.                 delay(300);

  348.                 OLED_Fill(0x00); //屏全灭
  349.                 delay(200);
  350.                
  351.                 OLED_ShowCHinese64(64*0,0,0);
  352.                 OLED_ShowCHinese64(64*1,0,1);
  353.                 delay(2000);
  354.                 OLED_CLS();//清屏


  355.                 OLED_ShowCHinese32(32*0,0,0);
  356.                 OLED_ShowCHinese32(32*1,0,1);
  357.                 OLED_ShowCHinese32(32*2,0,2);
  358.                 OLED_ShowCHinese32(32*3,0,3);
  359.                 OLED_ShowCHinese32(32*0,4,4);
  360.                 OLED_ShowCHinese32(32*1,4,5);
  361.                 OLED_ShowCHinese32(32*2,4,6);
  362.                 OLED_ShowCHinese32(32*3,4,7);
  363.                 delay(4000);
  364.                 OLED_CLS();//清屏
  365.                
  366.                 OLED_P16x16Ch(16*2,0,2);
  367.                 OLED_P16x16Ch(16*3,0,3);
  368.                 OLED_P16x16Ch(16*4,0,4);
  369.                 OLED_P16x16Ch(16*5,0,5);
  370.                 delay(500);
  371.                 OLED_P16x16Chf(16*2,0,2);
  372.                 OLED_P16x16Chf(16*3,0,3);
  373.                 OLED_P16x16Chf(16*4,0,4);
  374.                 OLED_P16x16Chf(16*5,0,5);
  375.                 delay(500);
  376.                 OLED_P16x16Ch(16*2,0,2);
  377.                 OLED_P16x16Ch(16*3,0,3);
  378.                 OLED_P16x16Ch(16*4,0,4);
  379.                 OLED_P16x16Ch(16*5,0,5);
  380.                 delay(500);
  381.                 OLED_P16x16Chf(16*2,0,2);
  382.                 OLED_P16x16Chf(16*3,0,3);
  383.                 OLED_P16x16Chf(16*4,0,4);
  384.                 OLED_P16x16Chf(16*5,0,5);
  385.                 delay(500);
  386.                 OLED_P16x16Ch(16*2,0,2);
  387.                 OLED_P16x16Ch(16*3,0,3);
  388.                 OLED_P16x16Ch(16*4,0,4);
  389.                 OLED_P16x16Ch(16*5,0,5);
  390.                 delay(500);
  391.                 OLED_P16x16Chf(16*2,0,2);
  392.                 OLED_P16x16Chf(16*3,0,3);
  393.                 OLED_P16x16Chf(16*4,0,4);
  394.                 OLED_P16x16Chf(16*5,0,5);
  395.                 delay(500);
  396.                 OLED_CLS();//清屏
  397.                
  398.                
  399.                 for(i=0; i<8; i++)//通过点整显示汉字 -- i表示字表数组的位置
  400.                 {
  401.                         OLED_P16x16Ch(i*16,0,i);
  402.                          OLED_P16x16Ch(i*16,2,i+8);
  403.                          OLED_P16x16Ch(i*16,4,i+16);
  404.                          OLED_P16x16Ch(i*16,6,i+24);
  405.                 }
  406.                 OLED_P8x16Str(80,4,"51");
  407.                 delay(8000);
  408.                 OLED_CLS();//清屏

  409.                 OLED_ShowChar(8*8,0,'0'+count/10%10,16);
  410.                 OLED_ShowChar(8*9,0,'0'+count%10,16);

  411.                 OLED_ShowCharf(8*14-1,0,'0'+count/10%10,16);
  412.                 OLED_ShowCharf(8*15-1,0,'0'+count%10,16);

  413.                 sprintf(s,"Hum:%d",count);
  414.                 OLED_P8x16Str(0,0,s);
  415.         
  416.                 sprintf(t,"Temp:%.4f", count2);//产生:"3.1416"        
  417.                 OLED_P8x16Str(0,2,t);

  418.                 OLED_P8x16Str(0,4," Keil uvision4");
  419.                 OLED_P6x8Str(0,6,"  Altium Designer   ");
  420.                 OLED_P6x8Str(0,7," Proteus7  Proteus8 ");
  421.                 delay(5000);
  422.                 OLED_CLS();

  423.                 Draw_BMP(0,0,128,8,BMP1);  //图片显示(图片显示慎用,生成的字表较大,会占用较多空间,FLASH空间8K以下慎用)
  424.                 delay(3000);
  425.                 Draw_BMP(0,0,128,8,BMP2);
  426.                 delay(3000);

  427.                 OLED_CLS();//清屏
  428.         }
  429. }
复制代码



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

使用道具 举报

沙发
ID:96682 发表于 2019-6-20 03:51 | 只看该作者
楼主的仿真图就没对
回复

使用道具 举报

板凳
ID:453205 发表于 2019-6-20 22:00 | 只看该作者
wc86110 发表于 2019-6-20 03:51
楼主的仿真图就没对

帮帮忙,看看哪里出问题
回复

使用道具 举报

地板
ID:401564 发表于 2019-6-21 10:14 | 只看该作者
这个公司的OLED我用过,但驱动是1106的,但实际上是一样的,就是初始化有点不同,但是是可以显示的。
你不要去完全的复制别人的代码,你只要看一下别人的算法就可以,初始化函数可以是复制的,然后自己想办法只显示一个“1”或者是“A”之类的,只要是能让它显示一个你想要的显示的字,你就会知道怎么操作这屏幕了。
这是IIC屏幕,就4根线,你还要仿真,我都是直接插上就可以烧录了
回复

使用道具 举报

5#
ID:294123 发表于 2020-2-11 18:42 | 只看该作者
Y_G_G 发表于 2019-6-21 10:14
这个公司的OLED我用过,但驱动是1106的,但实际上是一样的,就是初始化有点不同,但是是可以显示的。
你不 ...

因为作业要求要仿真呀
回复

使用道具 举报

6#
ID:735675 发表于 2020-4-22 22:46 | 只看该作者
本帖最后由 Grey_World 于 2020-4-25 00:23 编辑

这样接线,地址0x78.

1.jpg (755.16 KB, 下载次数: 63)

1.jpg
回复

使用道具 举报

7#
ID:168565 发表于 2020-9-28 14:47 | 只看该作者
SDA  SCL 反了吧!

2020-9-28 14-45-02.png (29.31 KB, 下载次数: 72)

2020-9-28 14-45-02.png
回复

使用道具 举报

8#
ID:151348 发表于 2020-12-22 09:41 | 只看该作者
renfuquan 发表于 2020-9-28 14:47
SDA  SCL 反了吧!

正解!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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