当前位置:   article > 正文

51单片机与oled显示屏_基于51单片机的oled的显示屏

基于51单片机的oled的显示屏

根据IIC协议来操作oled 

IIC操作可以看我的上一篇文章:IIC协议,利用io模拟

 1、OLED写命令

传输中的参数介绍 

 SA0:通过翻译可以知道SA0使用来确定地址的,配置0和1分别代表2个不同的地址

原文:The slave address is following the start condition for recognition use. The slave
address is either “b0111100” or “b0111101” by changing the SA0 to LOW or HIGH
(D/C pin acts as SA0).

R/W#:通过翻译可以知道R/W#配置为0时模块变为写模式

原文:The write mode is established by setting the R/W# bit to logic “0”.

CO:通过翻译可以知道当CO被设置为0时表示将开始传输信息了

原文:If the Co bit is set as logic “0”, the transmission of the following information will
contain data bytes only.

D/C:通过翻译可以知道当D/C为0 时下面一个字节传输的是一个命令,当设置为1时传输的字节就是数据

原文: The D/C# bit determines the next data byte is acted as a command or a data. If the
D/C# bit is set to logic “0”, it defines the following data byte as a command. If the
D/C# bit is set to logic “1”, it defines the following data byte as a data which will be
stored at the GDDRAM. The GDDRAM column address pointer will be increased
by one automatically after each data write.

 根据写命令时序图,我们可以分别写出写命令和写指令两个函数

  1. void IIC_Oled_send_byte(char SendByte)//发送数据
  2. {
  3. Start_signal();//起始信号
  4. IIC_send_byte(0x78);//确定地址01111000倒数第二个是地址位倒数第一个0写入模式
  5. IIC_ACK();//应答信号
  6. IIC_send_byte(0x40);//写数据
  7. IIC_ACK();//应答信号
  8. IIC_send_byte(SendByte);//发送一个字节
  9. IIC_ACK();//应答信号
  10. TerminationSignal();//终止信号
  11. }
  12. void IIC_Oled_send_cmd(char Sendcmd)//发送指令
  13. {
  14. Start_signal();//起始信号
  15. IIC_send_byte(0x78);//确定地址
  16. IIC_ACK();//应答信号
  17. IIC_send_byte(0x00);//写指令
  18. IIC_ACK();//应答信号
  19. IIC_send_byte(Sendcmd);//发送一个字节
  20. IIC_ACK();//应答信号
  21. TerminationSignal();//终止信号
  22. }

2、OLED初始化

我们在使用OLED显示屏的时候要先对其进行初始化,初始化的方法文档里有介绍,就是发送一系列指令

  1. void OLED_Initial()//oled初始化发送以下指令
  2. {
  3. //01display off (0xae)
  4. IIC_Oled_send_cmd(0xae);
  5. //02set low column address (0x00)
  6. IIC_Oled_send_cmd(0x00);
  7. //03set high column address (0x10)
  8. IIC_Oled_send_cmd(0x10);
  9. //04set start line address (0x40)
  10. IIC_Oled_send_cmd(0x40);
  11. //05set page address (0xb0)
  12. IIC_Oled_send_cmd(0xb0);
  13. //06)contract control (0x81)
  14. IIC_Oled_send_cmd(0x81);
  15. //07send 0xff (多字节指令)
  16. IIC_Oled_send_cmd(0xff);
  17. //08set segment remap (0xa1)
  18. IIC_Oled_send_cmd(0xa1);
  19. //09set normal/reverse (0xa6)
  20. IIC_Oled_send_cmd(0xa6);
  21. //10set multiplex ratio (1 to 64) (0xa8 )
  22. IIC_Oled_send_cmd(0xa8);
  23. //11set duty 1/32 (0x3f)
  24. IIC_Oled_send_cmd(0x3f);
  25. //12)com scan direction (0xc8)
  26. IIC_Oled_send_cmd(0xc8);
  27. //13set display offset (0xd3)
  28. IIC_Oled_send_cmd(0xd3);
  29. //14send 0x00
  30. IIC_Oled_send_cmd(0x00);
  31. //15set osc division (0xd5)
  32. IIC_Oled_send_cmd(0xd5);
  33. //16send 0x80
  34. IIC_Oled_send_cmd(0x80);
  35. //17set area color mode off (0xd8)
  36. IIC_Oled_send_cmd(0xd8);
  37. //18send 0x05
  38. IIC_Oled_send_cmd(0x05);
  39. //19set pre-charge period (0xd9)
  40. IIC_Oled_send_cmd(0xd9);
  41. //20send 0xf1
  42. IIC_Oled_send_cmd(0xf1);
  43. //21set com pin configuration (0xda)
  44. IIC_Oled_send_cmd(0xda);
  45. //22send 0x12
  46. IIC_Oled_send_cmd(0x12);
  47. //23set Vcomh (0xdb)
  48. IIC_Oled_send_cmd(0xdb);
  49. //24send 0x30
  50. IIC_Oled_send_cmd(0x30);
  51. //25set charge pump enable (0x8d)
  52. IIC_Oled_send_cmd(0x8d);
  53. //26send 0x14
  54. IIC_Oled_send_cmd(0x14);
  55. //27)turn on oled panel(0xaf)
  56. IIC_Oled_send_cmd(0xaf);
  57. }

3、清屏函数

清屏函数实际上是将所有的显示都设置为0,就是灭的状态

  1. void CLS()//清屏函数
  2. {
  3. int i,j;
  4. for(i = 0; i<8 ;i++){
  5. IIC_Oled_send_cmd(0xB0+i);//page0--page7
  6. //每次写的时候从第一列开始
  7. IIC_Oled_send_cmd(0x00);
  8. IIC_Oled_send_cmd(0x10);
  9. for(j = 0;j<128;j++){
  10. IIC_Oled_send_byte(0x00);//每写入数据地址自动偏移
  11. }
  12. }
  13. }

有了以上内容我们就可以对OLED 进行操作了

具体操作步骤

//模块初始化

//设置页寻址模式
//00100000
//0x02
//设置大行64行分为8个大行page
//10110xxx
//配置列,用一个高位一个低位设置
//0000xxxx
//0001xxxx

 寻址模式的设置

设置page

设置页寻址模式下的列起始地址

4、代码实现

  1. #include "reg52.h"
  2. #include "intrins.h"
  3. #include "reg52.h"
  4. sbit scl = P0^0;
  5. sbit sda = P0^1;
  6. void Start_signal()//起始信号
  7. {
  8. scl = 1;
  9. sda = 0;
  10. sda = 1;
  11. _nop_();
  12. sda = 0;
  13. _nop_();
  14. scl = 0;
  15. }
  16. void TerminationSignal()//终止信号
  17. {
  18. scl = 0;
  19. sda = 0;
  20. scl = 1;
  21. _nop_();
  22. sda = 1;
  23. _nop_();
  24. sda = 0;
  25. _nop_();
  26. }
  27. char IIC_ACK()//应答信号
  28. {
  29. char ACK;
  30. scl = 0;
  31. _nop_();
  32. scl = 1;
  33. _nop_();
  34. ACK = sda;
  35. scl = 0;
  36. _nop_();
  37. return ACK;
  38. }
  39. void IIC_send_byte(char SendByte)//发送一个字节
  40. {
  41. int i = 0;
  42. for(;i<8;i++){
  43. scl = 0;//数据翻转
  44. sda = SendByte & 0x80;//发送最高位,发送顺序是由高往低的
  45. _nop_();//数据准备时间
  46. scl = 1;//发送数据
  47. _nop_();//数据发送时间
  48. SendByte<<= 1;
  49. }
  50. }
  51. void IIC_Oled_send_byte(char SendByte)//发送数据
  52. {
  53. Start_signal();//起始信号
  54. IIC_send_byte(0x78);//确定地址01111000倒数第二个是地址位倒数第一个0写入模式
  55. IIC_ACK();//应答信号
  56. IIC_send_byte(0x40);//写数据
  57. IIC_ACK();//应答信号
  58. IIC_send_byte(SendByte);//发送一个字节
  59. IIC_ACK();//应答信号
  60. TerminationSignal();//终止信号
  61. }
  62. void IIC_Oled_send_cmd(char Sendcmd)//发送指令
  63. {
  64. Start_signal();//起始信号
  65. IIC_send_byte(0x78);//确定地址
  66. IIC_ACK();//应答信号
  67. IIC_send_byte(0x00);//写指令
  68. IIC_ACK();//应答信号
  69. IIC_send_byte(Sendcmd);//发送一个字节
  70. IIC_ACK();//应答信号
  71. TerminationSignal();//终止信号
  72. }
  73. void OLED_Initial()//oled初始化发送以下指令
  74. {
  75. //01display off (0xae)
  76. IIC_Oled_send_cmd(0xae);
  77. //02set low column address (0x00)
  78. IIC_Oled_send_cmd(0x00);
  79. //03set high column address (0x10)
  80. IIC_Oled_send_cmd(0x10);
  81. //04set start line address (0x40)
  82. IIC_Oled_send_cmd(0x40);
  83. //05set page address (0xb0)
  84. IIC_Oled_send_cmd(0xb0);
  85. //06)contract control (0x81)
  86. IIC_Oled_send_cmd(0x81);
  87. //07send 0xff (多字节指令)
  88. IIC_Oled_send_cmd(0xff);
  89. //08set segment remap (0xa1)
  90. IIC_Oled_send_cmd(0xa1);
  91. //09set normal/reverse (0xa6)
  92. IIC_Oled_send_cmd(0xa6);
  93. //10set multiplex ratio (1 to 64) (0xa8 )
  94. IIC_Oled_send_cmd(0xa8);
  95. //11set duty 1/32 (0x3f)
  96. IIC_Oled_send_cmd(0x3f);
  97. //12)com scan direction (0xc8)
  98. IIC_Oled_send_cmd(0xc8);
  99. //13set display offset (0xd3)
  100. IIC_Oled_send_cmd(0xd3);
  101. //14send 0x00
  102. IIC_Oled_send_cmd(0x00);
  103. //15set osc division (0xd5)
  104. IIC_Oled_send_cmd(0xd5);
  105. //16send 0x80
  106. IIC_Oled_send_cmd(0x80);
  107. //17set area color mode off (0xd8)
  108. IIC_Oled_send_cmd(0xd8);
  109. //18send 0x05
  110. IIC_Oled_send_cmd(0x05);
  111. //19set pre-charge period (0xd9)
  112. IIC_Oled_send_cmd(0xd9);
  113. //20send 0xf1
  114. IIC_Oled_send_cmd(0xf1);
  115. //21set com pin configuration (0xda)
  116. IIC_Oled_send_cmd(0xda);
  117. //22send 0x12
  118. IIC_Oled_send_cmd(0x12);
  119. //23set Vcomh (0xdb)
  120. IIC_Oled_send_cmd(0xdb);
  121. //24send 0x30
  122. IIC_Oled_send_cmd(0x30);
  123. //25set charge pump enable (0x8d)
  124. IIC_Oled_send_cmd(0x8d);
  125. //26send 0x14
  126. IIC_Oled_send_cmd(0x14);
  127. //27)turn on oled panel(0xaf)
  128. IIC_Oled_send_cmd(0xaf);
  129. }
  130. void CLS()//清屏函数
  131. {
  132. int i,j;
  133. for(i = 0; i<8 ;i++){
  134. IIC_Oled_send_cmd(0xB0+i);//page0--page7
  135. //每次写的时候从第一列开始
  136. IIC_Oled_send_cmd(0x00);
  137. IIC_Oled_send_cmd(0x10);
  138. for(j = 0;j<128;j++){
  139. IIC_Oled_send_byte(0x00);//每写入数据地址自动偏移
  140. }
  141. }
  142. }
  143. /*-- 文字: S --*/
  144. /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
  145. char s1[8]={0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00};
  146. char s2[8]={0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00};
  147. void main()
  148. {
  149. int i;
  150. //1.OLED初始化
  151. OLED_init();
  152. //2.选择一个位置
  153. //2.1 选择寻址模式——确认页寻址模式
  154. OLED_write_order(0x20);
  155. OLED_write_order(0x02);
  156. CLS();
  157. //2.2 选择PAGE0
  158. IIC_Oled_send_cmd(0xb0);
  159. IIC_Oled_send_cmd(0x00);
  160. IIC_Oled_send_cmd(0x10);
  161. //3.显示
  162. for(i = 0;i < 8;i++){
  163. IIC_Oled_send_byte(s1[i]);
  164. }
  165. IIC_Oled_send_cmd(0xb1);
  166. IIC_Oled_send_cmd(0x00);
  167. IIC_Oled_send_cmd(0x10);
  168. for(i = 0;i < 8;i++){
  169. IIC_Oled_send_byte(s2[i]);
  170. }
  171. while(1);
  172. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/598758
推荐阅读
相关标签
  

闽ICP备14008679号