Fixed AutoChangeGain

This commit is contained in:
Taylor Liao
2021-08-12 17:49:06 +08:00
parent 09c47df3bb
commit 3ee0945114
6 changed files with 113 additions and 82 deletions
@@ -94,7 +94,7 @@ static void AD5940_sftreset(){
static void setEIS_EIS (void)
{
// AD5940_init();
AD5940_init();
select_REG(LPDACCON0); //2128 //DC on
w32_REG(0x00000001); //LPDAC enabled
@@ -104,8 +104,20 @@ static void setEIS_EIS (void)
select_REG(DE0RESCON); //20F8 //DE0's gain
w32_REG(0x000000FF);
// select_REG(HSRTIACON);
// w32_REG(0x00000200); //4pF + 10000K
select_REG(HSRTIACON);
w32_REG(0x00000200); //4pF + 200R
if (instru.ADCGainLv != HSRTIA_GAIN_AUTO) {
instru.AutoGainEnable = 0;
} else {
instru.AutoGainEnable = 1;
instru.ADCGainLv = HSRTIA_200R;
HSTIAGainCtrl(instru.ADCGainLv);
}
DAC_outputV(instru.dcbias);
SetWGAmp(instru.acamp);
SetAVG(instru.avgnum);
select_REG(ADCCON); //21A8
w32_REG(0x00000101);
@@ -164,42 +164,46 @@ static void VinADCGainCtrl(uint8_t VinADCLevel){
}
static void HSTIAGainCtrl(uint8_t HSTIALevel) {
/* HSRTIACON[12:5] = CTIACON, fix at 16pF;
/* HSRTIACON[12:5] = CTIACON, fix at 4pF;
HSRTIACON[4] = TIASW6CON, diode not in parallel with RTIA;
HSRTIACON[3:0] = RTIA */
uint32_t reg = 0;
uint32_t reg;
uint8_t data;
select_REG(HSRTIACON);
reg = r32_REG();
if (HSTIALevel == HSRTIA_160K) {
// ADC gain level = 0, using 160k resister
reg = (uint32_t)(RTIA160k) & 0x0000000F;
reg = reg & 0x0000010F;
} else if (HSTIALevel == HSRTIA_20K) {
data = RTIA160k;
}
else if (HSTIALevel == HSRTIA_20K) {
// ADC gain level = 1, using 20k resister
reg = (uint32_t)(RTIA20k) & 0x0000000F;
reg = reg & 0x0000010F;
data = RTIA20k;
}
else if (HSTIALevel == HSRTIA_5K) {
// ADC gain level = 2, using 5k resister
reg = (uint32_t)(RTIA5k) & 0x0000000F;
reg = reg & 0x0000010F;
data = RTIA5k;
}
else if (HSTIALevel == HSRTIA_200R) {
// ADC gain level = 3, using 200R resister
reg = (uint32_t)(RTIA200R) & 0x0000000F;
reg = reg & 0x0000010F;
data = RTIA200R;
}
else if (HSTIALevel == HSRTIA_GAIN_AUTO) {
// ADC gain level = 4, using 200R resister
reg = (uint32_t)(RTIA200R) & 0x0000000F;
reg = reg & 0x0000010F;
} else {
// default left RTIA open
reg = (uint32_t)(RTIAopen) & 0x0000000F;
reg = reg & 0x0000010F;
data = RTIA200R;
}
reg = (reg & (~15)) | (data);
select_REG(HSRTIACON);
w32_REG(reg);
if(HSTIALevel == 0 || HSTIALevel == 1 || HSTIALevel == 2 || HSTIALevel == 3){
lastIinADCGainLevel = HSTIALevel;
}else{
lastIinADCGainLevel = 3;
}
record_flag = false;
}
@@ -720,11 +724,11 @@ static uint32_t read_HSTIA_Iin(){
}
RealCurrent = correctedDFT * 1000 / HSRTIATable[instru.ADCGainLv] * 13; // Irms[nA]
RealCurrentRMS = RealCurrent * 10000 / 7071;
// RealCurrentRMS = RealCurrent * 10000 / 7071;
InputNotify(NOTIFY_CURRENT, RealCurrent);
InputNotify(NOTIFY_VOLT, HSRTIATable[instru.ADCGainLv]);
InputNotify(NOTIFY_IMPEDANCE, correctedDFT);
InputNotify(NOTIFY_VOLT, instru.ADCGainLv);
// InputNotify(NOTIFY_IMPEDANCE, correctedDFT);
return RealCurrent;
}
@@ -905,7 +909,7 @@ static void AutoChangeHSTIAGain(int32_t RealCurrent){
I_GAIN_3M_counter++;
if(I_GAIN_3M_counter > 2){
instru.ADCGainLv = HSRTIA_160K;
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
I_GAIN_3M_counter = 0;
}
}
@@ -914,7 +918,7 @@ static void AutoChangeHSTIAGain(int32_t RealCurrent){
I_GAIN_100K_counter++;
if(I_GAIN_100K_counter > 2){
instru.ADCGainLv = HSRTIA_20K;
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
I_GAIN_100K_counter = 0;
}
}
@@ -923,7 +927,7 @@ static void AutoChangeHSTIAGain(int32_t RealCurrent){
I_GAIN_3K_counter++;
if(I_GAIN_3K_counter > 2){
instru.ADCGainLv = HSRTIA_5K;
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
I_GAIN_3K_counter = 0;
}
}
@@ -945,7 +949,7 @@ static void AutoChangeHSTIAGain(int32_t RealCurrent){
I_GAIN_100R_counter++;
if(I_GAIN_100R_counter > 2){
instru.ADCGainLv = HSRTIA_200R;
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
I_GAIN_100R_counter = 0;
}
}
@@ -955,7 +959,7 @@ static void AutoChangeHSTIAGain(int32_t RealCurrent){
I_GAIN_3M_counter++;
if(I_GAIN_3M_counter > 2){
instru.ADCGainLv = HSRTIA_160K;
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
I_GAIN_3M_counter = 0;
}
}
@@ -964,7 +968,7 @@ static void AutoChangeHSTIAGain(int32_t RealCurrent){
I_GAIN_100K_counter++;
if(I_GAIN_100K_counter > 2){
instru.ADCGainLv = HSRTIA_20K;
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
I_GAIN_100K_counter = 0;
}
}
@@ -986,7 +990,7 @@ static void AutoChangeHSTIAGain(int32_t RealCurrent){
I_GAIN_3M_counter++;
if(I_GAIN_3M_counter > 2){
instru.ADCGainLv = HSRTIA_160K;
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
I_GAIN_3M_counter = 0;
}
}
@@ -996,7 +1000,7 @@ static void AutoChangeHSTIAGain(int32_t RealCurrent){
I_GAIN_100R_counter++;
if(I_GAIN_100R_counter > 2){
instru.ADCGainLv = HSRTIA_200R;
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
I_GAIN_100R_counter = 0;
}
}
@@ -1005,7 +1009,7 @@ static void AutoChangeHSTIAGain(int32_t RealCurrent){
I_GAIN_3K_counter++;
if(I_GAIN_3K_counter > 2){
instru.ADCGainLv = HSRTIA_5K;
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
I_GAIN_3K_counter = 0;
}
}
@@ -1028,7 +1032,7 @@ static void AutoChangeHSTIAGain(int32_t RealCurrent){
I_GAIN_100R_counter++;
if(I_GAIN_100R_counter > 2){
instru.ADCGainLv = HSRTIA_200R;
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
I_GAIN_100R_counter = 0;
}
}
@@ -1037,7 +1041,7 @@ static void AutoChangeHSTIAGain(int32_t RealCurrent){
I_GAIN_3K_counter++;
if(I_GAIN_3K_counter > 2){
instru.ADCGainLv = HSRTIA_5K;
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
I_GAIN_3K_counter = 0;
}
}
@@ -1046,7 +1050,7 @@ static void AutoChangeHSTIAGain(int32_t RealCurrent){
I_GAIN_100K_counter++;
if(I_GAIN_100K_counter > 2){
instru.ADCGainLv = HSRTIA_20K;
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
I_GAIN_100K_counter = 0;
}
@@ -51,7 +51,9 @@ struct wm_meas_t {
struct wm_eis_ctx_t {
struct wm_meas_t measure;
int16_t _phase;
int32_t _magI;
int32_t _mag;
int32_t _real;
int32_t _imag;
uint32_t _f1;
uint32_t _f2;
uint32_t _fd1;
@@ -196,7 +198,9 @@ static int __eis_create(void)
m->_VoViSwitch = instru.VoViSwitch;
p->_phase = 0;
p->_magI = 0;
p->_mag = 0;
p->_real = 0;
p->_imag = 0;
p->_f1 = instru.f1;
p->_f2 = instru.f2;
p->_fmax = instru.fmax;
@@ -29,13 +29,13 @@ enum all_mode_e {
CURVE_IT = 0x06, // I-T Graph //0x60,
CURVE_CC = 0x07, // Constant Current (CC) //0xD0,
CURVE_OCP = 0x08, // Open Circuit Potential (OCP)
CURVE_CV = 0xD1, // Cyclic Voltammetry (CV) //0xC0,
CURVE_CV = 0x09, // Cyclic Voltammetry (CV) //0xC0,
CURVE_LSV = 0x0A, // Linear Sweep Voltammetry (LSV) //0x02,
CURVE_CA = 0x0B, // Chronoamperometric Graph (CA) //0x03,
CURVE_PULSE = 0x0C, //0x94,
CURVE_EIS = 0x09, //Should Change to 0xD1
CURVE_EIS = 0xD1, //Should Change to 0xD1
CURVE_EIS_CV = 0xD2,
CURVE_CALI_DAC = 0xF0, //0x93,
@@ -128,10 +128,20 @@ static uint16_t GrabAmp() //Grab input amp from DAC amp code (range 0x0 ~ 0x7FF)
return realAmp; //uV
}
static int16_t CalcPhase()
static void CurrentMag2ZMag(){
struct wm_eis_ctx_t *eis = (struct wm_eis_ctx_t *)wm_get();
eis->_mag = GrabAmp() * 1000 / read_HSTIA_Iin();
// InputNotify(NOTIFY_CURRENT, GrabAmp());
// InputNotify(NOTIFY_VOLT, read_HSTIA_Iin());
// InputNotify(NOTIFY_IMPEDANCE, eis->_mag);
return;
}
static void CalcPhase()
{
struct wm_eis_ctx_t *eis = (struct wm_eis_ctx_t *)wm_get();
static int16_t measPhase, correctPhase;
static int16_t measPhase;
double ret;
ret = (round(atan((double)instru.imag / (double)instru.real) * 1000) * 180 + 1570) / 314; //degree * 10
@@ -154,18 +164,25 @@ static int16_t CalcPhase()
}
}
correctPhase = (measPhase - (25 * (int64_t)instru.fset - 89078000) / 100000) / 10;
eis->_phase = (measPhase - (20 * (int64_t)instru.fset - 89078000) / 100000) / 10;
return correctPhase;
}
// InputNotify(NOTIFY_IMPEDANCE, measPhase);
// InputNotify(NOTIFY_CURRENT, eis->_cnt);
static int32_t correctReal()
{
return;
}
static int32_t correctImag()
static void correctReal()
{
struct wm_eis_ctx_t *eis = (struct wm_eis_ctx_t *)wm_get();
eis->_real = eis->_mag * cos((double)eis->_phase * 314 / 18000);
return;
}
static void correctImag()
{
struct wm_eis_ctx_t *eis = (struct wm_eis_ctx_t *)wm_get();
eis->_imag = eis->_mag * sin((double)eis->_phase * 314 / 18000);
return;
}
@@ -292,14 +309,11 @@ static void HSTIA_change_gain(void)
/* read Iin and do NOT record the Iin after changing gain twice */
MEAS_CURR(wm) = read_HSTIA_Iin();
if (instru.AutoGainEnable && firstGainChange) {
if (instru.AutoGainEnable) {
AutoChangeHSTIAGain(MEAS_CURR(wm));
if (instru.eliteFxn == CURVE_EIS) {
firstGainChange = false;
}
} else {
if (lastIinADCGainLevel != instru.ADCGainLv) {
HSTIAGainCtrl2(instru.ADCGainLv);
HSTIAGainCtrl(instru.ADCGainLv);
}
}
@@ -462,6 +476,7 @@ static void CV_Plot(void)
static void CC_Plot(void) //real and imag impedance plot
{
struct wm_eis_ctx_t *eis = (struct wm_eis_ctx_t *)wm_get();
static uint8_t ADC_cnt = 0;
static uint32_t mag;
void *wm = wm_get();
@@ -471,27 +486,33 @@ static void CC_Plot(void) //real and imag impedance plot
ADC_cnt++;
} else if (ADC_cnt == 1) {
// HSTIA_change_gain();
MEAS_CURR(wm) = read_HSTIA_Iin();
HSTIA_change_gain();
ADC_cnt++;
} else if (ADC_cnt == 2) {
CalcPhase();
// instru.real = neg_18bit(ReadRealZ());
ADC_cnt++;
} else if (ADC_cnt == 3) {
// instru.imag = neg_18bit(ReadImagZ());
CurrentMag2ZMag();
ADC_cnt++;
} else if (ADC_cnt == 4) {
ADC_cnt++;
} else if (ADC_cnt == 5) {
ADC_cnt = 0;
}
// InputNotify(NOTIFY_CURRENT, instru.imag);
// InputNotify(NOTIFY_CURRENT, MEAS_CURR(wm));
// InputNotify(NOTIFY_VOLT, instru.real);
// InputNotify(NOTIFY_IMPEDANCE, instru.ADCGainLv);
// InputNotify(NOTIFY_CURRENT, MEAS_CURR(wm));
// InputNotify(NOTIFY_VOLT, instru.fset);
// InputNotify(NOTIFY_IMPEDANCE, GrabAmp());
InputNotify(NOTIFY_CURRENT, eis->_mag);
InputNotify(NOTIFY_VOLT, instru.fset);
InputNotify(NOTIFY_IMPEDANCE, eis->_phase);
return;
}
@@ -440,9 +440,9 @@ characteristic change event
// product information
#define DEVICE_NAME "Elite"
#define MAJOR_PRODUCT_NUMBER 0 //0:Elite ,1:Neulive
#define MINOR_PRODUCT_NUMBER 2 //1:Elite_legacy(Ori_Neulive) 2:Elite_zm 3:Elite_bat 4:Elite_EIS
#define MINOR_PRODUCT_NUMBER 4 //1:Elite_legacy(Ori_Neulive) 2:Elite_zm 3:Elite_bat 4:Elite_EIS
#define MAJOR_VERSION_NUMBER 1
#define MINOR_VERSION_NUMBER 6
#define MINOR_VERSION_NUMBER 0
#define ELITE_VERSION_EIS
//#define ELITE_VERSION_1_4
//#define ELITE_VERSION_1_3
@@ -685,8 +685,8 @@ static void update_ZM_instruction(uint8 *ins) {
case CURVE_EIS: { //0xD1
if (ins[3] == PARA_1) { //3000D1 01
instru.sampleRate = 15; //ms //vscan
instru.f1 = 67114;//((uint32_t)(ins[4]) << 24) | ((uint32_t)(ins[5]) << 16) | ((uint32_t)(ins[6]) << 8) | (uint32_t)(ins[7]); //FREQ_START
instru.f2 = 6711409;//((uint32_t)(ins[8]) << 24) | ((uint32_t)(ins[9]) << 16) | ((uint32_t)(ins[10]) << 8) | (uint32_t)(ins[11]); //FREQ_STOP
instru.f1 = ((uint32_t)(ins[4]) << 24) | ((uint32_t)(ins[5]) << 16) | ((uint32_t)(ins[6]) << 8) | (uint32_t)(ins[7]); //FREQ_START
instru.f2 = ((uint32_t)(ins[8]) << 24) | ((uint32_t)(ins[9]) << 16) | ((uint32_t)(ins[10]) << 8) | (uint32_t)(ins[11]); //FREQ_STOP
instru.fmax = (uint32_t)VMAX(instru.f1, instru.f2);
instru.fmin = (uint32_t)VMIN(instru.f1, instru.f2);
instru.delay = 1;//((uint16_t)(ins[12]) << 8) | (uint16_t)(ins[13]); //DELAY how many periods
@@ -699,26 +699,16 @@ static void update_ZM_instruction(uint8 *ins) {
}
} else if (ins[3] == PARA_2) { //3000D1 02
instru.eliteFxn = CURVE_EIS;
instru.dcbias = 0;//((uint16_t)(ins[4]) << 8) | (uint16_t)(ins[5]);
instru.acamp = 26;//((uint16_t)(ins[6]) << 8) | (uint16_t)(ins[7]);
instru.avgnum = 2;//(uint8_t)(ins[8]);
instru.rtia = 2;//(uint8_t)(ins[9]);
instru.dcbias = ((uint16_t)(ins[4]) << 8) | (uint16_t)(ins[5]);
instru.acamp = ((uint16_t)(ins[6]) << 8) | (uint16_t)(ins[7]);
instru.avgnum = (uint8_t)(ins[8]);
instru.rtia = (uint8_t)(ins[9]);
instru.ppd = 10;//((uint16_t)(ins[10]) << 8) | (uint16_t)(ins[11]);
instru.scale = 0;//(uint8_t)(ins[12]);
setEIS_EIS();
DAC_outputV(instru.dcbias);
SetWGAmp(instru.acamp);
SetAVG(instru.avgnum);
instru.scale = (uint8_t)(ins[12]);
instru.ADCGainLv = instru.rtia;
// if (instru.ADCGainLv != I_GAIN_AUTO) {
// instru.AutoGainEnable = 0;
// } else {
// instru.AutoGainEnable = 1;
// instru.ADCGainLv = HSRTIA_200R;
// HSTIAGainCtrl(instru.ADCGainLv);
// }
setEIS_EIS();
ModeLED(WORKING);
}