358 lines
13 KiB
C++
358 lines
13 KiB
C++
|
|
//********************Subroutine: setCIN************
|
|
|
|
void setCIN(byte* settings, char* cmd){
|
|
float f;
|
|
|
|
if (cmd[3]*cmd[4]-cmd[5]==4813){ //check if command is CIN and not MUC or else
|
|
for (byte i=0;i<32;i++){ //copy command into settings string
|
|
settings[i]=cmd[i];
|
|
}
|
|
}
|
|
|
|
switch (settings[7]){
|
|
|
|
case '0': //disable CIN 1 and CIN 2
|
|
set_bits(CAP_SETUP,0,8,settings[7]-'0'); //set cap channel off
|
|
writeR(CAP_DAC_A,0); //disable capdac A
|
|
writeR(CAP_DAC_B,0); //disable capdac B
|
|
break;
|
|
|
|
case '1': //CIN 1 selected
|
|
set_bits(CAP_SETUP,6,2,settings[7]-'0'+1); //set CIN1
|
|
set_bits(CAP_SETUP,5,1,settings[9]-'0'); //set differential mode
|
|
setCapdacA(settings,11); //set CapdacA
|
|
setCapdacB(settings,16); //set CapdacB
|
|
break;
|
|
|
|
|
|
case '2': //CIN 2 selected
|
|
set_bits(CAP_SETUP,6,2,settings[7]-'0'+1); //set CIN2
|
|
set_bits(CAP_SETUP,5,1,settings[21]-'0'); //set differential mode
|
|
setCapdacA(settings,23); //set CapdacA
|
|
setCapdacB(settings,28); //set CapdacB
|
|
break;
|
|
|
|
default:
|
|
Serial.println("setC_ERROR");
|
|
}
|
|
}
|
|
|
|
//********************Subroutine: setVT*************
|
|
|
|
void setVT(char* cmd){
|
|
set_bits(VT_SETUP,5,3,cmd[6]-'0'); //select temp sensor int/ext,vdd monitor, ext voltage input
|
|
set_bits(VT_SETUP,4,1,cmd[8]-'0'); //set ext voltage Reference
|
|
}
|
|
|
|
//********************Subroutine: setCT*************
|
|
|
|
void setCT(char* cmd){
|
|
set_bits(CONFIGURATION,6,2,cmd[8]-'0'); //set conversion time VT channel
|
|
set_bits(CONFIGURATION,3,3,cmd[6]-'0'); //set conversion time C channel
|
|
|
|
}
|
|
|
|
//********************Subroutine: setEXC************
|
|
|
|
void setEXC(char* cmd){
|
|
set_bits(EXC_SETUP,2,2,cmd[7]-'0'); //config EXC Pin A
|
|
set_bits(EXC_SETUP,4,2,cmd[9]-'0'); //config EXC Pin B
|
|
set_bits(EXC_SETUP,0,2,cmd[11]-'0'); //set Vdd
|
|
set_bits(EXC_SETUP,6,1,cmd[13]-'0'); //set EXCON
|
|
}
|
|
|
|
//********************Subroutine: setVTCHOP*********
|
|
|
|
void setVTCHOP(char* cmd){
|
|
set_bits(VT_SETUP,0,1,cmd[10]-'0'); //set VT chop
|
|
}
|
|
|
|
//********************Subroutine: setCLKCTRL********
|
|
|
|
void setCLKCTRL(char* cmd){
|
|
set_bits(EXC_SETUP,7,1,cmd[11]-'0'); //set CLKCTRL
|
|
}
|
|
|
|
//********************Subroutine: setVTSHORT********
|
|
|
|
void setVTSHORT(char* cmd){
|
|
set_bits(VT_SETUP,1,1,cmd[11]-'0'); //set VT short
|
|
}
|
|
//Example:
|
|
//setVTSHORT_1
|
|
|
|
//********************Subroutine: setCAPCHOP********
|
|
/*
|
|
CAPCHOP approximately doubles the conversion time
|
|
*/
|
|
void setCAPCHOP(char* cmd){
|
|
set_bits(CAP_SETUP,0,1,cmd[11]-'0'); //set CAP chop
|
|
}
|
|
|
|
//********************Subroutine: setCONVMOD********
|
|
|
|
void setCONVMOD(char* cmd){
|
|
set_bits(CONFIGURATION,0,3,cmd[11]-'0'); //set converter mode
|
|
}
|
|
|
|
//********************Subroutine: setCOF************
|
|
/*
|
|
set an accurate c-channel offset
|
|
*/
|
|
void setCOF(char* cmd){
|
|
unsigned int big=65535; //16Bit full range
|
|
big*=make_long(cmd,7); //return a long type
|
|
big/=6;
|
|
big/=10000; //delete additional digits
|
|
if (big<65536){
|
|
writeR(CAP_OFFSET,big/256); //write in register
|
|
writeR(CAP_OFFSET+1,big%256); //write in register
|
|
}
|
|
else{
|
|
Serial.println("SETCOF-ERROR");
|
|
}
|
|
}
|
|
|
|
//********************Subroutine: setGAINOF*********
|
|
/*
|
|
Set gain of the c-channel
|
|
*/
|
|
void setCGAINOF(char* cmd){
|
|
unsigned int big;
|
|
big=make_long(cmd,11); //convert string into integer value
|
|
if (big<65536){
|
|
writeR(CAP_GAIN,big/256); //split value in two registers
|
|
writeR(CAP_GAIN+1,big%256); //part two
|
|
}
|
|
else{
|
|
Serial.println("SETCGAINOF-ERROR");
|
|
}
|
|
}
|
|
|
|
//********************Subroutine: setVGAINOF********
|
|
/*
|
|
change factory calibratet VT-channel gain
|
|
*/
|
|
void setVGAINOF(char* cmd){
|
|
unsigned int big;
|
|
big=make_long(cmd,11); //convert string into integer value
|
|
if (big<65536){
|
|
writeR(VOLTAGE_GAIN,big/256); //split value in two registers
|
|
writeR(VOLTAGE_GAIN+1,big%256); //part two
|
|
}
|
|
else{
|
|
Serial.println("SETVGAINOF-ERROR");
|
|
}
|
|
}
|
|
|
|
/*********************Subroutine********************
|
|
read routines
|
|
**********************Subroutine: readCVT***********/
|
|
|
|
void readCVT(byte* settings){
|
|
switch (settings[7]){ //check which C channel is enabled
|
|
|
|
case '0':
|
|
Serial.print("-999.999,-999.999,"); //if both disabled, print twice -999.999
|
|
printll(rettemp(readVT(),0),4);
|
|
Serial.println("");
|
|
break;
|
|
|
|
case '1':
|
|
printll(retcap(readC(),0),8); //read capacitance on enabled channel
|
|
Serial.print(",-999.999,"); //print -999.999 for disabled channel
|
|
printll(rettemp(readVT(),0),4); //read temperature
|
|
Serial.println("");
|
|
break;
|
|
|
|
case '2': //analog to case '1'
|
|
Serial.print("-999.999,");
|
|
printll(retcap(readC(),0),8);
|
|
Serial.print(",");
|
|
printll(rettemp(readVT(),0),4);
|
|
Serial.println("");
|
|
break;
|
|
|
|
default:
|
|
Serial.println("READ-ERROR");
|
|
}
|
|
}
|
|
|
|
//********************Subroutine: readAVCVT*********
|
|
void readAVCVT(){
|
|
float f;
|
|
if (n>1){ //avoid divisions by 0
|
|
for (byte i=0;i<2;i++){
|
|
vari[i]/=(n-1); //calculate variance
|
|
f=float(vari[i]); //convert long long to float
|
|
f/=100; //remove additional decimals (because of integer arithmetic)
|
|
f=sqrt(f); //calculate standard deviation
|
|
f/=8388608.; //convert bit value into pF
|
|
f*=4.096; //multiply with half mesure scale
|
|
av[i]/=n; //calculate average
|
|
if (i==0){
|
|
printll(retcap(mini[i],0),8); //convert bit value to pF
|
|
Serial.print(",");
|
|
printll(retcap(maxi[i],0),8); //convert bit value to pF
|
|
Serial.print(",");
|
|
printll(retcap(av[i],3),8); //convert to pF and print average
|
|
Serial.print(",");
|
|
}
|
|
else{
|
|
printll(rettemp(mini[i],0),4); //convert bit value to pF
|
|
Serial.print(",");
|
|
printll(rettemp(maxi[i],0),4); //convert bit value to pF
|
|
Serial.print(",");
|
|
printll(rettemp(av[i],3),4); //convert to pF and print average
|
|
Serial.print(",");
|
|
}
|
|
Serial.print(f,5);
|
|
Serial.print(",");
|
|
}
|
|
Serial.print(millis()-ti); //display mesured interval
|
|
Serial.print(",");
|
|
Serial.println(n); //display number of mesurements
|
|
}
|
|
else{
|
|
Serial.println("n<2 !!!");
|
|
}
|
|
}
|
|
//********************Subroutine: readSTATUS********
|
|
void readSTATUS(){
|
|
displayStatus(); //Display all Registers
|
|
}
|
|
//********************Subroutine: readMUC***********
|
|
/*
|
|
read on CIN 1 and CIN 2 multiplex. Mesure first with
|
|
actual settings, afterward load settings for other
|
|
channel and restore previous settings.Between
|
|
multiplex delays for 700ms for updating values
|
|
on c-channel.
|
|
*/
|
|
|
|
void readMUC(byte* settings, char* cmd){
|
|
long f;
|
|
switch (settings[7]){ //check current enabled channel
|
|
|
|
case '0': //both channels disabled
|
|
Serial.print("-999.999,-999.999,"); //show that both capacitance channels are disabled
|
|
printll(rettemp(readVT(),0),4); //read+print vt channel
|
|
break;
|
|
|
|
case '1': //channel 1 enabled
|
|
printll(retcap(readC(),0),8); //read+print capacitance
|
|
Serial.print(",");
|
|
settings[7]='2'; //change settings string for next step. Channel 2 will be enabled
|
|
setCIN(settings,cmd); //change coonfigurations and enable channel 2 (due to the change of settings string)
|
|
delay(700); //wait for updating the new configured channel
|
|
printll(retcap(readC(),0),8); //read+print capacitance on channel 2
|
|
Serial.print(",");
|
|
settings[7]='1'; //change settings string
|
|
setCIN(settings,cmd); //change configurations and enable channel 1
|
|
printll(rettemp(readVT(),0),4); //read+print vt channel
|
|
break;
|
|
|
|
case '2': //channel 2 enabled
|
|
f=readC(); //read first channel 2 and store result
|
|
settings[7]='1'; //start multiplex mode etc. (analog case '1')
|
|
setCIN(settings,cmd);
|
|
//delay(700);
|
|
while(bitRead(readR(0),0)==1);
|
|
while(bitRead(readR(0),0)==1);
|
|
printll(retcap(readC(),0),8);
|
|
settings[7]='2';
|
|
setCIN(settings,cmd);
|
|
Serial.print(",");
|
|
printll(retcap(f,0),8);
|
|
Serial.print(",");
|
|
printll(rettemp(readVT(),0),4);
|
|
break;
|
|
|
|
default:
|
|
Serial.println("MULTIPLEX-ERROR");
|
|
}
|
|
Serial.println("");
|
|
|
|
}
|
|
//********************Subroutine: saveSETTINGS******
|
|
/*
|
|
save the current settings in EEPROM incl. settings-string
|
|
*/
|
|
|
|
void saveSettings(){
|
|
byte a;
|
|
unsigned int b=0;
|
|
|
|
for (byte i=0;i<12;i++){ //go through every write-register and
|
|
a=readR(i+7);
|
|
if(a!=EEPROM.read(i)){ //proof if the current register has the same value as before
|
|
EEPROM.write(i,a); //write the value into EEPROM
|
|
b=1; //indicate if any change has been made
|
|
}
|
|
}
|
|
|
|
for (byte i=0;i<33;i++){
|
|
if (EEPROM.read(i+12)!=settings[i]){ //check settings-string
|
|
EEPROM.write(i+12,settings[i]); //if a value is different, write to EEPROM
|
|
b=1; //indicate if any change has been made
|
|
}
|
|
}
|
|
|
|
for (byte i=0;i<33;i++){
|
|
if (EEPROM.read(i+45)!=limits[i]){ //check limits-string
|
|
EEPROM.write(i+45,limits[i]); //if a value is different, write to EEPROM
|
|
b=1; //indicate if any change has been made
|
|
}
|
|
}
|
|
|
|
|
|
if (b==1){ //if changes have been saved
|
|
b=(long(EEPROM.read(78))*256)+EEPROM.read(79); //read how many have already been saved
|
|
b++; //increase 1
|
|
if (b>65534){ //ERROR if settings have already been changed 65534 times
|
|
Serial.println("ATTENTION CURRENT USED EEPROM FOR SAVESETTINGS HAS TO BE CHANGED.PLEASE CONSULT THE DEVELOPER A.BECKERT.");
|
|
}
|
|
|
|
EEPROM.write(78,b/256); //write new save-number to EEPROM
|
|
EEPROM.write(79,b%256); //write new save-number to EEPROM
|
|
|
|
}
|
|
}
|
|
//********************Subroutine: setLIMIT**********
|
|
/*
|
|
create limits string
|
|
*/
|
|
void setLIMIT(byte* limits, char*cmd){
|
|
for (byte i=0;i<32;i++){ //copy command into settings string
|
|
limits[i]=cmd[i];
|
|
}
|
|
}
|
|
//setLIMIT 04.99,11.21,05.02,12.17"
|
|
//********************Subroutine: restoreDefaults***
|
|
/*
|
|
restore factory defaults
|
|
*/
|
|
void restoreDefaults(){
|
|
for (byte i=0;i<78;i++){
|
|
if (EEPROM.read(i)!=EEPROM.read(80+i)){
|
|
EEPROM.write(i,EEPROM.read(80+i));
|
|
}
|
|
}
|
|
//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
|
|
}
|
|
}
|
|
|