一种RC去耦式倾角测量电路
一种RC去耦式倾角测量电路,通过合理的电路设计布局,有效抑制噪声干扰,实现高精度的倾角测量。其运行代码如下:
/发布日期:2022.10.17------------------------/
/* Includes ----------------------------------*/
#include “main.h”
#include “stm32f1xx_hal.h”
#include “iwdg.h”
#include “spi.h”
#include “usart.h”
#include “gpio.h”
#include “ADS.h”
#include “MyFlash.h”
#include “string.h”
#include “math.h”
/* 参数 -------------------------------------*/
#define PI2 6.283185307179586
uint16_t wait=0;
uint8_t SweepNum=9;
double FilterDataX[9];
double FilterDataY[9];
double RawDataX[9];
double RawDataY[9];
double SenX=0.4;
double SenY=0.4;
double VnullX,VnullY;
double VX,VY,VT,VX_cal,VY_cal,Vtemp,AX,AY;
double OffX[4],OffY[4],CT[4],Wmode;
/* 串口参数 ---------------------------------*/
uint8_t RxFlag=0;
uint8_t ReceiveNum=0;
char RxBuff[40];
uint8_t RxBuff0[1];
/* -------------------------------------------*/
void SystemClock_Config(void);
void SendData(double ax,double ay,double t);
/* -------------------------------------------/
int main(void)
{
HAL_Init();
SystemClock_Config();
HAL_Delay(100500);
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_SPI1_Init();
//MX_IWDG_Init();
/---------------------------------------------/
for(uint8_t loop=0;loop<=SweepNum-1;loop++)
{
FilterDataX[loop]=0;FilterDataY[loop]=0;
RawDataX[loop]=0;RawDataY[loop]=0;
}
ReadFlash();
/---------------------------------------------/
InitADS();
HAL_Delay(100);
wait=0;
while(GainSetCal())
{
//HAL_IWDG_Refresh(&hiwdg);
HAL_Delay(100);
wait=wait+1;
if(wait>=1000)
{
break;
}
}
wait=0;
while(OffSetCal())
{
//HAL_IWDG_Refresh(&hiwdg);
HAL_Delay(100);
wait=wait+1;
if(wait>=1000)
{
break;
}
}
/-----打开串口中断----------------------------/
HAL_UART_Receive_IT(&huart1,RxBuff0,1);
/-----打开串口中断----------------------------/
while (1)
{
InitADS_ChannleAX();
HAL_Delay(10);
VX=ReadOnceAD();
InitADS_ChannleAY();
HAL_Delay(10);
VY=ReadOnceAD();
/-----------------------------------------------/
InitADS_ChannleT();
HAL_Delay(10);
VT=ReadOnceAD();
Vtemp=(VX_cal-0.9)/SenX;//转换为g
if(Vtemp>1){Vtemp=1;}
if(Vtemp<-1){Vtemp=-1;}
AX=asin(Vtemp)/PI2*360;
Vtemp=(VY_cal-0.9)/SenY;//转换为g
if(Vtemp>1){Vtemp=1;}
if(Vtemp<-1){Vtemp=-1;}
AY=asin(Vtemp)/PI2*360;
SendData(AX,AY,VT);
CommandRx();
//HAL_IWDG_Refresh(&hiwdg);
}
}
/----------------------------------------------/
void SendData(double ax,double ay,double t)
{
uint16_t Dx,Dy,Dt;
uint8_t TxData[8];
if(ax>=30){ax=30;}
if(ax<=-30){ax=-30;}
if(ay>=30){ay=30;}
if(ay<=-30){ay=-30;}
if(t>=70){t=70;}
if(t<=-55){t=-55;}
if(ax>=0)
{
Dx=(uint16_t)(ax*1000);
}
else
{
Dx=65536-(uint16_t)(-1.0*ax*1000);
}
if(ay>=0)
{
Dy=(uint16_t)(ay*1000);
}
else
{
Dy=65536-(uint16_t)(-1.0*ay*1000);
}
if(t>=0)
{
Dt=(uint16_t)(t*10);
}
else
{
Dt=65536-(uint16_t)(-1.0*t*10);
}
TxData[0]=0xAA;
TxData[1]=Dx&0x00FF;TxData[2]=(Dx&0xFF00)>>8;
TxData[3]=Dy&0x00FF;TxData[4]=(Dy&0xFF00)>>8;
TxData[5]=Dt&0x00FF;TxData[6]=(Dt&0xFF00)>>8;
TxData[7]=TxData[1]+TxData[2]+TxData[3]+TxData[4]+TxData[5]+TxData[6];
HAL_UART_Transmit(&huart1,TxData,8,2000);
}
/-----------------------------------------------/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Initializes the CPU, AHB and APB busses clocks */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(FILE, LINE);
}
/**Initializes the CPU, AHB and APB busses clocks */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
_Error_Handler(FILE, LINE);
}
/**Configure the Systick interrupt time */
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000/100);
/Configure the Systick /
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/ SysTick_IRQn interrupt configuration /
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/-----------------------------------------------/
void _Error_Handler(char file, int line)
{
__set_FAULTMASK(1);//
NVIC_SystemReset();//
}
/-----------------------------------------------/
#ifdef USE_FULL_ASSERT
/
- @brief Reports the name of the source file and the source line number
-
where the assert_param error has occurred.
- @param file: pointer to the source file name
- @param line: assert_param error line source number
- @retval None
/
void assert_failed(uint8_t file, uint32_t line)
{
__set_FAULTMASK(1);//
NVIC_SystemReset();//
}
#endif /* USE_FULL_ASSERT /
/*********************** © COPYRIGHT STMicroelectronics *END OF FILE/
/发布日期:2022.10.17------------------------/