基于STM32F407的AD7606 8通道并行采样功能实现

函数说明

本程序采用16位并行模式下对AD7606的8个采样通道进行同步采样,并对采样后的数据进行卡尔曼滤波处理。

GPIO初始化函数

void AD7606Init(void) //AD06060初始化函数
{
 GPIO_InitTypeDef GPIO_InitStructure;
 
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC|\
             RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOF|\
             RCC_AHB1Periph_GPIOG|RCC_AHB1Periph_GPIOH|RCC_AHB1Periph_GPIOI,ENABLE); //使能GPIOA的时钟
 /***********************8GPIO输出配置******************************/
 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_100MHz; //高速GPIO
 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
 GPIO_Init(GPIOB,&GPIO_InitStructure);//PB7
    
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
 GPIO_Init(GPIOI,&GPIO_InitStructure);//PI4
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
 GPIO_Init(GPIOE,&GPIO_InitStructure);//PE0,1

 
 /************************GPIO输入配置*********************************/
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12;
 GPIO_Init(GPIOC,&GPIO_InitStructure);
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|\
                GPIO_Pin_5|GPIO_Pin_6;
 GPIO_Init(GPIOD,&GPIO_InitStructure);//PD0,1,2,3,4,5,6
 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
 GPIO_Init(GPIOG,&GPIO_InitStructure);
  
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_6|GPIO_Pin_7;
 GPIO_Init(GPIOI,&GPIO_InitStructure);
 
 AD760600_RESET = 0;
 AD760600_CONVST =1;
 AD760600_CS = 1;
 AD760600_RD_SCLK = 1;
 

AD7606驱动函数


/***********************AD760600驱动函数****************************/
void AD760600Reset(void)
{
 AD760600_RESET = 1;
 delay_us(1);
 AD760600_RESET = 0;
 AD760600_CONVST = 0;
 delay_us(1);
 AD760600_CONVST = 1;
}
void AD760600ReadOneSample(u16*Dout,u8 Channels)
{
 u8 i,j;
 u8 Date;
 AD760600Reset();
 u8 Read_Busy ;
 Read_Busy = AD760600_BUSY;
 while(Read_Busy == 1)
 {
  Read_Busy = AD760600_BUSY;
 }
 AD760600_CS = 0;
 for(i=0;i<Channels;i++) //Channels 通道数
 {
  AD760600_RD_SCLK = 0;
  Date = AD760600_DB0<<0|AD760600_DB1<<1|AD760600_DB2<<2|AD760600_DB3<<3|AD760600_DB4<<4|AD760600_DB5<<5|\
       AD760600_DB6<<6|AD760600_DB7<<7|AD760600_DB8<<8|AD760600_DB9<<9|AD760600_DB10<<10|AD760600_DB11<<11|AD760600_DB12<<12|\
       AD760600_DB13|AD760600_DB14<<14|AD760600_DB15<<15;
  AD760600_RD_SCLK = 1;
  delay_us(1);
  Dout[i] = Date;
 }
 AD760600_CS = 1;
 for(j=1;j<=8;j++)
 {
  Dout[j] = Kalman(Dout[j]);
  }
 
}

卡尔曼滤波

#define KalMan_Q 1
#define KalMan_R 1000
float Kalman(float z)
{
 static float x_1;
 float x_mid = x_1;
 float x_now;
 
 static float p_1;
 float p_mid = p_1;
 float p_now;
 
 float k;
 x_mid = x_1;
 p_mid = p_1 +KalMan_Q;
 
 k = p_mid/(p_mid+KalMan_R);
 x_now = x_mid+k*(z-x_mid);
 p_now = (1-k)*p_mid;
 p_1 = p_now;
 x_1 = x_now;
 
 return x_now;
  
}
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值