|
大家好,我最近在使用lps22hb这颗芯片,发现读到的压力值不对,只有0.7kPa,温度值正确。
官方提供了read_data_simple.c的例程,使用的是cube平台,我想使用smt32的库函数。我修改了原文中的platform_write和platform_read两个函数,如下:
- /*********************************************************************
- * SPI1_Init : LPS22HB
- *********************************************************************/
- void SPI1_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- SPI_InitTypeDef SPI_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; // SCK/MISO/MOSI
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // CS
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_SetBits(GPIOA, GPIO_Pin_4); // CS拉高
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // 全双工
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // 主机模式
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // 8位帧结构
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // 串行同步时钟的空闲状态为高电平
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // 串行同步时钟的第2个跳变沿(上升或下降)数据被采样
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // NSS信号由软件(SSI位)控制, 片选
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; // 波特率预分频值为256, 72M/256=281.25K
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // 数据传输从MSB位开始
- SPI_InitStructure.SPI_CRCPolynomial = 7; // CRC值计算的多项式
- SPI_Init(SPI1, &SPI_InitStructure); // 根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
- SPI_Cmd(SPI1, ENABLE); // 使能SPI外设
- }
- /*********************************************************************
- * SPI1 speed setting
- * SPI_BaudRatePrescaler_2 2分频
- * SPI_BaudRatePrescaler_8 8分频
- * SPI_BaudRatePrescaler_16 16分频
- * SPI_BaudRatePrescaler_256 256分频
- *********************************************************************/
- /*void SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)
- {
- assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
- SPI1->CR1 &= 0XFFC7;
- SPI1->CR1 |= SPI_BaudRatePrescaler; // 设置SPI1速度
- SPI_Cmd(SPI1,ENABLE);
- }*/
- u8 SPIx_Send_Byte(SPI_TypeDef* SPIx, u8 dat)
- {
- u8 retry = 0; // 超时处理, 避免程序死在这里
- while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET) // 检查指定的SPI标志位设置与否: 发送缓存空标志位, 为1表示空
- {
- retry++;
- if(retry > 200)
- return 0;
- }
- SPI_I2S_SendData(SPIx, dat); // 通过外设SPIx发送1字节
- retry = 0;
- while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET) // 检查指定的SPI标志位设置与否: 接收缓存非空标志位, 为1表示包含有效数据
- {
- retry++;
- if(retry > 200)
- return 0;
- }
- return SPI_I2S_ReceiveData(SPIx); // 返回SPIx最近接收的数据
- }
- void SPIx_Send_nByte(SPI_TypeDef* SPIx, u8 *buf, u16 len)
- {
- u16 i;
- for (i=0; i<len; i++)
- {
- SPIx_Send_Byte(SPIx, *buf++);
- }
- }
- void SPIx_Read_nByte(SPI_TypeDef* SPIx, u8 *buf, u16 len)
- {
- u16 i;
- for (i=0; i<len; i++)
- {
- *buf++ = SPIx_Send_Byte(SPIx, 0xFF); // 发送一个哑字节以读取数据
- }
- }
- static int32_t platform_write(void *handle, uint8_t Reg, uint8_t *Bufp, uint16_t len)
- {
- YL_Cs_L();
- SPIx_Send_Byte(handle, Reg);
- SPIx_Send_nByte(handle, Bufp, len);
- YL_Cs_H();
- return 0;
- }
- static int32_t platform_read(void *handle, uint8_t Reg, uint8_t *Bufp, uint16_t len)
- {
- Reg |= 0x80;
- YL_Cs_L();
- SPIx_Send_Byte(handle, Reg);
- SPIx_Read_nByte(handle, Bufp, len);
- YL_Cs_H();
- return 0;
- }
- /*********************************************************************
- * Lps_Config : 获取压力传感器数据
- * err : 0-正确, 1-ID号有错
- *********************************************************************/
- u8 Lps_Config(void)
- {
- u8 err=0;
- /* Initialize mems driver interface */
- dev_ctx.write_reg = platform_write;
- dev_ctx.read_reg = platform_read;
- dev_ctx.handle = SPI1;
-
- /* Check device ID */
- lps22hb_device_id_get(&dev_ctx, &whoamI);
- if (whoamI != LPS22HB_ID)
- {
- err = 1;
- }
-
- /* Restore default configuration */
- lps22hb_reset_set(&dev_ctx, PROPERTY_ENABLE);
- do {
- lps22hb_reset_get(&dev_ctx, &rst);
- } while (rst);
-
- /* Enable Block Data Update */
- //lps22hb_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
-
- /* Can be enabled low pass filter on output */
- lps22hb_low_pass_filter_mode_set(&dev_ctx, LPS22HB_LPF_ODR_DIV_2);
-
- /* Can be set Data-ready signal on INT_DRDY pin */
- //lps22hb_drdy_on_int_set(&dev_ctx, PROPERTY_ENABLE);
-
- /* Set Output Data Rate */
- lps22hb_data_rate_set(&dev_ctx, LPS22HB_ODR_10_Hz);
- return err;
- }
- /*********************************************************************
- * Get_Lps_Data : 获取压力传感器数据
- * err : 0-正确, 1-ID号有错, 2-读取出错
- *********************************************************************/
- u8 Get_Lps_Data(float *pressure_kPa, float *temperature_degC)
- {
- uint8_t reg;
- u8 err = 0;
- float pressure_hPa = 0.0;
-
- /* Read output only if new value is available */
- lps22hb_press_data_ready_get(&dev_ctx, ®);
- if (reg)
- {
- memset(data_raw_pressure.u8bit, 0x00, sizeof(int32_t));
- lps22hb_pressure_raw_get(&dev_ctx, data_raw_pressure.u8bit);
- pressure_hPa = lps22hb_from_lsb_to_hpa(data_raw_pressure.i32bit);
- *pressure_kPa = pressure_hPa/10.0;
-
- memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t));
- lps22hb_temperature_raw_get(&dev_ctx, data_raw_temperature.u8bit);
- *temperature_degC = lps22hb_from_lsb_to_degc(data_raw_temperature.i16bit);
- }
- else err = 1;
- return err;
- }
复制代码
请大家帮我分析下,代码上有没有问题?
|
|