1、CUBEMA配置
2、配置代码
// HAL_ADCEx_Calibration_Start(&hadc1);
// HAL_ADCEx_Calibration_Start(&hadc2);
HAL_ADCEx_Calibration_Start(&hadc3);//校准ADC
main里面添加任务:
// ADC1_Process();//未测试
// ADC2_Process();//未测试
ADC3_Process();//测试成功
//卡尔曼滤波
int KalmanFilter4(int inData4)
{
static float prevData4 = 0; //先前数值
static float p4 = 20.0f, q4 = 0.0001f, r4 = 0.005f, kGain4 = 0.001f; // q控制误差 r控制响应速度
p4 = p4 + q4;
kGain4 = p4 / ( p4 + r4 ); //计算卡尔曼增益
inData4 = prevData4 + ( kGain4 * ( inData4 - prevData4 ) ); //计算本次滤波估计值
p4 = ( 1 - kGain4 ) * p4; //更新测量方差
prevData4 = inData4;
return inData4; //返回滤波值
}
// ADC执行程序
#define chanle3 4
uint16_t ADC3_Value[chanle3] ;
float ADC3_1 =0.0f;
float ADC3_2 =0.0f;
float ADC3_3 =0.0f;
float ADC3_4 =0.0f;
int ADC_value_buf[4];
void ADC3_Process(void)//已经准确获取电压
{
HAL_ADC_Start_DMA(&hadc3,(uint32_t *)ADC3_Value,chanle3);
// Usart1Printf("%d\r %d\r %d\r %d\r\n",ADC3_Value[0],ADC3_Value[1],ADC3_Value[2],ADC3_Value[3]);
//卡尔曼滤波
ADC3_Value[0]= KalmanFilter1(ADC3_Value[0]);
ADC3_4 =ADC3_Value[0]/ 4095.0f * 3.3f ;
ADC3_Value[1]= KalmanFilter2(ADC3_Value[1]);
ADC3_3 =ADC3_Value[1]/ 4095.0f * 3.3f ;
ADC3_Value[2]= KalmanFilter3(ADC3_Value[2]);
ADC3_2 =ADC3_Value[2]/ 4095.0f * 3.3f ;
ADC3_Value[3]= KalmanFilter4(ADC3_Value[3]);
ADC3_1 =ADC3_Value[3]/ 4095.0f * 3.3f ;
Usart1Printf(" %.6f,%.6f,%.6f,%.6f\n",ADC3_4,ADC3_3,ADC3_2,ADC3_1);
}
3、上位机显示效果
参考电源直接一条直线!!!
DAC输出的正弦波形也非常平滑!!!