代码如下:
#include "AD7705.H"
#include "systick.h"
unsigned int AD_value1[10] , AD_value;
void AD7705_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure AD7705 pins: PB13->DOUT and PB14->DRDY */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure I2C2 pins: PB12->DIN and PB15->SCLK */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
DIN_H; SCLK_H;
}
void Write_ad7705 ( uint8_t SendByte )
{
uint8_t i;
for ( i=0x00; i<8; i++ )
{
if ( SendByte & 0x80 )
DIN_H;
else
DIN_L;
//delay_us ( 3 );
SCLK_L;
delay_us ( 3000 );
SCLK_H;
delay_us ( 3000 );
SendByte <<= 1;
}
DIN_H;
}
uint8_t Read_ad7705reg ( )
{
uint8_t i,data_read;
//判断DRDY是否为低电平
while ( DRDY_read );
for ( i=0x00; i<8; i++ )
{
SCLK_L;
delay_us ( 3000 );
data_read <<= 1;
//逻辑或
if (DOUT_read )
data_read |= 1;//( uint8_t )DOUT_read;
delay_us ( 3000 );
SCLK_H;
delay_us ( 3000 );
}
return ( data_read );
}
void Inti_ad7705 ()
{
Write_ad7705 ( 0x20 );//写通信寄存器,下一个是写入时钟寄存器 ,通道0
Write_ad7705 ( 0x09 );//分频,更新率25Hz
Write_ad7705 ( 0x10 );//写通信寄存器 下一个是写入配置寄存器
Write_ad7705 ( 0x6a );//自校准模式 双极性工作,增益32
}
uint16_t Read_ad7705 ()
{
uint8_t Msb_data,Lsb_data;
uint16_t temp;
uint8_t i,j,counter = 0x00;
float aver_ad = 0;
for ( i=0x00; i<10; i++ )
{
Write_ad7705 ( 0x38 );
Msb_data = Read_ad7705reg ();
Lsb_data = Read_ad7705reg ();
temp = Msb_data;
temp<<= 8 ;
AD_value1[i] = temp + Lsb_data;
}
delay_us ( 200 );
//从小到大排序
for ( i=0x00; i<10; i++ )
for ( j=i+1; j<10; j++ )
{
if ( AD_value1[i] < AD_value1[j] )
{
temp = AD_value1[i];
AD_value1[i] = AD_value1[j];
AD_value1[j] = temp;
}
}
delay_us ( 200 );
//舍去极大值
for ( i=0x01; i<10; i++ )
{
if ( AD_value1[0] > AD_value1[i] )
{
AD_value1[0] = 0;
break;
}
else
{
AD_value1[i] = 0;
}
}
delay_us ( 200 );
//舍去极小值
for ( i=0x09; j>0; j-- )
{
if ( AD_value1[9] < AD_value1[i] )
{
AD_value1[9] = 0;
break;
}
else
{
AD_value1[i] = 0;
}
}
delay_us ( 200 );
for ( i= 0x00; i<10; i++ )
{
if ( AD_value1[i] != 0x00 )
{
aver_ad = aver_ad + AD_value1[i] ;
counter++;
}
}
AD_value = ( unsigned int ) ( aver_ad / ( float) counter ) ;
return 0;
}
头文件:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
/* Private define ------------------------------------------------------------*/
#define SCLK_H GPIOB->BSRR = GPIO_Pin_15 /* GPIO_SetBits(GPIOB , GPIO_Pin_6) */
#define SCLK_L GPIOB->BRR = GPIO_Pin_15 /* GPIO_ResetBits(GPIOB , GPIO_Pin_6) */
#define DIN_H GPIOB->BSRR = GPIO_Pin_12 /* GPIO_SetBits(GPIOB , GPIO_Pin_7) */
#define DIN_L GPIOB->BRR = GPIO_Pin_12 /* GPIO_ResetBits(GPIOB , GPIO_Pin_7) */
#define DOUT_read GPIOB->IDR & GPIO_Pin_13 /* GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_7) */
#define DRDY_read GPIOB->IDR & GPIO_Pin_14 /* GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_7) */
/* Private function prototypes -----------------------------------------------*/
void AD7705_Configuration(void);
void Inti_ad7705 (void);
uint8_t Read_ad7705reg (void);
void AD7705_Reset(void);
uint16_t Read_ad7705 ( void ); |