Compare commits

..

1 Commits

Author SHA1 Message Date
Benny Liu 440158f806 calibration mode 2019-10-01 18:46:51 +08:00
23 changed files with 476 additions and 926 deletions
@@ -16,7 +16,7 @@
# sources were generated) is:
# C:\ti\simplelink\ble_sdk_2_02_02_25\examples\cc2650em\simple_peripheral\ccs\config\src
#
GEN_SRC_DIR ?= ../../../../../ti/simplelink/ble_sdk_2_02_02_25/examples/cc2650em/simple_peripheral/ccs/config/src
GEN_SRC_DIR ?= ../../config/src
ifeq (,$(wildcard $(GEN_SRC_DIR)))
$(error "ERROR: GEN_SRC_DIR must be set to the directory containing the generated sources")
@@ -1,12 +1,12 @@
XOPTS = -I"C:/ti/xdctools_3_32_00_06_core/packages/" -Dxdc_target_types__=C:/ti/tirtos_cc13xx_cc26xx_2_21_01_08/products/bios_6_46_01_38/packages/ti/targets/arm/elf/std.h -Dxdc_target_name__=M3
XOPTS = -I"C:/ti/xdctools_3_32_02_25_core/packages/" -Dxdc_target_types__=C:/ti/tirtos_cc13xx_cc26xx_2_21_01_08/products/bios_6_46_01_38/packages/ti/targets/arm/elf/std.h -Dxdc_target_name__=M3
vpath % C:/ti/tirtos_cc13xx_cc26xx_2_21_01_08/products/bios_6_46_01_38/packages/ti/sysbios/
vpath %.c C:/ti/xdctools_3_32_00_06_core/packages/
vpath %.c C:/ti/xdctools_3_32_02_25_core/packages/
CCOPTS = --endian=little -mv7M3 --abi=eabi -q -ms --opt_for_speed=0 --program_level_compile -o3 -g --optimize_with_debug -Dti_sysbios_knl_Task_minimizeLatency__D=FALSE -Dti_sysbios_family_arm_cc26xx_Boot_driverlibVersion=2 -Dti_sysbios_knl_Clock_stopCheckNext__D=TRUE -Dti_sysbios_family_arm_m3_Hwi_enableException__D=TRUE -Dti_sysbios_family_arm_m3_Hwi_disablePriority__D=32U -Dti_sysbios_family_arm_m3_Hwi_numSparseInterrupts__D=0U
XDC_ROOT = C:/ti/xdctools_3_32_00_06_core/packages/
XDC_ROOT = C:/ti/xdctools_3_32_02_25_core/packages/
BIOS_ROOT = C:/ti/tirtos_cc13xx_cc26xx_2_21_01_08/products/bios_6_46_01_38/packages/ti/sysbios/
@@ -16,14 +16,14 @@ BIOS_INC = -I"C:/ti/tirtos_cc13xx_cc26xx_2_21_01_08/products/bios_6_46_01_38/pa
TARGET_INC = -I"C:/ti/tirtos_cc13xx_cc26xx_2_21_01_08/products/bios_6_46_01_38/packages/"
INCS = $(BIOS_INC) $(TARGET_INC) --include_path="C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.4.LTS/include" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/headstage" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/icall/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/profiles/roles/cc26xx" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/profiles/roles" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/profiles/dev_info" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/profiles/simple_profile/cc26xx" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/profiles/simple_profile" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/common/cc26xx" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/heapmgr" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/controller/cc26xx/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/hal/src/target/_common" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/target" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/hal/src/target/_common/cc26xx" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/hal/src/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/osal/src/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/services/src/sdata" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/services/src/saddr" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/icall/src/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/rom" --include_path="C:/ti/tirtos_cc13xx_cc26xx_2_21_01_08/products/cc26xxware_2_24_03_17272" -IC:/ti/tirtos_cc13xx_cc26xx_2_21_01_08/products/bios_6_46_01_38/packages/
INCS = $(BIOS_INC) $(TARGET_INC) --include_path="C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.3.LTS/include" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/headstage" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/icall/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/profiles/roles/cc26xx" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/profiles/roles" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/profiles/dev_info" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/profiles/simple_profile/cc26xx" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/profiles/simple_profile" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/common/cc26xx" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/heapmgr" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/controller/cc26xx/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/hal/src/target/_common" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/target" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/hal/src/target/_common/cc26xx" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/hal/src/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/osal/src/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/services/src/sdata" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/services/src/saddr" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/components/icall/src/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/inc" --include_path="C:/ti/simplelink/ble_sdk_2_02_02_25/src/rom" --include_path="C:/ti/tirtos_cc13xx_cc26xx_2_21_01_08/products/cc26xxware_2_24_03_17272" -IC:/ti/tirtos_cc13xx_cc26xx_2_21_01_08/products/bios_6_46_01_38/packages/
CC = C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.4.LTS/bin/armcl -c $(CCOPTS) -I C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.4.LTS/include
ASM = C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.4.LTS/bin/armcl -c $(CCOPTS) -I C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.4.LTS/include
AR = C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.4.LTS/bin/armar rq
CC = C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.3.LTS/bin/armcl -c $(CCOPTS) -I C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.3.LTS/include
ASM = C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.3.LTS/bin/armcl -c $(CCOPTS) -I C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.3.LTS/include
AR = C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.3.LTS/bin/armar rq
DEL = C:/ti/xdctools_3_32_00_06_core/packages/../bin/rm -f
CP = C:/ti/xdctools_3_32_00_06_core/packages/../bin/cp -f
DEL = C:/ti/xdctools_3_32_02_25_core/packages/../bin/rm -f
CP = C:/ti/xdctools_3_32_02_25_core/packages/../bin/cp -f
define RM
$(if $(wildcard $1),$(DEL) $1,:)
@@ -76,11 +76,6 @@ static void ADCGainControl(uint8_t ADCLevel){
PIN_setOutputValue(pin_handle, Turnon10K, 0);
PIN_setOutputValue(pin_handle, Turnon100R, 1);
}
else if(ADCLevel == 3){
// ADC gain level = 0, auto gain (using 200R resister)
PIN_setOutputValue(pin_handle, Turnon10K, 0);
PIN_setOutputValue(pin_handle, Turnon100R, 0);
}
else{
// default using 200R resister
PIN_setOutputValue(pin_handle, Turnon10K, 0);
@@ -126,103 +121,4 @@ static void ADCChannelSelect(uint8_t ADCChannel){
}
}
static void ReadVolt(uint8_t *buf){
// Read data twice since the first data we get is previous data
ADCChannelSelect(ADC_CH_VOLT);
CPUdelay(10);
ADC_read(buf);
ADCChannelSelect(ADC_CH_VOLT);
CPUdelay(10);
ADC_read(buf);
}
static void ReadCurrent(uint8_t *buf){
// Read data twice since the first data we get is previous data
ADCGainControl(INSTRUCTION.ADCGainLevel);
ADCChannelSelect(ADC_CH_CURRENT);
CPUdelay(10);
ADC_read(buf);
ADCChannelSelect(ADC_CH_CURRENT);
CPUdelay(10);
ADC_read(buf);
}
// theoretical boundary <20, 10~500, >100 (uA)
#define GAIN_SMALL_BOUNDARY 40000 // 40 uA = 40,000,000 pA
#define GAIN_MID_BOUNDARY1 20000 // 20 uA = 20,000,000 pA
#define GAIN_MID_BOUNDARY2 400000 // 400 uA = 400,000,000 pA
#define GAIN_LARGE_BOUNDARY 200000 // 200 uA = 200,000 nA
static int32_t AutoGainReadCurrent(uint8_t *buf){
int32_t Real_Current = 0;
if(INSTRUCTION.ADCGainLevel == GAIN_AUTO){
INSTRUCTION.ADCGainLevel = GAIN_200R;
}
if(INSTRUCTION.ADCGainLevel == GAIN_200R){
ReadCurrent(spi_ADC_rxbuf);
Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
// switch to mid range current
if(Real_Current < GAIN_LARGE_BOUNDARY && Real_Current > -1*GAIN_LARGE_BOUNDARY){
// LED_color(DARKLED, 0x00, 0x0F, 0xFF);
INSTRUCTION.ADCGainLevel = GAIN_10K;
ReadCurrent(spi_ADC_rxbuf);
Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
// switch to small range current
if (Real_Current < GAIN_MID_BOUNDARY1 && Real_Current > -1*GAIN_MID_BOUNDARY1){
// LED_color(DARKLED, 0x00, 0x00, 0xFF);
INSTRUCTION.ADCGainLevel = GAIN_200K;
ReadCurrent(spi_ADC_rxbuf);
Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
}
}
}
else if(INSTRUCTION.ADCGainLevel == GAIN_10K){
ReadCurrent(spi_ADC_rxbuf);
Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
// switch to large range current
if(Real_Current > GAIN_MID_BOUNDARY2 || Real_Current < -1*GAIN_MID_BOUNDARY2){
// LED_color(DARKLED, 0xFF, 0x0F, 0x0F);
INSTRUCTION.ADCGainLevel = GAIN_200R;
ReadCurrent(spi_ADC_rxbuf);
Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
}
// switch to small range current
else if (Real_Current < GAIN_MID_BOUNDARY1 && Real_Current > -1*GAIN_MID_BOUNDARY1){
// LED_color(DARKLED, 0x00, 0x00, 0xFF);
INSTRUCTION.ADCGainLevel = GAIN_200K;
ReadCurrent(spi_ADC_rxbuf);
Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
}
}
else if(INSTRUCTION.ADCGainLevel == GAIN_200K){
ReadCurrent(spi_ADC_rxbuf);
Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
// switch to mid range current
if(Real_Current > GAIN_SMALL_BOUNDARY || Real_Current < -1*GAIN_SMALL_BOUNDARY){
// LED_color(DARKLED, 0x00, 0x0F, 0xFF);
INSTRUCTION.ADCGainLevel = GAIN_10K;
ReadCurrent(spi_ADC_rxbuf);
Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
// switch to large range current
// if(Real_Current > GAIN_MID_BOUNDARY2 || Real_Current < -1*GAIN_MID_BOUNDARY2){
// LED_color(DARKLED, 0xFF, 0x0F, 0x0F);
// INSTRUCTION.ADCGainLevel = GAIN_200R;
// ReadCurrent(spi_ADC_rxbuf);
// Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
// }
}
}
return Real_Current;
}
#endif
@@ -2,47 +2,41 @@
#ifndef ELITECCMODE
#define ELITECCMODE
#define CC_ZERO_POINT 1500000
#define MAX_DAC_UC 50000
#define MIN_DAC_UC 0
#define CURRENT_LV_FOUR 4
#define CURRENT_LV_THREE 3
#define CURRENT_LV_TWO 2
#define CURRENT_LV_ONE 1
#define CURRENT_LV_ZERO 0
static void CCModeDACControl(int32_t IUC_Measure_Difference);
/*********************************************************************
* @struct Constant Current Code
*
* @brief A struct to handle CC mode command
*/
typedef struct _CURRENT_USER_CODE {
/** current value **/
// current value divide current level into 3,000,001 pieces
// 1,500,000 is zero point
int32_t value;
/** ADC level range: 0-2 **/
// constant current value will decide ADC gain level
// if |1500000 - value| > 10000 (+-100 uA) => lv = GAIN_200R
// else if |1500000 - valule| > 1000 (+-10 uA) => lv = GAIN_10K
// else lv = GAIN_200K
/** current level range: 0-4 **/
// current level = 0 => 0-499 nA => ADCGainLevel = 200K
// current level = 1 => 500-999 nA => ADCGainLevel = 10K
// current level = 2 => 0-499 uA => ADCGainLevel = 10K
// current level = 3 => 500-999 uA => ADCGainLevel = 200R
// current level = 4 => 0-499 mA => ADCGainLevel = 200R
uint8_t lv;
/* Vmax and Vmin */
// Vmax protect battery charge
// Vmin protect battery discharge
// uint = mV
uint16_t Vmax;
uint16_t Vmin;
/** transform a current user code (IUC) to real current in nA **/
int32_t (*_Transform2RealnA)(struct _CURRENT_USER_CODE *);
/** current value **/
// current value divide current level into 50000 pieces
uint16_t value;
/** Measure Current **/
int32_t _MeasureCurrent;
/** transform a current user code (IUC) to real current in pA **/
// handle current lv 0~2
int32_t (*_Transform2RealpA)(struct _CURRENT_USER_CODE *);
/** transform an IUC to real current in nA **/
// handle current lv 3~4
int32_t (*_Transform2RealnA)(struct _CURRENT_USER_CODE *);
/** MeasureCurrent operation **/
void (*SetMeasureCurrent)(struct _CURRENT_USER_CODE *, int32_t);
@@ -51,109 +45,174 @@ typedef struct _CURRENT_USER_CODE {
//static CURRENT_USER_CODE CurrentUserCode;
static int32_t CCModeReadCurrent(void *WorkModeData){
static int32_t CCModeReadCurrent(CURRENT_USER_CODE *CurrentUserCode){
int32_t Real_Current = 0;
CURRENT_USER_CODE *CurrentUserCode = WorkModeData;
// CurrentUserCode = WorkModeData;
CCModeReset = 0; // This flag will control DAC working
CCModeDACEnable = 1; // This flag will control DAC working
// set current value and ADC gain level
CCCurrent2IUC(CurrentUserCode);
// if(CurrentUserCode->lv == CURRENT_LV_FOUR){
// Real_Current = CurrentUserCode->_Transform2RealnA(CurrentUserCode);
// }
// else{
// Real_Current = CurrentUserCode->_Transform2RealpA(CurrentUserCode);
// }
// set ADC gain according to constant current value
INSTRUCTION.ADCGainLevel = CurrentUserCode->lv;
SetCCModeGain(CurrentUserCode);
// read ADC current
ReadCurrent(spi_ADC_rxbuf);
ADCGainControl(INSTRUCTION.ADCGainLevel);
ADCChannelSelect(ADC_CH_CURRENT);
CPUdelay(10);
ADC_read(spi_ADC_rxbuf);
// decode ADC value and put it into notify buffer
Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
CurrentUserCode->SetMeasureCurrent(CurrentUserCode, Real_Current);
Real_Current = 8787877;
CurrentUserCode->SetMeasureCurrent(CurrentUserCode, Real_Current);
return Real_Current;
}
static int32_t CCModeVoltOut(void *WorkModeData){
int32_t MeasureCurrent = 0, IUCCurrent = 0;
CURRENT_USER_CODE *CurrentUserCode = WorkModeData;
static int32_t CCModeVoltOut(CURRENT_USER_CODE *CurrentUserCode){
int32_t MeasureCurrent = 0;
if(!CCModeDACEnable){
if(CCModeReset){
// DAC should not work now
return 0;
}
IUCCurrent = CurrentUserCode->_Transform2RealnA(CurrentUserCode);
if(CurrentUserCode->lv == GAIN_200K || CurrentUserCode->lv == GAIN_10K ){
MeasureCurrent = CurrentUserCode->GetMeasureCurrent(CurrentUserCode);
CCModeDACControl(IUCCurrent - MeasureCurrent);
}
else{
MeasureCurrent = CurrentUserCode->GetMeasureCurrent(CurrentUserCode);
CCModeDACControl(IUCCurrent - MeasureCurrent);
}
// NotifyCurrent[0] = (uint8_t) (IUCCurrent >> 24);
// NotifyCurrent[1] = (uint8_t) ((IUCCurrent & 0x00FF0000) >> 16);
// NotifyCurrent[2] = (uint8_t) ((IUCCurrent & 0x0000FF00) >> 8);
// NotifyCurrent[3] = (uint8_t) (IUCCurrent & 0x000000FF);
//
// NotifyVolt[0] = (uint8_t) (MeasureCurrent >> 24);
// NotifyVolt[1] = (uint8_t) ((MeasureCurrent & 0x00FF0000) >> 16);
// NotifyVolt[2] = (uint8_t) ((MeasureCurrent & 0x0000FF00) >> 8);
// NotifyVolt[3] = (uint8_t) (MeasureCurrent & 0x000000FF);
// MeasureCurrent = CurrentUserCode->GetMeasureCurrent(CurrentUserCode);
DACCode2Real2Notify(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
CCModeDACEnable = 0;
NotifyCurrent[0] = (uint8_t) (MeasureCurrent >> 24);
NotifyCurrent[1] = (uint8_t) ((MeasureCurrent & 0x00FF0000) >> 16);
NotifyCurrent[2] = (uint8_t) ((MeasureCurrent & 0x0000FF00) >> 8);
NotifyCurrent[3] = (uint8_t) (MeasureCurrent & 0x000000FF);
NotifyVolt[0] = (uint8_t) (MeasureCurrent >> 24);
NotifyVolt[1] = (uint8_t) ((MeasureCurrent & 0x00FF0000) >> 16);
NotifyVolt[2] = (uint8_t) ((MeasureCurrent & 0x0000FF00) >> 8);
NotifyVolt[3] = (uint8_t) (MeasureCurrent & 0x000000FF);
// INSTRUCTION.VoltConstant = 24999 + 500;
// DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
return MeasureCurrent;
}
static void CCModeDACControl(int32_t IUC_Measure_Difference){
if(IUC_Measure_Difference > 100000 || IUC_Measure_Difference < -100000){
INSTRUCTION.VoltConstant = INSTRUCTION.VoltConstant + IUC_Measure_Difference/1e4;
DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
}
else if(IUC_Measure_Difference > 1000 || IUC_Measure_Difference < -1000){
INSTRUCTION.VoltConstant = INSTRUCTION.VoltConstant + IUC_Measure_Difference/1e3;
DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
}
else if(IUC_Measure_Difference > 0){
INSTRUCTION.VoltConstant = INSTRUCTION.VoltConstant + 1;
DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
}
else if(IUC_Measure_Difference < 0){
INSTRUCTION.VoltConstant = INSTRUCTION.VoltConstant - 1;
DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
static void SetCCModeGain(CURRENT_USER_CODE *CurrentUserCode){
switch(CurrentUserCode->lv){
case CURRENT_LV_FOUR:{
INSTRUCTION.ADCGainLevel = GAIN_200R;
break;
}
case CURRENT_LV_THREE:{
INSTRUCTION.ADCGainLevel = GAIN_200R;
break;
}
case CURRENT_LV_TWO:{
INSTRUCTION.ADCGainLevel = GAIN_10K;
break;
}
case CURRENT_LV_ONE:{
INSTRUCTION.ADCGainLevel = GAIN_200K;
break;
}
case CURRENT_LV_ZERO:{
INSTRUCTION.ADCGainLevel = GAIN_200K;
break;
}
default :{
INSTRUCTION.ADCGainLevel = GAIN_200R;
break;
}
}
}
/* Transform setting CC into IUC
*
* User code in CC mode : 0 ~ 3000000
* Real current value : -15.00000 ~ 15.00000 mA
* => user code = 1500000 mapping to 0.00000 mA
*/
static void CCCurrent2IUC(CURRENT_USER_CODE *CurrentUserCode){
int32_t CurrentValue = 0;
CurrentUserCode->value = INSTRUCTION.ConstantCurrent;
CurrentValue = CurrentUserCode->value - CC_ZERO_POINT;
/* set ADC level */
// largest current
if (CurrentValue > 10000 || CurrentValue < -10000){
CurrentUserCode->lv = GAIN_200R;
if (INSTRUCTION.CurrentLV == CURRENT_LV_MA){
// largest current ( 0~500 mA)
CurrentUserCode->lv = CURRENT_LV_FOUR;
CurrentUserCode->value = (uint16_t) (INSTRUCTION.ConstantCurrent);
}
// mid range current
else if (CurrentValue > 1000 || CurrentValue < -1000){
CurrentUserCode->lv = GAIN_10K;
else if (INSTRUCTION.CurrentLV == CURRENT_LV_UA){
if(INSTRUCTION.ConstantCurrent >= 50000){
// mid range current ( 500 uA ~ 999 uA)
CurrentUserCode->lv = CURRENT_LV_THREE;
CurrentUserCode->value = (uint16_t) (INSTRUCTION.ConstantCurrent - 50000);
}
else{
// mid range current ( 0 uA ~ 499 uA)
CurrentUserCode->lv = CURRENT_LV_TWO;
CurrentUserCode->value = (uint16_t) (INSTRUCTION.ConstantCurrent);
}
}
// least range current
else{
CurrentUserCode->lv = GAIN_200K;
if(INSTRUCTION.ConstantCurrent >= 50000){
// mid range current ( 500 nA ~ 999 nA)
CurrentUserCode->lv = CURRENT_LV_ONE;
CurrentUserCode->value = (uint16_t) (INSTRUCTION.ConstantCurrent - 50000);
}
else{
// mid range current ( 0 nA ~ 499 nA)
CurrentUserCode->lv = CURRENT_LV_ZERO;
CurrentUserCode->value = (uint16_t) (INSTRUCTION.ConstantCurrent);
}
}
}
//static int32_t IUC2RealnA(){
//
//}
//
//static int32_t IUC2RealpA{
//
//}
/*********************************************************************
* @fn Transform2RealpA
*
* @brief transform an IUC into real current value in pA.
*
* @param self, which is an IUC
*
* @return an int32_t current value in pA
*/
static int32_t _Transform2RealpA(CURRENT_USER_CODE *self){
int32_t IUCReal;
/** current level range: 0-4 **/
// current level = 0 => 0-499 nA => ADCGainLevel = 200K
// current level = 1 => 500-999 nA => ADCGainLevel = 10K
// current level = 2 => 0-499 uA => ADCGainLevel = 10K
// current level = 3 => 500-999 uA => ADCGainLevel = 200R
// current level = 4 => 0-499 mA => ADCGainLevel = 200R
// Saturate if current > 500 uA
if (self->lv == CURRENT_LV_FOUR){
return 0xFFFFFFFF;
}
if (self->lv == CURRENT_LV_THREE){
return 0xFFFFFFFF;
}
// 0-499 nA
if (self->lv == CURRENT_LV_ZERO){
IUCReal = (int32_t) (self->value) * 1e3;
}
// 500-999 nA
else if (self->lv == CURRENT_LV_ONE){
IUCReal = ((int32_t) (self->value) * 1e3);
IUCReal = IUCReal + 500e3;
}
// 0-499 uA
else if (self->lv == CURRENT_LV_TWO){
IUCReal = (int32_t) (self->value) * 1e6;
}
return IUCReal;
}
/*********************************************************************
* @fn Transform2RealnA
*
@@ -166,30 +225,86 @@ static void CCCurrent2IUC(CURRENT_USER_CODE *CurrentUserCode){
static int32_t _Transform2RealnA(CURRENT_USER_CODE *self){
int32_t IUCReal;
// self->value : 0 ~ 3000000 (which is -1500000 ~ 1500000 (10nA) )
IUCReal = (self->value - CC_ZERO_POINT) * 10;
// Saturate if current < 500 uA
if (self->lv == CURRENT_LV_ZERO | self->lv == CURRENT_LV_ONE | self->lv == CURRENT_LV_TWO){
return 0;
}
// 500-999 uA
if (self->lv == CURRENT_LV_THREE){
IUCReal = (int32_t) (self->value) * 1e3;
IUCReal = IUCReal + 500e3;
}
// 0-499 mA
else if (self->lv == 4){
IUCReal = (int32_t) (self->value) * 1e6;
}
return IUCReal;
}
/*********************************************************************
* @fn CompareCurrent
*
* @brief compare an int32 current with CURRENT_USER_CODE (IUC) type current.
*
* @param unit is current unit (0 = pA, 1 = nA)
* value is current value
*
* @return 0 if equal
* 1 if IUC is larger
* 2 if int32 current is larger.
*/
static uint8_t CompareCurrent(CURRENT_USER_CODE *self, uint8_t unit, int32_t value){
int32_t ErrorRangeIUCReal;
// unit = pA
if (unit == 0){
if (self->_Transform2RealpA(self) > value){
return 1;
}
else if (self->_Transform2RealpA(self) < value){
return 2;
}
else{
return 0;
}
}
// unit = nA
else if (unit == 1){
if (self->_Transform2RealnA(self) > value){
return 1;
}
else if (self->_Transform2RealnA(self) < value){
return 2;
}
else{
return 0;
}
}
}
static void SetMeasureCurrent(CURRENT_USER_CODE *self, int32_t current){
self->_MeasureCurrent = current;
}
static int32_t GetMeasureCurrent(CURRENT_USER_CODE *self){
LED_color(DARKLED, 0x0F, 0x00, 0xFF);
return self->_MeasureCurrent;
}
static CURRENT_USER_CODE *InitCurrentUserCode(){
CURRENT_USER_CODE *CurrentUserCode = malloc(sizeof(CURRENT_USER_CODE));
CurrentUserCode->value = CC_ZERO_POINT;
CurrentUserCode->lv = GAIN_AUTO;
CurrentUserCode->Vmax = MAX_DAC_UC; // max DAC UserCode
CurrentUserCode->Vmin = MIN_DAC_UC; // min DAC UserCode
CurrentUserCode-> _MeasureCurrent = 0;
CurrentUserCode->lv = 0;
CurrentUserCode->value = 0;
CurrentUserCode->_MeasureCurrent = 0;
CurrentUserCode->_Transform2RealnA = &_Transform2RealnA;
CurrentUserCode->_Transform2RealpA = &_Transform2RealpA;
CurrentUserCode->SetMeasureCurrent = &SetMeasureCurrent;
CurrentUserCode->GetMeasureCurrent = &GetMeasureCurrent;
return CurrentUserCode;
}
#endif
@@ -133,13 +133,15 @@ static uint16_t DPVCurve() {
}
static uint16_t CVCurve() {
static uint16_t DACOutCode;
static bool direction_up; // direction_up = true, if Vfinal > Vorigin
static bool current_direction_up; // current_direction_up = true, Vstep => positive. vice versa
static uint8_t ramp0;
static uint8_t ramp1;
static uint16_t outputV;
static bool direction_up;
static bool current_direction_up;
// reset origin volt at the begin
if (DACReset) {
DACUserCode = INSTRUCTION.VoltOrigin;
outputV = INSTRUCTION.VoltOrigin;
if (INSTRUCTION.VoltFinal > INSTRUCTION.VoltOrigin) {
direction_up = true;
current_direction_up = true;
@@ -147,99 +149,51 @@ static uint16_t CVCurve() {
direction_up = false;
current_direction_up = false;
}
DACOutCode = Usercode_Correction_to_DAC(DACUserCode);
DAC_outputV(DACOutCode); // output VOLT_ORIGIN
DACReset = false;
return DACOutCode;
ramp0 = (uint8_t)(INSTRUCTION.VoltOrigin & 0x00FF); // right byte
ramp1 = (uint8_t)((INSTRUCTION.VoltOrigin >> 8) & 0x00FF); // left byte
DACReset = false;
}
if (StepTimeCounter == INSTRUCTION.StepTime) {
// output a certain volt
DAC_outputV(outputV);
// Decide next direction
if (direction_up) {
if (DACUserCode >= INSTRUCTION.VoltFinal) {
current_direction_up = false; // problem occurs when origin == 0000 final == ffff!!!!!!
} else if (DACUserCode <= INSTRUCTION.VoltOrigin) {
current_direction_up = true;
if (INSTRUCTION.CycleNumber == 0) {
PeriodicEvent = false; // periodic event end
DACReset = true;
}
INSTRUCTION.CycleNumber--;
}
} else {
if (DACUserCode <= INSTRUCTION.VoltFinal) {
current_direction_up = true; // problem occurs when origin == 0000 final == ffff!!!!!!
} else if (DACUserCode >= INSTRUCTION.VoltOrigin) {
current_direction_up = false;
if (INSTRUCTION.CycleNumber == 0) {
PeriodicEvent = false; // periodic event end
DACReset = true;
}
INSTRUCTION.CycleNumber--;
if (direction_up) {
if (outputV >= INSTRUCTION.VoltFinal) {
current_direction_up = false; // problem occurs when origin == 0000 final == ffff!!!!!!
} else if (outputV <= INSTRUCTION.VoltOrigin) {
current_direction_up = true;
if (INSTRUCTION.CycleNumber == 0) {
PeriodicEvent = false; // periodic event end
DACReset = true;
}
INSTRUCTION.CycleNumber--;
}
// Next output voltage
if (direction_up) {
if (current_direction_up) {
// DACUserCode overflow ?
if (DACUserCode + INSTRUCTION.Step < DACUserCode) {
DACUserCode = INSTRUCTION.VoltFinal;
}
else if (DACUserCode + INSTRUCTION.Step > INSTRUCTION.VoltFinal) {
DACUserCode = INSTRUCTION.VoltFinal;
}
else {
DACUserCode = DACUserCode + INSTRUCTION.Step;
}
}
else {
// DACUserCode underflow ?
if (DACUserCode - INSTRUCTION.Step > DACUserCode || DACUserCode > 60000) {
DACUserCode = INSTRUCTION.VoltOrigin;
}
// reach Vorigin ?
else if (DACUserCode - INSTRUCTION.Step < INSTRUCTION.VoltOrigin) {
DACUserCode = INSTRUCTION.VoltOrigin;
}
else {
DACUserCode = DACUserCode - INSTRUCTION.Step;
}
} else {
if (outputV <= INSTRUCTION.VoltFinal) {
current_direction_up = true; // problem occurs when origin == 0000 final == ffff!!!!!!
} else if (outputV >= INSTRUCTION.VoltOrigin) {
current_direction_up = false;
if (INSTRUCTION.CycleNumber == 0) {
PeriodicEvent = false; // periodic event end
DACReset = true;
}
INSTRUCTION.CycleNumber--;
}
else {
if (current_direction_up) {
if (DACUserCode + INSTRUCTION.Step < DACUserCode) {
DACUserCode = INSTRUCTION.VoltOrigin;
}
else if (DACUserCode + INSTRUCTION.Step > INSTRUCTION.VoltOrigin) {
DACUserCode = INSTRUCTION.VoltOrigin;
}
else {
DACUserCode = DACUserCode + INSTRUCTION.Step;
}
}
else {
if (DACUserCode - INSTRUCTION.Step > DACUserCode || DACUserCode > 60000) {
DACUserCode = INSTRUCTION.VoltFinal;
}
else if (DACUserCode - INSTRUCTION.Step < INSTRUCTION.VoltFinal) {
DACUserCode = INSTRUCTION.VoltFinal;
}
else {
DACUserCode = DACUserCode - INSTRUCTION.Step;
}
}
}
DACOutCode = Usercode_Correction_to_DAC(DACUserCode);
DAC_outputV(DACOutCode);
}
return DACOutCode;
if (current_direction_up) {
if (outputV + INSTRUCTION.Step < outputV)
outputV = 0xffff;
else
outputV = outputV + INSTRUCTION.Step;
} else {
if (outputV - INSTRUCTION.Step > outputV)
outputV = 0x0000;
else
outputV = outputV - INSTRUCTION.Step;
}
return outputV;
}
#endif
@@ -57,9 +57,4 @@ static uint16_t DAC_outputV(uint16_t voltLV) {
#endif
static int32_t User2Real(uint16_t UserCode){
/* transfer usercode to real voltage value (mV) */
return (int32_t) ((UserCode - 25000)*2)/10;
}
#endif
@@ -29,7 +29,7 @@
*/
#define BOARD_MERCURY
#define BOARD_GENIUS
typedef struct _formula{
@@ -66,8 +66,8 @@ struct _correction{
.DAC2RealV.coeff = (-18959656),
.DAC2RealV.offset = 565743281498,
.Usercode2DAC.coeff = (-10517325),
.Usercode2DAC.offset = 561574831511,
.Usercode2DAC.coeff = (-10548714),
.Usercode2DAC.offset = 562100522714,
.Gain0Boundary[0] = 0x5F75,
.Gain0Boundary[1] = 0x5FB2,
@@ -82,11 +82,11 @@ struct _correction{
.ADC_volt.coeff = (-6259045),
.ADC_volt.offset = 150606390230,
.ADC_current[0].coeff = 30675739,
.ADC_current[0].offset = (-736666253953),
.ADC_current[0].coeff = 27661202,
.ADC_current[0].offset = (-664225386769),
.ADC_current[1].coeff = 749057318,
.ADC_current[1].offset = (-17984432358007),
.ADC_current[1].coeff = 663176124,
.ADC_current[1].offset = (-15925056526152),
.ADC_current[2].coeff = 31242587,
.ADC_current[2].offset = (-750184492407),
@@ -144,8 +144,8 @@ struct _correction{
.ADC_current[1].coeff = 658398533,
.ADC_current[1].offset = -16001498741131,
.ADC_current[2].coeff = 30908351000,
.ADC_current[2].offset = -746548614824000,
.ADC_current[2].coeff = 30908351,
.ADC_current[2].offset = -746548614824,
.DAC2RealV.coeff = (-19007867),
.DAC2RealV.offset = 646316924837,
@@ -204,8 +204,8 @@ struct _correction{
.ADC_volt.coeff = (-6236652),
.ADC_volt.offset = 101533279052,
.ADC_current[0].coeff = 31094976,
.ADC_current[0].offset = (-507114075439),
.ADC_current[0].coeff = 309083900,
.ADC_current[0].offset = (-7414775955140),
.ADC_current[1].coeff = 31218018,
.ADC_current[1].offset = (-508593562044),
@@ -283,339 +283,59 @@ struct _correction{
};
#endif
#ifdef BOARD_LITTLE_STAR
#ifdef BOARD_BAY_BAY
{
.ADC_volt.coeff = (-6224192),
.ADC_volt.offset = 101472884698,
.ADC_volt.coeff = (-6279056),
.ADC_volt.offset = 150985844279,
.ADC_current[0].coeff = 31293602,
.ADC_current[0].offset = (-510187416959),
.ADC_current[0].coeff = 31788227 ,
.ADC_current[0].offset = (-765340735866),
.ADC_current[1].coeff = 655130048,
.ADC_current[1].offset = (-10680093830418),
.ADC_current[1].coeff = 657619858,
.ADC_current[1].offset = (-15835988865283),
.ADC_current[2].coeff = 31450484,
.ADC_current[2].offset = (-512697942950),
.ADC_current[2].coeff = 31116362,
.ADC_current[2].offset = (-749402214847),
.DAC2RealV.coeff = (-18690126),
.DAC2RealV.offset = 564319610294 ,
.DAC2RealV.coeff = (-18935149),
.DAC2RealV.offset = 643063752893,
.Usercode2DAC.coeff = (-10524846),
.Usercode2DAC.offset = 561713962333,
.Usercode2DAC.coeff = (-10567567),
.Usercode2DAC.offset = 603991718526,
.Gain0Boundary[0] = 0x5E2F,
.Gain0Boundary[1] = 0x5E96,
.Gain0Boundary[0] = 0x5DE5,
.Gain0Boundary[1] = 0x5E30,
.Gain1Boundary[0] = 0x5878,
.Gain1Boundary[1] = 0x645A
};
#endif
#ifdef BOARD_517
{
.ADC_volt.coeff = (-6244769),
.ADC_volt.offset = 101714685687,
.ADC_current[0].coeff = 30919726,
.ADC_current[0].offset = (-503489101786),
.ADC_current[1].coeff = 654824495,
.ADC_current[1].offset = (-10660542778914),
.ADC_current[2].coeff = 31376265,
.ADC_current[2].offset = (-510797752348),
.DAC2RealV.coeff = (-18690126),
.DAC2RealV.offset = 564319610294 ,
.Usercode2DAC.coeff = (-10500774),
.Usercode2DAC.offset = 560779455904,
.Gain0Boundary[0] = 0x5E2F,
.Gain0Boundary[1] = 0x5E96,
.Gain1Boundary[0] = 0x5878,
.Gain1Boundary[1] = 0x645A
};
#endif
#ifdef BOARD_FISH_VET
{
.ADC_volt.coeff = (-6243954),
.ADC_volt.offset = 101956814341,
.ADC_current[0].coeff = 6208753,
.ADC_current[0].offset = (-101076436901),
.ADC_current[1].coeff = 68760643,
.ADC_current[1].offset = (-1123221851971),
.ADC_current[2].coeff = 61882330,
.ADC_current[2].offset = (-1010385966159),
.DAC2RealV.coeff = (-18690126),
.DAC2RealV.offset = 564319610294,
.Usercode2DAC.coeff = (-10517326),
.Usercode2DAC.offset = 561574831512,
.Gain0Boundary[0] = 0x5E2F,
.Gain0Boundary[1] = 0x5E96,
.Gain1Boundary[0] = 0x5878,
.Gain1Boundary[1] = 0x645A
.Gain1Boundary[0] = 0x5820,
.Gain1Boundary[1] = 0x6408
};
#endif
#ifdef BOARD_KELLY
{
.ADC_volt.coeff = (-6238112),
.ADC_volt.offset = 101628014509,
.ADC_volt.coeff = (-6279056),
.ADC_volt.offset = 150985844279,
.ADC_current[0].coeff = 6087943,
.ADC_current[0].offset = (-99768174580),
.ADC_current[0].coeff = 31788227 ,
.ADC_current[0].offset = (-765340735866),
.ADC_current[1].coeff = 68915156,
.ADC_current[1].offset = (-1121470119188),
.ADC_current[1].coeff = 657619858,
.ADC_current[1].offset = (-15835988865283),
.ADC_current[2].coeff = 61800515,
.ADC_current[2].offset = (-1006755993534),
.ADC_current[2].coeff = 31116362,
.ADC_current[2].offset = (-749402214847),
.DAC2RealV.coeff = (-18690126),
.DAC2RealV.offset = 564319610294,
.DAC2RealV.coeff = (-18935149),
.DAC2RealV.offset = 643063752893,
.Usercode2DAC.coeff = (-10528309),
.Usercode2DAC.offset = 561035476688,
.Usercode2DAC.coeff = (-10567567),
.Usercode2DAC.offset = 603991718526,
.Gain0Boundary[0] = 0x5E2F,
.Gain0Boundary[1] = 0x5E96,
.Gain0Boundary[0] = 0x5DE5,
.Gain0Boundary[1] = 0x5E30,
.Gain1Boundary[0] = 0x5878,
.Gain1Boundary[1] = 0x645A
};
#endif
#ifdef BOARD_BAY_BAY
{
.ADC_volt.coeff = (-6223734),
.ADC_volt.offset = 101647006833,
.ADC_current[0].coeff = 31039179,
.ADC_current[0].offset = (-506383432096),
.ADC_current[1].coeff = 647940355,
.ADC_current[1].offset = (-10611041889224),
.ADC_current[2].coeff = 31094976,
.ADC_current[2].offset = (-507114075439),
.DAC2RealV.coeff = (-18690126),
.DAC2RealV.offset = 564319610294,
.Usercode2DAC.coeff = (-10541677),
.Usercode2DAC.offset = 562208801371,
.Gain0Boundary[0] = 0x5E2F,
.Gain0Boundary[1] = 0x5E96,
.Gain1Boundary[0] = 0x5878,
.Gain1Boundary[1] = 0x645A
};
#endif
#ifdef BOARD_MEOWMI
{
.ADC_volt.coeff = (-6265015),
.ADC_volt.offset = 101843650153,
.ADC_current[0].coeff = 62522034,
.ADC_current[0].offset = (-1016702373525),
.ADC_current[1].coeff = 31613132,
.ADC_current[1].offset = (-514033175600),
.ADC_current[2].coeff = 565897139,
.ADC_current[2].offset = (-9201204539440),
.DAC2RealV.coeff = (-18990774),
.DAC2RealV.offset = 570886531263,
.Usercode2DAC.coeff = (-10541427),
.Usercode2DAC.offset = 562159124753,
.Gain0Boundary[0] = 0x5D96,
.Gain0Boundary[1] = 0x5DD9,
.Gain1Boundary[0] = 0x57CD,
.Gain1Boundary[1] = 0x639F
};
#endif
#ifdef BOARD_EUROPEAN
{
.ADC_volt.coeff = (-6264190),
.ADC_volt.offset = 101683809669,
.ADC_current[0].coeff = 31301451,
.ADC_current[0].offset = (-508301866021),
.ADC_current[1].coeff = 656423459,
.ADC_current[1].offset = (-10660544072862),
.ADC_current[2].coeff = 31414514,
.ADC_current[2].offset = (-510185549182),
.DAC2RealV.coeff = (-18990774),
.DAC2RealV.offset = 570886531263,
.Usercode2DAC.coeff = (-10513774),
.Usercode2DAC.offset = 559795292677,
.Gain0Boundary[0] = 0x5D96,
.Gain0Boundary[1] = 0x5DD9,
.Gain1Boundary[0] = 0x57CD,
.Gain1Boundary[1] = 0x639F
};
#endif
#ifdef BOARD_EARTH
{
.ADC_volt.coeff = (-6256660),
.ADC_volt.offset = 101658275678,
.ADC_current[0].coeff = 31271240,
.ADC_current[0].offset = (-508496329863),
.ADC_current[1].coeff = 659931818,
.ADC_current[1].offset = (-10729666444387),
.ADC_current[2].coeff = 31485559000,
.ADC_current[2].offset = (-511907957163000),
.DAC2RealV.coeff = (-19047143),
.DAC2RealV.offset = 565935714286,
.Usercode2DAC.coeff = (-10500262),
.Usercode2DAC.offset = 559630236100,
.Gain0Boundary[0] = 0x5D96,
.Gain0Boundary[1] = 0x5DD9,
.Gain1Boundary[0] = 0x57CD,
.Gain1Boundary[1] = 0x639F
};
#endif
#ifdef BOARD_WATER_STAR
{
.ADC_volt.coeff = (-6259808),
.ADC_volt.offset = 102009860128,
.ADC_current[0].coeff = 31335917,
.ADC_current[0].offset = (-511426612252),
.ADC_current[1].coeff = 658172815,
.ADC_current[1].offset = (-10738251896209),
.ADC_current[2].coeff = 31482687000,
.ADC_current[2].offset = (-513650531545000),
.DAC2RealV.coeff = (-10548297),
.DAC2RealV.offset = 562611756757,
.Usercode2DAC.coeff = (-10500262),
.Usercode2DAC.offset = 559630236100,
.Gain0Boundary[0] = 0x5D96,
.Gain0Boundary[1] = 0x5DD9,
.Gain1Boundary[0] = 0x57CD,
.Gain1Boundary[1] = 0x639F
};
#endif
#ifdef BOARD_MARS
{
.ADC_volt.coeff = (-6270623),
.ADC_volt.offset = 102383421553,
.ADC_current[0].coeff = 31187022,
.ADC_current[0].offset = (-509159321195),
.ADC_current[1].coeff = 655981611,
.ADC_current[1].offset = (-10709717111320),
.ADC_current[2].coeff = 31256968,
.ADC_current[2].offset = (-510275213115),
.DAC2RealV.coeff = (-18937347),
.DAC2RealV.offset = 568558163265,
.Usercode2DAC.coeff = (-10561141),
.Usercode2DAC.offset = 564249134291,
.Gain0Boundary[0] = 0x5D96,
.Gain0Boundary[1] = 0x5DD9,
.Gain1Boundary[0] = 0x57CD,
.Gain1Boundary[1] = 0x639F
};
#endif
#ifdef BOARD_VENUS
{
.ADC_volt.coeff = (-6268996),
.ADC_volt.offset = 102204055818,
.ADC_current[0].coeff = 31131930,
.ADC_current[0].offset = (-507382432547),
.ADC_current[1].coeff = 654620883,
.ADC_current[1].offset = (-10668953588943),
.ADC_current[2].coeff = 31245260000,
.ADC_current[2].offset = (-509181085054000),
.DAC2RealV.coeff = (-19009388),
.DAC2RealV.offset = 567032653061,
.Usercode2DAC.coeff = (-10521117),
.Usercode2DAC.offset = 561308254899,
.Gain0Boundary[0] = 0x5D96,
.Gain0Boundary[1] = 0x5DD9,
.Gain1Boundary[0] = 0x57CD,
.Gain1Boundary[1] = 0x639F
};
#endif
#ifdef BOARD_MERCURY
{
.ADC_volt.coeff = (-6259808),
.ADC_volt.offset = 102009860128,
.ADC_current[0].coeff = 31335917,
.ADC_current[0].offset = (-511426612252),
.ADC_current[1].coeff = 658172815,
.ADC_current[1].offset = (-10738251896209),
.ADC_current[2].coeff = 31482687000,
.ADC_current[2].offset = (-513650531545000),
.DAC2RealV.coeff = (-19009388),
.DAC2RealV.offset = 567032653061,
.Usercode2DAC.coeff = (-10548297),
.Usercode2DAC.offset = 562611756757,
.Gain0Boundary[0] = 0x5D96,
.Gain0Boundary[1] = 0x5DD9,
.Gain1Boundary[0] = 0x57CD,
.Gain1Boundary[1] = 0x639F
.Gain1Boundary[0] = 0x5820,
.Gain1Boundary[1] = 0x6408
};
#endif
@@ -643,35 +363,34 @@ static int32_t DecodeADCCurrent(uint8_t ADCGain, uint16_t ADC_measure){
}
static int32_t DecodeResister(uint8_t ADCGainLevel, uint16_t CurrentMeasure, uint16_t VoltMeasure){
long long ADCRealCurrent=0, ADCRealVolt=0;
int32_t resister_32;
long long ADCRealResister = 0, ADCRealCurrent=0, ADCRealVolt=0;
int32_t current_32, volt_32, resister_32;
// get measure current
ADCRealCurrent = (Correction.ADC_current[ADCGainLevel].coeff * CurrentMeasure + Correction.ADC_current[ADCGainLevel].offset)/1e7;
current_32 = (int32_t) (ADCRealCurrent);
// get measure volt
// This step is necessary, if the measure resister !>> 10 ohm
ADCRealVolt = (Correction.ADC_volt.coeff * VoltMeasure + Correction.ADC_volt.offset);
ADCRealVolt = ADCRealVolt / 1e4;
volt_32 = (int32_t) (ADCRealVolt);
// if (INSTRUCTION.ADCGainLevel == GAIN_200R){
resister_32 = (int32_t) ((ADCRealVolt) / (ADCRealCurrent/1e3)); // nV / uA = mV
// }
// else{
// resister_32 = (int32_t) ((ADCRealVolt) / (ADCRealCurrent/1e6)); // nV / uA = mV
// }
int32_t volt_32 = (int32_t) (ADCRealVolt);
int32_t current_32 = (int32_t) (ADCRealCurrent);
NotifyVolt[0] = (uint8_t) (volt_32 >> 24);
NotifyVolt[1] = (uint8_t) ((volt_32 & 0x00FF0000) >> 16);
NotifyVolt[2] = (uint8_t) ((volt_32 & 0x0000FF00) >> 8);
NotifyVolt[3] = (uint8_t) (volt_32 & 0x000000FF);
NotifyCurrent[0] = (uint8_t) (current_32 >> 24);
NotifyCurrent[1] = (uint8_t) ((current_32 & 0x00FF0000) >> 16);
NotifyCurrent[2] = (uint8_t) ((current_32 & 0x0000FF00) >> 8);
NotifyCurrent[3] = (uint8_t) (current_32 & 0x000000FF);
if (INSTRUCTION.ADCGainLevel == GAIN_200R){
resister_32 = (int32_t) ((ADCRealVolt) / (ADCRealCurrent/1e3)); // nV / uA = mV
}
else{
resister_32 = (int32_t) ((ADCRealVolt) / (ADCRealCurrent/1e6)); // nV / uA = mV
}
// NotifyVolt[0] = (uint8_t) (volt_32 >> 24);
// NotifyVolt[1] = (uint8_t) ((volt_32 & 0x00FF0000) >> 16);
// NotifyVolt[2] = (uint8_t) ((volt_32 & 0x0000FF00) >> 8);
// NotifyVolt[3] = (uint8_t) (volt_32 & 0x000000FF);
//
// NotifyCurrent[0] = (uint8_t) (current_32 >> 24);
// NotifyCurrent[1] = (uint8_t) ((current_32 & 0x00FF0000) >> 16);
// NotifyCurrent[2] = (uint8_t) ((current_32 & 0x0000FF00) >> 8);
// NotifyCurrent[3] = (uint8_t) (current_32 & 0x000000FF);
NotifyImpedance[0] = (uint8_t) (resister_32 >> 24);
NotifyImpedance[1] = (uint8_t) ((resister_32 & 0x00FF0000) >> 16);
@@ -685,7 +404,7 @@ static int32_t DecodeResister(uint8_t ADCGainLevel, uint16_t CurrentMeasure, uin
static int32_t DecodeADCValue(uint8_t ADCGain, uint8_t ADCChannel, uint8_t *ADC_raw){
uint16_t ADC_measure = (uint16_t) (ADC_raw[0] << 8) | (uint16_t) (ADC_raw[1]);
int32_t ADCRealVolt = 0, ret = 0, ADCRealCurrent = 0;
int32_t ADCRealVolt = 0, ret = 0, ADCRealCurrent = 0, ADCRealResister = 0;
// return real volt to controller
if(ADCChannel == ADC_CH_VOLT){
@@ -700,20 +419,12 @@ static int32_t DecodeADCValue(uint8_t ADCGain, uint8_t ADCChannel, uint8_t *ADC_
// return real current to controller
else if(ADCChannel == ADC_CH_CURRENT){
if ( (INSTRUCTION.eliteFxn == IV_CURVE) || (INSTRUCTION.eliteFxn == CV_CURVE)) {
// wait 0.1 sec until circuit stable => discard first data means wait 0.1 sec
if(DiscardIVFirstData){
DiscardIVFirstData = 0;
return 0;
}
if (INSTRUCTION.eliteFxn == IV_CURVE) {
// return a real time current (used for deciding auto gain)
ret = DecodeADCCurrent(ADCGain, ADC_measure);
ADCRealCurrent_long = ADCRealCurrent_long + ret;
avg_number ++;
ADCRealCurrent_long += DecodeADCCurrent(ADCGain, ADC_measure);
avg_number++;
if (StepTimeCounter == INSTRUCTION.StepTime - 1) {
DiscardIVFirstData = 1;
if (StepTimeCounter == INSTRUCTION.StepTime) {
ADCRealCurrent_long = ADCRealCurrent_long / avg_number;
NotifyCurrent[0] = (uint8_t) (ADCRealCurrent_long >> 24);
NotifyCurrent[1] = (uint8_t) ((ADCRealCurrent_long & 0x00FF0000) >> 16);
@@ -723,8 +434,6 @@ static int32_t DecodeADCValue(uint8_t ADCGain, uint8_t ADCChannel, uint8_t *ADC_
ADCRealCurrent_long = 0;
}
}
// IT curve
else {
ADCRealCurrent = DecodeADCCurrent(ADCGain, ADC_measure);
NotifyCurrent[0] = (uint8_t) (ADCRealCurrent >> 24);
@@ -2,35 +2,20 @@
#ifndef ELITEIT
#define ELITEIT
//static int32_t IT_Plot() {
// // read ADC current
// int32_t Real_Current = 0;
// ADCGainControl(INSTRUCTION.ADCGainLevel);
// ADCChannelSelect(ADC_CH_CURRENT);
// CPUdelay(10);
// ADC_read(spi_ADC_rxbuf);
//
// // check if ADC over/under flow
// // let the output saturate if over/under flow
//// ADC_overflow(INSTRUCTION.ADCGainLevel, spi_ADC_rxbuf);
//
// // decode ADC value and put it into notify buffer
// Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
//
// return Real_Current;
//}
static int32_t IT_Plot() {
static int32_t IT_Plot() {
// read ADC current
int32_t Real_Current = 0;
ADCGainControl(INSTRUCTION.ADCGainLevel);
ADCChannelSelect(ADC_CH_CURRENT);
CPUdelay(10);
ADC_read(spi_ADC_rxbuf);
if(INSTRUCTION.AutoGainEnable){
Real_Current = AutoGainReadCurrent(spi_ADC_rxbuf);
}
else{
ReadCurrent(spi_ADC_rxbuf);
Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
}
// check if ADC over/under flow
// let the output saturate if over/under flow
// ADC_overflow(INSTRUCTION.ADCGainLevel, spi_ADC_rxbuf);
// decode ADC value and put it into notify buffer
Real_Current = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
return Real_Current;
}
@@ -13,7 +13,7 @@ static uint16_t VoltScan() {
Voltage = SWVCurve();
} else if (INSTRUCTION.eliteFxn == DIFFERENTIAL_PULSE_VOLTAMMETRY) {
Voltage = DPVCurve();
} else if (INSTRUCTION.eliteFxn == CV_CURVE) {
} else if (INSTRUCTION.eliteFxn == CYCLIC_VOLTAMMETRY) {
Voltage = CVCurve();
}
@@ -52,22 +52,8 @@ static uint16_t OneWayVoltScan() {
DACReset = true;
}
} else {
DACUserCode = DACUserCode - INSTRUCTION.Step;
// check if DACUserCode underflow
if(DACUserCode >= 60000){
// LED_color(DARKLED, 0xFF, 0x00, 0x00);
DACUserCode = INSTRUCTION.VoltFinal;
}
// int32_t DACUC = DACUserCode;
// NotifyImpedance[0] = (uint8_t) (DACUC >> 24);
// NotifyImpedance[1] = (uint8_t) ((DACUC & 0x00FF0000) >> 16);
// NotifyImpedance[2] = (uint8_t) ((DACUC & 0x0000FF00) >> 8);
// NotifyImpedance[3] = (uint8_t) (DACUC & 0x000000FF);
// output the next output volt
DACUserCode = DACUserCode - INSTRUCTION.Step;
DACOutCode = Usercode_Correction_to_DAC(DACUserCode);
DAC_outputV(DACOutCode);
@@ -30,24 +30,23 @@ struct HEADSTAGE_INSTRUCTION {
/** Sample rate **/
// SampleRate = SampleRateTable[SampleRateIndex]
uint8_t SampleRateIndex;
uint32_t SampleRate;
uint16_t SampleRate;
/** DAC parameter **/
// volt san parameter
uint16_t VoltOrigin;
uint16_t VoltFinal;
uint16_t Step;
uint16_t StepTime;
uint8_t StepTime;
// constant volt
uint16_t VoltConstant;
/** ADC parameter **/
uint8_t ADCGainLevel;
uint8_t AutoGainEnable;
/** Constant Current Parameter **/
int32_t ConstantCurrent;
uint8_t CurrentLV; // nA? uA? mA?
uint32_t ConstantCurrent;
/** Resister Measure **/
uint8_t ResisterMeter;
@@ -76,10 +75,10 @@ static void InitEliteInstruction(){
INSTRUCTION.VoltFinal = DAC_POS_MAX;
INSTRUCTION.Step = 0x0005; // 0x0005 = 1mV
INSTRUCTION.StepTime = STEPTIME_HALF_SEC; // about 0.5 sec
INSTRUCTION.VoltConstant = 25000; // is about 0V
INSTRUCTION.ADCGainLevel = GAIN_AUTO;
INSTRUCTION.AutoGainEnable = 1;
INSTRUCTION.ResisterMeter = RESISTER_METER_LARGE;
INSTRUCTION.VoltConstant = 24999; // is about 0V
INSTRUCTION.ADCGainLevel = GAIN_200R;
INSTRUCTION.ResisterMeter = RESISTER_METER_SMALL;
INSTRUCTION.CurrentLV = 0x00;
INSTRUCTION.ConstantCurrent = 0x00000000;
INSTRUCTION.eliteFxn = 0; // default is a null event
INSTRUCTION.CycleNumber = 0;
@@ -98,7 +97,7 @@ static void GetInstructionParameter(uint8 *ins){
// CurrentLV=0 => unit is nA
// CurrentLV=1 => unit is uA
// CurrentLV=2 => unit is mA
// INSTRUCTION.CurrentLV = (*ins);
INSTRUCTION.CurrentLV = (*ins);
// ConstantCurrentRange=0 => current value is 0~499
// ConstantCurrentRange=1 => current value is 500~999
@@ -29,7 +29,7 @@ static void WorkModeLED() {
WORKLED();
break;
}
case CV_CURVE: {
case CYCLIC_VOLTAMMETRY: {
WORKLED();
break;
}
@@ -50,17 +50,14 @@ static void WorkModeLED() {
break;
}
case VT_CURVE: {
WORKLED();
// WORKLED();
break;
}
case IT_CURVE: {
WORKLED();
break;
}
case CONSTANT_CURRENT:{
WORKLED();
break;
}
case VIS_RST: {
LEDPowerON();
break;
@@ -85,7 +82,7 @@ static void KeyWorkModeLED() {
LED_color(LIGHTLED, 0xF0, 0xF0, 0x00);
break;
}
case CV_CURVE:{
case CYCLIC_VOLTAMMETRY:{
LED_color(LIGHTLED, 0xF0, 0xF0, 0x00);
break;
}
@@ -5,16 +5,12 @@
static void reset() {
PeriodicEvent = false;
DACReset = true;
CCModeDACEnable = 0;
CCModeReset = 1;
InitEliteInstruction();
SampleRate_counter = 1;
StepTimeCounter = 1;
DiscardIVFirstData = 1;
avg_number = 0;
ADCRealCurrent_long = 0;
ADCGainControl(INSTRUCTION.ADCGainLevel);
DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
if (INSTRUCTION.eliteFxn == CONSTANT_CURRENT){
INSTRUCTION.eliteFxn = 0;
@@ -52,15 +48,12 @@ static void reset() {
static void Eliteinterrupt() {
PeriodicEvent = false;
DACReset = true;
CCModeDACEnable = 0;
CCModeReset = 1;
InitEliteInstruction();
StepTimeCounter = 1;
SampleRate_counter = 1;
DiscardIVFirstData = 1;
avg_number = 0;
ADCRealCurrent_long = 0;
ADCGainControl(INSTRUCTION.ADCGainLevel);
DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
LEDPowerON();
for (int i = 0; i < BLE_INS_BUFF_SIZE; i++) {
@@ -94,11 +87,10 @@ static void Eliteinterrupt() {
static void CleanBuffer() {
PeriodicEvent = false;
DACReset = true;
CCModeDACEnable = 0;
CCModeReset = 1;
// InitEliteInstruction();
SampleRate_counter = 1;
StepTimeCounter = 1;
DiscardIVFirstData = 1;
avg_number = 0;
ADCRealCurrent_long = 0;
@@ -7,7 +7,9 @@ static void VT_Plot() {
uint8_t ADCGain = 0;
// read ADC volt
ReadVolt(spi_ADC_rxbuf);
ADCChannelSelect(ADC_CH_VOLT);
CPUdelay(10);
ADC_read(spi_ADC_rxbuf);
// decode ADC value and put it into notify buffer
DecodeADCValue(ADCGain, ADC_CH_VOLT, spi_ADC_rxbuf);
@@ -10,97 +10,61 @@ static void ZT_notify(int32_t impedance);
// change the output voltage step
// => get a R-T curve (with resolution = 1 sample/volt step )
static void ZT_Plot() {
// int32_t Real_Resister = 0;
int32_t Real_Resister = 0;
static uint16_t CurrentMeasure=0, VoltMeasure=0;
uint8_t SPICurrent[SPI_ADC_SIZE]={0}, SPIVolt[SPI_ADC_SIZE]={0};
static uint8_t VoltCurrentSwitch = 0;
int32_t volt_32 = 0;
int32_t current_32 = 0;
int32_t resister_32 = 0;
if(INSTRUCTION.AutoGainEnable){
current_32 = AutoGainReadCurrent(SPICurrent);
// set ADC GAIN
if(INSTRUCTION.ResisterMeter == RESISTER_METER_SMALL){
INSTRUCTION.ADCGainLevel = GAIN_200R;
}
else if(INSTRUCTION.ResisterMeter == RESISTER_METER_MIDDLE1){
INSTRUCTION.ADCGainLevel = GAIN_200R;
}
else if(INSTRUCTION.ResisterMeter == RESISTER_METER_MIDDLE2){
INSTRUCTION.ADCGainLevel = GAIN_10K;
}
else{
ReadCurrent(spi_ADC_rxbuf);
current_32 = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
INSTRUCTION.ADCGainLevel = GAIN_200K;
}
ADCGainControl(INSTRUCTION.ADCGainLevel);
if(VoltCurrentSwitch < 9){
ADCChannelSelect(ADC_CH_CURRENT);
CPUdelay(10);
ADC_read(SPICurrent);
VoltCurrentSwitch ++;
}
else if(VoltCurrentSwitch == 9){
// read current
ADCChannelSelect(ADC_CH_CURRENT);
CPUdelay(10);
ADC_read(SPICurrent);
CurrentMeasure = (uint16_t) (SPICurrent[0] << 8) | (uint16_t) (SPICurrent[1]);
VoltCurrentSwitch ++;
}
else if(VoltCurrentSwitch <18){
// read volt
ADCChannelSelect(ADC_CH_VOLT);
CPUdelay(10);
ADC_read(SPIVolt);
VoltCurrentSwitch++;
}
else if(VoltCurrentSwitch == 18){
// read volt
ADCChannelSelect(ADC_CH_VOLT);
CPUdelay(10);
ADC_read(SPIVolt);
VoltMeasure = (uint16_t) (SPIVolt[0] << 8) | (uint16_t) (SPIVolt[1]);
VoltCurrentSwitch++;
}
else{
VoltCurrentSwitch = 0;
}
volt_32 = User2Real(INSTRUCTION.VoltConstant)*1e4;
// ReadVolt(SPIVolt);
// VoltMeasure = (uint16_t) (SPIVolt[0] << 8) | (uint16_t) (SPIVolt[1]);
// volt_32 = DecodeADCVolt(VoltMeasure)*1e4;
resister_32 = volt_32 / current_32;
NotifyVolt[0] = (uint8_t) (volt_32 >> 24);
NotifyVolt[1] = (uint8_t) ((volt_32 & 0x00FF0000) >> 16);
NotifyVolt[2] = (uint8_t) ((volt_32 & 0x0000FF00) >> 8);
NotifyVolt[3] = (uint8_t) (volt_32 & 0x000000FF);
NotifyCurrent[0] = (uint8_t) (current_32 >> 24);
NotifyCurrent[1] = (uint8_t) ((current_32 & 0x00FF0000) >> 16);
NotifyCurrent[2] = (uint8_t) ((current_32 & 0x0000FF00) >> 8);
NotifyCurrent[3] = (uint8_t) (current_32 & 0x000000FF);
NotifyImpedance[0] = (uint8_t) (resister_32 >> 24);
NotifyImpedance[1] = (uint8_t) ((resister_32 & 0x00FF0000) >> 16);
NotifyImpedance[2] = (uint8_t) ((resister_32 & 0x0000FF00) >> 8);
NotifyImpedance[3] = (uint8_t) (resister_32 & 0x000000FF);
// set ADC GAIN
// if(INSTRUCTION.ResisterMeter == RESISTER_METER_LARGE){
// INSTRUCTION.ADCGainLevel = GAIN_200R;
// }
// else if(INSTRUCTION.ResisterMeter == RESISTER_METER_MIDDLE2){
// INSTRUCTION.ADCGainLevel = GAIN_200R;
// }
// else if(INSTRUCTION.ResisterMeter == RESISTER_METER_MIDDLE1){
// INSTRUCTION.ADCGainLevel = GAIN_10K;
// }
// else{
// INSTRUCTION.ADCGainLevel = GAIN_200K;
// }
// ADCGainControl(INSTRUCTION.ADCGainLevel);
// Use 9-th measure value as real-measure value
// because some value in the begin are garbage
// if(VoltCurrentSwitch < 9){
// ADCChannelSelect(ADC_CH_CURRENT);
// CPUdelay(10);
// ADC_read(SPICurrent);
// VoltCurrentSwitch ++;
// }
// else if(VoltCurrentSwitch == 9){
// // read current
// ADCChannelSelect(ADC_CH_CURRENT);
// CPUdelay(10);
// ADC_read(SPICurrent);
// CurrentMeasure = (uint16_t) (SPICurrent[0] << 8) | (uint16_t) (SPICurrent[1]);
// VoltCurrentSwitch ++;
// }
// else if(VoltCurrentSwitch <18){
// // read volt
// ADCChannelSelect(ADC_CH_VOLT);
// CPUdelay(10);
// ADC_read(SPIVolt);
// VoltCurrentSwitch++;
// }
// else if(VoltCurrentSwitch == 18){
// // read volt
// ADCChannelSelect(ADC_CH_VOLT);
// CPUdelay(10);
// ADC_read(SPIVolt);
// VoltMeasure = (uint16_t) (SPIVolt[0] << 8) | (uint16_t) (SPIVolt[1]);
// VoltCurrentSwitch++;
// }
// else{
// VoltCurrentSwitch = 0;
// }
// decode ADC value and put it into notify buffer
// DecodeResister(INSTRUCTION.ADCGainLevel, CurrentMeasure, VoltMeasure);
DecodeResister(INSTRUCTION.ADCGainLevel, CurrentMeasure, VoltMeasure);
// Real_Resister = DecodeADCValue(INSTRUCTION.ADCGainLevel, ADC_CH_CURRENT, spi_ADC_rxbuf);
}
@@ -190,14 +190,14 @@ MUX
PGA
programmable gain amplifier configuration
(Full Scale Range = FSR)
000 = FSR is 6.144 V
001 = FSR is 4.096 V
010 = FSR is 2.048 V (default)
011 = FSR is 1.024 V
100 = FSR is 0.512 V
101 = FSR is 0.256 V
110 = FSR is 0.256 V
111 = FSR is 0.256 V
000 = FSR is 6.144 V
001 = FSR is 4.096 V
010 = FSR is 2.048 V (default)
011 = FSR is 1.024 V
100 = FSR is 0.512 V
101 = FSR is 0.256 V
110 = FSR is 0.256 V
111 = FSR is 0.256 V
M
ADC operating mode
@@ -579,7 +579,7 @@ static void set_update_instruction_callback(update_instruction_callback_type cal
// real instruction
#define IV_CURVE 0b00010000
#define CV_CURVE 0b00100000
#define CYCLIC_VOLTAMMETRY 0b00100000
#define VOLT_OUTPUT 0b00110000
#define ZT_CURVE 0b01000000
#define VT_CURVE 0b01010000
@@ -607,12 +607,11 @@ static int32_t DAC_to_realV(uint16_t DACcode);
#define DAC_NEG_MAX 0xFFFF
static uint16_t DACUserCode = 0x0000;
static uint32_t SampleRateTable[6] = {100, 1000, 10000, 50000, 100000, 1000000}; // 1 =>100 Hz, 10000=>0.01 Hz
static uint32_t SampleRate_counter = 1;
static uint32_t SampleRateTable[6] = {10, 100, 1000, 5000, 10000, 100000}; // 1 =>100 Hz, 10000=>0.01 Hz
static uint16_t SampleRate_counter = 1;
// record value for IV curve to calculate average current
static uint8_t DiscardIVFirstData = 1;
static uint16_t avg_number = 0;
static int16_t avg_number = 0;
static long long ADCRealCurrent_long = 0;
// Step time macro
@@ -621,7 +620,7 @@ static long long ADCRealCurrent_long = 0;
#define STEPTIME_TWO_SEC 20000
// Constant Current Mode function
static uint8_t CCModeDACEnable = 0;
static uint8_t CCModeReset = 1;
static int32_t CCModeReadCurrent();
static int32_t CCModeVoltOut();
static void SetCCModeGain();
@@ -636,7 +635,8 @@ static uint16_t PulseWidth_16;
static uint8_t PulsePeriod;
static uint16_t PulsePeriod_16;
static uint16_t StepTimeCounter = 1;
static uint16_t StepTime_16 = 0;
static uint8_t StepTimeCounter = 1;
// real instruction fxn
static uint16_t VoltScan(); // used in I-V and cyclic
@@ -700,7 +700,7 @@ static void update_ZM_instruction(uint8 *ins) {
INSTRUCTION.chip_id = chip_ID;
uint8_t oper = ins[1] & 0xF0; // this is don't care in RIS
// uint8_t data_length = ins[1] & 0x0F;
uint8_t data_length = ins[1] & 0x0F;
if (!If10Von) {
// TurnOn10V();
@@ -711,10 +711,10 @@ static void update_ZM_instruction(uint8 *ins) {
case INS_TYPE_RIS: {
switch (ins[2]) {
case IV_CURVE: {
// CleanBuffer();
CleanBuffer();
INSTRUCTION.eliteFxn = IV_CURVE;
DACReset = true;
INSTRUCTION.SampleRate = 1000;
INSTRUCTION.SampleRate = 10;
if (ins[3] | ins[4]) {
INSTRUCTION.VoltOrigin = ((uint16_t)(ins[3]) << 8) | (uint16_t)(ins[4]);
@@ -802,19 +802,18 @@ static void update_ZM_instruction(uint8 *ins) {
break;
}
case CV_CURVE: {
// CleanBuffer();
INSTRUCTION.eliteFxn = CV_CURVE;
case CYCLIC_VOLTAMMETRY: {
CleanBuffer();
INSTRUCTION.eliteFxn = CYCLIC_VOLTAMMETRY;
DACReset = true;
INSTRUCTION.SampleRate = 1000;
if (ins[3] | ins[4]) {
INSTRUCTION.VoltOrigin = ((uint16_t)(ins[3]) << 8) | (uint16_t)(ins[4]);
// INSTRUCTION.VoltOrigin = Usercode_Correction_to_DAC(INSTRUCTION.VoltOrigin);
INSTRUCTION.VoltOrigin = Usercode_Correction_to_DAC(INSTRUCTION.VoltOrigin);
}
if (ins[5] | ins[6]) {
INSTRUCTION.VoltFinal = ((uint16_t)(ins[5]) << 8) | (uint16_t)(ins[6]);
// INSTRUCTION.VoltFinal = Usercode_Correction_to_DAC(INSTRUCTION.VoltFinal);
INSTRUCTION.VoltFinal = Usercode_Correction_to_DAC(INSTRUCTION.VoltFinal);
}
if (ins[7] | ins[8]) {
@@ -832,25 +831,24 @@ static void update_ZM_instruction(uint8 *ins) {
break;
}
case VOLT_OUTPUT: {
INSTRUCTION.eliteFxn = VOLT_OUTPUT;
INSTRUCTION.VoltConstant = ( ((uint16_t)(ins[3])) << 8) | (uint16_t)(ins[4]);
DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
// DAC_outputV(INSTRUCTION.VoltConstant);
// DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
DAC_outputV(INSTRUCTION.VoltConstant);
break;
}
// impedance test
case ZT_CURVE: {
// CleanBuffer();
CleanBuffer();
INSTRUCTION.eliteFxn = ZT_CURVE;
// INSTRUCTION.VoltConstant = ( ((uint16_t)(ins[3])) << 8) | (uint16_t)(ins[4]);
break;
}
case VT_CURVE: {
// CleanBuffer();
CleanBuffer();
INSTRUCTION.eliteFxn = VT_CURVE;
// SimpleProfile_SetParameter(BLE_DAT_BUFF_CHAR, BLE_DAT_BUFF_SIZE, not_buf);
// VT_Plot(); // enable 10v = 0
@@ -858,7 +856,7 @@ static void update_ZM_instruction(uint8 *ins) {
}
case IT_CURVE: {
// CleanBuffer();
CleanBuffer();
INSTRUCTION.eliteFxn = IT_CURVE;
// IT_Plot(); // enable 10v = 1
break;
@@ -883,8 +881,8 @@ static void update_ZM_instruction(uint8 *ins) {
case CONSTANT_CURRENT:{
INSTRUCTION.eliteFxn = CONSTANT_CURRENT;
INSTRUCTION.SampleRate = 2;
INSTRUCTION.ConstantCurrent = ( (uint32_t) (ins[3])<<24 | (uint32_t) (ins[4])<<16 | (uint32_t) (ins[5])<<8 | (uint32_t) (ins[6]) );
INSTRUCTION.CurrentLV = ins[3];
INSTRUCTION.ConstantCurrent = ( (uint32_t) (ins[4])<<24 | (uint32_t) (ins[5])<<16 | (uint32_t) (ins[6])<<8 | (uint32_t) (ins[7]) );
// GetInstructionParameter(ins+2);
// CCCurrent2IUC();
break;
@@ -892,21 +890,6 @@ static void update_ZM_instruction(uint8 *ins) {
case SET_ADC_GAIN: {
INSTRUCTION.ADCGainLevel = ins[3];
if(INSTRUCTION.ADCGainLevel != GAIN_AUTO){
INSTRUCTION.AutoGainEnable = 0;
}
else{
INSTRUCTION.AutoGainEnable = 1;
}
// if(INSTRUCTION.ADCGainLevel == GAIN_200R){
// LED_color(DARKLED, 0x0F, 0x00, 0x00);
// }
// else if(INSTRUCTION.ADCGainLevel == GAIN_10K){
// LED_color(DARKLED, 0x0F, 0x00, 0x0F);
// }
// else if(INSTRUCTION.ADCGainLevel == GAIN_200K){
// LED_color(DARKLED, 0x0F, 0x02, 0xFF);
// }
break;
}
@@ -920,27 +903,15 @@ static void update_ZM_instruction(uint8 *ins) {
int32_t ADCRealValue = 0;
uint8_t CIS_buf[9] = {0};
// for(int i=0 ; i<10 ; i++){
ADCGainControl(ins[3]);
ADCChannelSelect(ins[4]);
CPUdelay(10);
ADC_read(spi_ADC_rxbuf);
// CPUdelay(10);
//
// ADCValueTemp = ( uint16_t) (spi_ADC_rxbuf[0]) << 8 | (uint16_t) (spi_ADC_rxbuf[1]);
// ADCValueAVG = ADCValueAVG + ADCValueTemp;
// }
// ADCValueAVG = ADCValueAVG / 10;
// ADCValueTemp = (uint16_t) (ADCValueAVG);
ADCGainControl(ins[3]);
ADCChannelSelect(ins[4]);
CPUdelay(1600);
ADC_read(spi_ADC_rxbuf);
CIS_buf[0] = chip_ID;
for(int i=0; i<4 ; i++){
CIS_buf[i+1] = spi_ADC_rxbuf[i];
for (int i = 0; i < 4; i++) {
CIS_buf[i + 1] = spi_ADC_rxbuf[i];
}
// CIS_buf[1] = (uint8_t) ((ADCValueTemp & 0xFF00) >> 8);
// CIS_buf[2] = (uint8_t) (ADCValueTemp & 0x00FF);
// CIS_buf[3] = spi_ADC_rxbuf[2];
// CIS_buf[4] = spi_ADC_rxbuf[3];
// decode ADC measure value
ADCRealValue = DecodeADCValue(ins[3], ins[4], spi_ADC_rxbuf);
@@ -1,7 +1,7 @@
/*
* impedance_meter.h
*
* Created on: 2019/01/15
* Created on: 2019~115
* Author: benny
*/
#ifndef HEADSTAGE_H
@@ -20,7 +20,7 @@
#include <ti/drivers/PIN.h>
#include "board.h"
static void SimpleBLEPeripheral_performPeriodicTask(void *WorkModeData);
static void SimpleBLEPeripheral_performPeriodicTask(CURRENT_USER_CODE *CurrentUserCode);
static void SimpleBLEPeripheral_clockHandler(UArg arg) {
// Store the event.
@@ -51,7 +51,6 @@ static void ZM_init() {
PIN_setOutputValue(pin_handle, DAC_CS, 1); // DAC_CS HIGH
InitEliteInstruction();
ADCGainControl(GAIN_AUTO);
elite_gptimer_open();
// PIN_registerIntCb(pin_handle, switch_on_callback);
@@ -73,7 +72,6 @@ static void DACCode2Real2Notify(uint16_t DACcode) {
#define IsPeriodicMode() ( \
(INSTRUCTION.eliteFxn == IV_CURVE) || \
(INSTRUCTION.eliteFxn == CV_CURVE) || \
(INSTRUCTION.eliteFxn == IT_CURVE) || \
(INSTRUCTION.eliteFxn == VT_CURVE) || \
(INSTRUCTION.eliteFxn == ZT_CURVE) || \
@@ -89,9 +87,10 @@ static void DACCode2Real2Notify(uint16_t DACcode) {
*
* @return None.
*/
static void SimpleBLEPeripheral_performPeriodicTask(void *WorkModeData) {
static void SimpleBLEPeripheral_performPeriodicTask(CURRENT_USER_CODE *CurrentUserCode) {
if ( IsPeriodicMode() ){
// XXX Using nwe clock => StepTime/SampleRate should change
if (StepTimeCounter == INSTRUCTION.StepTime){
StepTimeCounter = 1;
}
@@ -113,45 +112,36 @@ static void SimpleBLEPeripheral_performPeriodicTask(void *WorkModeData) {
// In IV, CV, and func-gen mode, DAC will output voltage
// else DAC do nothing.
EliteDACControl(WorkModeData);
EliteDACControl();
// Control ADC to sample rate
EliteADCControl(WorkModeData);
EliteADCControl(CurrentUserCode);
// Notify control, check if we need to send notify
EliteNotifyControl();
}
}
static void EliteDACControl(void *WorkModeData) {
if ((INSTRUCTION.eliteFxn == IV_CURVE) || (INSTRUCTION.eliteFxn == CV_CURVE)) {
static void EliteDACControl(CURRENT_USER_CODE *CurrentUserCode) {
if (INSTRUCTION.eliteFxn == IV_CURVE) {
// output a certain voltage and put it into NotifyVolt
DACCode2Real2Notify(VoltScan());
}
else if (INSTRUCTION.eliteFxn == ZT_CURVE){
if(INSTRUCTION.ResisterMeter == RESISTER_METER_SMALL){
// output 1V
if (DACReset) {
INSTRUCTION.VoltConstant = 25000 + 5000;
DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
DACReset = false;
}
// output 5mV
INSTRUCTION.VoltConstant = 24999 + 50;
DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
}
else{
// output 1V
if (DACReset) {
INSTRUCTION.VoltConstant = 25000 + 5000;
DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
DACReset = false;
}
// output 100mV
INSTRUCTION.VoltConstant = 24999 + 500;
DAC_outputV(Usercode_Correction_to_DAC(INSTRUCTION.VoltConstant));
}
}
else if(INSTRUCTION.eliteFxn == CONSTANT_CURRENT){
if (DACReset) {
DAC_outputV(Usercode_Correction_to_DAC(25000));
DACReset = false;
}
CCModeVoltOut(WorkModeData);
CCModeVoltOut(CurrentUserCode);
// DAC_outputV(Usercode_Correction_to_DAC(CurrentUserCode->value));
}
else{
@@ -160,17 +150,13 @@ static void EliteDACControl(void *WorkModeData) {
}
}
static void EliteADCControl(uint32_t **WorkModeData) {
if (SampleRate_counter == INSTRUCTION.SampleRate-1) {
static void EliteADCControl(CURRENT_USER_CODE *CurrentUserCode) {
if (SampleRate_counter == INSTRUCTION.SampleRate) {
switch (INSTRUCTION.eliteFxn) {
case IV_CURVE:{
IT_Plot();
break;
}
case CV_CURVE:{
IT_Plot();
break;
}
case IT_CURVE:{
IT_Plot();
break;
@@ -185,7 +171,8 @@ static void EliteADCControl(uint32_t **WorkModeData) {
break;
}
case CONSTANT_CURRENT:{
CCModeReadCurrent(WorkModeData);
CCModeReadCurrent(CurrentUserCode);
break;
}
default:{
@@ -196,7 +183,7 @@ static void EliteADCControl(uint32_t **WorkModeData) {
}
static void EliteNotifyControl() {
if ((INSTRUCTION.eliteFxn == IV_CURVE) || (INSTRUCTION.eliteFxn == CV_CURVE)) {
if ((INSTRUCTION.eliteFxn == IV_CURVE)) {
// output the last notify, and reset Elite
if (!PeriodicEvent) {
SendNotify();
@@ -551,16 +551,14 @@ static void SimpleBLEPeripheral_taskFxn(UArg a0, UArg a1) {
ZM_init();
Elite_SPI_init();
void *WorkModeData = malloc(sizeof(uint32_t));
CURRENT_USER_CODE *CurrentUserCode = InitCurrentUserCode();
WorkModeData = &CurrentUserCode;
uint8_t key = 0;
uint16_t counter6994 = 0;
bool EliteOn = 0;
// init DAC, set output ~= 0 V
DAC_outputV(Usercode_Correction_to_DAC(25000));
DAC_outputV(Usercode_Correction_to_DAC(24999));
elite_gptimer_start();
// Application main loops
@@ -636,7 +634,7 @@ static void SimpleBLEPeripheral_taskFxn(UArg a0, UArg a1) {
// if there is periodic event
else {
// Perform periodic application task
SimpleBLEPeripheral_performPeriodicTask(WorkModeData );
SimpleBLEPeripheral_performPeriodicTask(CurrentUserCode);
key = PIN_getInputValue(switch_on);
EliteKeyPress(key); // onPress=> key = 0; 1.lighten LED 2.long press shut down 2650