文件:

STM32_Project_I2C_Connect5.zip

main.c

  1. /**********************
  2. 芯片:STM32F103
  3. 实现功能:
  4. 引脚:
  5. ***********************/
  6. #include "stm32f10x.h"
  7. #include "main.h"
  8. #include "bsp_DMAusart2.h"
  9. #include "bsp_tim3.h"
  10. #include "bsp_tim2.h"
  11. #include "bsp_SysTick.h"
  12. #include "bsp_dwt.h"
  13. #include "bsp_i2c.h"
  14. #include "bsp_crc.h"
  15. #include "bsp_mpu6050.h"
  16. #include "bsp_bh1750.h"
  17. #include "bsp_hdc1080.h"
  18. extern unsigned int Task_Delay[NumOfTask];
  19. uint8_t Scan_key_flag;
  20. float DWT_UsedTime = 0; //DWT
  21. int main()
  22. {
  23. // 初始化USART1
  24. USART2_DMA_Init(9600, TwoUART.Tx_buf, TwoUART.Rx_buf, USART2_REC_LEN);
  25. USART2_DMASendString("AppStart!\n", sizeof("AppStart!"));
  26. // 初始化定时器
  27. TIM2_Init();
  28. //TIM3_Init(36-1, 1);1us
  29. TIM3_Int_Init(71, 999); // 1ms
  30. // HDC1080 传感器
  31. HDC1080_Init();
  32. uint8_t Delay = 0;
  33. uint8_t buf[4] = {0};
  34. while(1)
  35. {
  36. if(Scan_key_flag == 1)
  37. {
  38. Scan_key_flag = 0;
  39. Delay++; // 计数
  40. }
  41. DWT_UsedTime = (uint32_t)0u;
  42. DWT_CYCCNT = (uint32_t)0u;
  43. DWT_UsedTime = DWT_TS_GET();
  44. char buffer0[128] = {0};
  45. HDC1080_Read_TemperAndHumidity( buf, &Delay);
  46. DWT_UsedTime = DWT_TS_GET();
  47. char buffer5[18] = {0};
  48. sprintf(buffer5, "\r\n时间: %6.7f", DWT_UsedTime/72000000.0); // 计数耗时时间
  49. USART2_DMASendString(buffer5, 18);
  50. sprintf(buffer0, "DeviceID: %x ManufacturerID: %x 温度: %8.2f 湿度: %8.2f\n",
  51. 0, 0, ((((buf[0] << 8 | buf[1])/65536.0)*165)-40),
  52. ((buf[2] << 8 | buf[3])/65536.0)*100);
  53. USART2_DMASendString(buffer0, 128);
  54. }
  55. }

bsp_hdc1080.h

  1. #ifndef _BSP_HDC1080_H
  2. #define _BSP_HDC1080_H
  3. #include "stm32f10x.h"
  4. #define SlaveAddress 0x80 // i2c设备地址
  5. #define HDC1080_READ_Config 0x02 // 配置寄存器地址
  6. #define HDC1080_READ_Temperature 0x00 // 温度寄存器地址
  7. #define HDC1080_READ_Humidity 0x01 // 湿度寄存器地址
  8. #define HDC1080_READ_ManufacturerID 0xFE // ManufacturerID
  9. #define HDC1080_READ_DeviceID 0xFF // 设备寄存器ID
  10. void HDC1080_ReadData(uint8_t *Read, uint8_t registe, u8 num);
  11. float HDC1080_Read_Temper();
  12. float HDC1080_Read_Humidity();
  13. void HDC1080_DetectionConfig(); // 设备检测配置
  14. uint16_t HDC1080_Read_ManufacturerID(void);
  15. uint16_t HDC1080_Read_DeviceID(void);
  16. void HDC1080_Read_TemperAndHumidity(uint8_t *buf, uint8_t *Delay);
  17. void HDC1080_Init();
  18. #endif //_BSP_HDC1080_H

bsp_hdc1080.c

  1. #include "bsp_hdc1080.h"
  2. #include "bsp_i2c.h"
  3. #include "bsp_tim2.h"
  4. #include "bsp_tim3.h"
  5. #include "bsp_DMAusart2.h"
  6. // 写配置指令
  7. void HDC1080_Write_Config(uint8_t addr, uint8_t *buffer, uint8_t len)
  8. {
  9. i2c_Start();
  10. i2c_SendByte(SlaveAddress);
  11. i2c_WaitAck();
  12. i2c_SendByte(addr);
  13. i2c_WaitAck();
  14. while(len--)
  15. {
  16. i2c_SendByte(*buffer);
  17. i2c_WaitAck();
  18. buffer++;
  19. }
  20. i2c_Stop();
  21. }
  22. // 读取数据 阻塞式
  23. void HDC1080_ReadData(uint8_t *buf, uint8_t registe, uint8_t num)
  24. {
  25. uint16_t i;
  26. //uint16_t temp = 0;
  27. i2c_Start();
  28. i2c_SendByte(SlaveAddress); // 设备地址+读信号
  29. i2c_WaitAck();
  30. i2c_SendByte(registe); // 将地址指针设置为0x00以进行温度测量
  31. i2c_WaitAck();
  32. i2c_Stop();
  33. Delay_ms(10);// 延时10ms
  34. i2c_Start();
  35. i2c_SendByte(SlaveAddress + 1);
  36. i2c_WaitAck();
  37. for(i=0;i<(num-1);i++){
  38. *buf = i2c_ReadByte(1);
  39. buf++;
  40. }
  41. *buf = i2c_ReadByte(0);
  42. i2c_Stop();
  43. }
  44. // 读温度
  45. float HDC1080_Read_Temper()
  46. {
  47. float temperature;
  48. uint8_t buf[2];
  49. float t;
  50. HDC1080_ReadData(buf, HDC1080_READ_Temperature, 2);
  51. t = buf[0] << 8 | buf[1];
  52. temperature = ((t/65536.0)*165)-40;
  53. return temperature;
  54. }
  55. // 读湿度
  56. float HDC1080_Read_Humidity(void)
  57. {
  58. float humidity;
  59. uint8_t buf[2];
  60. float t;
  61. HDC1080_ReadData(buf, HDC1080_READ_Humidity, 2);
  62. t = buf[0] << 8 | buf[1];
  63. humidity = ((t/65536.0)*100);
  64. return humidity;
  65. }
  66. // 进行读操作寄存器钩子函数
  67. static void HDC1080_Read_Conf_CallbackFunction(uint8_t Register)
  68. {
  69. i2c_Start();
  70. i2c_SendByte(SlaveAddress); // 设备地址+读信号
  71. i2c_WaitAck();
  72. i2c_SendByte(Register); // 寄存器指针指向
  73. i2c_WaitAck();
  74. i2c_Stop();
  75. }
  76. // 读温湿度数据
  77. static void HDC1080_ReadData_CallbackFunction(uint8_t * buf, uint8_t num)
  78. {
  79. uint8_t i = 0;
  80. i2c_Start();
  81. i2c_SendByte(SlaveAddress + 1);
  82. i2c_WaitAck();
  83. for(; i < num-1; i++)
  84. {
  85. *buf = i2c_ReadByte(1);
  86. buf++;
  87. }
  88. *buf = i2c_ReadByte(1);
  89. i2c_Stop();
  90. }
  91. // 无阻塞读温度和湿度
  92. void HDC1080_Read_TemperAndHumidity(uint8_t *buf, uint8_t *Delay)
  93. {
  94. //uint16_t i;
  95. float data;
  96. //uint8_t buf[4] = {0};
  97. static uint8_t status = 0;
  98. switch(status)
  99. {
  100. case 0:// 进行寄存器配置
  101. status = 1;
  102. *Delay = 0;
  103. HDC1080_Read_Conf_CallbackFunction(HDC1080_READ_Temperature); // 寄存器指针指向温度的寄存器
  104. break;
  105. case 1:
  106. if(*Delay == 8) // 延时ms
  107. {
  108. status = 2;
  109. *Delay = 0;
  110. HDC1080_ReadData_CallbackFunction(buf, 2);
  111. //return ((((buf[0] << 8 | buf[1])/65536.0)*165)-40);
  112. }
  113. break;
  114. case 2:// 寄存器配置
  115. status = 3;
  116. *Delay = 0;
  117. HDC1080_Read_Conf_CallbackFunction(HDC1080_READ_Humidity); // 寄存器指针指向湿度度的寄存器
  118. break;
  119. case 3:
  120. if(*Delay == 8)
  121. {
  122. status = 0;
  123. *Delay = 0;
  124. buf = buf+2;
  125. HDC1080_ReadData_CallbackFunction(buf, 2);
  126. }
  127. break;
  128. }
  129. }
  130. // 读设备id
  131. uint16_t HDC1080_Read_ManufacturerID(void)
  132. {
  133. uint8_t buf[2];
  134. HDC1080_ReadData(buf, HDC1080_READ_ManufacturerID, 2);
  135. return (buf[0] << 8 | buf[1]);
  136. }
  137. // 读设备id
  138. uint16_t HDC1080_Read_DeviceID(void)
  139. {
  140. uint8_t buf[2];
  141. HDC1080_ReadData(buf, HDC1080_READ_DeviceID, 2);
  142. return (buf[0] << 8 | buf[1]);
  143. }
  144. // 复位
  145. void HDC1080_Soft_Reset(void)
  146. {
  147. uint8_t temp[2];
  148. temp[0] = 0x80;
  149. temp[1] = 0x00;
  150. HDC1080_Write_Config(HDC1080_READ_Config, temp, 2);
  151. Delay_ms(16);//there should be waiting for more than 15 ms.
  152. }
  153. // 在寄存器地址0x02中配置采集参数
  154. void HDC1080_Setting(void)
  155. {
  156. uint16_t tempcom = 0;
  157. uint8_t temp[2];
  158. tempcom |= 1 << 12 ;//通过将位[12]设置为1,将采集模式设置为测量温度和湿度
  159. // 都设置为14bit 分辨率
  160. temp[0] = (uint8_t)tempcom;
  161. temp[1] = (uint8_t)(tempcom >> 8);
  162. HDC1080_Write_Config(0x02, temp, 2);
  163. }
  164. // 初始化
  165. void HDC1080_Init()
  166. {
  167. HDC1080_Soft_Reset();
  168. HDC1080_Setting();
  169. }