OLED.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "stm32f10x.h"
  2. #include "OLED_Font.h"
  3. /*引脚配置*/
  4. #define OLED_W_SCL(x) GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
  5. #define OLED_W_SDA(x) GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))
  6. /*引脚初始化*/
  7. void OLED_I2C_Init(void)
  8. {
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  10. GPIO_InitTypeDef GPIO_InitStructure;
  11. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  12. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  14. GPIO_Init(GPIOB, &GPIO_InitStructure);
  15. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  16. GPIO_Init(GPIOB, &GPIO_InitStructure);
  17. OLED_W_SCL(1);
  18. OLED_W_SDA(1);
  19. }
  20. /**
  21. * @brief I2C开始
  22. * @param 无
  23. * @retval 无
  24. */
  25. void OLED_I2C_Start(void)
  26. {
  27. OLED_W_SDA(1);
  28. OLED_W_SCL(1);
  29. OLED_W_SDA(0);
  30. OLED_W_SCL(0);
  31. }
  32. /**
  33. * @brief I2C停止
  34. * @param 无
  35. * @retval 无
  36. */
  37. void OLED_I2C_Stop(void)
  38. {
  39. OLED_W_SDA(0);
  40. OLED_W_SCL(1);
  41. OLED_W_SDA(1);
  42. }
  43. /**
  44. * @brief I2C发送一个字节
  45. * @param Byte 要发送的一个字节
  46. * @retval 无
  47. */
  48. void OLED_I2C_SendByte(uint8_t Byte)
  49. {
  50. uint8_t i;
  51. for (i = 0; i < 8; i++)
  52. {
  53. OLED_W_SDA(Byte & (0x80 >> i));
  54. OLED_W_SCL(1);
  55. OLED_W_SCL(0);
  56. }
  57. OLED_W_SCL(1); //额外的一个时钟,不处理应答信号
  58. OLED_W_SCL(0);
  59. }
  60. /**
  61. * @brief OLED写命令
  62. * @param Command 要写入的命令
  63. * @retval 无
  64. */
  65. void OLED_WriteCommand(uint8_t Command)
  66. {
  67. OLED_I2C_Start();
  68. OLED_I2C_SendByte(0x78); //从机地址
  69. OLED_I2C_SendByte(0x00); //写命令
  70. OLED_I2C_SendByte(Command);
  71. OLED_I2C_Stop();
  72. }
  73. /**
  74. * @brief OLED写数据
  75. * @param Data 要写入的数据
  76. * @retval 无
  77. */
  78. void OLED_WriteData(uint8_t Data)
  79. {
  80. OLED_I2C_Start();
  81. OLED_I2C_SendByte(0x78); //从机地址
  82. OLED_I2C_SendByte(0x40); //写数据
  83. OLED_I2C_SendByte(Data);
  84. OLED_I2C_Stop();
  85. }
  86. /**
  87. * @brief OLED设置光标位置
  88. * @param Y 以左上角为原点,向下方向的坐标,范围:0~7
  89. * @param X 以左上角为原点,向右方向的坐标,范围:0~127
  90. * @retval 无
  91. */
  92. void OLED_SetCursor(uint8_t Y, uint8_t X)
  93. {
  94. OLED_WriteCommand(0xB0 | Y); //设置Y位置
  95. OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置高4位
  96. OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置低4位
  97. }
  98. /**
  99. * @brief OLED清屏
  100. * @param 无
  101. * @retval 无
  102. */
  103. void OLED_Clear(void)
  104. {
  105. uint8_t i, j;
  106. for (j = 0; j < 8; j++)
  107. {
  108. OLED_SetCursor(j, 0);
  109. for(i = 0; i < 128; i++)
  110. {
  111. OLED_WriteData(0x00);
  112. }
  113. }
  114. }
  115. /**
  116. * @brief OLED清屏一行
  117. * @param 无
  118. * @retval 无
  119. */
  120. void OLED_Clear_Row(uint8_t Row)
  121. {
  122. uint8_t i, j;
  123. for (j = (Row - 1)*2; j < (Row - 1)*2 + 2; j++)
  124. {
  125. OLED_SetCursor(j, 0);
  126. for(i = 0; i < 128; i++)
  127. {
  128. OLED_WriteData(0x00);
  129. }
  130. }
  131. }
  132. /**
  133. * @brief OLED显示一个字符
  134. * @param Line 行位置,范围:1~4
  135. * @param Column 列位置,范围:1~16
  136. * @param Char 要显示的一个字符,范围:ASCII可见字符
  137. * @retval 无
  138. */
  139. void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
  140. {
  141. uint8_t i;
  142. OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //设置光标位置在上半部分
  143. for (i = 0; i < 8; i++)
  144. {
  145. OLED_WriteData(OLED_F8x16[Char - ' '][i]); //显示上半部分内容
  146. }
  147. OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //设置光标位置在下半部分
  148. for (i = 0; i < 8; i++)
  149. {
  150. OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //显示下半部分内容
  151. }
  152. }
  153. /**
  154. * @brief OLED显示字符串
  155. * @param Line 起始行位置,范围:1~4
  156. * @param Column 起始列位置,范围:1~16
  157. * @param String 要显示的字符串,范围:ASCII可见字符
  158. * @retval 无
  159. */
  160. void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
  161. {
  162. uint8_t i;
  163. for (i = 0; String[i] != '\0'; i++)
  164. {
  165. OLED_ShowChar(Line, Column + i, String[i]);
  166. }
  167. }
  168. /**
  169. * @brief OLED次方函数
  170. * @retval 返回值等于X的Y次方
  171. */
  172. uint32_t OLED_Pow(uint32_t X, uint32_t Y)
  173. {
  174. uint32_t Result = 1;
  175. while (Y--)
  176. {
  177. Result *= X;
  178. }
  179. return Result;
  180. }
  181. /**
  182. * @brief OLED显示数字(十进制,正数)
  183. * @param Line 起始行位置,范围:1~4
  184. * @param Column 起始列位置,范围:1~16
  185. * @param Number 要显示的数字,范围:0~4294967295
  186. * @param Length 要显示数字的长度,范围:1~10
  187. * @retval 无
  188. */
  189. void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  190. {
  191. uint8_t i;
  192. for (i = 0; i < Length; i++)
  193. {
  194. OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
  195. }
  196. }
  197. /**
  198. * @brief OLED显示数字(十进制,带符号数)
  199. * @param Line 起始行位置,范围:1~4
  200. * @param Column 起始列位置,范围:1~16
  201. * @param Number 要显示的数字,范围:-2147483648~2147483647
  202. * @param Length 要显示数字的长度,范围:1~10
  203. * @retval 无
  204. */
  205. void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
  206. {
  207. uint8_t i;
  208. uint32_t Number1;
  209. if (Number >= 0)
  210. {
  211. OLED_ShowChar(Line, Column, '+');
  212. Number1 = Number;
  213. }
  214. else
  215. {
  216. OLED_ShowChar(Line, Column, '-');
  217. Number1 = -Number;
  218. }
  219. for (i = 0; i < Length; i++)
  220. {
  221. OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
  222. }
  223. }
  224. /**
  225. * @brief OLED显示数字(十六进制,正数)
  226. * @param Line 起始行位置,范围:1~4
  227. * @param Column 起始列位置,范围:1~16
  228. * @param Number 要显示的数字,范围:0~0xFFFFFFFF
  229. * @param Length 要显示数字的长度,范围:1~8
  230. * @retval 无
  231. */
  232. void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  233. {
  234. uint8_t i, SingleNumber;
  235. for (i = 0; i < Length; i++)
  236. {
  237. SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
  238. if (SingleNumber < 10)
  239. {
  240. OLED_ShowChar(Line, Column + i, SingleNumber + '0');
  241. }
  242. else
  243. {
  244. OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
  245. }
  246. }
  247. }
  248. /**
  249. * @brief OLED显示数字(二进制,正数)
  250. * @param Line 起始行位置,范围:1~4
  251. * @param Column 起始列位置,范围:1~16
  252. * @param Number 要显示的数字,范围:0~1111 1111 1111 1111
  253. * @param Length 要显示数字的长度,范围:1~16
  254. * @retval 无
  255. */
  256. void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  257. {
  258. uint8_t i;
  259. for (i = 0; i < Length; i++)
  260. {
  261. OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
  262. }
  263. }
  264. /**
  265. * @brief OLED初始化
  266. * @param 无
  267. * @retval 无
  268. */
  269. void OLED_Init(void)
  270. {
  271. uint32_t i, j;
  272. for (i = 0; i < 1000; i++) //上电延时
  273. {
  274. for (j = 0; j < 1000; j++);
  275. }
  276. OLED_I2C_Init(); //端口初始化
  277. OLED_WriteCommand(0xAE); //关闭显示
  278. OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率
  279. OLED_WriteCommand(0x80);
  280. OLED_WriteCommand(0xA8); //设置多路复用率
  281. OLED_WriteCommand(0x3F);
  282. OLED_WriteCommand(0xD3); //设置显示偏移
  283. OLED_WriteCommand(0x00);
  284. OLED_WriteCommand(0x40); //设置显示开始行
  285. OLED_WriteCommand(0xA1); //设置左右方向,0xA1正常 0xA0左右反置
  286. OLED_WriteCommand(0xC8); //设置上下方向,0xC8正常 0xC0上下反置
  287. OLED_WriteCommand(0xDA); //设置COM引脚硬件配置
  288. OLED_WriteCommand(0x12);
  289. OLED_WriteCommand(0x81); //设置对比度控制
  290. OLED_WriteCommand(0xCF);
  291. OLED_WriteCommand(0xD9); //设置预充电周期
  292. OLED_WriteCommand(0xF1);
  293. OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别
  294. OLED_WriteCommand(0x30);
  295. OLED_WriteCommand(0xA4); //设置整个显示打开/关闭
  296. OLED_WriteCommand(0xA6); //设置正常/倒转显示
  297. OLED_WriteCommand(0x8D); //设置充电泵
  298. OLED_WriteCommand(0x14);
  299. OLED_WriteCommand(0xAF); //开启显示
  300. OLED_Clear(); //OLED清屏
  301. }