SHT10驱动程序,C语言,keil编写
时间: 2023-06-13 21:07:54 浏览: 114
基于Keil软件的C程序的程序代码
以下是SHT10驱动程序的C语言代码,使用Keil进行编写:
```c
#include "SHT10.h"
#define SCK_H() SHT10_SCK_PORT->BSRR = SHT10_SCK_PIN
#define SCK_L() SHT10_SCK_PORT->BRR = SHT10_SCK_PIN
#define DATA_H() SHT10_DATA_PORT->BSRR = SHT10_DATA_PIN
#define DATA_L() SHT10_DATA_PORT->BRR = SHT10_DATA_PIN
#define DATA_IN() SHT10_DATA_PORT->CRL &= 0xFFFFFFF0
#define DATA_OUT() SHT10_DATA_PORT->CRL &= 0xFFFFFFF0; SHT10_DATA_PORT->CRL |= 0x00000001
void SHT10_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(SHT10_SCK_RCC | SHT10_DATA_RCC, ENABLE);
GPIO_InitStructure.GPIO_Pin = SHT10_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SHT10_SCK_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SHT10_DATA_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SHT10_DATA_PORT, &GPIO_InitStructure);
SCK_L();
DATA_H();
}
static void SHT10_Reset(void)
{
uint8_t i;
DATA_OUT();
DATA_H();
SCK_L();
delay_us(200);
SCK_H();
delay_us(2);
DATA_L();
delay_us(2);
SCK_L();
delay_us(200);
SCK_H();
delay_us(2);
DATA_H();
delay_us(2);
SCK_L();
DATA_IN();
for (i = 0; i < 100; i++)
{
if ((SHT10_DATA_PORT->IDR & SHT10_DATA_PIN) == 0)
{
break;
}
delay_us(1);
}
if (i == 100)
{
return;
}
for (i = 0; i < 100; i++)
{
if ((SHT10_DATA_PORT->IDR & SHT10_DATA_PIN) != 0)
{
break;
}
delay_us(1);
}
if (i == 100)
{
return;
}
for (i = 0; i < 100; i++)
{
if ((SHT10_DATA_PORT->IDR & SHT10_DATA_PIN) == 0)
{
break;
}
delay_us(1);
}
if (i == 100)
{
return;
}
}
static uint8_t SHT10_WriteByte(uint8_t byte)
{
uint8_t i, ack;
DATA_OUT();
for (i = 0; i < 8; i++)
{
if (byte & 0x80)
{
DATA_H();
}
else
{
DATA_L();
}
byte <<= 1;
SCK_H();
delay_us(2);
SCK_L();
}
DATA_IN();
SCK_H();
delay_us(2);
ack = SHT10_DATA_PORT->IDR & SHT10_DATA_PIN;
SCK_L();
return ack;
}
static uint8_t SHT10_ReadByte(uint8_t ack)
{
uint8_t i, byte;
byte = 0;
DATA_IN();
for (i = 0; i < 8; i++)
{
byte <<= 1;
SCK_H();
delay_us(2);
if (SHT10_DATA_PORT->IDR & SHT10_DATA_PIN)
{
byte |= 0x01;
}
SCK_L();
}
DATA_OUT();
if (ack)
{
DATA_L();
}
else
{
DATA_H();
}
SCK_H();
delay_us(2);
SCK_L();
return byte;
}
static uint16_t SHT10_ReadRawTemp(void)
{
uint16_t temp;
SHT10_Reset();
SHT10_WriteByte(SHT10_MEASURE_TEMP);
delay_ms(20);
temp = SHT10_ReadByte(1);
temp <<= 8;
temp |= SHT10_ReadByte(0);
SHT10_Reset();
return temp;
}
static uint16_t SHT10_ReadRawHumidity(void)
{
uint16_t humidity;
SHT10_Reset();
SHT10_WriteByte(SHT10_MEASURE_HUMIDITY);
delay_ms(20);
humidity = SHT10_ReadByte(1);
humidity <<= 8;
humidity |= SHT10_ReadByte(0);
SHT10_Reset();
return humidity;
}
float SHT10_ReadTemp(void)
{
float temp;
temp = SHT10_ReadRawTemp();
temp *= 0.01;
temp -= 40.0;
return temp;
}
float SHT10_ReadHumidity(void)
{
float humidity;
humidity = SHT10_ReadRawHumidity();
humidity *= 0.04;
humidity -= 4.0;
humidity += (0.648 * SHT10_ReadRawTemp()) - 12.0;
return humidity;
}
```
该代码中包含以下函数:
- `SHT10_Init()`:SHT10初始化函数,配置SCK和DATA引脚并将SCK引脚置为低电平,DATA引脚置为高电平。
- `SHT10_Reset()`:SHT10复位函数,发送复位指令并等待SHT10应答。
- `SHT10_WriteByte()`:向SHT10写入一个字节数据,并等待SHT10应答。
- `SHT10_ReadByte()`:从SHT10读取一个字节数据,并向SHT10发送应答信号。
- `SHT10_ReadRawTemp()`:从SHT10读取温度原始数据。
- `SHT10_ReadRawHumidity()`:从SHT10读取湿度原始数据。
- `SHT10_ReadTemp()`:从SHT10读取温度值。
- `SHT10_ReadHumidity()`:从SHT10读取湿度值。
以上代码中的一些宏定义需要根据具体的硬件连接进行修改,例如SHT10_SCK_PORT、SHT10_SCK_PIN、SHT10_DATA_PORT、SHT10_DATA_PIN、SHT10_SCK_RCC和SHT10_DATA_RCC等。此外,该代码中还使用了一些延时函数,需要根据实际情况进行调整或使用其他方式代替。
阅读全文