[研电赛技术支持] IIC协议——OLED屏幕

[复制链接]
 楼主| chenci2013 发表于 2023-11-19 20:22 | 显示全部楼层 |阅读模式

  1. #include "intrins.h"

  2. sbit sda=P0^3;
  3. sbit scl=P0^1;

  4. void i2c_start(void);   
  5. void i2c_stop(void);
  6. char ack(void);
  7. void send_messege_i2c(char messege);
  8. void w_cmd_i2c(char cmd);
  9. void w_data_i2c(char data1);
  10. void init_i2c(void);
  11. void clear_oled(void);

  12. code char tx[]={
  13. 0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
  15. 0x01,0x03,0x03,0x03,0x07,0x07,0x05,0x07,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x01,
  16. 0x05,0x0F,0x0F,0x0F,0x07,0x05,0x07,0x05,0x07,0x07,0x07,0x0F,0x0F,0x0F,0x0F,0x0F,
  17. 0x0F,0x4F,0x0F,0x0F,0x0F,0x0F,0x0F,0x1F,0x1F,0x1F,0x0F,0x0F,0x1F,0x1F,0x1F,0x1F,
  18. 0x1F,0x3F,0x1F,0x1F,0x7F,0x1F,0x1F,0x9F,0x9F,0x3F,0x7F,0x6F,0x6F,0x5F,0x9F,0xBF,
  19. 0xBF,0xBF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  20. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  21. 0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
  27. 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x03,0x1F,0x1F,0x1F,0x3F,0x3F,0x1F,
  28. 0xCF,0xEF,0xF7,0xD7,0xDF,0xFB,0xFF,0x9F,0x9E,0x9F,0x8F,0x8F,0x9F,0x9F,0x9F,0xBF,
  29. 0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
  31. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32. 0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xF0,0xF0,0xF0,0xF8,0xF8,0xF8,0xF8,0xFC,0xFC,
  33. 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFC,0xE0,0xC0,0x80,
  34. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  35. 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  36. 0x00,0x01,0x0C,0x44,0x60,0xF5,0xE0,0xC0,0xC0,0xC0,0xC0,0xC0,0xD8,0xFE,0xE7,0xE5,
  37. 0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  39. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xC0,0x80,0x80,0x00,0x08,0x08,
  40. 0xC8,0x98,0x98,0x88,0x80,0x80,0xC0,0x81,0x81,0xC7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  41. 0xFF,0xDF,0x87,0x87,0xC3,0xC3,0xA3,0x91,0x91,0x11,0x11,0x19,0x99,0x89,0x09,0x09,
  42. 0x01,0x83,0xC3,0xC2,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44. 0x00,0x00,0x00,0x00,0x00,0x03,0x37,0x3D,0xBF,0xBF,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,
  45. 0xFF,0xFF,0x00,0x00,0x20,0x40,0x40,0x40,0x40,0x06,0x06,0x00,0x00,0x00,0x00,0x00,
  46. 0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47. 0x00,0x00,0x00,0x1E,0x3E,0x7C,0x70,0xE0,0xC3,0x1F,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,
  48. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,
  49. 0x7F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  50. 0xFF,0xFF,0xFF,0xFF,0x1F,0x00,0x00,0x40,0x70,0x3C,0x1C,0x00,0x00,0x00,0x00,0x00,
  51. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52. 0x00,0xC0,0xA0,0xC0,0xC0,0x00,0xC5,0xFF,0xFF,0xFF,0xFF,0xFD,0xFD,0xFF,0xFF,0xFF,
  53. 0xFF,0xFF,0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x00,0xC0,0xC0,0x00,0xC0,0xC0,0x00,
  54. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
  55. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x0F,0x3F,0x7F,0xFF,0xFF,
  56. 0xFF,0xFF,0xFF,0xFF,0xDF,0x9F,0x9F,0x1F,0x1F,0x1E,0x9E,0xDE,0xDE,0xDE,0xDE,0xDE,
  57. 0xDE,0x1E,0x1C,0x1C,0x1F,0x1F,0x9F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x3F,0x1F,
  58. 0x1F,0x07,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,
  59. 0x40,0x40,0x00,0x00,0x00,0x00,0x30,0x08,0x08,0x03,0x80,0x80,0x80,0xF8,0xF8,0xFE,
  60. 0xEF,0xC7,0xE7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFD,0xFD,0xFF,0xFF,0xFF,0xFF,
  61. 0xFF,0xFF,0x00,0x00,0xE0,0xEF,0xE0,0xEF,0xE0,0x80,0x0F,0x6F,0x60,0xCF,0xAF,0xBF,
  62. 0x23,0x84,0xC0,0xA0,0xE0,0xE0,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,
  63. 0x02,0x04,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  64. 0x01,0x07,0x0F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是page7的内容

  77. void main(void)
  78. {       
  79.         unsigned char i;
  80.         unsigned int j;
  81.         /*--  文字:  我  --*/
  82. /*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
  83.        
  84.        
  85.         init_i2c();
  86.         clear_oled();   //清屏函数
  87.        
  88.         w_cmd_i2c(0x20);  //选择页寻址模式
  89.         w_cmd_i2c(0x02);
  90.        
  91.         //w_cmd_i2c(0xB0);  //选择page  1011 0xxx
  92.         //w_cmd_i2c(0x00);    //设置起始地址  低位
  93.         //w_cmd_i2c(0x10);    //设置起始地址  高位
  94.         //w_data_i2c(0x80);  //发送数据

  95.         for(i=0;i<8;i++){
  96.                
  97.                 w_cmd_i2c(0xB0+i);    //变换page  共有0-7 8个
  98.                 w_cmd_i2c(0x00);    //设置起始地址  低位
  99.                 w_cmd_i2c(0x10);    //设置起始地址  高位
  100.                 //i=3;
  101.                 for(j=128*i; j<(128*(i+1)); j++){   //变换的是tx中的数据
  102.                         w_data_i2c(tx[j]);  //tx中的数据  
  103.                 }
  104.         }

  105.         while(1){}
  106. }  
  107. //下面是函数封装


  108. void clear_oled(void)
  109. {
  110.         char i;  int j;

  111.         for(i=0;i<8;i++){
  112.                 w_cmd_i2c(0xB0+i);    //变换page
  113.                 w_cmd_i2c(0x00);    //设置起始地址  低位
  114.                 w_cmd_i2c(0x10);    //设置起始地址  高位
  115.                 for(j=0;j<128;j++){
  116.                         w_data_i2c(0x00);  //写0就行  
  117.                 }
  118.         }
  119.        
  120. }

  121. void init_i2c(void)
  122. {
  123.         w_cmd_i2c(0xAE);//--display off
  124.         w_cmd_i2c(0x00);//---set low column address
  125.         w_cmd_i2c(0x10);//---set high column address
  126.         w_cmd_i2c(0x40);//--set start line address
  127.         w_cmd_i2c(0xB0);//--set page address
  128.         w_cmd_i2c(0x81); // contract control
  129.         w_cmd_i2c(0xFF);//--128
  130.         w_cmd_i2c(0xA1);//set segment remap
  131.         w_cmd_i2c(0xA6);//--normal / reverse
  132.         w_cmd_i2c(0xA8);//--set multiplex ratio(1 to 64)
  133.         w_cmd_i2c(0x3F);//--1/32 duty
  134.         w_cmd_i2c(0xC8);//Com scan direction
  135.         w_cmd_i2c(0xD3);//-set display offset
  136.         w_cmd_i2c(0x00);//
  137.         w_cmd_i2c(0xD5);//set osc division
  138.         w_cmd_i2c(0x80);//
  139.         w_cmd_i2c(0xD8);//set area color mode off
  140.         w_cmd_i2c(0x05);//
  141.         w_cmd_i2c(0xD9);//Set Pre-Charge Period
  142.         w_cmd_i2c(0xF1);//
  143.         w_cmd_i2c(0xDA);//set com pin configuartion
  144.         w_cmd_i2c(0x12);//
  145.         w_cmd_i2c(0xDB);//set Vcomh
  146.         w_cmd_i2c(0x30);//
  147.         w_cmd_i2c(0x8D);//set charge pump enable
  148.         w_cmd_i2c(0x14);//
  149.         w_cmd_i2c(0xAF);//--turn on oled panel
  150. }

  151. void w_cmd_i2c(char cmd)
  152. {
  153.         i2c_start();
  154.         send_messege_i2c(0x78);
  155.         ack();
  156.         send_messege_i2c(0x00);
  157.         ack();
  158.         send_messege_i2c(cmd);
  159.         ack();
  160.         i2c_stop();
  161. }

  162. void w_data_i2c(char data1)
  163. {
  164.         i2c_start();
  165.         send_messege_i2c(0x78);
  166.         ack();
  167.         send_messege_i2c(0x40);
  168.         ack();
  169.         send_messege_i2c(data1);
  170.         ack();
  171.         i2c_stop();
  172. }

  173. void i2c_start(void)
  174. {
  175.         scl=1;
  176.         sda=0;       
  177.         sda=1;  //我的  
  178.         /*
  179.         scl=0;  //老师的
  180.         sda=1;
  181.         scl=1;*/
  182.        
  183.         _nop_();   //
  184.         sda=0;
  185.         _nop_();   //
  186.         scl=0;
  187. }

  188. void i2c_stop(void)
  189. {
  190.         scl=0;
  191.         sda=0;
  192.         scl=1;
  193.         _nop_();   //
  194.         sda=1;
  195.         _nop_();   //
  196.         sda=0;  //老师不要这个
  197. }

  198. char ack(void)      //应答信号
  199. {
  200.         char flag;
  201.         sda=1;
  202.         //_nop_();  //老师要的
  203.         scl=1;
  204.         _nop_();
  205.         flag=sda;
  206.         //_nop_();  //老师要的
  207.         scl=0;
  208.         _nop_();
  209.         return flag;
  210. }

  211. void send_messege_i2c(char messege)
  212. {
  213.         char i;
  214.        
  215.         for(i=0;i<8;i++){
  216.                 scl=0;
  217.                 _nop_();   //必要
  218.                 sda= messege & 0x80 ;       
  219.                 //_nop_();  //老师要的
  220.                 scl=1;
  221.                 _nop_();
  222.                 scl=0;
  223.                 messege=messege<<1;
  224.         }
  225. }
  226. 原文链接:https://blog.csdn.net/m0_67794575/article/details/130902543


chenqianqian 发表于 2023-11-22 08:39 来自手机 | 显示全部楼层
IIC速率还是慢了点
您需要登录后才可以回帖 登录 | 注册

本版积分规则

124

主题

6644

帖子

4

粉丝
快速回复 在线客服 返回列表 返回顶部