119 lines
4.1 KiB
C++
119 lines
4.1 KiB
C++
/****************************************************
|
|
Programmed during 2011-05-02 and 2011-07-29 for the
|
|
project ACM1219 as a serial interface
|
|
Designed and developed by Adrian Beckert
|
|
Contact: adrian.beckert@gmx.ch
|
|
*****************************************************/
|
|
|
|
#include <Wire.h>
|
|
#include <EEPROM.h>
|
|
|
|
#define I2C_ADDRESS 0x48 //define register adresses
|
|
#define STATUS 0x00
|
|
#define CAP_DATA 0x01
|
|
#define VT_DATA 0x04
|
|
#define CAP_SETUP 0x07
|
|
#define VT_SETUP 0x08
|
|
#define EXC_SETUP 0x09
|
|
#define CONFIGURATION 0x0A
|
|
#define CAP_DAC_A 0x0B
|
|
#define CAP_DAC_B 0x0C
|
|
#define CAP_OFFSET 0x0D
|
|
#define CAP_GAIN 0x0F
|
|
#define VOLTAGE_GAIN 0x11
|
|
|
|
//initial operation of EEPROM for booting and factory default settins
|
|
byte values[159]={128,0,75,57,158,0,128,0,77,208,91,37,115,101,
|
|
116,67,73,78,32,49,44,48,44,48,52,46,48,44,57,57,46,57,44,48,
|
|
44,57,57,46,57,44,57,57,46,57,0,115,101,116,76,73,77,73,84,32,
|
|
48,48,46,48,49,44,48,56,46,49,56,44,48,48,46,48,48,44,48,48,46,
|
|
48,48,0,0,1,128,0,75,57,158,0,128,0,77,208,91,37,115,101,
|
|
116,67,73,78,32,49,44,48,44,48,52,46,48,44,57,57,46,57,44,48,
|
|
44,57,57,46,57,44,57,57,46,57,0,115,101,116,76,73,77,73,84,32,
|
|
48,48,46,48,49,44,48,56,46,49,56,44,48,48,46,48,48,44,48,48,46,
|
|
48,48,0};
|
|
|
|
//variables for statistics
|
|
unsigned long ti=0; //millis() is an unsigned long type (runs over after 49.7 days)
|
|
unsigned int n=0;
|
|
long long av[2];
|
|
long long vari[2];
|
|
long mini[2];
|
|
long maxi[2];
|
|
|
|
//string for saving settings from setCIN
|
|
byte settings[33];
|
|
byte limits[33];
|
|
|
|
void setup(){
|
|
if (EEPROM.read(77)!=0){ //check if EEPROM may have been already used or not
|
|
for (byte i=0;i<158;i++){
|
|
EEPROM.write(i,values[i]); //write values in EEPROM
|
|
}
|
|
}
|
|
|
|
pinMode(2,OUTPUT); //set pin 2 as output
|
|
pinMode(3,OUTPUT); //set pin 3 as output
|
|
|
|
Serial.begin(9600);
|
|
Wire.begin();
|
|
|
|
byte control;
|
|
Wire.beginTransmission(0x00); //general call
|
|
Wire.write(0x06); //reset AD7746
|
|
control =Wire.endTransmission();
|
|
delay(1); //wait for reboot
|
|
if (control!=0){
|
|
Serial.println("DEVICE CONNECTION ERROR");
|
|
}
|
|
|
|
//Set configurations saved in EEPROM
|
|
for (byte i=0;i<12;i++){
|
|
writeR(i+7,EEPROM.read(i)); //set registers as saved in EEPROM
|
|
}
|
|
|
|
//get Settings-String of EEPROM
|
|
for (byte i=12;i<45;i++){
|
|
settings[i-12]=EEPROM.read(i); //set settings-string as saved in EEPROM
|
|
}
|
|
|
|
//get limits-String of EEPROM
|
|
for (byte i=45;i<78;i++){
|
|
limits[i-45]=EEPROM.read(i); //set settings-string as saved in EEPROM
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void loop(){
|
|
byte cvt[7];
|
|
byte control=0;
|
|
if (Serial.available()>0){ //check if command is available
|
|
char cmd[33];
|
|
serial_gets(33,cmd); //read incoming command
|
|
analyse_cmd(cmd); //analyse command
|
|
control=1; //indicate that a command was processed
|
|
}
|
|
|
|
if (n>50000){control=1;} //indicate if the averaging function risks to overflow
|
|
if (control==1){ //if indicator is =1
|
|
ti=millis(); //reset time
|
|
n=0; //reset number of measurements
|
|
for (byte i=0;i<2;i++){ //reset all other statistical variables
|
|
av[i]=0;
|
|
vari[i]=0;
|
|
mini[i]=16777215;
|
|
maxi[i]=0;
|
|
}
|
|
}
|
|
else{
|
|
if (bitRead(readR(0),0)==0){ //check status register if new data is available
|
|
readRegisters(7,cvt); //read new data
|
|
n++;
|
|
accumulate(cvt); //update statistical variables
|
|
}
|
|
}
|
|
}
|