找回密码
 注册

QQ登录

只需一步,快速开始

搜索

STM32的OLED驱动 SPI通讯协议

[复制链接]
coolice 发表于 2021-8-17 11:38:42 | 显示全部楼层 |阅读模式
  1. #include "oled.h"
  2. #include "oledfont.h"                  
  3. //向SSD1306写入一个字节。
  4. //dat:要写入的数据/命令
  5. //cmd:数据/命令标志 0,表示命令;1,表示数据;
  6. void OLED_WriteCmd(unsigned char cmd)
  7. {   
  8.     unsigned char i;               
  9.     DC(0);        
  10.     CS(0);
  11.     for(i=0;i<8;i++)
  12.     {            
  13.         SCL(0);
  14.     if(cmd&0x80)
  15.     {
  16.         DIN(1);
  17.     }
  18.         else
  19.         DIN(0);
  20.         SCL(1);
  21.         cmd<<=1;   
  22.     }                        
  23.     CS(1);
  24.     DC(1);        
  25. }
  26. void OLED_Writebyte(unsigned char dat)
  27. {   
  28. unsigned char i;              
  29. DC(1);        
  30. CS(0);
  31. for(i=0;i<8;i++)
  32. {            
  33. SCL(0);
  34. if(dat&0x80)
  35. {
  36. DIN(1);
  37. }
  38. else
  39. DIN(0);
  40. SCL(1);
  41. dat<<=1;   
  42. }                        
  43. CS(1);
  44. DC(1);        
  45. }
  46. void OLED_Set_Pos(unsigned char x, unsigned char y)
  47. {
  48. OLED_WriteCmd(0xb0+y);
  49. OLED_WriteCmd(((x&0xf0)>>4)|0x10);
  50. OLED_WriteCmd((x&0x0f)|0x01);
  51. }         
  52. //开启OLED显示   
  53. void OLED_Display_On(void)
  54. {
  55. OLED_WriteCmd(0X8D);  //SET DCDC命令
  56. OLED_WriteCmd(0X14);  //DCDC ON
  57. OLED_WriteCmd(0XAF);  //DISPLAY ON
  58. }
  59. //关闭OLED显示     
  60. void OLED_Display_Off(void)
  61. {
  62. OLED_WriteCmd(0X8D);  //SET DCDC命令
  63. OLED_WriteCmd(0X10);  //DCDC OFF
  64. OLED_WriteCmd(0XAE);  //DISPLAY OFF
  65. }                    
  66. //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!     
  67. void OLED_Clear(void)  
  68. {  
  69. unsigned char i,n;         
  70. for(i=0;i<8;i++)  
  71. {  
  72. OLED_WriteCmd (0xb0+i);    //设置页地址(0~7)
  73. OLED_WriteCmd (0x00);      //设置显示位置—列低地址
  74. OLED_WriteCmd (0x10);      //设置显示位置—列高地址   
  75. for(n=0;n<128;n++)OLED_Writebyte(0);
  76. } //更新显示
  77. }


  78. //在指定位置显示一个字符,包括部分字符
  79. //x:0~127
  80. //y:0~63
  81. //mode:0,反白显示;1,正常显示                 
  82. //size:选择字体 16/12
  83. void OLED_WriteChar(unsigned char x,unsigned char y,unsigned char chr)
  84. {      
  85. unsigned char c=0,i=0;  
  86. c=chr-' ';//得到偏移后的值         
  87. if(x>127)
  88. {
  89. x=0;
  90. y=y+2;
  91. }
  92. OLED_Set_Pos(x,y);  
  93. for(i=0;i<8;i++)
  94. OLED_Writebyte(F8X16[c*16+i]);
  95. OLED_Set_Pos(x,y+1);
  96. for(i=0;i<8;i++)
  97. OLED_Writebyte(F8X16[c*16+i+8]);
  98. }
  99. //m^n函数
  100. unsigned int oled_pow(unsigned char m,unsigned char n)
  101. {
  102. unsigned int result=1;   
  103. while(n--)result*=m;   
  104. return result;
  105. }                 
  106. //显示2个数字
  107. //x,y :起点坐标  
  108. //len :数字的位数
  109. //size:字体大小
  110. //mode:模式   0,填充模式;1,叠加模式
  111. //num:数值(0~4294967295);           
  112. void OLED_WriteNum1(unsigned char x,unsigned char y,unsigned int num,unsigned char len,unsigned char size2)
  113. {           
  114. unsigned char t,temp;
  115. unsigned char enWrite=0;                           
  116. for(t=0;t<len;t++)
  117. {
  118. temp=(num/oled_pow(10,len-t-1))%10;
  119. if(enWrite==0&&t<(len-1))
  120. {
  121. if(temp==0)
  122. {
  123. OLED_WriteChar(x+(size2/2)*t,y,' ');
  124. continue;
  125. }else enWrite=1;

  126. }
  127. OLED_WriteChar(x+(size2/2)*t,y,temp+'0');
  128. }

  129. }
  130. void OLED_WriteNum2(unsigned char x,unsigned char y,unsigned int num)
  131. {
  132. num=num+0x30;
  133. OLED_WriteChar(x,y,num);
  134. }
  135. //显示一个字符号串
  136. void OLED_WriteString(unsigned char x,unsigned char y,unsigned char *chr)
  137. {
  138. unsigned char j=0;
  139. while (chr[j]!='\0')
  140. {       OLED_WriteChar(x,y,chr[j]);
  141. x+=8;
  142. if(x>120){x=0;y+=2;}
  143. j++;
  144. }
  145. }
  146. //显示汉字
  147. void OLED_WriteChinese(unsigned char x,unsigned char y,unsigned char no)
  148. {                  
  149. unsigned char t,adder=0;
  150. OLED_Set_Pos(x,y);  
  151. for(t=0;t<16;t++)
  152. {
  153. OLED_Writebyte(Hzk[2*no][t]);
  154. adder+=1;
  155. }   
  156. OLED_Set_Pos(x,y+1);   
  157. for(t=0;t<16;t++)
  158. {   
  159. OLED_Writebyte(Hzk[2*no+1][t]);
  160. adder+=1;
  161. }                  
  162. }
  163. /***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
  164. void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
  165. {   
  166. unsigned int j=0;
  167. unsigned char x,y;

  168. if(y1%8==0) y=y1/8;      
  169. else y=y1/8+1;
  170. for(y=y0;y<y1;y++)
  171. {
  172. OLED_Set_Pos(x0,y);
  173. for(x=x0;x<x1;x++)
  174. {      
  175. OLED_Writebyte(BMP[j++]);           
  176. }
  177. }
  178. }


  179. //初始化SSD1306                        
  180. void OLED_Init(void)
  181. {

  182. GPIO_InitTypeDef  GPIO_InitStructure;//定义GPIO结构体

  183. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);   //使能PC,D,G端口时钟

  184. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7;  //PD3,PD6推挽输出  
  185. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         //推挽输出
  186. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
  187. GPIO_Init(GPIOA, &GPIO_InitStructure);    //初始化GPIOD3,6
  188. GPIO_SetBits(GPIOA,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7);   //PA3,PA6 输出高


  189. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;     //PD3,PD6推挽输出  
  190. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         //推挽输出
  191. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
  192. GPIO_Init(GPIOB, &GPIO_InitStructure);    //初始化GPIOD3,6


  193. RES(1);
  194. delay_ms(100);
  195. RES(0);
  196. delay_ms(100);
  197. RES(1);


  198. OLED_WriteCmd(0xAE);//--turn off oled panel
  199. OLED_WriteCmd(0x00);//---set low column address
  200. OLED_WriteCmd(0x10);//---set high column address
  201. OLED_WriteCmd(0x40);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  202. OLED_WriteCmd(0x81);//--set contrast control register
  203. OLED_WriteCmd(0xCF); // Set SEG Output Current Brightness
  204. OLED_WriteCmd(0xA1);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  205. OLED_WriteCmd(0xC8);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  206. OLED_WriteCmd(0xA6);//--set normal display
  207. OLED_WriteCmd(0xA8);//--set multiplex ratio(1 to 64)
  208. OLED_WriteCmd(0x3f);//--1/64 duty
  209. OLED_WriteCmd(0xD3);//-set display offset   Shift Mapping RAM Counter (0x00~0x3F)
  210. OLED_WriteCmd(0x00);//-not offset
  211. OLED_WriteCmd(0xd5);//--set display clock divide ratio/oscillator frequency
  212. OLED_WriteCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
  213. OLED_WriteCmd(0xD9);//--set pre-charge period
  214. OLED_WriteCmd(0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  215. OLED_WriteCmd(0xDA);//--set com pins hardware configuration
  216. OLED_WriteCmd(0x12);
  217. OLED_WriteCmd(0xDB);//--set vcomh
  218. OLED_WriteCmd(0x40);//Set VCOM Deselect Level
  219. OLED_WriteCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
  220. OLED_WriteCmd(0x02);//
  221. OLED_WriteCmd(0x8D);//--set Charge Pump enable/disable
  222. OLED_WriteCmd(0x14);//--set(0x10) disable
  223. OLED_WriteCmd(0xA4);// Disable Entire Display On (0xa4/0xa5)
  224. OLED_WriteCmd(0xA6);// Disable Inverse Display On (0xa6/a7)
  225. OLED_WriteCmd(0xAF);//--turn on oled panel

  226. OLED_WriteCmd(0xAF); /*display ON*/
  227. OLED_Clear();
  228. OLED_Set_Pos(0,0);  
  229. }
  230. void CS(unsigned char i)
  231. {
  232. if(i==0)
  233. {
  234. GPIO_ResetBits(GPIOA,GPIO_Pin_4);   //CS(0)
  235. }
  236. if(i==1)
  237. {
  238. GPIO_SetBits(GPIOA,GPIO_Pin_4);
  239. }
  240. }
  241. void DC(unsigned char i)
  242. {
  243. if(i==0)
  244. {
  245. GPIO_ResetBits(GPIOB,GPIO_Pin_1);   //CS(0)
  246. }
  247. if(i==1)
  248. {
  249. GPIO_SetBits(GPIOB,GPIO_Pin_1);
  250. }
  251. }
  252. void RES(unsigned char i)
  253. {
  254. if(i==0)
  255. {
  256. GPIO_ResetBits(GPIOB,GPIO_Pin_0);   //CS(0)
  257. }
  258. if(i==1)
  259. {
  260. GPIO_SetBits(GPIOB,GPIO_Pin_0);
  261. }
  262. }
  263. void DIN(unsigned char i)
  264. {
  265. if(i==0)
  266. {
  267. GPIO_ResetBits(GPIOA,GPIO_Pin_7);   //CS(0)
  268. }
  269. if(i==1)
  270. {
  271. GPIO_SetBits(GPIOA,GPIO_Pin_7);
  272. }
  273. }
  274. void SCL(unsigned char i)
  275. {
  276. if(i==0)
  277. {
  278. GPIO_ResetBits(GPIOA,GPIO_Pin_5);   //CS(0)
  279. }
  280. if(i==1)
  281. {
  282. GPIO_SetBits(GPIOA,GPIO_Pin_5);
  283. }
  284. }
复制代码


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

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-11-5 22:33

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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