www.pudn.com > STM32-AD7705.rar > ad7705.c, change:2014-03-30,size:5557b


/************************************************************************** 
*文件名: 
×作者: 
×日期:2014.3.29 
×版本:V0.0.1 
×描述:STM32用SPI方式与AD7705进行通讯的相关函数 
***************************************************************************/ 
#include "ad7705.h" 
 
 
static __inline unsigned char SPIx_ReadWriteByte(unsigned char data) { 
	while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); 
	SPI_I2S_SendData(SPI2, data); 
	while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); 
	return SPI_I2S_ReceiveData(SPI2); 
} 
static void SPIx_Init(void) 
{ 
	SPI_InitTypeDef SPI_InitStructure; 
	GPIO_InitTypeDef GPIO_InitStructure; 
	//AFIO时钟在端口复用或外部中断时开启!!!! 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE );	//spi2端口时钟使能 
	RCC_APB1PeriphClockCmd(	RCC_APB1Periph_SPI2, ENABLE ); //spi2使能 
	 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;// AdDrdy  忙信号 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ;//PB11配置成上拉输入 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOB, &GPIO_InitStructure);	 
	 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
	GPIO_Init(GPIOB, &GPIO_InitStructure);	            //CE 
	 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
	GPIO_Init(GPIOB, &GPIO_InitStructure);				//SCK,MOSI 
	 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
	GPIO_Init(GPIOB, &GPIO_InitStructure);				 //MISO 
	 
	SPI_Cmd(SPI2, DISABLE); //失能能SPI外设 
	 
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工 
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;		//设置SPI工作模式:设置为主SPI 
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;		//设置SPI的数据大小:SPI发送接收8位帧结构 
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;		//选择了串行时钟的稳态:时钟悬空高 
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;	//数据捕获于第二个时钟 
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;		//NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制 
 	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;		//定义波特率预分频的值:波特率预分频值为256 
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;	//指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始 
	SPI_InitStructure.SPI_CRCPolynomial = 7;	//CRC值计算的多项式 
	SPI_Init(SPI2, &SPI_InitStructure);  //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器 
   
	SPI_Cmd(SPI2, ENABLE); //使能SPI外设 
	 
	CS_ADC_LOW();	 
	SPIx_ReadWriteByte(0xff);//启动传输		  
	CS_ADC_HIGH(); 
}    
 
 
 
//写数据 
void AD7705_WriteByte(u8 Dst_Addr)    
{    
  CS_ADC_LOW();//使能器件   
	Delay_us(20); 
	SPIx_ReadWriteByte(Dst_Addr); 
 	Delay_us(100); 
  CS_ADC_HIGH();//使能器件   
} 
 
 
 
/********AD7705初始化函数***********/ 
 
void Init_AD7705(unsigned char chnanel) { 
	u8 i; 
	SPIx_Init(); 
     
  for(i=0;i<100;i++)/* 多于连续32个 DIN=1 使串口复位 */  
  { 
		AD7705_WriteByte(0xff);//持续DIN高电平写操作,恢复AD7705接口 
	}   
	Delay_ms(1); 
	switch(chnanel) 
	{ 
		case 1:  
			AD7705_WriteByte(0x20); /* 写通讯寄存器选中ch1*/  
			AD7705_WriteByte(0x01); /* 2.4576MHz时钟,20Hz数据更新速率 */ 
			AD7705_WriteByte(0x10); /*选择设置寄存器,使用chnanel 1*/ 
			AD7705_WriteByte(0x44); //写设置寄存器 ,设置成单极性、无缓冲、增益为1、滤波器工作、自校准 
		break; 
		case 2:  
			AD7705_WriteByte(0x21); /* 写通讯寄存器选中ch2 */  
			AD7705_WriteByte(0x06); /* 2.4576MHz时钟,50Hz数据更新速率 */ 
			AD7705_WriteByte(0x11); /*选择设置寄存器,使用chnane 2*/ 
			AD7705_WriteByte(0x44); //写设置寄存器,设置成单极性、无缓冲、增益为1、滤波器工作、自校准 
		break; 
		default:	 
			break; 
	}  
} 
 
/* 读AD7705转换数据 输入通道channel */  
static unsigned int GetData7705Channel(unsigned char channel) { 
	u16 DataL = 0; 
  u16 DataH = 0; 
	unsigned int re; 
	 
	if((channel!=1)&&(channel!=2)) { 
		re = 0xffff; 
		return re; 
	} 
	Init_AD7705(channel);			//初始化通道1 
	Delay_ms(1); 
	AD7705_WriteByte(0x37+channel);   //选中channel数据寄存器读   
  while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11)==1);  //待数据准备好AdDrdy=0   
	CS_ADC_LOW(); //使能器件   
	Delay_us(20); 
	DataH = SPIx_ReadWriteByte(0xff); 
	DataL = SPIx_ReadWriteByte(0xff);  
	Delay_us(100); 
  CS_ADC_HIGH();   //取消片选 	 
	DataH = DataH << 8;  
  re  =  DataH | DataL;  
 
    return re;  
} 
 
static float ADC_7705(unsigned char channel) { 
	return GetData7705Channel(channel)*(2500.0/65535);  
} 
 
 
 
 u8 AD7705_ReadByte(u8 Dst_Addr)    
{    
 u8 re=0; 
 CS_ADC_LOW();  
 Delay_us(20); 
 re = SPIx_ReadWriteByte(Dst_Addr); 
  Delay_us(100); 
 re = SPIx_ReadWriteByte(0xff); 
    CS_ADC_HIGH(); 
  
 return re;   
} 
 
 
 
 
float ADC_7705Average(unsigned char channel){ 
	unsigned char i =0; 
	float ResultValue = 0; 
	for(i=0;i<10;i++)  
		ResultValue = ResultValue + ADC_7705(channel)/10;  
	 
	return ResultValue; 
}