add calculations of T & P by ADC; Release mode still have problems

This commit is contained in:
Edward Emelianov 2023-05-15 22:58:39 +03:00
parent a67da85f55
commit 54155fdca9
10 changed files with 127 additions and 24 deletions

View File

@ -197,6 +197,7 @@ static int write_reg(uint8_t reg, uint8_t val){
// read compensation data & write registers // read compensation data & write registers
int BMP280_init(){ int BMP280_init(){
IWDG->KR = IWDG_REFRESH;
DBG("INI:\n"); DBG("INI:\n");
if(!read_reg(BMP280_REG_ID, &params.ID)){ if(!read_reg(BMP280_REG_ID, &params.ID)){
DBG("Can't get ID\n"); DBG("Can't get ID\n");

View File

@ -17,6 +17,7 @@
*/ */
#include "adc.h" #include "adc.h"
#include "hardware.h" // ADCvals
/** /**
* @brief ADCx_array - arrays for ADC channels with median filtering: * @brief ADCx_array - arrays for ADC channels with median filtering:
@ -65,6 +66,7 @@ TRUE_INLINE void enADC(ADC_TypeDef *chnl){
*/ */
// Setup ADC // Setup ADC
void adc_setup(){ void adc_setup(){
IWDG->KR = IWDG_REFRESH;
RCC->AHBENR |= RCC_AHBENR_ADC12EN; // Enable clocking RCC->AHBENR |= RCC_AHBENR_ADC12EN; // Enable clocking
ADC12_COMMON->CCR = ADC_CCR_TSEN | ADC_CCR_CKMODE; // enable Tsens, HCLK/4 ADC12_COMMON->CCR = ADC_CCR_TSEN | ADC_CCR_CKMODE; // enable Tsens, HCLK/4
calADC(ADC1); calADC(ADC1);
@ -128,19 +130,68 @@ uint16_t getADCval(int nch){
} }
// get voltage @input nch (V) // get voltage @input nch (V)
float getADCvoltage(int nch){ float getADCvoltage(uint16_t ADCval){
float v = getADCval(nch) * 3.3; float v = (float)ADCval * 3.3;
v /= 4096.f; // 12bit ADC return v/4096.f; // 12bit ADC
return v;
} }
// return MCU temperature (degrees of celsius) // return MCU temperature (degrees of celsius)
float getMCUtemp(){ float getMCUtemp(){
// make correction on Vdd value float temperature = ADCvals[ADC_TSENS] - (float) *TEMP30_CAL_ADDR;
int32_t ADval = getADCval(ADC_TSENS);
float temperature = ADval - (float) *TEMP30_CAL_ADDR;
temperature *= (110.f - 30.f); temperature *= (110.f - 30.f);
temperature /= (float)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR); temperature /= (float)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR);
temperature += 30.f; temperature += 30.f;
return(temperature); return(temperature);
} }
// calculate R (Ohms) by given `ADCval` for main 10 ADC channels with 1k in upper arm of divider
float calcR(uint16_t ADCval){
return 1000.f/(4096.f/((float)ADCval) - 1.f);
}
/****** R(T, K):
T -= 273.15; % convert to K
_A = 3.9083e-03;
_B = -5.7750e-07;
_C = 0.;
if(T < 0.); _C = -4.1830e-12; endif
R = 1000.*(1 + _A*T + _B*T.^2 - _C.*T.^3*100. + _C.*T.^4);
=====> for T=[70:400] Kelvins
function T = pt1000Tapp(R)
k1 = 27.645;
k2 = 0.235268;
k3 = 1.0242e-05;
k4 = 0.;
if(R < 1000)
k1 = 31.067;
k2 = 2.2272e-01;
k3 = 2.5251e-05;
k4 = -5.9001e-09;
endif
T = k1 + k2*R + k3*R.^2 + k4*R.^3;
endfunction
mean(T-Tapp)= -3.3824e-04
std(T-Tapp')= 3.2089e-03
max(abs(T-Tapp'))= 0.011899
********/
// approximate calculation of T (K) for platinum 1k PTC
float calcT(uint16_t ADCval){
float R = calcR(ADCval);
if(R < 1000.){
return (31.067 + R * (2.2272e-01 + R * (2.5251e-05 - R * 5.9001e-09)));
}
return (27.645 + R * (0.235268 + R * 1.0242e-05));
}
// MPX5050: V=VS(P x 0.018 + 0.04); for 3v3 ADU=4096(P*0.018+0.04) ====>
// 0.018P=ADU/4096-0.04,
// P(kPa) = 55.556*(ADU/4096-0.04)
float calcPres5050(){
float adu = (float)ADCvals[ADC_EXT]/4096. - 0.04;
return 55.556*adu;
}

View File

@ -47,7 +47,14 @@
// starting index of ADC2 // starting index of ADC2
#define ADC2START (9*NUMBER_OF_ADC1_CHANNELS) #define ADC2START (9*NUMBER_OF_ADC1_CHANNELS)
// ADC level for not connected TRD
#define ADC_NOCONN (4000)
void adc_setup(); void adc_setup();
float getMCUtemp(); float getMCUtemp();
uint16_t getADCval(int nch); uint16_t getADCval(int nch);
float getADCvoltage(int nch); float getADCvoltage(uint16_t ADCval);
float calcR(uint16_t ADCval);
float calcT(uint16_t ADCval);
float calcPres5050();

View File

@ -108,7 +108,7 @@ TRUE_INLINE void pwm_setup(){
} }
#ifndef EBUG #ifndef EBUG
TRUE_INLINE void iwdg_setup(){ void iwdg_setup(){
uint32_t tmout = 16000000; uint32_t tmout = 16000000;
/* Enable the peripheral clock RTC */ /* Enable the peripheral clock RTC */
/* (1) Enable the LSI (40kHz) */ /* (1) Enable the LSI (40kHz) */
@ -135,11 +135,10 @@ TRUE_INLINE void iwdg_setup(){
void hw_setup(){ void hw_setup(){
RCC->AHBENR |= RCC_AHBENR_DMA1EN | RCC_AHBENR_DMA2EN; RCC->AHBENR |= RCC_AHBENR_DMA1EN | RCC_AHBENR_DMA2EN;
gpio_setup(); gpio_setup();
IWDG->KR = IWDG_REFRESH;
i2c_setup(HIGH_SPEED); i2c_setup(HIGH_SPEED);
IWDG->KR = IWDG_REFRESH;
pwm_setup(); pwm_setup();
#ifndef EBUG
iwdg_setup();
#endif
} }
void setPWM(int nch, uint16_t val){ void setPWM(int nch, uint16_t val){

View File

@ -120,3 +120,7 @@ void hw_setup();
void setPWM(int nch, uint16_t val); void setPWM(int nch, uint16_t val);
uint16_t getPWM(int nch); uint16_t getPWM(int nch);
#ifndef EBUG
void iwdg_setup();
#endif

View File

@ -42,6 +42,7 @@ static uint8_t I2Cbuf[256], i2cbuflen = 0; // buffer for DMA tx/rx and its len
static inline int isI2Cbusy(){ static inline int isI2Cbusy(){
cntr = Tms; cntr = Tms;
do{ do{
IWDG->KR = IWDG_REFRESH;
if(Tms - cntr > I2C_TIMEOUT){ USND("Timeout, DMA transfer in progress?"); return 1;} if(Tms - cntr > I2C_TIMEOUT){ USND("Timeout, DMA transfer in progress?"); return 1;}
}while(I2Cbusy); }while(I2Cbusy);
return 0; return 0;

View File

@ -89,6 +89,9 @@ int main(void){
BMP280_setup(0); BMP280_setup(0);
BMP280_start(); BMP280_start();
USBPU_ON(); USBPU_ON();
#ifndef EBUG
iwdg_setup();
#endif
// CAN_message *can_mesg; // CAN_message *can_mesg;
while(1){ while(1){
IWDG->KR = IWDG_REFRESH; IWDG->KR = IWDG_REFRESH;

View File

@ -106,23 +106,60 @@ static void refresh_adcwin(btnevtmask evtmask){
if(evtmask) return; if(evtmask) return;
cls(); cls();
SetFontScale(1); SetFontScale(1);
int Y = fontheight + 1, wmax = 0; int Y0 = fontheight*2 + 5; // output
int X, Y = Y0, Xstart = 20, Xraw = 0, XR = 0, XK = 0;
setFGcolor(COLOR_CYAN); setFGcolor(COLOR_CYAN);
for(int i = 0; i < NUMBER_OF_AIN_CHANNELS; ++i, Y+=fontheight+2){ for(int i = 0; i < NUMBER_OF_AIN_CHANNELS; ++i, Y+=fontheight+2){
int X = 20; //X = PutStringAt(X, Y, "ADC input ");
X = PutStringAt(X, Y, "ADC input "); X = PutStringAt(Xstart, Y, u2str(i));
X = PutStringAt(X, Y, u2str(i)); if(X > Xraw) Xraw = X;
if(X > wmax) wmax = X;
} }
wmax += 20; Xraw += 20; // Xraw - starting X for text 'RAW'
setFGcolor(COLOR_WHITE); Y = Y0;
Y = fontheight + 1; setFGcolor(COLOR_LIGHTBLUE);
// RAW values
for(int i = 0; i < NUMBER_OF_AIN_CHANNELS; ++i, Y+=fontheight+2){ for(int i = 0; i < NUMBER_OF_AIN_CHANNELS; ++i, Y+=fontheight+2){
PutStringAt(wmax, Y, u2str(ADCvals[i])); X = PutStringAt(Xraw, Y, u2str(ADCvals[i]));
if(X > XR) XR = X;
} }
XR += 20; // XR - starting X for text 'R, Ohm'
Y = Y0;
setFGcolor(COLOR_LIGHTGREEN); setFGcolor(COLOR_LIGHTGREEN);
// R in Ohms
for(int i = 0; i < NUMBER_OF_AIN_CHANNELS; ++i, Y+=fontheight+2){
if(ADCvals[i] > ADC_NOCONN) X = PutStringAt(XR, Y, "NC");
else X = PutStringAt(XR, Y, float2str(calcR(ADCvals[i]), 3));
if(X > XK) XK = X;
}
Y = Y0;
XK += 20; // XK - starting X for text 'T, K'
// T in K
for(int i = 0; i < NUMBER_OF_AIN_CHANNELS; ++i, Y+=fontheight+2){
if(ADCvals[i] < ADC_NOCONN){
float K = calcT(ADCvals[i]);
setFGcolor(COLOR_YELLOW);
PutStringAt(XK, Y, float2str(K, 1));
setFGcolor(COLOR_GOLD);
PutStringAt(XK+80, Y, float2str(K-273.15, 1));
}
}
setFGcolor(COLOR_BLUE);
Y0 = fontheight + 1;
// print header
PutStringAt(Xstart, Y0, "#");
PutStringAt(Xraw, Y0, "Raw");
PutStringAt(XR, Y0, "R, Ohm");
PutStringAt(XK, Y0, "T, K");
PutStringAt(XK+80, Y0, "degC");
setFGcolor(COLOR_CHOCOLATE);
Y += 5; Y += 5;
PutStringAt(20, Y, "ADC ext"); PutStringAt(wmax, Y, u2str(ADCvals[ADC_EXT])); PutStringAt(Xstart, Y, "P"); PutStringAt(Xraw, Y, u2str(ADCvals[ADC_EXT]));
float P = calcPres5050();
if(P > 30) setFGcolor(COLOR_RED);
else if(P > 10) setFGcolor(COLOR_YELLOW);
else setFGcolor(COLOR_GREEN);
X = PutStringAt(XR, Y, float2str(P, 1));
PutStringAt(X+16, Y, "kPa");
//setFGcolor(COLOR_LIGHTYELLOW); //setFGcolor(COLOR_LIGHTYELLOW);
//Y += 2 + fontheight; //Y += 2 + fontheight;
//PutStringAt(20, Y, "T MCU"); PutStringAt(wmax, Y, float2str(getMCUtemp(), 0)); //PutStringAt(20, Y, "T MCU"); PutStringAt(wmax, Y, float2str(getMCUtemp(), 0));

View File

@ -1,2 +1,2 @@
#define BUILD_NUMBER "294" #define BUILD_NUMBER "300"
#define BUILD_DATE "2023-05-14" #define BUILD_DATE "2023-05-15"