Counters Done. It worked, but now it doesn't weird

This commit is contained in:
Taylor Liao
2021-08-16 14:23:16 +08:00
parent 7edcb7517a
commit c133fa74c3
8 changed files with 73 additions and 55 deletions
@@ -725,8 +725,8 @@ static uint32_t read_HSTIA_Iin(){
RealCurrent = correctedDFT * 1000 / HSRTIATable[instru.ADCGainLv] * 13; // Irms[nA]
// RealCurrentRMS = RealCurrent * 10000 / 7071;
InputNotify(NOTIFY_CURRENT, RealCurrent);
InputNotify(NOTIFY_VOLT, instru.ADCGainLv);
// InputNotify(NOTIFY_CURRENT, RealCurrent);
// InputNotify(NOTIFY_VOLT, instru.ADCGainLv);
// InputNotify(NOTIFY_IMPEDANCE, correctedDFT);
return RealCurrent;
@@ -1139,66 +1139,74 @@ static void SetDFTNUM(uint16_t dft_num){
w32_REG(code);
}
static void SetSamplingTime(){
static uint32_t SetSamplingTime(uint32_t freq){
//separate the whole range of frequency into 7 pieces//
uint32_t time;
// 0.015Hz | 136s
if (instru.fset == 1) {
if (freq >= 1) {
SetADCDataRate(ADC800Ksps);
SetSinc2OSR(Sinc2OSR1333);
SetSinc3OSR(Sinc3OSR5);
SetDFTNUM(DFTNUM16384);
time = 1360000;
}
// 10000Hz | 0.22ms
else if (instru.fset >= 671141) {
else if (freq >= 1000000) {
SetADCDataRate(ADC1M6sps);
SetSinc2OSR(Sinc2OSR22);
SetSinc3OSR(Sinc3OSR4);
SetDFTNUM(DFTNUM4);
time = 2;
}
// 1000Hz | 3.52ms
else if (instru.fset >= 67114) {
else if (freq >= 100000) {
SetADCDataRate(ADC1M6sps);
SetSinc2OSR(Sinc2OSR22);
SetSinc3OSR(Sinc3OSR4);
SetDFTNUM(DFTNUM64);
time = 35;
}
// 100Hz | 28.16ms
else if (instru.fset >= 6711) {
else if (freq >= 10000) {
SetADCDataRate(ADC1M6sps);
SetSinc2OSR(Sinc2OSR22);
SetSinc3OSR(Sinc3OSR4);
SetDFTNUM(DFTNUM512);
time = 281;
}
// 10Hz | 225ms
else if (instru.fset >= 671) {
else if (freq >= 1000) {
SetADCDataRate(ADC1M6sps);
SetSinc2OSR(Sinc2OSR22);
SetSinc3OSR(Sinc3OSR4);
SetDFTNUM(DFTNUM4096);
time = 2250;
}
// 1Hz | 2.25s
else if (instru.fset >= 67) {
else if (freq >= 100) {
SetADCDataRate(ADC800Ksps);
SetSinc2OSR(Sinc2OSR22);
SetSinc3OSR(Sinc3OSR5);
SetDFTNUM(DFTNUM16384);
time = 22250;
}
//0.1Hz | 26.34s
else if (instru.fset >= 7) {
else if (freq >= 10) {
SetADCDataRate(ADC800Ksps);
SetSinc2OSR(Sinc2OSR267);
SetSinc3OSR(Sinc3OSR5);
SetDFTNUM(DFTNUM16384);
time = 263400;
}
return;
return time;
}
//EIS function//
@@ -116,33 +116,37 @@ static void VoutGainControl(uint8_t VOUTLevel){
record_flag = false;
}
static uint32_t CalcPeriod(){ //One Second = 10000
static uint32_t CalcPeriod(uint32_t freq){ //One Second = 10000
uint32_t period;
if (instru.fset == 1) {
if (freq == 1) {
period = 666667;
} else {
period = (1000000 + instru.fset / 2) / instru.fset; // [sec]
period = (1000000 + freq / 2) / freq; // [sec]
}
if (period < 2){
period = 2; //0.2ms
}
InputNotify(NOTIFY_IMPEDANCE, period);
return period;
}
static uint32_t CalcDelayTime(){
static uint32_t CalcDelayTime(uint32_t freq, bool delayFlag){
uint32_t delayTime;
delayTime = CalcPeriod() * instru.delay; //get delay time
delayTime = CalcPeriod(freq) * instru.delay; //get delay time
if (delayTime < 20) {
delayTime = 2;
} else {
delayTime = (delayTime + 5) / 10;
}
InputNotify(NOTIFY_CURRENT, delayTime);
if(delayFlag){
delayTime += 2 * CalcPeriod(freq); //delay+reading time
} else {
delayTime = 2 * CalcPeriod(freq);
}
return delayTime;
}
@@ -39,7 +39,7 @@ static void eis_fscan(void)
}
if(eis->_direction_up) {
if(eis->_sweepIndex == 1){
if(eis->_sweepIndex == 0){
if(eis->_decadeIndex < eis->_decades) {
eis->_fd1 = eis->_f1 * pow(10, eis->_decadeIndex);
eis->_fd2 = eis->_f1 * pow(10, eis->_decadeIndex + 1);
@@ -56,7 +56,7 @@ static void eis_fscan(void)
instru.fset = eis->_fd1 + eis->_sweepIndex * ((eis->_fd2 - eis->_fd1) / (eis->_ppd - 1));
}
} else { //reverse
if(eis->_sweepIndex == 1){
if(eis->_sweepIndex == 0){
if(eis->_decadeIndex < eis->_decades){
eis->_fd1 = eis->_f1 * pow(10, -eis->_decadeIndex);
eis->_fd2 = eis->_f1 * pow(10, -(eis->_decadeIndex + 1));
@@ -75,8 +75,8 @@ static void eis_fscan(void)
}
if(++eis->_sweepIndex == eis->_ppd) {
eis->_sweepIndex = 1;
if(++eis->_sweepIndex == (eis->_ppd - 1)) {
eis->_sweepIndex = 0;
eis->_decadeIndex ++;
}
@@ -89,16 +89,17 @@ static void eis_fscan(void)
instru.fset = eis->_fmin;
}
}
}
instru.sampleRate = CalcDelayTime(instru.fset, true);
instru.notifyRate = CalcDelayTime(instru.fset, true);
DAC_outputF(Freq2DAC(instru.fset));
SetSamplingTime();
SetSamplingTime(instru.fset);
// InputNotify(NOTIFY_CURRENT, instru.fset);
// InputNotify(NOTIFY_VOLT, eis->_fd1);
// InputNotify(NOTIFY_IMPEDANCE, eis->_fd2);
// InputNotify(NOTIFY_VOLT, instru.sampleRate);
InputNotify(NOTIFY_IMPEDANCE, SetSamplingTime(instru.fset));
}
#endif
@@ -43,6 +43,7 @@ struct HEADSTAGE_INSTRUCTION {
uint8_t periodIndex;
uint32_t delayTime;
/** ADC parameter **/
uint8_t notifyRateIndex;
uint32_t sampleRate;
@@ -262,6 +263,7 @@ static void InitEliteInstruction(){
instru.periodIndex = 0;
instru.delayTime = 0;
//pulse mode
instru.sti_t1 = 0;
instru.sti_t2 = 0;
@@ -211,7 +211,7 @@ static int __eis_create(void)
p->_cnt = 0;
p->_ppd = instru.ppd; //points per decade
p->_decades = 0;
p->_sweepIndex = 1;
p->_sweepIndex = 0;
p->_decadeIndex = 0;
p->_direction_up = true;
p->_switchPos = false;
@@ -474,16 +474,17 @@ static void EIS_Plot(void) //real and imag impedance plot
void *wm = wm_get();
if (ADC_cnt == 0) {
CalcPeriod();
CalcPeriod(instru.fset);
ADC_cnt++;
} else if (ADC_cnt == 1) {
// HSTIA_change_gain();
CalcDelayTime();
ADC_cnt++;
} else if (ADC_cnt == 2) {
// CalcPhase();
ADC_cnt++;
} else if (ADC_cnt == 3) {
@@ -570,7 +570,7 @@ static bool vscanReset;
static bool mode_init;
static bool leadTimeReset;
static bool firstTimeReset;
static bool delay_flag;
static bool fset_flag;
//pulse mode variable
static bool stiFirstTime;
@@ -684,14 +684,14 @@ static void update_ZM_instruction(uint8 *ins) {
switch (ins[2]) {
case CURVE_EIS: { //0xD1
if (ins[3] == PARA_1) { //3000D1 01
instru.sampleRate = 15; //ms //read
instru.f1 = 67;//((uint32_t)(ins[4]) << 24) | ((uint32_t)(ins[5]) << 16) | ((uint32_t)(ins[6]) << 8) | (uint32_t)(ins[7]); //FREQ_START
instru.f2 = 6711;//((uint32_t)(ins[8]) << 24) | ((uint32_t)(ins[9]) << 16) | ((uint32_t)(ins[10]) << 8) | (uint32_t)(ins[11]); //FREQ_STOP
// instru.sampleRate = CalcDelayTime(instru.f1, true);
instru.f1 = 6711;//((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.fmax = (uint32_t)VMAX(instru.f1, instru.f2);
instru.fmin = (uint32_t)VMIN(instru.f1, instru.f2);
instru.delay = 10;//((uint16_t)(ins[12]) << 8) | (uint16_t)(ins[13]); //DELAY/10 how many periods
instru.VsetRate = 10000;//instru.delay + 2 * CalcPeriod(); //set
instru.notifyRate = 10000;//instru.delay + 2 * CalcPeriod(); // need to consider average number as well
// instru.VsetRate = CalcDelayTime(instru.f1, true);//set
// instru.notifyRate = CalcDelayTime(instru.f1, true);// need to consider average number as well
if (instru.f1 > instru.f2){
instru.directionInit = 0; //0:reverse 1:forward
} else if (instru.f1 <= instru.f2){
@@ -140,7 +140,7 @@ static void SimpleBLEPeripheral_performPeriodicTask(void) {
mode_init = false;
batteryADC_flag = false;
record_flag = true;
delay_flag = true;
fset_flag = true;
firstTimeReset = true;
notifyFirst_flag = true;
first_highz_flag = true;
@@ -206,30 +206,33 @@ static void SimpleBLEPeripheral_performPeriodicTask(void) {
//vscan counter //fset counter
GPT.VscanRateCounter = GPT.VscanRateCounter + GPT.DeltaGptimerCounter;
if (GPT.VscanRateCounter >= instru.VsetRate) {
if (GPT.VscanRateCounter >= instru.VsetRate * 2) {
GPT.GptimerMultiple = GPT.VscanRateCounter / instru.VsetRate;
} else {
GPT.GptimerMultiple = 1;
if(instru.eliteFxn == CURVE_EIS){
if(fset_flag){
eis_fscan();
fset_flag = false;
}
GPT.VscanRateCounter -= instru.VsetRate * GPT.GptimerMultiple; //To get right time
vscan_flag = true;
if (vscan_flag) {
vscan_ctrl(); //set
vscan_flag = false;
} else {
if (GPT.VscanRateCounter >= instru.VsetRate) {
if (GPT.VscanRateCounter >= instru.VsetRate * 2) {
GPT.GptimerMultiple = GPT.VscanRateCounter / instru.VsetRate;
} else {
GPT.GptimerMultiple = 1;
}
GPT.VscanRateCounter -= instru.VsetRate * GPT.GptimerMultiple; //To get right time
vscan_flag = true;
if (vscan_flag) {
vscan_ctrl(); //set
vscan_flag = false;
}
}
}
//ADC counter //read data counter
GPT.SampleRateCounter = GPT.SampleRateCounter + GPT.DeltaGptimerCounter;
GPT.DelayTimeCounter = GPT.DelayTimeCounter + GPT.DeltaGptimerCounter;
if(GPT.SampleRateCounter >= instru.sampleRate){
GPT.SampleRateCounter = 0; //To get right data, ADC must be delay 1.5ms
ADC_flag = true;
// if(GPT.DelayTimeCounter >= instru.delayTime){
// delay_flag = false;
// }
if(ADC_flag){// && !delay_flag){
if(ADC_flag){
EliteADCControl(); //read data
ADC_flag = false;
}
@@ -247,7 +250,7 @@ static void SimpleBLEPeripheral_performPeriodicTask(void) {
if(notify_flag){
SendNotify(); //send
notify_flag = false;
delay_flag = true;
fset_flag = true;
}
}
@@ -391,9 +394,8 @@ static void EliteADCControl(void) //CURVE_IV => CC_Plot() | CURVE_CV => Iin_Vin_
struct wm_eis_ctx_t *eis = (struct wm_eis_ctx_t *)wm_get();
// InputNotify(NOTIFY_CURRENT, eis->_mag);
InputNotify(NOTIFY_VOLT, instru.fset);
// InputNotify(NOTIFY_IMPEDANCE, eis->_phase);
// InputNotify(NOTIFY_CURRENT, instru.delayTime);
// InputNotify(NOTIFY_IMPEDANCE, SetSamplingTime());
InputNotify(NOTIFY_CURRENT, instru.sampleRate);
}
break;