Тема: AD7793
// AD7793
#define SCLK 2
#define CS 3
#define DIN 4
#define DOUT 5
#define AVDD_MONITOR 0b111 // CONF
#define TEMP_SENSOR 0b110 // CONF
#define CH1 0b000 // CONF
#define CH2 0b001 // CONF
#define CH3 0b011 // CONF
#define BIPOLAR 0b00000000 // CONF
#define UNIPOLAR 0b00010000 // CONF
#define BIPOLAR 0b00000000 // CONF
#define UNIPOLAR 0b00010000 // CONF
#define GAIN_1 0b000 // CONF
#define GAIN_2 0b001 // CONF
#define GAIN_4 0b010 // CONF
#define GAIN_8 0b011 // CONF
#define GAIN_16 0b100 // CONF
#define GAIN_32 0b101 // CONF
#define GAIN_64 0b110 // CONF
#define GAIN_128 0b111 // CONF
#define F_ADC_500 0b0001 // MODE
#define F_ADC_250 0b0010 // MODE
#define F_ADC_125 0b0011 // MODE
#define F_ADC_62_5 0b0100 // MODE
#define F_ADC_50_0 0b0101 // MODE
#define F_ADC_39_2 0b0110 // MODE
#define F_ADC_33_3 0b0111 // MODE
#define F_ADC_19_6 0b1000 // MODE
#define F_ADC_16_7 0b1001 // MODE
#define F_ADC_12_5 0b1011 // MODE
#define F_ADC_10_0 0b1100 // MODE
#define F_ADC_8_33 0b1101 // MODE
#define F_ADC_6_25 0b1110 // MODE
#define F_ADC_4_17 0b1111 // MODE
void setup(){
Serial.begin(9600);
pinMode(SCLK,OUTPUT);pinMode(CS,OUTPUT);pinMode(DIN,OUTPUT);pinMode(DOUT,INPUT);
digitalWrite(SCLK,HIGH);digitalWrite(CS,HIGH);digitalWrite(DIN,HIGH);
delay(100);
reset();
write_byte(0x08,(0x20<<8)|F_ADC_4_17, 16);// MODE REGISTER
write_byte(0x10,((BIPOLAR|GAIN_1)<<8)|0x90|AVDD_MONITOR, 16); // CONFIGURATION REGISTER
write_byte(0x08,(0xC0<<8)|0x0A, 16);// MODE REGISTER System Zero-Scale Calibration
write_byte(0x08,(0xE0<<8)|0x0A, 16);// MODE REGISTER System Full-Scale Calibration
Serial.print("Stat ");Serial.println(read_byte(0x40,8),BIN);// STATUS REGISTER
Serial.print("ID ");Serial.println(read_byte(0x60,8),BIN);// ID REGISTER
Serial.print("Ofst ");Serial.println(read_byte(0x70,24));//OFFSET REGISTER
Serial.print("ful ");Serial.println(read_byte(0x78,24));//OFULL-SCALE REGISTER
write_byte(0x28,0x00, 8);// IO REGISTER
}
void loop(){
// READ ADC //////////////////////////////////////////////////////////////////
write_byte(0x08,(0x20<<8)|F_ADC_4_17, 16);// MODE REGISTER
write_byte(0x10,((BIPOLAR|GAIN_1)<<8)|0x90|CH1, 16); // CONFIGURATION REGISTER
long adc = read_byte(0x58,24);
// BIPOLAR
Serial.print("Data b_B ");Serial.println(adc-0x800000);//DATA REGISTER
Serial.print("Data V_B ");Serial.println((1.17/0x800000*(adc-0x800000)),6);//DATA REGISTER
// UNIPOLAR
// Serial.print("Data b_U ");Serial.println(adc);//DATA REGISTER
// Serial.print("Data V_U ");Serial.println((1.17/0xFFFFFF*adc),6);//DATA REGISTER
Serial.println();
delay(5);
// READ AVDD ///////////////////////////////////////////////////////////////////
write_byte(0x08,(0x20<<8)|F_ADC_4_17, 16);// MODE REGISTER
write_byte(0x10,((BIPOLAR|GAIN_1)<<8)|0x90|AVDD_MONITOR, 16); // CONFIGURATION REGISTER
adc = read_byte(0x58,24);
// BIPOLAR
Serial.print("Data AV b_B ");Serial.println(adc-0x800000);//DATA REGISTER
Serial.print("Data AV V_B ");Serial.println((1.17/0x800000*(adc-0x800000)*6),4);//DATA REGISTER
Serial.println();
delay(5);
// TEMP SENSOR ///////////////////////////////////////////////////////////////////
write_byte(0x08,(0x20<<8)|F_ADC_4_17, 16);// MODE REGISTER
write_byte(0x10,((BIPOLAR|GAIN_1)<<8)|0x90|TEMP_SENSOR, 16); // CONFIGURATION REGISTER
adc = read_byte(0x58,24);
// BIPOLAR
Serial.print("Data T b_B ");Serial.println(adc-0x800000);//DATA REGISTER
Serial.print("Data T V_B ");Serial.println(1.170/0x800000*(adc-0x800000),4);//DATA REGISTER
Serial.println();
delay(1000);
}
void write_byte(byte reg, long data, int bit_){
digitalWrite(CS,LOW);
for(int i = 7; i >= 0; i--){
digitalWrite(SCLK,LOW);
digitalWrite(DIN, (reg >> i) & 1);
digitalWrite(SCLK,HIGH);
}
for(int i = bit_-1; i >= 0; i--){
digitalWrite(SCLK,LOW);
digitalWrite(DIN, (data >> i) & 1);
digitalWrite(SCLK,HIGH);
}
digitalWrite(CS,HIGH);
}
long read_byte(byte reg, int bit_){
digitalWrite(CS,LOW);
while(digitalRead(DOUT)!=LOW);
for(int i = 7; i >= 0; i--){
digitalWrite(SCLK,LOW);
digitalWrite(DIN, (reg >> i) & 1);
digitalWrite(SCLK,HIGH);
}
while(digitalRead(DOUT)!=LOW);
long data_out=0;
long dat;
for(int i = bit_-1; i >= 0; i--){
digitalWrite(SCLK,LOW);
digitalWrite(SCLK,HIGH);
dat = digitalRead(DOUT);
data_out |= (dat<<i);
}
digitalWrite(CS,HIGH);
return data_out;
}
void reset(){
digitalWrite(CS,LOW);
for(int i = 31; i >= 0; i--){
digitalWrite(SCLK,LOW);
digitalWrite(DIN, HIGH);
digitalWrite(SCLK,HIGH);
}
digitalWrite(CS,HIGH);
}