第一,初始化及配置
#define AD7191_PORT GPIOE
#define AD7191_PDOWN GPIO_Pin_4
#define AD7191_SCLK GPIO_Pin_3
#define AD7191_DOUT GPIO_Pin_2
#define AD7191_DOUT_PORT_SOURCE GPIO_PortSourceGPIOE
#define AD7191_DOUT_PIN_SOURCE GPIO_PinSource2
#define AD7191_DOUT_LINE EXTI_Line2
#define AD7191_DOUT_IRQn EXTI2_IRQn
#define AD7191_PDOWN_H AD7191_PORT->BSRR = AD7191_PDOWN
#define AD7191_PDOWN_L AD7191_PORT->BRR = AD7191_PDOWN
#define AD7191_SCLK_H AD7191_PORT->BSRR = AD7191_SCLK
#define AD7191_SCLK_L AD7191_PORT->BRR = AD7191_SCLK
#define AD7191_DOUT_READ (AD7191_PORT->IDR & AD7191_DOUT)
GPIO_InitStructure.GPIO_Pin = AD7191_PDOWN | AD7191_SCLK;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(AD7191_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = AD7191_DOUT;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(AD7191_PORT, &GPIO_InitStructure);
// 连接AD7191_DOUT至对应中断通道
GPIO_EXTILineConfig(AD7191_DOUT_PORT_SOURCE, AD7191_DOUT_PIN_SOURCE);
// 配置中断触发
EXTI_InitStructure.EXTI_Line = AD7191_DOUT_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannel = AD7191_DOUT_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
// 复位AD7191
AD7191_SCLK_H;
AD7191_PDOWN_H;
Delay_Ms(120); // dealy more than 80ms
AD7191_PDOWN_L;
Delay_Ms(120); // dealy more than 80ms
第二,读取AD7191
u32 AD7191_Read(void)
{
u8 i;
u32 temp = 0;
for(i=0;i<24;i++)
{
AD7191_SCLK_L;
Delay_Us(1);
temp = temp << 1;
if(AD7191_DOUT_READ)
temp |= 0x01;
AD7191_SCLK_H;
Delay_Us(1);
}
return temp;
}
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(AD7191_DOUT_LINE) != RESET)
{
AD.Code[AD.Cnt++] = AD7191_Read();
if(AD.Cnt >= Work_Para.Filter)
{
AD.Cnt = 0;
AD.Flg = 1;
}
EXTI_ClearITPendingBit(AD7191_DOUT_LINE);
}
}