230 lines
4.4 KiB
C
230 lines
4.4 KiB
C
#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) //<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;
|
||
}
|
||
|
||
/****************************
|
||
*<2A><><EFBFBD>ƣ<EFBFBD>set_mode
|
||
*<2A><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD>ôӻ<C3B4>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*
|
||
*
|
||
*<2A><><EFBFBD>أ<EFBFBD><D8A3><EFBFBD>
|
||
*****************************/
|
||
void hp203_set_mode()
|
||
{
|
||
IIC_Start();
|
||
IIC_SendByte(HP20X_ADDRESSCMD_WRITE);
|
||
|
||
if(!IIC_ReceiveAck()){
|
||
IIC_SendByte(HP20X_CONVERT_OSR1024);
|
||
if(!IIC_ReceiveAck()){
|
||
IIC_Stop();
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|
||
/****************************
|
||
*<2A><><EFBFBD>ƣ<EFBFBD>Hp203bReadPressure
|
||
*<2A><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD>ȡ<EFBFBD><C8A1>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD>Ρ<EFBFBD><CEA1>¶<EFBFBD><C2B6><EFBFBD><EFBFBD><EFBFBD>
|
||
*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Press--<2D><>ѹֵ
|
||
*
|
||
*
|
||
*<2A><><EFBFBD>أ<EFBFBD><D8A3><EFBFBD>
|
||
*****************************/
|
||
void Hp203bReadPressure(void)
|
||
{
|
||
|
||
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;
|
||
}
|
||
|
||
|