1.AD7606模块介绍
AD7606是16位8通道同步采样ADC
2.AD7606模块使用(串行传输)
串行传输时,数据引脚为 DB7
1.CUBEMX配置
2.驱动
AD7606.h
#ifndef _AD7606_H
#define _AD7606_H
#include "main.h"
#define rst_Set HAL_GPIO_WritePin(ad7606_REST_GPIO_Port, ad7606_REST_Pin, GPIO_PIN_SET)
#define rst_Reset HAL_GPIO_WritePin(ad7606_REST_GPIO_Port, ad7606_REST_Pin, GPIO_PIN_RESET)
#define convstB_Set HAL_GPIO_WritePin(ad7606_COB_GPIO_Port, ad7606_COB_Pin, GPIO_PIN_SET)
#define convstB_Reset HAL_GPIO_WritePin(ad7606_COB_GPIO_Port, ad7606_COB_Pin, GPIO_PIN_RESET)
#define convstA_Set HAL_GPIO_WritePin(ad7606_COA_GPIO_Port, ad7606_COA_Pin, GPIO_PIN_SET)
#define convstA_Reset HAL_GPIO_WritePin(ad7606_COA_GPIO_Port, ad7606_COA_Pin, GPIO_PIN_RESET)
#define STby_Set HAL_GPIO_WritePin(ad7606_STBY_GPIO_Port, ad7606_STBY_Pin, GPIO_PIN_SET)
#define STby_Reset HAL_GPIO_WritePin(ad7606_STBY_GPIO_Port, ad7606_STBY_Pin, GPIO_PIN_RESET)
#define OS12_Set HAL_GPIO_WritePin(ad7606_OSI2_GPIO_Port, ad7606_OSI2_Pin, GPIO_PIN_SET)
#define OS12_Reset HAL_GPIO_WritePin(ad7606_OSI2_GPIO_Port, ad7606_OSI2_Pin, GPIO_PIN_RESET)
#define OS11_Set HAL_GPIO_WritePin(ad7606_OSI1_GPIO_Port, ad7606_OSI1_Pin, GPIO_PIN_SET)
#define OS11_Reset HAL_GPIO_WritePin(ad7606_OSI1_GPIO_Port, ad7606_OSI1_Pin, GPIO_PIN_RESET)
#define OS10_Set HAL_GPIO_WritePin(ad7606_OSI0_GPIO_Port, ad7606_OSI0_Pin, GPIO_PIN_SET)
#define OS10_Reset HAL_GPIO_WritePin(ad7606_OSI0_GPIO_Port, ad7606_OSI0_Pin, GPIO_PIN_RESET)
#define frstdata_Set HAL_GPIO_WritePin(ad7606_FRD_GPIO_Port, ad7606_FRD_Pin, GPIO_PIN_SET)
#define frstdata_Reset HAL_GPIO_WritePin(ad7606_FRD_GPIO_Port, ad7606_FRD_Pin, GPIO_PIN_RESET)
#define busy_Set HAL_GPIO_WritePin(ad7606_BUSY_GPIO_Port, ad7606_BUSY_Pin, GPIO_PIN_SET)
#define busy_Reset HAL_GPIO_WritePin(ad7606_BUSY_GPIO_Port, ad7606_BUSY_Pin, GPIO_PIN_RESET)
#define cs_Set HAL_GPIO_WritePin(ad7606_CSN_GPIO_Port, ad7606_CSN_Pin, GPIO_PIN_SET)
#define cs_Reset HAL_GPIO_WritePin(ad7606_CSN_GPIO_Port, ad7606_CSN_Pin, GPIO_PIN_RESET)
#define clk_Set HAL_GPIO_WritePin(ad7606_RD_SC_GPIO_Port, ad7606_RD_SC_Pin, GPIO_PIN_SET)
#define clk_Reset HAL_GPIO_WritePin(ad7606_RD_SC_GPIO_Port, ad7606_RD_SC_Pin, GPIO_PIN_RESET)
void delay_100ns(void);
extern void GPIO_AD7606_Configuration(void);
extern void AD7606_Init(void);
extern void AD7606_startconvst(void);
extern void AD7606_reset(void) ;
extern void AD7606_read_data(int16_t * DB_data);
#endif
AD7606.c
#include "stm32f4xx.h"
#include "AD7606.h"
#include "delay.h"
void delay_100ns(void)
{
}
//-----------------------------------------------------------------
// 初始化程序区
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// void GPIO_AD7606_Configuration(void)
//-----------------------------------------------------------------
//
// 函数功能: AD7606引脚配置函数
// 入口参数: 无
// 返回参数: 无
// 全局变量: 无
// 调用模块: __HAL_RCC_GPIOA_CLK_ENABLE(); HAL_GPIO_DeInit();
// 注意事项: 用GPIO驱动方式和FSMC驱动方式下的引脚配置不一样
//
//-----------------------------------------------------------------
void GPIO_AD7606_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// AD7606
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//控制线配置
// CS_N RD/SCLK
GPIO_InitStructure.Pin = ad7606_CSN_Pin;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(ad7606_CSN_GPIO_Port, &GPIO_InitStructure);
GPIO_InitStructure.Pin = ad7606_RD_SC_Pin;
HAL_GPIO_Init(ad7606_RD_SC_GPIO_Port, &GPIO_InitStructure);
// FRSTDATA BUSY
GPIO_InitStructure.Pin = ad7606_FRD_Pin;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Mode = GPIO_MODE_EVT_RISING_FALLING;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(ad7606_FRD_GPIO_Port, &GPIO_InitStructure);
GPIO_InitStructure.Pin = ad7606_BUSY_Pin;
HAL_GPIO_Init(ad7606_BUSY_GPIO_Port, &GPIO_InitStructure);
// rst convstB convstA STby OS12 OS11 OS10
GPIO_InitStructure.Pin = ad7606_REST_Pin;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Mode = GPIO_MODE_EVT_RISING_FALLING;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(ad7606_REST_GPIO_Port, &GPIO_InitStructure);
GPIO_InitStructure.Pin = ad7606_REST_Pin;
HAL_GPIO_Init(ad7606_REST_GPIO_Port, &GPIO_InitStructure);
GPIO_InitStructure.Pin = ad7606_COB_Pin;
HAL_GPIO_Init(ad7606_COB_GPIO_Port, &GPIO_InitStructure);
GPIO_InitStructure.Pin = ad7606_COA_Pin;
HAL_GPIO_Init(ad7606_COA_GPIO_Port, &GPIO_InitStructure);
GPIO_InitStructure.Pin = ad7606_STBY_Pin;
HAL_GPIO_Init(ad7606_STBY_GPIO_Port, &GPIO_InitStructure);
GPIO_InitStructure.Pin = ad7606_OSI2_Pin;
HAL_GPIO_Init(ad7606_OSI2_GPIO_Port, &GPIO_InitStructure);
GPIO_InitStructure.Pin = ad7606_OSI1_Pin;
HAL_GPIO_Init(ad7606_OSI1_GPIO_Port, &GPIO_InitStructure);
GPIO_InitStructure.Pin = ad7606_OSI0_Pin;
HAL_GPIO_Init(ad7606_OSI0_GPIO_Port, &GPIO_InitStructure);
//数据线(1)
// DoutA
GPIO_InitStructure.Pin = ad7606_DB_Pin ;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(ad7606_DB_GPIO_Port, &GPIO_InitStructure);
}
//-----------------------------------------------------------------
// void AD7606_Init(void)
//-----------------------------------------------------------------
//
// 函数功能: AD7606初始化函数
// 入口参数: 无
// 返回参数: 无
// 全局变量: 无
// 调用模块:
// 注意事项: 无
//-----------------------------------------------------------------
void AD7606_Init(void)
{
convstA_Set;
convstB_Set;
STby_Set;
clk_Set;
cs_Set;
OS10_Reset;
OS11_Reset;
OS12_Reset;
AD7606_reset();
AD7606_startconvst();
}
/* * 名称:AD7606_startconvst() * 功能:启动转换 */
void AD7606_startconvst(void)
{
convstA_Reset;
convstB_Reset;
delay_us(11);
convstA_Set;
convstB_Set;
}
/* * 名称:AD7606_reset() * 功能:复位模块 */
void AD7606_reset(void)
{
rst_Reset;
delay_100ns();
rst_Set;
delay_us(1);
rst_Reset;
}
/*
* 名称:AD7606_read_data()
* 功能:读取数据
* 返回值:返回一个结构体指针,该指针为指向结构体数组的首地址
*/
void AD7606_read_data(int16_t * DB_data)
{
uint8_t i,j;
for(i=0;i<8;i++)
{
uint16_t DB_data1 = 0;
cs_Reset;
delay_us (3);
for(j=0;j<16;j++)
{
clk_Reset;
delay_us (4);
DB_data1 = ((uint16_t)(HAL_GPIO_ReadPin(ad7606_DB_GPIO_Port, ad7606_DB_Pin))<< (15-j)) + DB_data1;
clk_Set;
delay_us (4);
}
cs_Set;
DB_data[i] = (uint16_t)DB_data1;
}
}
main.c
#include "AD7606.h"
int16_t data[8] = {0};
AD7606_Init();
AD7606_startconvst(); // 启动转换
if( 0 == HAL_GPIO_ReadPin(ad7606_BUSY_GPIO_Port, ad7606_BUSY_Pin)) // 转换完成
{
AD7606_read_data(data);
sprintf((char*)temp,"%8.1f\n",(float)(data[0]*5000.0/32768));
HAL_UART_Transmit(&huart1, (uint8_t *)temp, strlen(temp), 1000);
delay_ms(50);
AD7606_startconvst();
}