AD7606使用
使用STM32F407开发板
ad7606.h文件
#ifndef _AD7606
#define _AD7606
#include "sys.h"
/***************
*@xg
*关于ADC接线
* AD7606接线 STM32F407VET6
* OS0-2 直接接地
* RANGE 测量量程选择 B12
* CA(CB) 开启转换 -- B11
* RD 时钟输入 SCK时钟线 B13
* RST 复位 B10
* BUSY (未接,可接采用中断接收数据)
* D7 MISO线 B7
* CS B1
*******************/
#define AD_PORT GPIOB
#define AD_PORT_RANGE GPIO_Pin_12
#define AD_PORT_CA_CB GPIO_Pin_11
#define AD_PORT_RD_SCK GPIO_Pin_13
#define AD_PORT_RST GPIO_Pin_10
#define AD_PORT_MISO GPIO_Pin_7
#define AD_PORT_CS GPIO_Pin_1
#define AD_CLK_CMD RCC_AHB1PeriphClockCmd
#define AD_CLK_Periph RCC_AHB1Periph_GPIOB
#define AD_CONVST_LOW() GPIO_ResetBits(AD_PORT,AD_PORT_CA_CB) //
#define AD_CONVST_HIGH() GPIO_SetBits(AD_PORT,AD_PORT_CA_CB)
#define AD_RESET_LOW() GPIO_ResetBits(AD_PORT,AD_PORT_RST) //
#define AD_RESET_HIGH() GPIO_SetBits(AD_PORT,AD_PORT_RST)
#define AD_CS_LOW() GPIO_ResetBits(AD_PORT,AD_PORT_CS) //
#define AD_CS_HIGH() GPIO_SetBits(AD_PORT,AD_PORT_CS)
#define AD_SCK_LOW() GPIO_ResetBits(AD_PORT,AD_PORT_RD_SCK) //
#define AD_SCK_HIGH() GPIO_SetBits(AD_PORT,AD_PORT_RD_SCK)
#define AD_RANGE_10() GPIO_SetBits(AD_PORT, AD_PORT_RANGE); //设置采样范围 为 正负10V
#define AD_RANGE_5() GPIO_ResetBits(AD_PORT, AD_PORT_RANGE); //设置采样范围 为 正负5V
#define AD_MISO() PBin(7)
void ad7606_StartConv(void);
void ad7606_Configuration(u16 ucOS);
u16 AD_Read_data();
#endif
ad7606.c文件
#include "ad7606.h"
#include "delay.h"
void bsp_InitAD7606(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
AD_CLK_CMD(AD_CLK_Periph, ENABLE); //使能AD_PORT时钟
//AD_MISO初始化设置
GPIO_InitStructure.GPIO_Pin = AD_PORT_MISO;//接口的定义
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//输入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//下拉
GPIO_Init(AD_PORT, &GPIO_InitStructure);//初始化GPIO
//AD_PORT_Pin初始化设置
GPIO_InitStructure.GPIO_Pin = AD_PORT_RANGE | AD_PORT_CA_CB | AD_PORT_RD_SCK | AD_PORT_RST | AD_PORT_CS; //输出接口的定义
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(AD_PORT, &GPIO_InitStructure);//初始化GPIOD
AD_RANGE_5(); //设置采样范围 为 正负5V
}
u16 AD_Read_data()
{
// cs低电平才可以读取数据
AD_CS_LOW();
u16 data = 0;
u16 data_size = 16;
for(u16 i = 0; i < data_size ; i++)
{
AD_SCK_LOW();
data = data << 1;
AD_SCK_HIGH(); //上升沿有效读取数据
delay_us(1); //稳定之后再读取数据
if(AD_MISO()) data++;
AD_SCK_HIGH();
}
AD_CS_HIGH();//读取完成
return data;
}
void ad7606_Reset(void)
{
/* AD7606是高电平复位,要求最小脉宽50ns */
AD_RESET_LOW();
AD_RESET_HIGH();
AD_RESET_HIGH();
AD_RESET_HIGH();
AD_RESET_HIGH();
AD_RESET_LOW();
}
/*
*********************************************************************************************************
* 函 数 名: ad7606_StartConv
* 功能说明: 启动AD7606的ADC转换
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
void ad7606_StartConv(void)
{
/* 上升沿开始转换,低电平持续时间至少25ns */
AD_CONVST_LOW();
AD_CONVST_LOW();
AD_CONVST_LOW(); /* 连续执行2次,低电平约50ns */
AD_CONVST_LOW();
AD_CONVST_LOW();
AD_CONVST_HIGH();
}
void ad7606_Configuration(u16 ucOS)
{
bsp_InitAD7606();
ad7606_Reset();
}