fix average

This commit is contained in:
105042004
2019-08-28 18:52:23 +08:00
parent 9d288dee41
commit e864d8fc10
3 changed files with 60 additions and 76 deletions
@@ -10,6 +10,28 @@
#define DACposMax 0x0000
#define DACnegMax 0xFFFF
/*
* Correction Array include all the correction coeff and offset
*
* Correction formula format is " RealValue = coeff * code + offset "
* RealValue can be RealCurrent, RealVolt, or RealResister ...
* code is the code we read from ADC buffer
*
* ADC measure Voltage
* RealVolt = Correction.ADC_volt.coeff * code + Correction.ADC_volt.offset
*
* ADC measure Current
* ADCGain: 0 => 200k, 1 => 10k, 2 => 200R
* RealCurrent = Correction.ADC_current[ADCGain].coeff * code + Correction.ADC_current[ADCGain].offset
*
* DAC output Voltage
* RealVolt = Correction.DAC2RealV.coeff * DACcode + Correction.DAC2RealV.offset
*
* Usercode to DACcode
* DACcode = Correction.Usercode2DAC.coeff * code + Correction.Usercode2DAC.offset
*
*/
#define BORAD_CHAO_I
typedef struct _formula{
@@ -24,7 +46,7 @@ struct _correction{
Formula ADC_volt;
Formula ADC_current[3];
Formula DAC2RealV;
Formula Usercode2RealV;
Formula Usercode2DAC;
uint16_t Gain0Boundary[2];
uint16_t Gain1Boundary[2];
@@ -46,8 +68,8 @@ struct _correction{
.DAC2RealV.coeff = (-1896),
.DAC2RealV.offset = 64893275,
.Usercode2RealV.coeff = (-1054),
.Usercode2RealV.offset = 60597718,
.Usercode2DAC.coeff = (-1054),
.Usercode2DAC.offset = 60597718,
.Gain0Boundary[0] = 0x5F75,
.Gain0Boundary[1] = 0x5FB2,
@@ -74,8 +96,8 @@ struct _correction{
.DAC2RealV.coeff = (0),
.DAC2RealV.offset = 0,
.Usercode2RealV.coeff = (0),
.Usercode2RealV.offset = 0,
.Usercode2DAC.coeff = (0),
.Usercode2DAC.offset = 0,
.Gain0Boundary[0] = 0,
.Gain0Boundary[1] = 0,
@@ -102,8 +124,8 @@ struct _correction{
.DAC2RealV.coeff = (-1896),
.DAC2RealV.offset = 64893275,
.Usercode2RealV.coeff = (-1054),
.Usercode2RealV.offset = 60597718,
.Usercode2DAC.coeff = (-1054),
.Usercode2DAC.offset = 60597718,
.Gain0Boundary[0] = 0x5D96,
.Gain0Boundary[1] = 0x5DD9,
@@ -130,8 +152,8 @@ struct _correction{
.DAC2RealV.coeff = (-1896),
.DAC2RealV.offset = 64893275,
.Usercode2RealV.coeff = (-1054),
.Usercode2RealV.offset = 60597718,
.Usercode2DAC.coeff = (-1054),
.Usercode2DAC.offset = 60597718,
.Gain0Boundary[0] = 0x5ECD,
.Gain0Boundary[1] = 0x5F0D,
@@ -141,31 +163,6 @@ struct _correction{
};
#endif
/*
* Correction Array include all the correction coeff and offset
*
* Correction formula format is " RealValue = coeff * code + offset "
* RealValue can be RealCurrent, RealVolt, or RealResister ...
* code is the code we read from ADC buffer
*
* ADC measure Voltage
* Correction[0] = ADC Volt coeff
* Correction[1] = ADC Volt offset => RealVolt = Correction[0] * code + Correction[1]
*
* ADC measure Current
* Correctino[2] = ADC gain_lv0 coeff
* Correction[3] = ADC gain_lv0 offset => RealCurrent = Correction[2] * code + Correction[3]
* Correctino[4] = ADC gain_lv1 coeff
* Correction[5] = ADC gain_lv1 offset => RealCurrent = Correction[4] * code + Correction[5]
* Correctino[6] = ADC gain_lv2 coeff
* Correction[7] = ADC gain_lv2 offset => RealCurrent = Correction[6] * code + Correction[7]
*
* DAC output Voltage
* Correction[8] = DAC coeff
* Correction[9] = DAC offset => RealVolt = Correction[8] * DACcode + Correction[9]
*
*/
// this function turn ADC measure value (0xXXXX) into real voltage
// unit should be mV
@@ -179,7 +176,6 @@ static int32_t DecodeADCVolt(uint16_t ADC_measure){
// this function turn ADC measure value (0xXXXX) into real current
// unit should be pA
/* Decode ADC current for twenty-one */
static int32_t DecodeADCCurrent(uint8_t ADCGain, uint16_t ADC_measure){
int32_t ADCRealCurrent = 0;
@@ -207,24 +203,18 @@ static int32_t DecodeADCValue(uint8_t ADCGain, uint8_t ADCChannel, uint8_t *ADC_
else if(ADCChannel == ADC_CH_CURRENT){
if (INSTRUCTION.eliteFxn == IVCurve) {
ADCRealCurrent += DecodeADCCurrent(ADCGain, ADC_measure);
ADCRealCurrent_long += DecodeADCCurrent(ADCGain, ADC_measure);
avg_number++;
if ((SampleRate_counter % 10) == 0) {
ADCRealCurrent = ADCRealCurrent / 10;
if (avg_number > 2) { // to discard the first 20 current sample data
ADCRealCurrent_avg = (ADCRealCurrent + ADCRealCurrent_avg*(avg_number - 3)) / (avg_number - 2);
}
avg_number ++;
ADCRealCurrent = 0;
}
if (StepTimeCounter == StepTime - 1) {
NotifyCurrent[0] = (uint8_t) (ADCRealCurrent_avg >> 24);
NotifyCurrent[1] = (uint8_t) ((ADCRealCurrent_avg & 0x00FF0000) >> 16);
NotifyCurrent[2] = (uint8_t) ((ADCRealCurrent_avg & 0x0000FF00) >> 8);
NotifyCurrent[3] = (uint8_t) (ADCRealCurrent_avg & 0x000000FF);
avg_number = 1;
ADCRealCurrent_avg = 0;
ADCRealCurrent_long = ADCRealCurrent_long / avg_number;
NotifyCurrent[0] = (uint8_t) (ADCRealCurrent_long >> 24);
NotifyCurrent[1] = (uint8_t) ((ADCRealCurrent_long & 0x00FF0000) >> 16);
NotifyCurrent[2] = (uint8_t) ((ADCRealCurrent_long & 0x0000FF00) >> 8);
NotifyCurrent[3] = (uint8_t) (ADCRealCurrent_long & 0x000000FF);
avg_number = 0;
ADCRealCurrent_long = 0;
}
}
else {
@@ -280,17 +270,14 @@ static void ADC_overflow(uint8_t gain, uint8_t *rawdata){
// User will enter -5V~+5V in UI.
// websever and controler use 0~50000 represent -5~+5V
// this function should turn 0~50000 into DACcode which output the exactly voltage user want
static uint16_t Usercode_Correction_to_DAC(uint16_t usercode)
{
// DACcode to real_voltage correction function
//DACcode = -1.0548523(usercode) + 60597.718
int32_t usercode_32;
uint16_t DACcode = 0;
usercode_32 = (int32_t)(usercode);
DACcode = (uint16_t) ((Correction.Usercode2RealV.coeff * usercode_32 + Correction.Usercode2RealV.offset)/1000);
DACcode = (uint16_t) ((Correction.Usercode2DAC.coeff * usercode_32 + Correction.Usercode2DAC.offset)/10e3);
return DACcode;
}
@@ -298,14 +285,12 @@ static uint16_t Usercode_Correction_to_DAC(uint16_t usercode)
static int32_t DAC_to_realV(uint16_t DACcode)
{
//volt = (DAC -6.4893275)/(-0.0001896)
int32_t RealV = 0;
int32_t volt_32 = 0;
volt_32 = DACcode;
RealV = Correction.DAC2RealV.coeff * volt_32 + Correction.DAC2RealV.offset;
RealV = RealV / 10e3; //(mV)
volt_32 = (int32_t)(DACcode);
RealV = (Correction.DAC2RealV.coeff * volt_32 + Correction.DAC2RealV.offset)/10e3; //(mV)
return RealV;
}
@@ -672,9 +672,9 @@ static uint16_t SampleRate = 1;
static uint16_t SampleRate_counter = 1;
// record value for IV curve to calculate average current
static int16_t avg_number = 1;
static int16_t avg_number = 0;
static int32_t ADCRealCurrent = 0;
static int32_t ADCRealCurrent_avg = 0;
static long long ADCRealCurrent_long = 0;
#define GAIN_200K 0x00
#define GAIN_10K 0x01
@@ -748,6 +748,7 @@ static void update_ZM_instruction(uint8 *ins) {
CleanBuffer();
INSTRUCTION.eliteFxn = IVCurve;
DACreset = true;
SampleRate = 10;
if (ins[3] | ins[4]) {
VoltOrigin = ((uint16_t)(ins[3]) << 8) | (uint16_t)(ins[4]);
@@ -423,8 +423,6 @@ static uint16_t CVCurve(){
}
if(current_direction_up) {
if ( outputV + Step < outputV ) outputV = 0xffff;
else outputV = outputV + Step;
@@ -733,9 +731,9 @@ static void reset(){
StepTimeCounter = 1;
SampleRate = 1;
SampleRate_counter = 1;
avg_number = 1;
avg_number = 0;
ADCRealCurrent = 0;
ADCRealCurrent_avg = 0;
ADCRealCurrent_long = 0;
LEDPowerON();
for(int i=0 ; i<BLE_INS_BUFF_SIZE ; i++){
@@ -775,9 +773,9 @@ static void Eliteinterrupt(){
StepTimeCounter = 1;
SampleRate = 1;
SampleRate_counter = 1;
avg_number = 1;
avg_number = 0;
ADCRealCurrent = 0;
ADCRealCurrent_avg = 0;
ADCRealCurrent_long = 0;
LEDPowerON();
for(int i=0 ; i<BLE_INS_BUFF_SIZE ; i++){
@@ -815,9 +813,9 @@ static void CleanBuffer(){
Step = 0x9E; // 10 = 0x0A ~= 3.05 mv ~= 3 mv
StepTime = 0x02; // 0x30 = 2'd48 ~= 2 second, 24 = 0x18
StepTimeCounter = 1;
avg_number = 1;
avg_number = 0;
ADCRealCurrent = 0;
ADCRealCurrent_avg = 0;
ADCRealCurrent_long = 0;
for(int i=0 ; i<SPI_LED_SIZE ; i++){
spi_LEDtxbuf[i] = 0;
@@ -919,7 +917,7 @@ static void EliteNotifyControl(){
if ((INSTRUCTION.eliteFxn == IVCurve) && (StepTimeCounter == StepTime - 1)) {
SendNotify();
}
else if ((INSTRUCTION.eliteFxn != IVCurve) && (StepTimeCounter == StepTime)) {
else if ((INSTRUCTION.eliteFxn != IVCurve) && (SampleRate_counter == SampleRate)) {
SendNotify();
}
}
@@ -989,19 +987,19 @@ static uint8_t OldStep2NewStep(uint8_t OldStep){
static uint8_t OldStep2NewStepTime(uint8_t StepTime){
switch(StepTime){
case 0:{
return 0x02;
return 20;
}
case 1:{
return 0x05;
return 50;
}
case 2:{
return 0x06;
return 60;
}
case 3:{
return 0x09;
return 90;
}
default:{
return 0x05;
return 50;
}
}
}