当前位置:   article > 正文

STM32—项目三(温湿度在oled屏显示)_温度传感器·oled屏幕显示

温度传感器·oled屏幕显示

目录

一、oled屏

二、DHT11温湿度传感器

三、实验

1.接线

2.配置

3.代码

1.main.c代码

2.oledfont.h头文件(需添加)

3.oled.c文件(需添加)

4.oled.h文件(需添加)

四、工程添加.c和.h文件可以参考


一、oled

        可参考STM32—IIC详解入门(oled屏显示字)_wlkq~的博客-CSDN博客

        上面链接为硬件IIC,下面代码是软件IIC

        部分代码(oled相关代码)

  1. void delay_us(uint32_t us)//微妙延时
  2. {
  3. uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us);
  4. while (delay--)
  5. {
  6. ;
  7. }
  8. }
  9. void iic_Start(void)//起始信号
  10. {
  11. SCL_LOW;
  12. SDA_HIGH;
  13. SCL_HIGH;
  14. delay_us(5);
  15. SDA_LOW;
  16. delay_us(5);
  17. }
  18. void iic_Stop(void)//终止信号
  19. {
  20. SCL_HIGH;
  21. SDA_LOW;
  22. delay_us(5);
  23. SDA_HIGH;
  24. delay_us(5);
  25. }
  26. //应答
  27. void iic_Ack()
  28. {
  29. SDA_LOW; //虚拟应答,因为OLEd屏幕坏了无应答,所以无需读引脚
  30. SCL_HIGH;
  31. SCL_LOW;
  32. }
  33. //iic发送一字节数据
  34. void IIC_send_byte(uint8_t data)
  35. {
  36. SCL_LOW;
  37. for(uint8_t i=0;i<8;i++){
  38. if(data & 0x80){
  39. SDA_HIGH;
  40. }else{
  41. SDA_LOW;
  42. }
  43. SCL_HIGH;
  44. SCL_LOW;
  45. data = data<< 1; /* 将下一位移至最高位 */
  46. }
  47. }
  48. //OLED写指令
  49. void oledWriteCmd(unsigned char writeCmd)
  50. {
  51. iic_Start();
  52. IIC_send_byte(0x78);
  53. iic_Ack();
  54. IIC_send_byte(0x00);
  55. iic_Ack();
  56. IIC_send_byte(writeCmd);
  57. iic_Ack();
  58. iic_Stop();
  59. }
  60. //写数据
  61. void Oled_Write_Data(unsigned char writedata)
  62. {
  63. iic_Start();//
  64. IIC_send_byte(0x78);
  65. iic_Ack();
  66. IIC_send_byte(0x40);
  67. iic_Ack();
  68. IIC_send_byte(writedata);
  69. iic_Ack();
  70. iic_Stop();
  71. }
  72. //OLED清屏
  73. void olceClean()
  74. {
  75. int i,j;
  76. for(i=0;i<8;i++){
  77. oledWriteCmd(0xB0 + i); //选择PAGE
  78. oledWriteCmd(0x00); //选择列
  79. oledWriteCmd(0x10);
  80. for(j = 0;j < 128; j++){
  81. Oled_Write_Data(0); //写入字符0
  82. }
  83. }
  84. }
  85. //OLCD初始化
  86. void oledInit(void)
  87. {
  88. HAL_Delay(500);
  89. oledWriteCmd(0xAE);
  90. oledWriteCmd(0x00);
  91. oledWriteCmd(0x10);
  92. oledWriteCmd(0x40);
  93. oledWriteCmd(0xB0);
  94. oledWriteCmd(0x81);
  95. oledWriteCmd(0xFF);
  96. oledWriteCmd(0xA1);
  97. oledWriteCmd(0xA6);
  98. oledWriteCmd(0xA8);
  99. oledWriteCmd(0x3F);
  100. oledWriteCmd(0xC8);
  101. oledWriteCmd(0xD3);
  102. oledWriteCmd(0x00);
  103. oledWriteCmd(0xD5);
  104. oledWriteCmd(0x80);
  105. oledWriteCmd(0xD8);
  106. oledWriteCmd(0x05);
  107. oledWriteCmd(0xD9);
  108. oledWriteCmd(0xF1);
  109. oledWriteCmd(0xDA);
  110. oledWriteCmd(0x12);
  111. oledWriteCmd(0xDB);
  112. oledWriteCmd(0x30);
  113. oledWriteCmd(0x8D);
  114. oledWriteCmd(0x14);
  115. oledWriteCmd(0xAF);
  116. }
  117. //以下代码厂家提供
  118. void Oled_Show_Char(char row,char col,char oledChar){ //row*2-2
  119. unsigned int i;
  120. oledWriteCmd(0xb0+(row*2-2)); //page 0
  121. oledWriteCmd(0x00+(col&0x0f)); //low
  122. oledWriteCmd(0x10+(col>>4)); //high
  123. for(i=((oledChar-32)*16);i<((oledChar-32)*16+8);i++){
  124. Oled_Write_Data(F8X16[i]); //写数据oledTable1
  125. }
  126. oledWriteCmd(0xb0+(row*2-1)); //page 1
  127. oledWriteCmd(0x00+(col&0x0f)); //low
  128. oledWriteCmd(0x10+(col>>4)); //high
  129. for(i=((oledChar-32)*16+8);i<((oledChar-32)*16+8+8);i++){
  130. Oled_Write_Data(F8X16[i]); //写数据oledTable1
  131. }
  132. }
  133. //以下代码厂家提供
  134. /******************************************************************************/
  135. // 函数名称:Oled_Show_Char
  136. // 输入参数:oledChar
  137. // 输出参数:无
  138. // 函数功能:OLED显示单个字符
  139. /******************************************************************************/
  140. void Oled_Show_Str(char row,char col,char *str){//页、列、字符串
  141. while(*str!=0){
  142. Oled_Show_Char(row,col,*str);
  143. str++;
  144. col += 8;
  145. }
  146. }

二、DHT11温湿度传感器

 

       1.总时序图(主机信号由代码设置波形,DHT信号由DHT11发出)

         2.初始化部分  检测模块是否存在(检测下图DHT响应信号有没有被拉低)

                部分代码初始化

  1. //初始化
  2. void DHT11_Start()
  3. {
  4. //下面是主机信号
  5. DHT_GPIO_Init(GPIO_MODE_OUTPUT_PP);//PA5设置为输出推挽模式
  6. DHT_HIGH;
  7. DHT_LOW;
  8. HAL_Delay(30);//ms
  9. DHT_HIGH;
  10. DHT_GPIO_Init(GPIO_MODE_INPUT);//PA5设置为输入模式
  11. //下面是DHT信号,如果符合DHT信号时序图就进行运行
  12. while(DHT_VALUE);
  13. //dht=0
  14. while(!DHT_VALUE);
  15. //dht=1
  16. while(DHT_VALUE);//之后开始传输数据
  17. //dht=0
  18. }

         3.接收数据部分(有效数据都是高电平,但持续时间不一样,可通过延时后是否被拉低来判断是0还是1)

                 DHT11传输0的时序分析

               DHT11传输1的时序分析  

         4.数据格式

        只有一根数据线DAT,发送序列指令给DHT11模块,模块就会传输一次完整的数据为40bit(8bit湿度整数数据+8bit湿度小数数据+8bi温度整数数据+8bit温度小数数据+8bit校验),高位先出。

                部分代码数据接收

  1. void DHT11_Read()
  2. {
  3. int i;
  4. int j;
  5. char tmp;//移位,存放8个bit
  6. char flag;//标志位
  7. DHT11_Start();//重启进入高速模式之后才发送bit
  8. DHT_GPIO_Init(GPIO_MODE_INPUT);//PA5设置为输入模式
  9. for(i = 0;i < 5;i++){//取4个字节
  10. for(j=0;j<8;j++){//每个字节取8个bit
  11. while(!DHT_VALUE);//卡g点数据来会从0变成1
  12. delay_us(40); //延时读取
  13. if(DHT_VALUE == 1){
  14. flag = 1;
  15. while(DHT_VALUE);//等待变位70us下一个bit
  16. }else{
  17. flag = 0;
  18. }
  19. tmp = tmp << 1;//左移一位或者 tmp<<=1,为了使先出来的bit到高位
  20. tmp |= flag;//可以为 tmp=tmp|flag
  21. }
  22. data[i] = tmp;//得到8个bit为一个字节,存放在数组里
  23. }
  24. }

三、实验(oled屏显示温湿度)

1.接线

oled:SCL->B5        SDA->B6        DH11:DAT->A5

2.配置

        1.SYS

        2.RCC

         4.GPIO(全部设置为输出且输出高电平)A5既作为输入,也作为输出,则不能直接在CubeMX里配置,需要自己写代码。

3.代码

1.main.c代码

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "gpio.h"
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. #include "oled.h"
  25. #include "stdio.h"
  26. /* USER CODE END Includes */
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */
  29. #define DHT_LOW HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET)
  30. #define DHT_HIGH HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET)
  31. #define DHT_VALUE HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5)
  32. /* USER CODE END PTD */
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35. /* USER CODE END PD */
  36. /* Private macro -------------------------------------------------------------*/
  37. /* USER CODE BEGIN PM */
  38. /* USER CODE END PM */
  39. /* Private variables ---------------------------------------------------------*/
  40. /* USER CODE BEGIN PV */
  41. /* USER CODE END PV */
  42. /* Private function prototypes -----------------------------------------------*/
  43. void SystemClock_Config(void);
  44. /* USER CODE BEGIN PFP */
  45. /* USER CODE END PFP */
  46. /* Private user code ---------------------------------------------------------*/
  47. /* USER CODE BEGIN 0 */
  48. void DHT_GPIO_Init(uint32_t Mode)//初始化GPIO口,PA5口
  49. {
  50. GPIO_InitTypeDef GPIO_InitStruct = {0};
  51. __HAL_RCC_GPIOA_CLK_ENABLE();//时钟
  52. GPIO_InitStruct.Pin = GPIO_PIN_5;
  53. GPIO_InitStruct.Mode = Mode;//输出还是输入
  54. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  55. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  56. }
  57. void DHT11_Start()
  58. {
  59. //下面是主机信号
  60. DHT_GPIO_Init(GPIO_MODE_OUTPUT_PP);//PA5设置为输出推挽模式
  61. DHT_HIGH;
  62. DHT_LOW;
  63. HAL_Delay(30);//ms
  64. DHT_HIGH;
  65. DHT_GPIO_Init(GPIO_MODE_INPUT);//PA5设置为输入模式
  66. //下面是DHT信号,如果符合DHT信号时序图就进行运行
  67. while(DHT_VALUE);
  68. //dht=0
  69. while(!DHT_VALUE);
  70. //dht=1
  71. while(DHT_VALUE);//之后开始传输数据
  72. //dht=0
  73. }
  74. uint8_t data[5];
  75. void DHT11_Read()
  76. {
  77. int i;
  78. int j;
  79. char tmp;//移位,存放8个bit
  80. char flag;//标志位
  81. DHT11_Start();//重启进入高速模式之后才发送bit
  82. DHT_GPIO_Init(GPIO_MODE_INPUT);//PA5设置为输入模式
  83. for(i = 0;i < 5;i++){//取4个字节
  84. for(j=0;j<8;j++){//每个字节取8个bit
  85. while(!DHT_VALUE);//卡g点数据来会从0变成1
  86. delay_us(40); //延时读取
  87. if(DHT_VALUE == 1){
  88. flag = 1;
  89. while(DHT_VALUE);//等待变位70us下一个bit
  90. }else{
  91. flag = 0;
  92. }
  93. tmp = tmp << 1;//左移一位或者 tmp<<=1,为了使先出来的bit到高位
  94. tmp |= flag;//可以为 tmp=tmp|flag
  95. }
  96. data[i] = tmp;//得到8个bit为一个字节,存放在数组里
  97. }
  98. }
  99. /* USER CODE END 0 */
  100. /**
  101. * @brief The application entry point.
  102. * @retval int
  103. */
  104. int main(void)
  105. {
  106. /* USER CODE BEGIN 1 */
  107. char SpeedMes[16];
  108. /* USER CODE END 1 */
  109. /* MCU Configuration--------------------------------------------------------*/
  110. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  111. HAL_Init();
  112. /* USER CODE BEGIN Init */
  113. /* USER CODE END Init */
  114. /* Configure the system clock */
  115. SystemClock_Config();
  116. /* USER CODE BEGIN SysInit */
  117. /* USER CODE END SysInit */
  118. /* Initialize all configured peripherals */
  119. MX_GPIO_Init();
  120. /* USER CODE BEGIN 2 */
  121. oledInit();//OLED初始化
  122. oledWriteCmd(0x20); //选择寻址方式
  123. oledWriteCmd(0x02); //02是页寻址、00是水平寻址、01是垂直寻址模式
  124. olceClean();//清屏
  125. /* USER CODE END 2 */
  126. /* Infinite loop */
  127. /* USER CODE BEGIN WHILE */
  128. while (1)
  129. {
  130. /* USER CODE END WHILE */
  131. /* USER CODE BEGIN 3 */
  132. olceClean();
  133. DHT11_Read();
  134. sprintf(SpeedMes,"H:%d.%d",data[0], data[1]);//每一秒换一次 juli代替%d 然后将引号字符串放在数组里
  135. Oled_Show_Str(1,1,SpeedMes);
  136. sprintf(SpeedMes,"T:%d.%d",data[2], data[3]);
  137. Oled_Show_Str(2,1,SpeedMes);
  138. HAL_Delay(600);
  139. }
  140. /* USER CODE END 3 */
  141. }
  142. /**
  143. * @brief System Clock Configuration
  144. * @retval None
  145. */
  146. void SystemClock_Config(void)
  147. {
  148. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  149. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  150. /** Initializes the RCC Oscillators according to the specified parameters
  151. * in the RCC_OscInitTypeDef structure.
  152. */
  153. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  154. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  155. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  156. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  157. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  158. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  159. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  160. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  161. {
  162. Error_Handler();
  163. }
  164. /** Initializes the CPU, AHB and APB buses clocks
  165. */
  166. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  167. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  168. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  169. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  170. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  171. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  172. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  173. {
  174. Error_Handler();
  175. }
  176. }
  177. /* USER CODE BEGIN 4 */
  178. /* USER CODE END 4 */
  179. /**
  180. * @brief This function is executed in case of error occurrence.
  181. * @retval None
  182. */
  183. void Error_Handler(void)
  184. {
  185. /* USER CODE BEGIN Error_Handler_Debug */
  186. /* User can add his own implementation to report the HAL error return state */
  187. __disable_irq();
  188. while (1)
  189. {
  190. }
  191. /* USER CODE END Error_Handler_Debug */
  192. }
  193. #ifdef USE_FULL_ASSERT
  194. /**
  195. * @brief Reports the name of the source file and the source line number
  196. * where the assert_param error has occurred.
  197. * @param file: pointer to the source file name
  198. * @param line: assert_param error line source number
  199. * @retval None
  200. */
  201. void assert_failed(uint8_t *file, uint32_t line)
  202. {
  203. /* USER CODE BEGIN 6 */
  204. /* User can add his own implementation to report the file name and line number,
  205. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  206. /* USER CODE END 6 */
  207. }
  208. #endif /* USE_FULL_ASSERT */

2.oledfont.h头文件(需添加)

  1. //
  2. const unsigned char F8X16[]=
  3. {
  4. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
  5. 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
  6. 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
  7. 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
  8. 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
  9. 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
  10. 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
  11. 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
  12. 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
  13. 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
  14. 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
  15. 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
  16. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
  17. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
  18. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
  19. 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
  20. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
  21. 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
  22. 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
  23. 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
  24. 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
  25. 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
  26. 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
  27. 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
  28. 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
  29. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
  30. 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
  31. 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
  32. 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
  33. 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
  34. 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
  35. 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
  36. 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
  37. 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
  38. 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
  39. 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
  40. 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
  41. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
  42. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
  43. 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
  44. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
  45. 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
  46. 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
  47. 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
  48. 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
  49. 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
  50. 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
  51. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
  52. 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
  53. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
  54. 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
  55. 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
  56. 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
  57. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
  58. 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
  59. 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
  60. 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
  61. 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
  62. 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
  63. 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
  64. 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
  65. 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
  66. 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
  67. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
  68. 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
  69. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
  70. 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
  71. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
  72. 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
  73. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
  74. 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
  75. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
  76. 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
  77. 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
  78. 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
  79. 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
  80. 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
  81. 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
  82. 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
  83. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
  84. 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
  85. 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
  86. 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
  87. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
  88. 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
  89. 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
  90. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
  91. 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
  92. 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
  93. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
  94. 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
  95. 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
  96. 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
  97. 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
  98. 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
  99. };

3.oled.c文件(需添加)

  1. #include "main.h"
  2. #include "gpio.h"
  3. #include "oled.h"
  4. #include "oledfont.h"
  5. void delay_us(uint32_t us)
  6. {
  7. uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us);
  8. while (delay--)
  9. {
  10. ;
  11. }
  12. }
  13. void iic_Start(void)//起始信号
  14. {
  15. SCL_LOW;
  16. SDA_HIGH;
  17. SCL_HIGH;
  18. delay_us(5);
  19. SDA_LOW;
  20. delay_us(5);
  21. }
  22. void iic_Stop(void)//终止信号
  23. {
  24. SCL_HIGH;
  25. SDA_LOW;
  26. delay_us(5);
  27. SDA_HIGH;
  28. delay_us(5);
  29. }
  30. //应答
  31. void iic_Ack()
  32. {
  33. SDA_LOW; //虚拟应答,因为OLEd屏幕坏了无应答
  34. SCL_HIGH;
  35. SCL_LOW;
  36. }
  37. //iic发送一字节数据
  38. void IIC_send_byte(uint8_t data)
  39. {
  40. SCL_LOW;
  41. for(uint8_t i=0;i<8;i++){
  42. if(data & 0x80){
  43. SDA_HIGH;
  44. }else{
  45. SDA_LOW;
  46. }
  47. SCL_HIGH;
  48. SCL_LOW;
  49. data = data<< 1; /* 将下一位移至最高位 */
  50. }
  51. //SCL_HIGH; /* 发送完成,释放数据线*/
  52. }
  53. //OLED写指令
  54. void oledWriteCmd(unsigned char writeCmd)
  55. {
  56. iic_Start();
  57. IIC_send_byte(0x78);
  58. iic_Ack();
  59. IIC_send_byte(0x00);
  60. iic_Ack();
  61. IIC_send_byte(writeCmd);
  62. iic_Ack();
  63. iic_Stop();
  64. }
  65. //写数据
  66. void Oled_Write_Data(unsigned char writedata)
  67. {
  68. iic_Start();//
  69. IIC_send_byte(0x78);
  70. iic_Ack();
  71. IIC_send_byte(0x40);
  72. iic_Ack();
  73. IIC_send_byte(writedata);
  74. iic_Ack();
  75. iic_Stop();
  76. }
  77. //OLED清屏
  78. void olceClean()
  79. {
  80. int i,j;
  81. for(i=0;i<8;i++){
  82. oledWriteCmd(0xB0 + i); //选择PAGE
  83. oledWriteCmd(0x00); //选择列
  84. oledWriteCmd(0x10);
  85. for(j = 0;j < 128; j++){
  86. Oled_Write_Data(0); //写入字符0
  87. }
  88. }
  89. }
  90. //OLCD初始化
  91. void oledInit(void)
  92. {
  93. HAL_Delay(500);
  94. oledWriteCmd(0xAE);
  95. oledWriteCmd(0x00);
  96. oledWriteCmd(0x10);
  97. oledWriteCmd(0x40);
  98. oledWriteCmd(0xB0);
  99. oledWriteCmd(0x81);
  100. oledWriteCmd(0xFF);
  101. oledWriteCmd(0xA1);
  102. oledWriteCmd(0xA6);
  103. oledWriteCmd(0xA8);
  104. oledWriteCmd(0x3F);
  105. oledWriteCmd(0xC8);
  106. oledWriteCmd(0xD3);
  107. oledWriteCmd(0x00);
  108. oledWriteCmd(0xD5);
  109. oledWriteCmd(0x80);
  110. oledWriteCmd(0xD8);
  111. oledWriteCmd(0x05);
  112. oledWriteCmd(0xD9);
  113. oledWriteCmd(0xF1);
  114. oledWriteCmd(0xDA);
  115. oledWriteCmd(0x12);
  116. oledWriteCmd(0xDB);
  117. oledWriteCmd(0x30);
  118. oledWriteCmd(0x8D);
  119. oledWriteCmd(0x14);
  120. oledWriteCmd(0xAF);
  121. }
  122. //以下代码厂家提供
  123. void Oled_Show_Char(char row,char col,char oledChar){ //row*2-2
  124. unsigned int i;
  125. oledWriteCmd(0xb0+(row*2-2)); //page 0
  126. oledWriteCmd(0x00+(col&0x0f)); //low
  127. oledWriteCmd(0x10+(col>>4)); //high
  128. for(i=((oledChar-32)*16);i<((oledChar-32)*16+8);i++){
  129. Oled_Write_Data(F8X16[i]); //写数据oledTable1
  130. }
  131. oledWriteCmd(0xb0+(row*2-1)); //page 1
  132. oledWriteCmd(0x00+(col&0x0f)); //low
  133. oledWriteCmd(0x10+(col>>4)); //high
  134. for(i=((oledChar-32)*16+8);i<((oledChar-32)*16+8+8);i++){
  135. Oled_Write_Data(F8X16[i]); //写数据oledTable1
  136. }
  137. }
  138. //以下代码厂家提供
  139. /******************************************************************************/
  140. // 函数名称:Oled_Show_Char
  141. // 输入参数:oledChar
  142. // 输出参数:无
  143. // 函数功能:OLED显示单个字符
  144. /******************************************************************************/
  145. void Oled_Show_Str(char row,char col,char *str){//页、列、字符串
  146. while(*str!=0){
  147. Oled_Show_Char(row,col,*str);
  148. str++;
  149. col += 8;
  150. }
  151. }

4.oled.h文件(需添加)

  1. #define SCL_HIGH HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET)//SCL高
  2. #define SCL_LOW HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET)
  3. #define SDA_HIGH HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET)//SDA高
  4. #define SDA_LOW HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET)
  5. void olceClean(void);
  6. void oledInit(void);
  7. void Oled_Show_Str(char row,char col,char *str);
  8. void oledWriteCmd(unsigned char writeCmd);
  9. void delay_us(unsigned int us);

四、工程添加.c和.h文件可以参考

STM32—SPI详解入门(使用SPI通讯读写W25Q128模块)_spi读写寄存器_wlkq~的博客-CSDN博客文章里面的第九、STM32工程添加.c和.h文件

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

闽ICP备14008679号