STM32通过io模拟SPI与ADC124S021通信

1 引脚定义

unsigned char ch_temp;

#define AD_FF 1

#define AD_ZF 2

#define AD_MF 3

#define AD_GM 4

#define ADC124S21_CS_PA4_OUT GPIO_Pin_4 //使用PA4作为输出,控制ADC124S21芯片的片选

#define ADC124S21_CLK_PA5_SCK GPIO_Pin_5 //使用PA5作为输出,模拟用作ADC124S21的CLK

#define ADC124S21_DATA_PA7_DIN GPIO_Pin_7 //使用PA7作为输出,连接ADC124S21的DIN

#define ADC124S21_DATA_PA6_DOUT GPIO_Pin_6 //使用PA6作为输入,连接ADC124S21的DOUT

2 define 高低电平

#define AD_CS_H GPIO_SetBits(GPIOA,ADC124S21_CS_PA4_OUT)

#define AD_CS_L GPIO_ResetBits(GPIOA,ADC124S21_CS_PA4_OUT)

#define AD_DIN_H GPIO_SetBits(GPIOA,ADC124S21_DATA_PA7_DIN)

#define AD_DIN_L GPIO_ResetBits(GPIOA,ADC124S21_DATA_PA7_DIN)

#define AD_SCLK_H GPIO_SetBits(GPIOA,ADC124S21_CLK_PA5_SCK)

#define AD_SCLK_L GPIO_ResetBits(GPIOA,ADC124S21_CLK_PA5_SCK)

#define AD_DOUTGPIO_ReadInputDataBit(GPIOA, ADC124S21_DATA_PA6_DOUT)

3 gpio口的配置

/**

  • 配置用于操作ADC124S21的模拟SPI GPIO端口

*/

int U11ADC124S021_GPIO_init()

{ GPIO_InitTypeDef ADC124S21_GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

ADC124S21_GPIO_InitStructure.GPIO_Pin = ADC124S21_CS_PA4_OUT; //片选引脚配置

ADC124S21_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

ADC124S21_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出

GPIO_Init(GPIOA, &ADC124S21_GPIO_InitStructure);

GPIO_SetBits(GPIOA,ADC124S21_CS_PA4_OUT);

ADC124S21_GPIO_InitStructure.GPIO_Pin = ADC124S21_CLK_PA5_SCK; //时钟引脚配置

ADC124S21_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

ADC124S21_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出

GPIO_Init(GPIOA, &ADC124S21_GPIO_InitStructure);

ADC124S21_GPIO_InitStructure.GPIO_Pin = ADC124S21_DATA_PA7_DIN; //DIN引脚配置

ADC124S21_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

ADC124S21_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出

GPIO_Init(GPIOA, &ADC124S21_GPIO_InitStructure);

ADC124S21_GPIO_InitStructure.GPIO_Pin = ADC124S21_DATA_PA6_DOUT; //DOUT引脚配置

ADC124S21_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

ADC124S21_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输出

GPIO_Init(GPIOA, &ADC124S21_GPIO_InitStructure);

return 0;

}

4 ad转换函数

/********************************************************************************************************

**函数名称:U11_AD_124S021()

**函数功能:用于AD转换

**输入变量:channel 通道数;channel=1,则选择通道1;channel=2,则选择通道2.

**输出变量:ad_val,AD转换值

********************************************************************************************************/

u32 U11_AD_124S021(unsigned char channel)

{ unsignedchar ch = 0;

unsigned char i;

unsigned int ad_val=0; //AD转换值

AD_SCLK_H; //时钟输入端置高

AD_CS_L; //片选控制端置低,AD转换使能

__nop();

__nop();

__nop();

if( channel == AD_FF )

ch = 0x00;   //选择通道1  聚焦值
复制代码

else if( channel == AD_ZF )

ch = 0x08;   //选择通道2  变倍值
复制代码

else if( channel == AD_MF )

ch = 0x10;   //选择通道3  天线电机值
复制代码

else if( channel == AD_GM )

ch = 0x18;   //选择通道4  光敏值
复制代码

ch_temp = ch;

for(i=0; i<8; i++)

{
   AD_SCLK_L;                                                             //时钟输入置低
   __nop();
   __nop();
   __nop();
   if((ch_temp&0x80)==0x00)
       AD_DIN_L;//通过串行方式输入通道选择
   else
       AD_DIN_H;
   ch_temp<<=1;
   AD_SCLK_H;                                                            //时钟输入置高,在此上升沿DIN实现1位输入
   __nop();
   __nop();
   __nop();
}
复制代码

for(i=0; i<8; i++)

{
   AD_SCLK_L;
   AD_SCLK_H;
}
复制代码

AD_CS_H;

ad_val=0;

AD_SCLK_H;

AD_CS_L;

__nop();

__nop();

__nop();

for(i=0; i<8; i++)

{
   ad_val=(ad_val<<1);
   AD_SCLK_L;
   __nop();
   __nop();
    __nop();
   if(AD_DOUT)
   {
       ad_val++;
   }
   else
   {
       __nop();
       __nop();
       __nop();
   }
   if((ch&0x80)==0x00)
       AD_DIN_L;
   else
       AD_DIN_H;
   ch<<=1;
   AD_SCLK_H;
   __nop();
   __nop();
   __nop();
}
复制代码

for(i=0; i<8; i++)

{
   ad_val=(ad_val<<1);
   AD_SCLK_L;
   __nop();
   __nop();
   __nop();
   if(AD_DOUT)
   {
        ad_val++;
   }
   else
   {
       __nop();
       __nop();
       __nop();
   }
   AD_SCLK_H;
}
复制代码

AD_CS_H;

ad_val=(ad_val&0x0fff);

//adtest=ad_val;
               returnad_val;
复制代码

}



上一篇:一线架构师实践指南:证券行业应如何构建一体化监控体系?


下一篇:thinkphp-条件判断-if标签2