小言_互联网的博客

单片机控制OLED12864显示屏之模块化编程

249人阅读  评论(0)

这里将我写的STC12C5A60S2单片机控制OLED12864显示屏的程序共享一下,如有不足,敬请指出!

实验视频:

单片机控制OLED12864显示屏实验效果

代码结构图:

程序下载链接:https://download.csdn.net/download/yagnruinihao/16746777点击下载

测试程序:OLED12864测试.c


  
  1. #include "OLED12864.h"
  2. #include "OLED12864WordCode.h"
  3. #include "common.h"
  4. void DisplayLogoTest(void);
  5. void DisplayAsciiTest(void) ;
  6. void DisplayScrollTest(void);
  7. void DisplayGrid(void);
  8. void DisplayBmp(void);
  9. void main(void)
  10. {
  11. unsigned char i= 0;
  12. unsigned char j= 0 ;
  13. OLED12864_Init(); //OLED初始化
  14. while( 1)
  15. {
  16. DisplayAsciiTest(); //ASCII显示实验
  17. OLED12864_CleanAll();
  18. DisplayScrollTest(); //滚动屏显示实验
  19. OLED12864_CleanAll();
  20. DisplayGrid(); //画棋盘实验
  21. OLED12864_CleanAll();
  22. DisplayBmp(); //图片显示实验
  23. }
  24. }
  25. void DisplayLogoTest(void)
  26. {
  27. unsigned char i ;
  28. //"微创电子"
  29. OLED12864_WriteFont16x16( 0, 8, 0);
  30. OLED12864_WriteFont16x16( 0, 24, 1);
  31. OLED12864_WriteFont16x16( 0, 40, 2);
  32. OLED12864_WriteFont16x16( 0, 56, 3);
  33. OLED12864_WriteFont16x16( 0, 72, 29);
  34. OLED12864_WriteFont16x16( 0, 88, 30);
  35. OLED12864_WriteFont16x16( 0, 104, 31);
  36. //横线
  37. for(i= 0 ; i< 16 ; i++)
  38. OLED12864_WriteFont16x08( 2,i* 8, 13);
  39. }
  40. void DisplayAsciiTest(void)
  41. {
  42. unsigned char i ;
  43. unsigned char bai,shi,ge ;
  44. //"微创电子工作室"+横线
  45. DisplayLogoTest();
  46. //"ASCII"
  47. OLED12864_WriteFont16x08( 4, 0, 33) ;
  48. OLED12864_WriteFont16x08( 4, 8, 51) ;
  49. OLED12864_WriteFont16x08( 4, 16, 35) ;
  50. OLED12864_WriteFont16x08( 4, 24, 41) ;
  51. OLED12864_WriteFont16x08( 4, 32, 41) ;
  52. //"值演示实验"
  53. OLED12864_WriteFont16x16( 4, 48, 4);
  54. OLED12864_WriteFont16x16( 4, 64, 5);
  55. OLED12864_WriteFont16x16( 4, 80, 6);
  56. OLED12864_WriteFont16x16( 4, 96, 7);
  57. OLED12864_WriteFont16x16( 4, 112, 8);
  58. //"For:""Value="
  59. OLED12864_WriteFont16x08( 6, 0, 38); //F
  60. OLED12864_WriteFont16x08( 6, 8, 79); //o
  61. OLED12864_WriteFont16x08( 6, 16, 82); //r
  62. OLED12864_WriteFont16x08( 6, 24, 26); //:
  63. OLED12864_WriteFont16x08( 6, 56, 54); //V
  64. OLED12864_WriteFont16x08( 6, 64, 65); //a
  65. OLED12864_WriteFont16x08( 6, 72, 76); //l
  66. OLED12864_WriteFont16x08( 6, 80, 85); //u
  67. OLED12864_WriteFont16x08( 6, 88, 69); //e
  68. OLED12864_WriteFont16x08( 6, 96, 29); //=
  69. //ASCII字符及其数值
  70. for(i= 0 ; i< 95 ; i++)
  71. {
  72. OLED12864_WriteFont16x08( 6, 32,i);
  73. bai = (i+ 32)% 1000/ 100 ;
  74. shi = (i+ 32)% 100/ 10 ;
  75. ge = (i+ 32)% 10 ;
  76. if(bai)
  77. OLED12864_WriteFont16x08( 6, 104,bai+ 16);
  78. else
  79. OLED12864_WriteFont16x08( 6, 104, 0);
  80. OLED12864_WriteFont16x08( 6, 112,shi+ 16);
  81. OLED12864_WriteFont16x08( 6, 120,ge+ 16);
  82. Delay_SomeNop( 10000);
  83. }
  84. }
  85. void DisplayScrollTest(void)
  86. {
  87. unsigned char i ;
  88. DisplayLogoTest();
  89. //"滚屏显示实验"
  90. OLED12864_WriteFont16x16( 4, 16, 9);
  91. OLED12864_WriteFont16x16( 4, 32, 10);
  92. OLED12864_WriteFont16x16( 4, 48, 11);
  93. OLED12864_WriteFont16x16( 4, 64, 12);
  94. OLED12864_WriteFont16x16( 4, 80, 13);
  95. OLED12864_WriteFont16x16( 4, 96, 14);
  96. //"倒计时:"
  97. OLED12864_WriteFont16x16( 6, 32, 23);
  98. OLED12864_WriteFont16x16( 6, 48, 24);
  99. OLED12864_WriteFont16x16( 6, 64, 25);
  100. OLED12864_WriteFont16x08( 6, 80, 26);
  101. for(i= 0 ; i<= 3 ; i++)
  102. {
  103. OLED12864_WriteFont16x08( 6, 88, 3-i+ 16);
  104. Delay_SomeNop( 40000);
  105. }
  106. //清除最后一行汉字
  107. for(i= 0 ; i < 16 ; i++)
  108. {
  109. OLED12864_WriteFont16x08( 6,i* 8, 0);
  110. }
  111. //开始横向滚屏
  112. OLED12864_HorizontalScrollDisplay(OLED12864_HORIZONTAL_SCROLL_LEFT,OLED12864_PAGE2, OLED12864_PAGE5, OLED12864_INTERVAL_2_FRAMES);
  113. Delay_SomeNop( 120000);
  114. OLED12864_HorizontalScrollDisplay(OLED12864_HORIZONTAL_SCROLL_RIGHT,OLED12864_PAGE2, OLED12864_PAGE5, OLED12864_INTERVAL_2_FRAMES);
  115. Delay_SomeNop( 120000);
  116. OLED12864_HorizontalScrollDisplay(OLED12864_HORIZONTAL_SCROLL_RIGHT,OLED12864_PAGE2, OLED12864_PAGE5, OLED12864_INTERVAL_2_FRAMES);
  117. Delay_SomeNop( 120000);
  118. OLED12864_HorizontalScrollDisplay(OLED12864_HORIZONTAL_SCROLL_LEFT,OLED12864_PAGE2, OLED12864_PAGE5, OLED12864_INTERVAL_2_FRAMES);
  119. Delay_SomeNop( 120000);
  120. OLED12864_StopScroll();
  121. Delay_SomeNop( 120000);
  122. //开始纵向滚屏
  123. OLED12864_VerticalScrollArea( 32, 32);
  124. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE7,OLED12864_PAGE7, OLED12864_INTERVAL_2_FRAMES, 0x01);
  125. Delay_SomeNop( 120000);
  126. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE7,OLED12864_PAGE7, OLED12864_INTERVAL_2_FRAMES, 0x3f);
  127. Delay_SomeNop( 120000);
  128. OLED12864_StopScroll();
  129. Delay_SomeNop( 120000);
  130. //开始斜向滚屏
  131. OLED12864_VerticalScrollArea( 0, 64);
  132. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x01);
  133. Delay_SomeNop( 120000);
  134. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x3f);
  135. Delay_SomeNop( 120000);
  136. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x3f);
  137. Delay_SomeNop( 120000);
  138. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x01);
  139. Delay_SomeNop( 120000);
  140. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x3f);
  141. Delay_SomeNop( 120000);
  142. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x01);
  143. Delay_SomeNop( 120000);
  144. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x01);
  145. Delay_SomeNop( 120000);
  146. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x3f);
  147. Delay_SomeNop( 120000);
  148. OLED12864_StopScroll();
  149. Delay_SomeNop( 120000);
  150. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE4,OLED12864_PAGE7, OLED12864_INTERVAL_2_FRAMES, 0x01);
  151. Delay_SomeNop( 200000);
  152. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE4,OLED12864_PAGE7, OLED12864_INTERVAL_2_FRAMES, 0x01);
  153. Delay_SomeNop( 200000);
  154. OLED12864_StopScroll();
  155. Delay_SomeNop( 200000);
  156. }
  157. void DisplayGrid(void)
  158. {
  159. unsigned char i,j,k;
  160. OLED12864_Fill( 0x0f);
  161. OLED12864_Fill( 0xf0);
  162. for(k= 0;k< 8;k++)
  163. {
  164. for(i= 0 ; i< 8;i++)
  165. {
  166. for(j= 0;j< 8;j++)
  167. {
  168. OLED12864_SetPosition(i , j+k* 16);
  169. OLED12864_WriteData( 0xff);
  170. }
  171. }
  172. }
  173. Delay_SomeNop( 120000);
  174. OLED12864_DisplayNormalOrInverse(OLED12864_SET_INVERSE_DISPLAY);
  175. Delay_SomeNop( 120000);
  176. OLED12864_DisplayNormalOrInverse(OLED12864_SET_NORMAL_DISPLAY);
  177. Delay_SomeNop( 120000);
  178. OLED12864_DisplayNormalOrInverse(OLED12864_SET_INVERSE_DISPLAY);
  179. Delay_SomeNop( 120000);
  180. OLED12864_DisplayNormalOrInverse(OLED12864_SET_NORMAL_DISPLAY);
  181. Delay_SomeNop( 120000);
  182. OLED12864_DisplayNormalOrInverse(OLED12864_SET_INVERSE_DISPLAY);
  183. Delay_SomeNop( 120000);
  184. OLED12864_DisplayNormalOrInverse(OLED12864_SET_NORMAL_DISPLAY);
  185. }
  186. void DisplayBmp(void)
  187. {
  188. OLED12864_DrawBmp( 0, 0, 7, 127 ,BMP1);
  189. OLED12864_VerticalScrollArea( 0, 64);
  190. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x01);
  191. Delay_SomeNop( 120000);
  192. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x3f);
  193. Delay_SomeNop( 120000);
  194. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x3f);
  195. Delay_SomeNop( 120000);
  196. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x01);
  197. Delay_SomeNop( 120000);
  198. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x01);
  199. Delay_SomeNop( 120000);
  200. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x3f);
  201. Delay_SomeNop( 120000);
  202. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x3f);
  203. Delay_SomeNop( 120000);
  204. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x01);
  205. Delay_SomeNop( 120000);
  206. OLED12864_StopScroll();
  207. OLED12864_CleanAll();
  208. OLED12864_DrawBmp( 0, 0, 7, 127 ,BMP2);
  209. Delay_SomeNop( 120000);
  210. OLED12864_DisplayNormalOrInverse(OLED12864_SET_INVERSE_DISPLAY);
  211. Delay_SomeNop( 120000);
  212. OLED12864_DisplayNormalOrInverse(OLED12864_SET_NORMAL_DISPLAY);
  213. Delay_SomeNop( 120000);
  214. OLED12864_DisplayNormalOrInverse(OLED12864_SET_INVERSE_DISPLAY);
  215. Delay_SomeNop( 120000);
  216. OLED12864_DisplayNormalOrInverse(OLED12864_SET_NORMAL_DISPLAY);
  217. Delay_SomeNop( 120000);
  218. OLED12864_CleanAll();
  219. OLED12864_DrawBmp( 0, 0, 7, 127 ,BMP4);
  220. Delay_SomeNop( 120000);
  221. OLED12864_VerticalScrollArea( 0, 64);
  222. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x01);
  223. Delay_SomeNop( 120000);
  224. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x3f);
  225. Delay_SomeNop( 120000);
  226. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x3f);
  227. Delay_SomeNop( 120000);
  228. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x01);
  229. Delay_SomeNop( 120000);
  230. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x01);
  231. Delay_SomeNop( 120000);
  232. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x3f);
  233. Delay_SomeNop( 120000);
  234. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_RIGHT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x3f);
  235. Delay_SomeNop( 120000);
  236. OLED12864_VHScrollDisplay(OLED12864_V_H_SCROLL_LEFT,OLED12864_PAGE0,OLED12864_PAGE7, OLED12864_INTERVAL_3_FRAMES, 0x01);
  237. Delay_SomeNop( 120000);
  238. OLED12864_StopScroll();
  239. OLED12864_CleanAll();
  240. OLED12864_DrawBmp( 0, 0, 7, 127 ,BMP3);
  241. Delay_SomeNop( 120000);
  242. OLED12864_CleanAll() ;
  243. }

/*################OLED12864.h################*/ 


  
  1. #ifndef __OLED12864_H__
  2. #define __OLED12864_H__
  3. #include "common.h"
  4. #define X_WIDTH 128
  5. #define Y_WIDTH 64
  6. //OLED12864的IIC接口硬件地址,可以通过修改硬件后面的0Ω电阻位置修改其硬件地址,在IIC模式下D/C引脚作为从机地址的SA0位
  7. //在SSD1306手册中:In I2C mode, this pin acts as SA0 for slave address selection.
  8. #define OLED12864_DEVICE_ADDRESS1 0x7a//地址一, 0x78 | 0x02
  9. #define OLED12864_DEVICE_ADDRESS2 0x78//地址二,0x78 | 0x00
  10. /*设置默认的硬件地址:OLED12864_DEVICE_ADDRESS1或OLED12864_DEVICE_ADDRESS2即0x78或者0x7a,和硬件电路保持一致*/
  11. #define OLED12864_DEFAULT_DEVICE_ADDRESS OLED12864_DEVICE_ADDRESS2
  12. //读、写操作标志位
  13. #define OLED12864_DEVICE_READ 0x01//读操作
  14. #define OLED12864_DEVICE_WRITE (0x01& (~(0x01<<0)))//写操作
  15. //D/C位选择(数据或命令)
  16. #define OLED12864_DATA_OPERATION 1//数据
  17. #define OLED12864_COMMAND_OPERATION 0//命令
  18. //control byte两种情况基值,表示下一个字节内容是数据还是命令
  19. #define OLED12864_NEXT_BYTE_DATA 0x40 //下一个字节内容为数据
  20. #define OLED12864_NEXT_BYTE_COMMAND (0X04 & (~(0x01<<2)))//下一个字节内容为命令
  21. //OLED12864纵向分为了8个Page,注意编号从0开始
  22. #define OLED12864_PAGE0 0//第一页
  23. #define OLED12864_PAGE1 1//第二页
  24. #define OLED12864_PAGE2 2//第三页
  25. #define OLED12864_PAGE3 3//第四页
  26. #define OLED12864_PAGE4 4//第五页
  27. #define OLED12864_PAGE5 5//第六页
  28. #define OLED12864_PAGE6 6//第七页
  29. #define OLED12864_PAGE7 7//第八页
  30. //OLED12864内存地址设置模式
  31. #define OLED12864_HORIZONTAL_ADDRESS_MODE 0x00//水平地址模式
  32. #define OLED12864_VERTICAL_ADDRESS_MODE 0x01//垂直地址模式
  33. #define OLED12864_PAGE_ADDRESS_MODE 0x02//页地址模式(复位后默认值)
  34. #define OLED12864_INVAILD 0x03//无效设置,不能识别,预留
  35. //VCOMH电源倍率选择
  36. #define OLED12864_VCOMH_DESELECT_LEVEL0 0x00//0.65 X Vcc
  37. #define OLED12864_VCOMH_DESELECT_LEVEL1 0x01//~0.77 X Vcc(复位默认值)
  38. #define OLED12864_VCOMH_DESELECT_LEVEL2 0x02//~0.83 X Vcc
  39. /*--------------------------SSD1306基础命令--------------------------*/
  40. #define OLED12864_SET_CONTRAST_CONTROL 0x81//对比对设置,取值范围1~255,复位值为0x7f
  41. #define OLED12864_ENTIRE_DISPLAY_ON_GDDRAM 0xA4//全局显示关闭,即根据GDDRAM内容显示
  42. #define OLED12864_ENTIRE_DISPLAY_ON_FORCE 0xA5//全局显示开启,即不论GDDRAM内容,而强制所有像素点点亮
  43. #define OLED12864_SET_NORMAL_DISPLAY 0xA6//正常显示,给GDDRAM则0为熄灭像素,1为点亮像素
  44. #define OLED12864_SET_INVERSE_DISPLAY 0xA7//逆显示,反转显示。给GDDRAM则1为熄灭像素,0为点亮像素
  45. #define OLED12864_SET_DISPLAY_OFF 0xAE//关显示,睡眠模式,复位后默认值
  46. #define OLED12864_SET_DISPLAY_ON 0xAF//开显示,普通模式
  47. /*--------------------------SSD1306滚动屏命令--------------------------*/
  48. //SSD1306水平滚动显示命令
  49. #define OLED12864_HORIZONTAL_SCROLL_RIGHT 0x26//设置屏幕持续向右滚动显示
  50. #define OLED12864_HORIZONTAL_SCROLL_LEFT 0x27//设置屏幕持续向左滚动显示
  51. //OLED12864滚动显示间隔(间隔的帧数值越小滚动越快,例如这里间隔2帧时最快,256帧最慢)
  52. #define OLED12864_INTERVAL_5_FRAMES 0//间隔5帧
  53. #define OLED12864_INTERVAL_64_FRAMES 1//间隔64帧
  54. #define OLED12864_INTERVAL_128_FRAMES 2//间隔128帧
  55. #define OLED12864_INTERVAL_256_FRAMES 3//间隔256帧
  56. #define OLED12864_INTERVAL_3_FRAMES 4//间隔3帧
  57. #define OLED12864_INTERVAL_4_FRAMES 5//间隔4帧
  58. #define OLED12864_INTERVAL_25_FRAMES 6//间隔25帧
  59. #define OLED12864_INTERVAL_2_FRAMES 7//间隔2帧
  60. //SSD1306水平和垂直同时滚动显示命令
  61. #define OLED12864_V_H_SCROLL_RIGHT 0x29//持续的垂直、水平向右滚动
  62. #define OLED12864_V_H_SCROLL_LEFT 0x2A//持续的垂直、水平向左滚动
  63. //SSD1306禁止滚动显示、使能滚动显示命令
  64. #define OLED12864_DEACTIVATE_SCROLL 0x2E//禁止滚动显示
  65. #define OLED12864_ATCIVATE_SCROLL 0x2F//使能滚动显示
  66. //设置垂直滚动区域命令
  67. #define OLED12864_SET_VERTICAL_SCROLL_AREA 0xA3//设置垂直滚动范围命令
  68. /*--------------------------SSD1306地址设置命令--------------------------*/
  69. //在页地址模式下,设置起始列地址的命令基值(命令=基值 | 参数值)
  70. #define OLED12864_LOWER_COLUMN_START_ADDRESS_BASE 0x00//页地址模式下设置起始列地址的低半字节的基值(仅用于页地址模式下)
  71. #define OLED12864_HIGHER_COLUMN_START_ADDRESS_BASE 0x10//页地址模式小设置起始列地址的高半字节的基值(仅用于页地址模式下)
  72. //SSD1306寻址设置命令地址
  73. #define OLED12864_SET_MEMORY_ADDRESS_MODE 0x20//设置内存地址模式(复位默认页地址模式)
  74. #define OLED12864_SET_COLUMN_ADDRESS 0x21//设置列起止地址和终止地址(仅用于水平地址模式和垂直地址模式下)
  75. #define OLED12864_SET_PAGE_ADDRESS 0x22//设置页起始地址而后终止地址(仅用于水平地址模式和垂直地址模式下)
  76. /*--------------------------SSD1306硬件配置命令--------------------------*/
  77. #define OLED12864_SET_DISPLAY_START_LINE_BASE 0x40//设置显示起始行命令基值,命令=基值 | 开始行位置
  78. #define OLED12864_SET_SEGMENT_REMAP_NORMAL 0xA0//设置段重映射,列0映射到段0处,即左右正常显示效果
  79. #define OLED12864_SET_SEGMENT_REMAP_FLIP 0xA1//设置段重映射,列127映射到段0处,即左右翻转显示鲜果
  80. #define OLED12864_SET_MULTIPLEX_RATIO 0xA8//设置多路复用率N
  81. #define OLED12864_SET_COM_OUTPUT_SCAN_DIRECTION_NORMAL 0xC0//普通模式,从COM[0]扫描到COM[N-1],即上下正常显示效果
  82. #define OLED12864_SET_COM_OUTPUT_SCAN_DIRECTION_FLIP 0xC8//重新绘制模式,从COM[N-1]扫描到CO0M,即上下翻转显示效果
  83. #define OLED12864_SET_DISPLAY_OFFSET 0xD3//设置垂直移动COM,范围0~63,复位默认值为0
  84. #define OLED12864_SET_COM_PIN_HARDWARE_CONFIGURATION 0xDA//设置COM引脚硬件配置
  85. /*--------------------------SSD1306时序和驱动命令--------------------------*/
  86. #define OLED12864_SET_DISPLAY_CLOCK_DIV_AND_FREQUENCY 0xD5//设置时钟分频因子和频率
  87. #define OLED12864_SET_PRE_CHARGE_PERIOD 0xD9//设置预充电周期
  88. #define OLED12864_SET_VCOMH_DESELECT_LEVEL 0xDB//设置VCOMH电源倍率
  89. #define OLED12864_NOP 0xE3//空操作命令,无任何操作
  90. /*--------------------------SSD1306时序和驱动命令--------------------------*/
  91. #define OLED12864_CHARGE_PUMP_SETING 0x8d//电荷泵的设置
  92. /*--------------------------一些默认设置----------------------------------*/
  93. //默认显示模式:正常显示OLED12864_SET_NORMAL_DISPLAY或者逆显示OLED12864_SET_INVERSE_DISPLAY
  94. #define OLED12864_DEFAULT_DISPLAY_MODE OLED12864_SET_NORMAL_DISPLAY/*默认正常显示*/
  95. //默认是否开启OLED显示功能:OLED12864_SET_DISPLAY_ON或者OLED12864_SET_DISPLAY_OFF
  96. #define OLED12864_DEFAULT_DISPLAY_ON_OR_OFF OLED12864_SET_DISPLAY_ON/*默认开启显示功能*/
  97. //默认驱动路数:全部
  98. #define OLED12864_DEFAULT_MULTIPLEX_RATIO 0x3f/*默认设置驱动路数为63+1(公式为N+1)*/
  99. //默认对比度:0xff最亮
  100. #define OLED12864_DEFAULT_DISPLAY_CONTRAST 0xff
  101. //上、下、左、右翻转显示设置,这里默认上下翻转、左右翻转(这个和看液晶屏方向有关,相对概念,可适当修改)
  102. #define OLED12864_DEFAULT_COM_SCAN_DIRECTION OLED12864_SET_COM_OUTPUT_SCAN_DIRECTION_FLIP
  103. #define OLED12864_DEFAULT_SEGMENT_DIRECTION OLED12864_SET_SEGMENT_REMAP_FLIP
  104. /*****************外部接口函数******************/
  105. //写数据
  106. extern void OLED12864_WriteData(unsigned char DataValue) ;
  107. //写命令
  108. extern void OLED12864_WriteCommand(unsigned char CommandValue) ;
  109. //设置书写位置
  110. extern char OLED12864_SetPosition(unsigned char Page, unsigned char Column) ;
  111. //写16x16的字体(高为16,即两页 ; 宽为16,即16列)
  112. extern char OLED12864_WriteFont16x16(unsigned char Page, unsigned char Column,unsigned char SerialNumber);
  113. //写16x8的字体(高为16,即两页 ; 宽为8,即8列)
  114. extern char OLED12864_WriteFont16x08(unsigned char Page,unsigned char Column,unsigned char SerialNumber);
  115. //OLED12864全屏写入某数据
  116. extern void OLED12864_Fill(unsigned char DataValue) ;
  117. //OLED12864初始化
  118. extern void OLED12864_Init(void) ;
  119. //OLED12864清屏
  120. extern void OLED12864_CleanAll(void) ;
  121. //OLED1864画图
  122. extern char OLED12864_DrawBmp(unsigned char StartPage,unsigned char StartColumn,unsigned char EndPage,unsigned char EndColumn,unsigned char BMP[]);
  123. //OLED12864开始滚动屏幕
  124. extern void OLED12864_StartScroll(void) ;
  125. //OLED12864结束滚动屏幕
  126. extern void OLED12864_StopScroll(void) ;
  127. //水平滚动屏幕显示
  128. extern char OLED12864_HorizontalScrollDisplay(unsigned char LeftOrRight, unsigned char StartPage,
  129. unsigned char EndPage, unsigned char Interval);
  130. //斜方向滚动显示
  131. extern char OLED12864_VHScrollDisplay(unsigned char VHL_or_VHR,unsigned char StartPage,unsigned char EndPage,unsigned char Interval,unsigned char V_offset) ;
  132. //设置垂直滚屏区域
  133. extern char OLED12864_VerticalScrollArea(unsigned char TopRow , unsigned char ScrollRowNumber) ;
  134. //开启全局显示,OLED12864根据GDDRAM内容显示(恢复OLED12864正常显示)
  135. extern void OLED12864_EntireDisplayOnGDDRAM(void) ;
  136. //关闭全局显示,强制点亮所有像素点(不论GDDRAM内容是什么)
  137. extern void OLED12864_EntireDisplayOnForce(void) ;
  138. //设置正常显示或者逆显示(决定高电平亮还是低电平亮)
  139. extern char OLED12864_DisplayNormalOrInverse(unsigned char NormalOrInverse) ;
  140. //开始OLED12864显示
  141. extern void OLED12864_SetDisplayOn(void) ;
  142. //关闭OLED12864显示
  143. extern void OLED12864_SetDisplayOff(void) ;
  144. //设置对比度
  145. extern void OLED12864_SetDisplayContrast(unsigned char Contraset) ;
  146. //设置水平左右是否镜面效果(仅影响后续数据,对GDDRAM内部数据不能产生左右镜面效果)
  147. extern char OLED12864_HorizontalFlip(unsigned char LeftOrRight) ;
  148. //设置垂直上下是否镜面效果(立刻显示效果)
  149. extern char OLED12864_VerticalFlip(unsigned char UpOrDown) ;
  150. #endif /*__OLED12864_H__*/

/*################OLED12864.c部分代码分析################*/ 

这里重点分析书写16x16中文汉字的四种算法(逐行式、逐列式、列行式、行列式)

算法一:逐行式


  
  1. /*逐列式,已实验成功,注意这里需要配合取模软件和OLED12864WordCode中数组*/
  2. for(j= 0;j< 16; j++)
  3. {
  4. OLED12864_SetPosition(Page,Column+j);
  5. OLED12864_WriteData(Font16x16[addr++]);
  6. OLED12864_SetPosition(Page + 1 , Column+j);
  7. OLED12864_WriteData(Font16x16[addr++]);
  8. }

算法二:逐列式


  
  1. /*逐行式(为了便于理解,未优化),已实验成功,注意这里需要配合取模软件和OLED12864WordCode中数组*/
  2. unsigned char temp ;
  3. OLED12864_SetPosition(Page,Column);
  4. for(j= 0;j< 8 ;j++)
  5. {
  6. temp =(((Font16x16[addr+ 0]&( 0x01<<j))>>j)<< 0) | (((Font16x16[addr+ 2]&( 0x01<<j))>>j)<< 1) |(((Font16x16[addr+ 4]&( 0x01<<j))>>j)<< 2) | \
  7. (((Font16x16[addr+ 6]&( 0x01<<j))>>j)<< 3) | (((Font16x16[addr+ 8]&( 0x01<<j))>>j)<< 4) |(((Font16x16[addr+ 10]&( 0x01<<j))>>j)<< 5) | \
  8. (((Font16x16[addr+ 12]&( 0x01<<j))>>j)<< 6) | (((Font16x16[addr+ 14]&( 0x01<<j))>>j)<< 7) ;
  9. OLED12864_WriteData(temp);
  10. }
  11. ++addr;
  12. for(j= 0;j< 8 ;j++)
  13. {
  14. temp =(((Font16x16[addr+ 0]&( 0x01<<j))>>j)<< 0) | (((Font16x16[addr+ 2]&( 0x01<<j))>>j)<< 1) |(((Font16x16[addr+ 4]&( 0x01<<j))>>j)<< 2) | \
  15. (((Font16x16[addr+ 6]&( 0x01<<j))>>j)<< 3) | (((Font16x16[addr+ 8]&( 0x01<<j))>>j)<< 4) |(((Font16x16[addr+ 10]&( 0x01<<j))>>j)<< 5) | \
  16. (((Font16x16[addr+ 12]&( 0x01<<j))>>j)<< 6) | (((Font16x16[addr+ 14]&( 0x01<<j))>>j)<< 7) ;
  17. OLED12864_WriteData(temp);
  18. }
  19. addr += 15;
  20. OLED12864_SetPosition(Page+ 1,Column);
  21. for(j= 0;j< 8 ;j++)
  22. {
  23. temp =(((Font16x16[addr+ 0]&( 0x01<<j))>>j)<< 0) | (((Font16x16[addr+ 2]&( 0x01<<j))>>j)<< 1) |(((Font16x16[addr+ 4]&( 0x01<<j))>>j)<< 2) | \
  24. (((Font16x16[addr+ 6]&( 0x01<<j))>>j)<< 3) | (((Font16x16[addr+ 8]&( 0x01<<j))>>j)<< 4) |(((Font16x16[addr+ 10]&( 0x01<<j))>>j)<< 5) | \
  25. (((Font16x16[addr+ 12]&( 0x01<<j))>>j)<< 6) | (((Font16x16[addr+ 14]&( 0x01<<j))>>j)<< 7) ;
  26. OLED12864_WriteData(temp);
  27. }
  28. ++addr;
  29. for(j= 0;j< 8 ;j++)
  30. {
  31. temp =(((Font16x16[addr+ 0]&( 0x01<<j))>>j)<< 0) | (((Font16x16[addr+ 2]&( 0x01<<j))>>j)<< 1) |(((Font16x16[addr+ 4]&( 0x01<<j))>>j)<< 2) | \
  32. (((Font16x16[addr+ 6]&( 0x01<<j))>>j)<< 3) | (((Font16x16[addr+ 8]&( 0x01<<j))>>j)<< 4) |(((Font16x16[addr+ 10]&( 0x01<<j))>>j)<< 5) | \
  33. (((Font16x16[addr+ 12]&( 0x01<<j))>>j)<< 6) | (((Font16x16[addr+ 14]&( 0x01<<j))>>j)<< 7) ;
  34. OLED12864_WriteData(temp);
  35. }

算法三:列行式(作者最后采用的方式)


  
  1. /*列行式,已实验成功,注意这里需要配合取模软件和OLED12864WordCode中数组*/
  2. OLED12864_SetPosition(Page,Column);
  3. for(j= 0;j< 16; j++)
  4. OLED12864_WriteData(Font16x16[addr++]); //写字上半部分
  5. OLED12864_SetPosition(Page+ 1,Column);
  6. for(j= 0;j< 16; j++)
  7. OLED12864_WriteData(Font16x16[addr++]); //写字下半部分

算法四:行列式


  
  1. /*行列式(为了便于理解,未优化,已实验成功),注意这里需要配合取模软件和OLED12864WordCode中数组*/
  2. unsigned char temp ;
  3. OLED12864_SetPosition(Page,Column);
  4. for(j= 0;j< 8; j++)
  5. {
  6. temp = (((Font16x16[addr+ 0]&( 0x01<<j))>>j)<< 0) | (((Font16x16[addr+ 1]&( 0x01<<j))>>j)<< 1) |(((Font16x16[addr+ 2]&( 0x01<<j))>>j)<< 2) | \
  7. (((Font16x16[addr+ 3]&( 0x01<<j))>>j)<< 3) | (((Font16x16[addr+ 4]&( 0x01<<j))>>j)<< 4) |(((Font16x16[addr+ 5]&( 0x01<<j))>>j)<< 5) | \
  8. (((Font16x16[addr+ 6]&( 0x01<<j))>>j)<< 6) | (((Font16x16[addr+ 7]&( 0x01<<j))>>j)<< 7);
  9. OLED12864_WriteData(temp) ;
  10. }
  11. OLED12864_SetPosition(Page+ 1,Column);
  12. addr += 8 ;
  13. for(j= 0;j< 8; j++)
  14. {
  15. temp = (((Font16x16[addr+ 0]&( 0x01<<j))>>j)<< 0) | (((Font16x16[addr+ 1]&( 0x01<<j))>>j)<< 1) |(((Font16x16[addr+ 2]&( 0x01<<j))>>j)<< 2) | \
  16. (((Font16x16[addr+ 3]&( 0x01<<j))>>j)<< 3) | (((Font16x16[addr+ 4]&( 0x01<<j))>>j)<< 4) |(((Font16x16[addr+ 5]&( 0x01<<j))>>j)<< 5) | \
  17. (((Font16x16[addr+ 6]&( 0x01<<j))>>j)<< 6) | (((Font16x16[addr+ 7]&( 0x01<<j))>>j)<< 7);
  18. OLED12864_WriteData(temp) ;
  19. }
  20. addr += 8 ;
  21. OLED12864_SetPosition(Page,Column+ 8);
  22. for(j= 0;j< 8; j++)
  23. {
  24. temp = (((Font16x16[addr+ 0]&( 0x01<<j))>>j)<< 0) | (((Font16x16[addr+ 1]&( 0x01<<j))>>j)<< 1) |(((Font16x16[addr+ 2]&( 0x01<<j))>>j)<< 2) | \
  25. (((Font16x16[addr+ 3]&( 0x01<<j))>>j)<< 3) | (((Font16x16[addr+ 4]&( 0x01<<j))>>j)<< 4) |(((Font16x16[addr+ 5]&( 0x01<<j))>>j)<< 5) | \
  26. (((Font16x16[addr+ 6]&( 0x01<<j))>>j)<< 6) | (((Font16x16[addr+ 7]&( 0x01<<j))>>j)<< 7);
  27. OLED12864_WriteData(temp) ;
  28. }
  29. addr += 8 ;
  30. OLED12864_SetPosition(Page+ 1,Column+ 8);
  31. for(j= 0;j< 8; j++)
  32. {
  33. temp = (((Font16x16[addr+ 0]&( 0x01<<j))>>j)<< 0) | (((Font16x16[addr+ 1]&( 0x01<<j))>>j)<< 1) |(((Font16x16[addr+ 2]&( 0x01<<j))>>j)<< 2) | \
  34. (((Font16x16[addr+ 3]&( 0x01<<j))>>j)<< 3) | (((Font16x16[addr+ 4]&( 0x01<<j))>>j)<< 4) |(((Font16x16[addr+ 5]&( 0x01<<j))>>j)<< 5) | \
  35. (((Font16x16[addr+ 6]&( 0x01<<j))>>j)<< 6) | (((Font16x16[addr+ 7]&( 0x01<<j))>>j)<< 7);
  36. OLED12864_WriteData(temp) ;
  37. }

【注意】这里采用那种算法,需要和取模软件相配套。

 


转载:https://blog.csdn.net/yagnruinihao/article/details/115842157
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场