micro_climate/Drivers/HP203B/hp203b.c

230 lines
4.4 KiB
C
Raw Normal View History

#include "hp203b.h"
2024-07-18 08:31:44 +00:00
#include "anemometer_dev.h"
#include "frt_protocol.h"
void SOFT_IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
// /*Configure GPIO pin Output Level */
GPIO_InitStruct.Pin = IIC_SCL_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(IIC_SCL_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = IIC_SDA_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(IIC_SDA_PORT, &GPIO_InitStruct);
HAL_GPIO_WritePin(IIC_SCL_PORT, IIC_SCL_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(IIC_SDA_PORT, IIC_SDA_PIN, GPIO_PIN_SET);
}
void HAL_Delay_us(uint32_t us)
{
uint32_t tickstart = 0;
tickstart = HAL_GetTick();
while ((HAL_GetTick() - tickstart) < (us / 1000));
}
void IIC_SDA(uint8_t status)
{
HAL_GPIO_WritePin(IIC_SDA_PORT, IIC_SDA_PIN, status);
HAL_Delay_us(10);
}
void IIC_SCL(uint8_t status)
{
HAL_GPIO_WritePin(IIC_SCL_PORT, IIC_SCL_PIN, status);
HAL_Delay_us(10);
}
void SDA_OUT()
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = IIC_SDA_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(IIC_SDA_PORT, &GPIO_InitStruct);
}
void SDA_IN()
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = IIC_SDA_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(IIC_SDA_PORT, &GPIO_InitStruct);
}
uint8_t IIC_SDA_Read(void)
{
uint8_t temp = 1;
HAL_Delay_us(10);
temp = HAL_GPIO_ReadPin(IIC_SDA_PORT, IIC_SDA_PIN);
return temp;
}
void IIC_Start(void)
{
IIC_SCL(0);
IIC_SDA(1);
IIC_SCL(1);
IIC_SDA(0);
IIC_SCL(0);
}
void IIC_Stop(void)
{
IIC_SCL(0);
IIC_SDA(0);
IIC_SCL(1);
IIC_SDA(1);
}
void IIC_SentAck(uint8_t AckBit)
{
IIC_SCL(0);
IIC_SDA(AckBit);
IIC_SCL(1);
IIC_SCL(0);
IIC_SDA(1);
}
uint8_t IIC_ReceiveAck(void)
{
uint8_t AckBit;
IIC_SCL(0);
IIC_SDA(1);
IIC_SCL(1);
AckBit = IIC_SDA_Read();
IIC_SCL(0);
IIC_SDA(1);
if(AckBit == 1){
return 1;
}
else if(AckBit == 0)
return 0;
}
void IIC_SendByte(uint8_t Byte) //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽں<D6BD><DABA><EFBFBD>
{
for (uint8_t i = 0; i < 8; i ++)
{
IIC_SCL(0);
(Byte&0x80)?IIC_SDA(1):IIC_SDA(0);
Byte<<=1;osDelay(10);
IIC_SCL(1);
}
IIC_SCL(0);
IIC_SDA(1);
}
uint8_t IIC_ReceiveByte(uint8_t ack) //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽں<D6BD><DABA><EFBFBD>
{
uint8_t Byte = 0x00;
for (uint8_t i = 0; i < 8; i ++)
{
IIC_SCL(1);
Byte<<=1;
if(IIC_SDA_Read()) Byte|=0x01;
IIC_SCL(0);
}
if(ack)IIC_SentAck(1);
else IIC_SentAck(0);
return Byte;
}
/****************************
2024-07-18 08:31:44 +00:00
*<EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>set_mode
*<EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ôӻ<EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-07-18 08:31:44 +00:00
*
*
*<EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><EFBFBD><EFBFBD>
*****************************/
2024-07-18 08:31:44 +00:00
void hp203_set_mode()
{
2024-07-18 08:31:44 +00:00
IIC_Start();
IIC_SendByte(HP20X_ADDRESSCMD_WRITE);
if(!IIC_ReceiveAck()){
IIC_SendByte(HP20X_CONVERT_OSR1024);
if(!IIC_ReceiveAck()){
IIC_Stop();
}
}
}
/****************************
2024-07-18 08:31:44 +00:00
*<EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>Hp203bReadPressure
*<EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ѹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ρ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-07-18 08:31:44 +00:00
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Press--<EFBFBD><EFBFBD>ѹֵ
*
*
*<EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><EFBFBD><EFBFBD>
*****************************/
2024-07-18 08:31:44 +00:00
void Hp203bReadPressure(void)
{
2024-07-18 08:31:44 +00:00
long Hp203b_Pressure = 0;
uint8_t Hp203bPressure_Temp[4] = {0};
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
IIC_Start();
IIC_SendByte(HP20X_ADDRESSCMD_WRITE);
while(IIC_ReceiveAck()){}
IIC_SendByte(HP20X_READ_P);
while(IIC_ReceiveAck()){}
IIC_Stop();
//<2F><>ȡ
IIC_Start();
IIC_SendByte(HP20X_ADDRESSCMD_READ);
while(IIC_ReceiveAck()){}
Hp203bPressure_Temp[0] = IIC_ReceiveByte(0);
Hp203bPressure_Temp[1] = IIC_ReceiveByte(0);
Hp203bPressure_Temp[2] = IIC_ReceiveByte(0);
Hp203bPressure_Temp[3] = IIC_ReceiveByte(0);
IIC_Stop();
Hp203b_Pressure = Hp203bPressure_Temp[0];
Hp203b_Pressure <<= 8;
Hp203b_Pressure |= Hp203bPressure_Temp[1];
Hp203b_Pressure <<= 8;
Hp203b_Pressure |= Hp203bPressure_Temp[2];
Hp203b_Pressure = Hp203b_Pressure / 100;
g_stMcs_Para.pressure = Hp203b_Pressure;
}
2024-07-18 08:31:44 +00:00