计算应该没问题吧,0度到正90度的计算都正确,但是从传感器采集的数值,始终不能到-90度,只能到-80度左右。
void SCA61T_SpiInit(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI3 and GPIO clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3 , ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
/* Connect PXx to SPI_SCK */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
/* Connect PXx to SPI_MISO */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SPI3);
/* Connect PXx to SPI_MOSI */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);
/* Configure SPI3 pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* SPI3 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI3, &SPI_InitStructure);
/* Enable SPI3 */
SPI_Cmd(SPI3, ENABLE);
/* Configure GPIO PIN for Lis Chip select */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Deselect : Chip Select high */
GPIO_SetBits(GPIOA, GPIO_Pin_15);
}
/*******************************************************************************
* Function Name : SCA61T_SPI_SendByte
* Description : Sends a Byte through the SPI interface and return the
* Byte received from the SPI bus.
* Input : Byte : Byte send.
* Output : None
* Return : The received byte value
*******************************************************************************/
uint8_t SCA61T_SPI_SendByte(uint8_t byte)
{
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);
/* Send Half Word through the SPI peripheral */
SPI_I2S_SendData(SPI3, byte);
/* Wait to receive a Half Word */
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the Half Word read from the SPI bus */
return (int8_t)SPI_I2S_ReceiveData(SPI3);
}
uint8_t SCA61T_ReadDataReg(uint8_t Reg, uint16_t* Data)
{
uint8_t value_h=0,value_l=0;
uint16_t value_temp=0;
SCA61T_CS_LOW();
SCA61T_SPI_SendByte(Reg); //read x data command
value_h = SCA61T_SPI_SendByte(0xFF); //read hight 8 bit data
value_l = SCA61T_SPI_SendByte(0xFF); //read low 8 bit data
SCA61T_CS_HIGH();
value_temp = ((uint16_t)value_h & 0x00FF);
value_temp = ((value_temp << 3) & 0x07F8) + (((value_l & 0xE0) >> 5) & 0x07); //hight 8 bit + low 3 bit = 11bit x data;
*Data = value_temp;
return MEMS_SUCCESS;
}
uint16_t SCA61T_Read_Xdata(void)
{
uint16_t x_value=0;
SCA61T_CS_HIGH();
vTaskDelay(1);
SCA61T_ReadDataReg(RDAX_REG,&x_value);
my_uDelay(10);
return x_value;
}
float SCA61T_Transformation_Angle(float senscomp)
{
float angle=0.00f;
uint8_t i=0;
uint16_t x_value=0;
uint32_t temp_value=0;
for(i=0; i<3; i++)
{
temp_value += SCA61T_Read_Xdata();
}
x_value = temp_value / i;
if(x_value <= 1024 - SENS) x_value = 1024-SENS;
if(x_value >= 1024 + SENS) x_value = 1024+SENS;
angle=asinf(((float)x_value-1024.00)/senscomp);
angle=angle*180/PI;
return angle;
}
|