文件:
STM32_Project_I2C_Connect5.zip
main.c
/**********************
芯片:STM32F103
实现功能:
引脚:
***********************/
#include "stm32f10x.h"
#include "main.h"
#include "bsp_DMAusart2.h"
#include "bsp_tim3.h"
#include "bsp_tim2.h"
#include "bsp_SysTick.h"
#include "bsp_dwt.h"
#include "bsp_i2c.h"
#include "bsp_crc.h"
#include "bsp_mpu6050.h"
#include "bsp_bh1750.h"
#include "bsp_hdc1080.h"
extern unsigned int Task_Delay[NumOfTask];
uint8_t Scan_key_flag;
float DWT_UsedTime = 0; //DWT
int main()
{
// 初始化USART1
USART2_DMA_Init(9600, TwoUART.Tx_buf, TwoUART.Rx_buf, USART2_REC_LEN);
USART2_DMASendString("AppStart!\n", sizeof("AppStart!"));
// 初始化定时器
TIM2_Init();
//TIM3_Init(36-1, 1);1us
TIM3_Int_Init(71, 999); // 1ms
// HDC1080 传感器
HDC1080_Init();
uint8_t Delay = 0;
uint8_t buf[4] = {0};
while(1)
{
if(Scan_key_flag == 1)
{
Scan_key_flag = 0;
Delay++; // 计数
}
DWT_UsedTime = (uint32_t)0u;
DWT_CYCCNT = (uint32_t)0u;
DWT_UsedTime = DWT_TS_GET();
char buffer0[128] = {0};
HDC1080_Read_TemperAndHumidity( buf, &Delay);
DWT_UsedTime = DWT_TS_GET();
char buffer5[18] = {0};
sprintf(buffer5, "\r\n时间: %6.7f", DWT_UsedTime/72000000.0); // 计数耗时时间
USART2_DMASendString(buffer5, 18);
sprintf(buffer0, "DeviceID: %x ManufacturerID: %x 温度: %8.2f 湿度: %8.2f\n",
0, 0, ((((buf[0] << 8 | buf[1])/65536.0)*165)-40),
((buf[2] << 8 | buf[3])/65536.0)*100);
USART2_DMASendString(buffer0, 128);
}
}
bsp_hdc1080.h
#ifndef _BSP_HDC1080_H
#define _BSP_HDC1080_H
#include "stm32f10x.h"
#define SlaveAddress 0x80 // i2c设备地址
#define HDC1080_READ_Config 0x02 // 配置寄存器地址
#define HDC1080_READ_Temperature 0x00 // 温度寄存器地址
#define HDC1080_READ_Humidity 0x01 // 湿度寄存器地址
#define HDC1080_READ_ManufacturerID 0xFE // ManufacturerID
#define HDC1080_READ_DeviceID 0xFF // 设备寄存器ID
void HDC1080_ReadData(uint8_t *Read, uint8_t registe, u8 num);
float HDC1080_Read_Temper();
float HDC1080_Read_Humidity();
void HDC1080_DetectionConfig(); // 设备检测配置
uint16_t HDC1080_Read_ManufacturerID(void);
uint16_t HDC1080_Read_DeviceID(void);
void HDC1080_Read_TemperAndHumidity(uint8_t *buf, uint8_t *Delay);
void HDC1080_Init();
#endif //_BSP_HDC1080_H
bsp_hdc1080.c
#include "bsp_hdc1080.h"
#include "bsp_i2c.h"
#include "bsp_tim2.h"
#include "bsp_tim3.h"
#include "bsp_DMAusart2.h"
// 写配置指令
void HDC1080_Write_Config(uint8_t addr, uint8_t *buffer, uint8_t len)
{
i2c_Start();
i2c_SendByte(SlaveAddress);
i2c_WaitAck();
i2c_SendByte(addr);
i2c_WaitAck();
while(len--)
{
i2c_SendByte(*buffer);
i2c_WaitAck();
buffer++;
}
i2c_Stop();
}
// 读取数据 阻塞式
void HDC1080_ReadData(uint8_t *buf, uint8_t registe, uint8_t num)
{
uint16_t i;
//uint16_t temp = 0;
i2c_Start();
i2c_SendByte(SlaveAddress); // 设备地址+读信号
i2c_WaitAck();
i2c_SendByte(registe); // 将地址指针设置为0x00以进行温度测量
i2c_WaitAck();
i2c_Stop();
Delay_ms(10);// 延时10ms
i2c_Start();
i2c_SendByte(SlaveAddress + 1);
i2c_WaitAck();
for(i=0;i<(num-1);i++){
*buf = i2c_ReadByte(1);
buf++;
}
*buf = i2c_ReadByte(0);
i2c_Stop();
}
// 读温度
float HDC1080_Read_Temper()
{
float temperature;
uint8_t buf[2];
float t;
HDC1080_ReadData(buf, HDC1080_READ_Temperature, 2);
t = buf[0] << 8 | buf[1];
temperature = ((t/65536.0)*165)-40;
return temperature;
}
// 读湿度
float HDC1080_Read_Humidity(void)
{
float humidity;
uint8_t buf[2];
float t;
HDC1080_ReadData(buf, HDC1080_READ_Humidity, 2);
t = buf[0] << 8 | buf[1];
humidity = ((t/65536.0)*100);
return humidity;
}
// 进行读操作寄存器钩子函数
static void HDC1080_Read_Conf_CallbackFunction(uint8_t Register)
{
i2c_Start();
i2c_SendByte(SlaveAddress); // 设备地址+读信号
i2c_WaitAck();
i2c_SendByte(Register); // 寄存器指针指向
i2c_WaitAck();
i2c_Stop();
}
// 读温湿度数据
static void HDC1080_ReadData_CallbackFunction(uint8_t * buf, uint8_t num)
{
uint8_t i = 0;
i2c_Start();
i2c_SendByte(SlaveAddress + 1);
i2c_WaitAck();
for(; i < num-1; i++)
{
*buf = i2c_ReadByte(1);
buf++;
}
*buf = i2c_ReadByte(1);
i2c_Stop();
}
// 无阻塞读温度和湿度
void HDC1080_Read_TemperAndHumidity(uint8_t *buf, uint8_t *Delay)
{
//uint16_t i;
float data;
//uint8_t buf[4] = {0};
static uint8_t status = 0;
switch(status)
{
case 0:// 进行寄存器配置
status = 1;
*Delay = 0;
HDC1080_Read_Conf_CallbackFunction(HDC1080_READ_Temperature); // 寄存器指针指向温度的寄存器
break;
case 1:
if(*Delay == 8) // 延时ms
{
status = 2;
*Delay = 0;
HDC1080_ReadData_CallbackFunction(buf, 2);
//return ((((buf[0] << 8 | buf[1])/65536.0)*165)-40);
}
break;
case 2:// 寄存器配置
status = 3;
*Delay = 0;
HDC1080_Read_Conf_CallbackFunction(HDC1080_READ_Humidity); // 寄存器指针指向湿度度的寄存器
break;
case 3:
if(*Delay == 8)
{
status = 0;
*Delay = 0;
buf = buf+2;
HDC1080_ReadData_CallbackFunction(buf, 2);
}
break;
}
}
// 读设备id
uint16_t HDC1080_Read_ManufacturerID(void)
{
uint8_t buf[2];
HDC1080_ReadData(buf, HDC1080_READ_ManufacturerID, 2);
return (buf[0] << 8 | buf[1]);
}
// 读设备id
uint16_t HDC1080_Read_DeviceID(void)
{
uint8_t buf[2];
HDC1080_ReadData(buf, HDC1080_READ_DeviceID, 2);
return (buf[0] << 8 | buf[1]);
}
// 复位
void HDC1080_Soft_Reset(void)
{
uint8_t temp[2];
temp[0] = 0x80;
temp[1] = 0x00;
HDC1080_Write_Config(HDC1080_READ_Config, temp, 2);
Delay_ms(16);//there should be waiting for more than 15 ms.
}
// 在寄存器地址0x02中配置采集参数
void HDC1080_Setting(void)
{
uint16_t tempcom = 0;
uint8_t temp[2];
tempcom |= 1 << 12 ;//通过将位[12]设置为1,将采集模式设置为测量温度和湿度
// 都设置为14bit 分辨率
temp[0] = (uint8_t)tempcom;
temp[1] = (uint8_t)(tempcom >> 8);
HDC1080_Write_Config(0x02, temp, 2);
}
// 初始化
void HDC1080_Init()
{
HDC1080_Soft_Reset();
HDC1080_Setting();
}