#include "ad7616.h"
//#define AD7616_BUSY GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10)
#define SensorAD7616_Open_Fault 0x20 //故障
#define SensorAD7616_Short_Fault 0x20 //故障
#define Sensor_Normal_Status 0x00 //正常
unsigned char AD7616_Minus_flag[9] ={0};
unsigned char ZW_ad7616_Status[9];
unsigned int ad7616_Open_Counter[9]; //7616故障数记录统计
unsigned int ad7616_Short_Counter[9]; //7616故障数记录统计
extern void delay_us(unsigned int x); //延时时间函数 后期需用示波器进行验证
extern void delay_ms(unsigned int x); //延时时间函数 后期需用示波器进行验证 临时函数
extern unsigned char SystemFaultCounter;
extern struct FaultSensor
{
unsigned char SensorNum;
unsigned char Fault;
}Fault[20];
void AD7616_START(void)
{
AD7616_Spi_Init(); //7616口初始化
AD7616_Reset(); //7616复位
AD7616_Register_Init(); //ad7616 寄存器 初始化
}
/*******************************************
函数名称: void AD7616_Spi_Init(void)
函数作用: spi初始化
函数变量: 无
创建日期:
**********************************************/
void AD7616_Spi_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* SPI1: SCK,MOSI MISO推挽输出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI1: CS推挽输出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_SPI1); //GPIOB15 GPIOB14 GPIOB10复用为SPI2通讯
GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1);
/* SPI 初始化定义 */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //SPI设置为双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置为主 SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //SPI发送接收 8 位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //时钟空闲为高
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //数据捕获于第二个时钟沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //软件控制 NSS 信号
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //波特率预分频值为2 50M/16 = 3M
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据传输从 MSB 位开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //定义了用于 CRC值计算的多项式
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
/* AD7616 reset 推挽输出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* AD7616 CONVST 推挽输出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* AD7616 BUSY */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
AD7616_Reset_On;
AD7616_Convst_Off;
AD7616_CS_On;
}
/*******************************************
函数名称: unsigned char SPI_WriteReadByte(char data)
函数作用: spi发送数据
函数变量: 无
创建日期:
**********************************************/
unsigned char SPI_WriteReadByte(char data)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
SPI_SendData(SPI1,data);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
return SPI_ReceiveData(SPI1);
}
/*******************************************
函数名称: void AD7616_Reset(void)
函数作用: 7616复位
函数变量: 无
创建日期:
**********************************************/
void AD7616_Reset(void)
{
AD7616_Reset_On;
delay_ms(100);
AD7616_Reset_Off;
delay_ms(55);//延时
AD7616_Reset_On;
delay_ms(55);
// AD7616_Reset_Off;
// delay_ms(50);//延时
// AD7616_Reset_On;
// delay_ms(50);
// AD7616_CS_Off;
// delay_ms(1);
// AD7616_CS_On;
// delay_ms(20);//延时
// AD7616_Start_Convst(); //启动convst
}
/*******************************************
函数名称: void ad7616_Start_Convst(void)
函数作用: 7616启动复位转换
函数变量: 无
创建日期:
**********************************************/
void AD7616_Start_Convst(void) //启动转换
{
AD7616_Convst_On;
delay_us(10);//延时
AD7616_Convst_Off;
}
/************************************************
函数名称: uint8_t AD7616_Busy_Check(void)
函数作用: 7616 忙信号查找 BUSY转换信号 10us
函数变量: 无
创建日期:2019/05/09 yhx
*************************************************/
uint8_t AD7616_Busy_Check(void)
{
// uint8_t stat =0;
uint8_t count =0;
// stat =AD7616_Busy ;
while(!AD7616_Busy&&(count<11)) //返回一个10us高电平脉冲应答 等待低信号后转换结束
{
count++; //高电平++
delay_us(1); //延时1us
};
if(count>=6) return 1;//
else
{
count=0;
return 0;
}
}
/**********************************************************************
函数名称: int32_t AD7616_Spi_Write(uint8_t Reg_addr,uint16_t Reg_Data,uint8_t Type)
函数作用: SPI发送数据
函数变量: 无
创建日期:
*************************************************************************/
int32_t AD7616_Spi_Write(uint8_t Reg_addr,uint16_t Reg_Data,uint8_t Type)
{
uint8_t buf[2];
uint8_t SPI_RecBufer[2];
uint8_t i,j;
AD7616_CS_Off;
if(Type==1) //通过类型对寄存器配置进行区分 0x02~0x07设置寄存器 0x20~0x3f 序列器堆栈寄存器
{
buf[0] = 0x80 | ((Reg_addr & 0x3F) << 1) | ((Reg_Data & 0x100) >> 8); //寻址寄存器+写入数据变量 16bit
buf[1] = (Reg_Data & 0xFF); //+写入数据变量
}
if(Type==0) //通过类型对寄存器配置进行区分 0x02~0x07设置寄存器 0x20~0x3f 序列器堆栈寄存器
{
buf[0] = Reg_addr | ((Reg_Data & 0x100) >> 8); //寻址寄存器+写入数据变量 16bit
buf[1] = (Reg_Data & 0xFF); //+写入数据变量
}
for(i = 0; i < 2; i++)
{
SPI_RecBufer[i] = SPI_WriteReadByte(buf[i]);
for(j = 0; j < 50; j++);
}
AD7616_CS_On;
return SPI_RecBufer[0];
}
/************************************************
函数名称: int32_t AD7616_Spi_Read(uint8_t Reg_Addr)
函数作用: SPI读数据函数
函数变量: 无
创建日期:
*************************************************/
int32_t AD7616_Spi_Read(uint8_t Reg_Addr)
{
uint8_t spi_wrtbuf[2];
uint16_t reg_data;
uint8_t i,j;
uint8_t spi_recbuf[2];
spi_wrtbuf[0] = 0x00 | ((Reg_Addr & 0x3F) << 1);//寻址