#include "hp203b.h" #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) //发送一个字节函数 { 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) //接收一个字节函数 { 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; } /**************************** *名称:set_mode *功能:配置从机模式,设置通道及采样率 *参数:无 * * *返回:无 *****************************/ void hp203_set_mode() { IIC_Start(); IIC_SendByte(HP20X_ADDRESSCMD_WRITE); if(!IIC_ReceiveAck()){ IIC_SendByte(HP20X_CONVERT_OSR1024); if(!IIC_ReceiveAck()){ IIC_Stop(); } } } /**************************** *名称:Hp203bReadPressure *功能:获取气压、海拔、温度数据 *参数:Press--气压值 * * *返回:无 *****************************/ void Hp203bReadPressure(void) { long Hp203b_Pressure = 0; uint8_t Hp203bPressure_Temp[4] = {0}; //请求读取 IIC_Start(); IIC_SendByte(HP20X_ADDRESSCMD_WRITE); while(IIC_ReceiveAck()){} IIC_SendByte(HP20X_READ_P); while(IIC_ReceiveAck()){} IIC_Stop(); //读取 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; }