操作寄存器主要根据上述图片选择写寄存器还是读存器
例如 写配置寄存器 需先发送0x10选择配置寄存器接着跟要配置的数据
模块采用高位先行,先传高字节
SPI 发送 10 00 10 08
配置寄存器使用 REFIN1(+) 与 REFIN1( − ) 之间施加的外部基准电压。
通道选择 AIN1 and AINCOM
增益选择1
其他寄存器配置同理
读配置寄存器只需要R/W位使能及为读 0x50
#include "AD7192.h"
volatile uint8_t display_flag = 0;
void ADC7190_Delay (int length)
{
while (length >0)
length--;
}
#define AD7190_GPIO GPIOA
#define AD7190_SCLK_PIN GPIO_PIN_5
#define AD7190_CS_PIN GPIO_PIN_4
#define AD7190_DIN_PIN GPIO_PIN_6
#define AD7190_DOUT_PIN GPIO_PIN_7
#define AD7190_SCLK_H AD7190_GPIO->BSRR |= AD7190_SCLK_PIN
#define AD7190_SCLK_L AD7190_GPIO->BRR |= AD7190_SCLK_PIN
#define AD7190_CS_H AD7190_GPIO->BSRR |= AD7190_CS_PIN
#define AD7190_CS_L AD7190_GPIO->BRR |= AD7190_CS_PIN
#define AD7190_DIN_H AD7190_GPIO->BSRR |= AD7190_DIN_PIN
#define AD7190_DIN_L AD7190_GPIO->BRR |= AD7190_DIN_PIN
#define AD7190_DOUT() (AD7190_GPIO->IDR & AD7190_DOUT_PIN)
//---------------------------------
//void WriteToAD7190(unsigned char count,unsigned char *buf);
//---------------------------------
//Function that writes to the AD7190 via the SPI port.
//--------------------------------------------------------------------------------
void WriteToAD7190(unsigned char count, unsigned char *buf)
{
unsigned char ValueToWrite = 0;
unsigned char i = 0;
unsigned char j = 0;
AD7190_SCLK_H;
ADC7190_Delay(1);
AD7190_CS_H;
ADC7190_Delay(1);
AD7190_CS_L;
ADC7190_Delay(1);
for(i=count;i>0;i--)
{
ValueToWrite = *(buf + i - 1);
for(j=0; j<8; j++)
{
AD7190_SCLK_L;
if(0x80 == (ValueToWrite & 0x80))
{
AD7190_DIN_H; //Send one to SDO pin
}
else
{
AD7190_DIN_L; //Send zero to SDO pin
}
ADC7190_Delay(1);
AD7190_SCLK_H;
ADC7190_Delay(1);
ValueToWrite <<= 1; //Rotate data
}
}
AD7190_CS_H;
}
//---------------------------------
//void ReadFromAD7190(unsigned char count,unsigned char *buf)
//---------------------------------
//Function that reads from the AD7190 via the SPI port.
//--------------------------------------------------------------------------------
void ReadFromAD7190(unsigned char count, unsigned char *buf)
{
unsigned char i = 0;
unsigned char j = 0;
unsigned char RotateData = 0;
AD7190_SCLK_H;
ADC7190_Delay(1);
AD7190_CS_H;
ADC7190_Delay(1);
AD7190_CS_L;
ADC7190_Delay(1);
for(j=count; j>0; j--)
{
for(i=0; i<8; i++)
{
AD7190_SCLK_L;
RotateData <<= 1; //Rotate data
ADC7190_Delay(1);
if(AD7190_DOUT()) //Read SDI of AD7190
{
RotateData |= 1;
}
AD7190_SCLK_H;
ADC7190_Delay(1);
}
*(buf + j - 1)= RotateData;
}
AD7190_CS_H;
}
void AD719x_Init(uint8 Gan)
{
uint8_t buf[3];
buf[0] = 0x10;
WriteToAD7190(1,buf); //write communication register 0x10 to control the progress to write configuration register
buf[2] = 0x00;
buf[1] = 0x10;//01 2 AIN1 to AIN2 ,02 2 AIN3 to AIN4
buf[0] = 0x08;
if(Gan == 1)buf[0] |= 0x03; //ÔöÒæÅäÖÃΪ128
WriteToAD7190(3,buf); //write configuration register,unipolar operation,gain=1,channel:AIN1 to AIN2
buf[0] = 0x08;
WriteToAD7190(1,buf); //write communication register 0x08 to control the progress to write mode register
buf[2] = 0x08;
buf[1] = 0x00;
buf[0] = 0x01;
WriteToAD7190(3,buf); //write mode register,internal 4.92MHz clock,output data rate=4.7Hz
// buf[0] = 0x50;
// WriteToAD7190(1,buf); //write communication register 0x50 to control the progress to read configuration register
// ReadFromAD7190(3,buf); //read configuration register
// buf[0] = 0x48;
// WriteToAD7190(1,buf); //write communication register 0x58 to control the progress to read data register
// ReadFromAD7190(3,buf); //read data register
// myprintf("%X,%X,%X\n",buf[0],buf[1],buf[2]);
}
unsigned int ad719x_Change_voltage(void)
{
unsigned int temp =0;
uint8_t buf[3];
buf[0] = 0x58;
WriteToAD7190(1,buf); //write communication register 0x58 to control the progress to read data register
ReadFromAD7190(3,buf); //read data register
temp = (buf[2] << 16) + (buf[1] << 8) + buf[0];
return temp;
}