/*
*********************************************************************************************************
*
* 模块名称 : AD7606驱动模块
* 文件名称 : bsp_spi_ad7606.c
* 版 本 : V1.3
* 说 明 : 驱动AD7606 ADC转换器 SPI接口
*
*
*********************************************************************************************************
*/
#include <stdio.h>
//#include "bsp.h"
#include "bsp_spi_ad7606.h"
//#include "bsp_timer.h"
FIFO_T g_tAD; /* 定义一个交换缓冲区,用于存储AD采集数据,并用于写入SD卡 */
/*
*********************************************************************************************************
* 函 数 名: bsp_InitAD7606
* 功能说明: 初始化AD7606 SPI口线
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
void bsp_InitAD7606(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* AD_SPI_CS_GPIO, AD_SPI_MOSI_GPIO, AD_SPI_MISO_GPIO, AD_SPI_DETECT_GPIO
and AD_SPI_SCK_GPIO Periph clock enable */
ENABLE_AD_SCK_CLK;
ENABLE_AD_MISO_CLK;
ENABLE_AD_CS_CLK;
ENABLE_AD_RESET_CLK;
ENABLE_AD_CONVST_CLK;
ENABLE_AD_RANGE_CLK;
ENABLE_AD_OS0_CLK;
ENABLE_AD_OS1_CLK;
ENABLE_AD_OS2_CLK;
GPIO_InitStructure.Pin = AD_SPI_SCK_PIN;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(AD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = AD_CS_PIN;
HAL_GPIO_Init(AD_CS_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = AD_SPI_MISO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(AD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
/* 配置其它的GPIO */
/* 使能GPIO时钟 */
/* 配置RESET GPIO */
GPIO_InitStructure.Pin = AD_RESET_PIN;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(AD_RESET_GPIO_PORT, &GPIO_InitStructure);
/* 配置CONVST GPIO */
GPIO_InitStructure.Pin = AD_CONVST_PIN;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(AD_CONVST_GPIO_PORT, &GPIO_InitStructure);
/* 配置RANGE GPIO */
GPIO_InitStructure.Pin = AD_RANGE_PIN;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(AD_RANGE_GPIO_PORT, &GPIO_InitStructure);
/* 配置OS0-2 GPIO */
GPIO_InitStructure.Pin = AD_OS0_PIN;
HAL_GPIO_Init(AD_OS0_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = AD_OS1_PIN;
HAL_GPIO_Init(AD_OS1_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = AD_OS2_PIN;
HAL_GPIO_Init(AD_OS2_GPIO_PORT, &GPIO_InitStructure);
/* 设置过采样模式 */
ad7606_SetOS(0);
/* 设置GPIO的初始状态 */
ad7606_Reset(); /* 硬件复位复AD7606 */
AD_CONVST_HIGH(); /* CONVST脚设置为高电平 */
}
/*
*********************************************************************************************************
* 函 数 名: ad7606_Reset
* 功能说明: 硬件复位AD7606
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
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_SetOS
* 功能说明: 设置过采样模式(数字滤波,硬件求平均值)
* 形 参:_ucMode : 0-6 0表示无过采样,1表示2倍,2表示4倍,3表示8倍,4表示16倍
* 5表示32倍,6表示64倍
* 返 回 值: 无
*********************************************************************************************************
*/
void ad7606_SetOS(uint8_t _ucMode)
{
if (_ucMode == 1)
{
AD_OS2_0();
AD_OS1_0();
AD_OS0_1();
}
else if (_ucMode == 2)
{
AD_OS2_0();
AD_OS1_1();
AD_OS0_0();
}
else if (_ucMode == 3)
{
AD_OS2_0();
AD_OS1_1();
AD_OS0_1();
}
else if (_ucMode == 4)
{
AD_OS2_1();
AD_OS1_0();
AD_OS0_0();
}
else if (_ucMode == 5)
{
AD_OS2_1();
AD_OS1_0();
AD_OS0_1();
}
else if (_ucMode == 6)
{
AD_OS2_1();
AD_OS1_1();
AD_OS0_0();
}
else /* 按0处理 */
{
AD_OS2_0();
AD_OS1_0();
AD_OS0_0();
}
}
/*
*********************************************************************************************************
* 函 数 名: 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_LOW();
AD_CONVST_LOW();
AD_CONVST_LOW();
AD_CONVST_LOW();
AD_CONVST_HIGH();
}
//SPI写数据
//向触摸屏IC写入1byte数据
//num:要写入的数据
void SPI_SendData(uint16_t data)
{
uint8_t count=0;
AD_SCK_LOW(); //下降沿有效
for(count=0;count<16;count++)
{
if(data&0x8000)
AD_MISO_LOW();
else
AD_MISO_HIGH();
data<<=1;
AD_SCK_LOW();
AD_SCK_HIGH(); //上升沿有效
}
}
//SPI读数据
//从触摸屏IC读取adc值
//CMD:指令
//返回值:读到的数据
//u16 SPI_ReceiveData(void)
//{
// u8 count=0;
// u16 Num=0;
// AD_SCK_LOW(); //先拉低时钟
// for(count=0;count<16;count++)//读出16位数据,只有高12位有效
// {
// Num<<=1;
// AD_SCK_LOW(); //下降沿有效
// AD_SCK_HIGH();
//
// if(AD_MISO_IN)Num++;
// }
// return(Num);
//}
uint16_t SPI_ReceiveData(void)
{
uint8_t count=0;
uint16_t Num=0;
AD_SCK_HIGH();
for(count=0;count<16;count++)//读出16位数据
{
Num<<=1;
AD_SCK_LOW(); //下降沿有效
//////////////////// delay_us(2);
if(AD_MISO_IN)Num++;
AD_SCK_HIGH();
//////////////////// delay_us(2);
}
return(Num);
}
/*
*********************************************************************************************************
* 函 数 名: ad7606_ReadBytes
* 功能说明: 读取AD7606的采样结果
* 形 参:
* 返 回 值: 无
*********************************************************************************************************
*/
uint16_t ad7606_ReadBytes(void)
{
uint16_t usData = 0;
//// /* Wait until the transmit buffer is empty */
//// while (SPI_I2S_GetFlagStatus(AD_SPI, SPI_I2S_FLAG_TXE) == RESET)
//// {
//// }
// /* Send the byte */
// SPI_I2S_SendData(AD_SPI, 0xFFFF);
//// SPI_SendData(0xFFFF);
//// /* Wait until a data is received */
//// while (SPI_I2S_GetFlagStatus(AD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
//// {
//// }
/* Get the received data */
//// usData = SPI_I2S_ReceiveData(AD_SPI);
usData = SPI_ReceiveData();
/* Return the shifted data */
return usData;
}
/*
*********************************************************************************************************
* 函 数 名: ad7606_IRQSrc
* 功能说明: 定时调用本函数,用于读取AD转换器数据
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
int16_t adc_value[8];
void ad7606_IRQSrc(void)
{
uint8_t i;
uint16_t usReadValue;
/* 读取数据
示波器监测,CS低电平持续时间 35us
*/
AD_CS_LOW();
// for (i = 0; i < CH_NUM; i++)
// {
// usReadValue = ad7606_ReadBytes();
// if (g_tAD.usWrite < FIFO_SIZE)
// {
// g_tAD.usBuf[g_tAD.usWrite] = usReadValue;
// ++g_tAD.usWrite;
// }
// }
for (i = 0; i < CH_NUM; i++)
{
usReadValue = ad7606_ReadBytes();
adc_value[i] = usReadValue;
}
AD_CS_HIGH();
// g_tAD.usWrite = 0;
ad7606_StartCon
评论0