目录
1、DS18B20读取
2、DHT11
2.1 宏定义
2.2 延时
2.3 设置引脚输出
2.4 设置引脚输入
2.5 复位
2.6 检测函数
2.7 读取DHT11一个位
2.7.1 数据位为0的电平信号显示
2.7.2 数据位为1的电平信号显示
2.8 读取DHT11一个字节
2.9 DHT11初始化
2.10 读取DHT11一次数据
3、光敏电阻
3.1 光敏电阻DO
3.1.1 CubeMx配置
3.1.2 初始化光敏电阻DO
3.2 光敏电阻AO
3.2.1 CubeMx配置
3.2.2 初始化光敏电阻AO
1、DS18B20读取
int16_t ds18b20_read(void)
{
uint8_t Val[2];
int16_t x;
ow_reset();
ow_byte_wr(OW_SKIP_ROM);
ow_byte_wr(DS18B20_CONVERT);
delay_us(750000);
ow_reset();
ow_byte_wr(OW_SKIP_ROM);
ow_byte_wr(DS18B20_READ);
int i;
for(i=0;i<2;i++)
{
Val[i] = ow_byte_rd();
}
x = Val[1];
x <<= 8;
x |= Val[0];
return x;
}
2、DHT11
2.1 宏定义
#define DHT11_PIN_PORT GPIOA
#define DHT11_PIN GPIO_PIN_7
#define DHT11_PIN_CLOCK __HAL_RCC_GPIOA_CLK_ENABLE()
#define DHT11_PIN_OUT_H HAL_GPIO_WritePin(DHT11_PIN_PORT, DHT11_PIN, GPIO_PIN_SET)
#define DHT11_PIN_OUT_L HAL_GPIO_WritePin(DHT11_PIN_PORT, DHT11_PIN, GPIO_PIN_RESET)
#define DHT11_PIN_IN HAL_GPIO_ReadPin(DHT11_PIN_PORT, DHT11_PIN)
2.2 延时
#define Delay_us(X) delay((X)*80/5)
void delay(unsigned int n)
{
while(n--);
}
2.3 设置引脚输出
void DHT11_PIN_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/**/
GPIO_InitStruct.Pin = DHT11_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//推挽输出
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;//高速
HAL_GPIO_Init(DHT11_PIN_PORT, &GPIO_InitStruct);
}
2.4 设置引脚输入
void DHT11_PIN_INPUT(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
DHT11_PIN_CLOCK;
/**/
GPIO_InitStruct.Pin = DHT11_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;//输入
GPIO_InitStruct.Pull = GPIO_PULLUP;//上拉
HAL_GPIO_Init(DHT11_PIN_PORT, &GPIO_InitStruct);
}
2.5 复位
//复位DHT11
void DHT11_Rst(void)
{
DHT11_PIN_OUT(); //设置为输出
DHT11_PIN_OUT_L; //低电平
HAL_Delay(20); //拉低至少18ms
DHT11_PIN_OUT_H; //高电平
Delay_us(60); //主机拉高20~40us
}
2.6 检测函数
//等待DHT11的回应
//返回1:未检测到DHT11的存在
//返回0:存在
uint8_t DHT11_Check(void)
{
uint8_t re = 0;
DHT11_PIN_INPUT(); //设置为输入
while (DHT11_PIN_IN && re < 100) //DHT11会拉低40~80us
{
re++;
Delay_us(1);
};
if(re >= 100)return 1;
else re = 0;
while (!DHT11_PIN_IN && re < 100) //DHT11拉低后会再次拉高40~80us
{
re++;
Delay_us(1);
};
if(re >= 100)return 1;
return 0;
}
2.7 读取DHT11一个位
2.7.1 数据位为0的电平信号显示
2.7.2 数据位为1的电平信号显示
//从DHT11读取一个位
uint8_t DHT11_Read_Bit(void)
{
uint8_t re = 0;
while(DHT11_PIN_IN && re < 110) //等待变为低电平
{
re++;
Delay_us(1);
}
re = 0;
while(!DHT11_PIN_IN && re < 110) //等待变高电平
{
re++;
Delay_us(1);
}
Delay_us(80);//等待40us
if(DHT11_PIN_IN)return 1;
else return 0;
}
2.8 读取DHT11一个字节
//从DHT11读取一个字节
uint8_t DHT11_Read_Byte(void)
{
uint8_t i, dat;
dat = 0;
for (i = 0; i < 8; i++)
{
dat <<= 1;
dat |= DHT11_Read_Bit();
}
return dat;
}
2.9 DHT11初始化
//初始化DHT11的IO口,同时检测DHT11的存在
uint8_t DHT11_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
DHT11_PIN_CLOCK;;
GPIO_InitStruct.Pin = DHT11_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//推挽输出
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;//告诉
HAL_GPIO_Init(DHT11_PIN_PORT, &GPIO_InitStruct);
DHT11_Rst();//复位
return DHT11_Check();
}
2.10 读取DHT11一次数据
//从DHT11读取一次数据
uint8_t DHT11_Read_Data(uint8_t *temp, uint8_t *humi)
{
uint8_t buf[5];
uint8_t i;
DHT11_Rst();
if(DHT11_Check() == 0)
{
for(i = 0; i < 5; i++)
{
buf[i] = DHT11_Read_Byte();
}
if((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4])
{
*humi = buf[0];
*temp = buf[2];
}
}
else return 1;
return 0;
}
3、光敏电阻
3.1 光敏电阻DO
3.1.1 CubeMx配置
3.1.2 初始化光敏电阻DO
void Photo_DO_Init()
{
/* USER CODE BEGIN ADC2_Init 0 */
/* USER CODE END ADC2_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC2_Init 1 */
/* USER CODE END ADC2_Init 1 */
/** Common config
*/
hadc2.Instance = ADC2;
hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2;
hadc2.Init.Resolution = ADC_RESOLUTION_12B;
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc2.Init.GainCompensation = 0;
hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc2.Init.LowPowerAutoWait = DISABLE;
hadc2.Init.ContinuousConvMode = DISABLE;
hadc2.Init.NbrOfConversion = 1;
hadc2.Init.DiscontinuousConvMode = DISABLE;
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc2.Init.DMAContinuousRequests = DISABLE;
hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc2.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc2) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_17;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC2_Init 2 */
/* USER CODE END ADC2_Init 2 */
}
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(adcHandle->Instance==ADC2)
{
/* USER CODE BEGIN ADC2_MspInit 0 */
/* USER CODE END ADC2_MspInit 0 */
/* ADC2 clock enable */
__HAL_RCC_ADC12_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**ADC2 GPIO Configuration
PA4 ------> ADC2_IN17
*/
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN ADC2_MspInit 1 */
/* USER CODE END ADC2_MspInit 1 */
}
}
3.2 光敏电阻AO
3.2.1 CubeMx配置
3.2.2 初始化光敏电阻AO
void Photo_AO_Init()
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PA3 */
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}