Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable speed/temperature multiplexing on same wire #14

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
//#define LEGALFLAG
#define BATTERYVOLTAGE_MAX 53000
#define R_TEMP_PULLUP 0
#define NTC_BETA 3900
#define SP_TEMP_MULTIPLEX 0 // high-pass ADC value on NTC-Hall multiplexed wire

/* ########################## Assert Selection ############################## */
/**
Expand Down Expand Up @@ -179,6 +181,19 @@ typedef struct

}MotorParams_t;

#ifdef R_TEMP_PULLUP

typedef struct
{

uint32_t num;
uint32_t denum;


}NTC_Mult_t;

#endif

/* USER CODE END Private defines */

#ifdef __cplusplus
Expand Down
34 changes: 30 additions & 4 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ uint16_t VirtAddVarTab[NB_OF_VAR] = { EEPROM_POS_HALL_ORDER,
EEPROM_POS_HALL_64
};

#if R_TEMP_PULLUP
uint16_t ui16_adc_temp;
#endif

enum state {Stop, SixStep, Regen, Running, BatteryCurrentLimit, Interpolation, PLL, IdleRun};
enum state SystemState;

Expand Down Expand Up @@ -261,6 +265,7 @@ static void MX_ADC2_Init(void);
static void MX_TIM2_Init(void);
static void MX_TIM3_Init(void);
int16_t T_NTC(uint16_t ADC);
void set_NTC_beta(void);

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

Expand All @@ -285,8 +290,9 @@ int8_t tics_to_speed (uint32_t tics);
int16_t internal_tics_to_speedx100 (uint32_t tics);
int16_t external_tics_to_speedx100 (uint32_t tics);



#if (R_TEMP_PULLUP)
NTC_Mult_t NTC_beta;
#endif


/* USER CODE END PFP */
Expand Down Expand Up @@ -586,6 +592,10 @@ int main(void)

get_standstill_position();

// Calculate multipliers for NTC 10k Beta value
#if (R_TEMP_PULLUP)
set_NTC_beta();
#endif

/* USER CODE END 2 */

Expand Down Expand Up @@ -966,7 +976,11 @@ int main(void)

if(ui8_KV_detect_flag){ui16_KV_detect_counter++;}
#if (R_TEMP_PULLUP)
MS.Temperature = T_NTC(adcData[6]); //Thank you Hendrik ;-)
ui16_adc_temp = adcData[6]; // adcData[5] on LSW12G
#if (SP_TEMP_MULTIPLEX) // ADC needs to be above this value to register temperature
if (ui16_adc_temp > SP_TEMP_MULTIPLEX) // 150 is a good starting point with 220R pull-up
#endif
MS.Temperature = T_NTC(ui16_adc_temp); //Thank you Hendrik ;-)
#else
MS.Temperature=25;
#endif
Expand Down Expand Up @@ -2406,10 +2420,22 @@ int16_t T_NTC(uint16_t ADC) // ADC 12 Bit, 10k Pullup, Rückgabewert in °C
while(R >> n > 1) n++;
R <<= 13;
for(n <<= 6; R >> (n >> 6) >> 13; n++) R -= (R >> 10)*11; // Annäherung 1-11/1024 für 2^(-1/64)
int16_t T6 = 2160580/(n+357)-1639; // Berechnung für 10 kOhm-NTC (bei 25 °C) mit beta=3900 K
int16_t T6 = NTC_beta.num/(n+NTC_beta.denum)-1639; // Berechnung für 10 kOhm-NTC (bei 25 °C) mit beta=NTC_BETA
return (T6 > 0 ? T6+3 : T6-2)/6; // Rundung

}

void set_NTC_beta()
{
// 6*64/ln(2)*NTC_BETA
// ((6*64/ln(2) << 16)*(NTC_BETA << 16)) >> 32
NTC_beta.num = (((uint64_t)36306609) * (((uint64_t)NTC_BETA) << 16)) >> 32;
// For 10k NTC:
// (64)/(ln(2)*298.15)*NTC_BETA-((64/ln(2)*ln(10000))-0.5
// (((64/(ln(2)*298.15) << 16) * (NTC_BETA << 16)) - ((64/ln(2)*ln(10000)) << 16 - 0.5 << 16) >> 32
NTC_beta.denum = ((uint64_t)((uint64_t)20295 * ((uint64_t)NTC_BETA << 16)) -
(uint64_t)3650351083316) >> 32;
}
#endif

/* USER CODE END 4 */
Expand Down
2 changes: 1 addition & 1 deletion Start_Compiling.bat
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ copy STLINK_system_stm32f1xx.c ..\Src\system_stm32f1xx.c

make all

ST-LINK_CLI.exe -c SWD -P LishuiFOC_01.hex -V
ST-LINK_CLI.exe -c SWD SWCLK=8 -P LishuiFOC_01.hex -V

pause

Expand Down
2 changes: 1 addition & 1 deletion WriteOptionBytes.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PATH = %PATH%;%1\STM32 ST-LINK Utility\ST-LINK Utility

ST-LINK_CLI.exe -OB RDP=0
ST-LINK_CLI.exe -c SWD SWCLK=8 -OB RDP=0

pause

Expand Down