int main(void) { HAL_Init(); Nucleo_BSP_Init(); MX_TIM1_Init(); MX_TIM21_Init(); HAL_TIM_Base_Start_IT(&htim21); while (1); }
int main(void) { /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_ADC_Init(); MX_TIM2_Init(); MX_TIM21_Init(); batpins battery3; batpins battery4; pwm_timers b3_tims; pwm_timers b4_tims; batprops props_bat3; batprops props_bat4; /* Battery 3 */ b3_tims.conv_timer = htim2; b3_tims.dchg_timer = htim21; battery3.v_adc_chan = ADC_CHANNEL_4; battery3.i_adc_chan = ADC_CHANNEL_8; battery3.chg_port = chg_onoff_3_GPIO_Port; battery3.chg_pin = chg_onoff_3_Pin; battery3.dchg_pin = TIM_CHANNEL_1; battery3.conv_chg_pin = TIM_CHANNEL_1; battery3.conv_dchg_pin = TIM_CHANNEL_2; battery3.pwm_tims = b3_tims; props_bat3.i_adc_val = 0; props_bat3.v_adc_val = 0; props_bat3.adc_val_old = adc_read(battery3.i_adc_chan); props_bat3.id_adc_stpt = 400 + props_bat3.adc_val_old; props_bat3.ic_adc_stpt = props_bat3.adc_val_old - 600; props_bat3.conv_bst_stpt = 200; // Need to calibrate this to boost to desired voltage props_bat3.pwm_chg_stpt = 0; // Initialized to 0. Program will change as needed. props_bat3.pwm_dchg_stpt = 720; // Initialize near where discharge FET turns on props_bat3.pi = 0; /* Battery 4 */ b4_tims.conv_timer = htim2; b4_tims.dchg_timer = htim21; battery4.v_adc_chan = ADC_CHANNEL_11; battery4.i_adc_chan = ADC_CHANNEL_10; battery4.chg_port = chg_onoff_4_GPIO_Port; battery4.chg_pin = chg_onoff_4_Pin; battery4.dchg_pin = TIM_CHANNEL_2; battery4.conv_chg_pin = B4_CHG_CHAN; // Change in h file (used in multiple locations, dma_offset func) battery4.conv_dchg_pin = TIM_CHANNEL_4; battery4.pwm_tims = b4_tims; props_bat4.i_adc_val = 0; props_bat4.v_adc_val = 0; props_bat4.adc_val_old = adc_read(battery4.i_adc_chan); props_bat4.id_adc_stpt = 500 + props_bat4.adc_val_old; props_bat4.ic_adc_stpt = props_bat4.adc_val_old - 200; props_bat4.conv_bst_stpt = 200; // Need to calibrate this to boost to desired voltage props_bat4.pwm_chg_stpt = 0; // Initialized to 0. Program will change as needed. props_bat4.pwm_dchg_stpt = 720; // Initialize near where discharge FET turns on props_bat4.pi = 0; /* Initialize global variables */ #ifdef BAT1 TimeCounter3 = 0; TimeCounter4 = 0; uint32_t restStartms3 = 0; uint32_t i3 = 0; uint32_t voltage3 = 0; uint32_t current3 = 720; status bat_stat3 = OK; i3_origin = props_bat3.adc_val_old; #endif #ifdef BAT2 uint32_t restStartms4 = 0; uint32_t i4 = 0; uint32_t voltage4 = 0; uint32_t current4 = 720; status bat_stat4 = OK; i4_origin = props_bat4.adc_val_old; #endif //uint32_t dc_pwm[1] = {800};//, 500, 200, 300, 400, 500, 600, 700, 250, 750}; //uint32_t test2[2] = {100, 900}; //uint32_t sine = 0; /* Initialize converter and charge / discharge pins */ conv_init(battery3); conv_init(battery4); //HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_3, &dc_pwm, (uint16_t)1); //HAL_Delay(10); //HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_4, &dc_pwm[8], (uint16_t)2); //HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_1, 200, (uint16_t)SINE_RES_500HZ); //HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_4, 800, (uint16_t)SINE_RES_500HZ); //Bat2 conv dchg //pwm_sine_Start(battery3.pwm_tims.conv_timer, battery3.conv_dchg_pin, dc_pwm, sine); // Boost (discharge) //pwm_sine_Start(battery3.pwm_tims.conv_timer, battery3.conv_chg_pin, dc_pwm, sine); // Buck (charge) //pwm_sine_Start(battery4.pwm_tims.conv_timer, battery4.conv_dchg_pin, test2, sine); // Boost (discharge) //pwm_sine_Start(battery4.pwm_tims.conv_timer, battery4.conv_chg_pin, 400, sine); // Buck (charge) //pwm_Set(battery3.pwm_tims.dchg_timer, battery3.dchg_pin, 750); // pwm_Set(battery4.pwm_tims.dchg_timer, battery4.dchg_pin, 760); //pwm_sine_Start(battery3.pwm_tims.conv_timer, battery3.conv_chg_pin, dc_pwm, sine); // Buck (charge) //HAL_GPIO_WritePin(battery3.chg_port, battery3.chg_pin, GPIO_PIN_SET); // Charging On //HAL_GPIO_WritePin(battery4.chg_port, battery4.chg_pin, GPIO_PIN_SET); // Charging On //pwm_sine_Start(battery4.pwm_tims.conv_timer, battery4.conv_chg_pin, dc_pwm, sine); // Buck (charge) //HAL_TIM_PWM_Start(battery4.pwm_tims.conv_timer, battery4.conv_dchg_pin); //HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); // Bat1 conv dchg uint8_t u8_oc3 = 0; uint8_t u8_oc4 = 0; // Wait for batteries to be connected //while(adc_read(battery3.v_adc_chan) < 500 || adc_read(battery4.v_adc_chan) < 500) {} /* Infinite loop */ while (1) { #ifdef BAT1 /* First battery */ if(TimeCounter3>=5) // 4ms, ie 2 periods of 500Hz sine wave { switch(bat_stat3) { case DISCHARGE: bat_stat3 = discharge_main(battery3, &props_bat3, &restStartms3, i3, bat_stat3); break; case CC: bat_stat3 = chg_ctrl(battery3, &props_bat3, i3, i3_origin); //HAL_Delay(1); break; case CV: bat_stat3 = cv_main(battery3, &props_bat3, &restStartms3, i3, i3_origin, bat_stat3); break; case FULL: if(HAL_GetTick() - restStartms3 >= REST) { props_bat3.i_adc_val = 0; props_bat3.v_adc_val = 0; props_bat3.adc_val_old = adc_read(battery3.i_adc_chan); bat_stat3 = DISCHARGE; } break; case LVDC: if(HAL_GetTick() - restStartms3 >= REST) { props_bat3.i_adc_val = 0; props_bat3.v_adc_val = 0; props_bat3.adc_val_old = adc_read(battery3.i_adc_chan); bat_stat3 = CC; } break; case OK: props_bat3.i_adc_val = 0; // normally reset in d/chg func, but not used so reset here props_bat3.v_adc_val = 0; // normally reset in d/chg func, but not used so reset here bat_stat3 = CC; break; case OVERCURRENT: bat_stat3 = OVERCURRENT; break; default: bat_stat3 = OK; break; } TimeCounter3 = 0; i3 = 0; } /* Update ADC readings */ current3 = adc_read(battery3.i_adc_chan); voltage3 = adc_read(battery3.v_adc_chan); props_bat3.i_adc_val = props_bat3.i_adc_val + current3; props_bat3.v_adc_val = props_bat3.v_adc_val + voltage3; /* Over-current protection */ if(current3>3950 || current3<100) { u8_oc3++; if(u8_oc3 > 15) { conv_init(battery3); bat_stat3 = OVERCURRENT; } } else { u8_oc3 = 0; } i3++; #endif #ifdef BAT2 /* Second battery */ if(TimeCounter4>=5) // 4ms, ie 2 periods of 500Hz sine wave { switch(bat_stat4) { case DISCHARGE: bat_stat4 = discharge_main(battery4, &props_bat4, &restStartms4, i4, bat_stat4); break; case CC: bat_stat4 = chg_ctrl(battery4, &props_bat4, i4, i4_origin); break; case CV: bat_stat4 = cv_main(battery4, &props_bat4, &restStartms4, i4, i4_origin, bat_stat4); break; case FULL: if(HAL_GetTick() - restStartms4 >= REST) { props_bat4.i_adc_val = 0; props_bat4.v_adc_val = 0; props_bat4.adc_val_old = adc_read(battery4.i_adc_chan); bat_stat4 = DISCHARGE; } break; case LVDC: if(HAL_GetTick() - restStartms4 >= REST) { props_bat4.i_adc_val = 0; props_bat4.v_adc_val = 0; props_bat4.adc_val_old = adc_read(battery4.i_adc_chan); bat_stat4 = CC; } break; case OK: props_bat4.i_adc_val = 0; // normally reset in d/chg func, but not used so reset here props_bat4.v_adc_val = 0; // normally reset in d/chg func, but not used so reset here bat_stat4 = CC; break; case OVERCURRENT: bat_stat4 = OVERCURRENT; break; default: bat_stat4 = OK; break; } TimeCounter4 = 0; i4 = 0; } /* Update ADC readings */ current4 = adc_read(battery4.i_adc_chan); voltage4 = adc_read(battery4.v_adc_chan); props_bat4.i_adc_val = props_bat4.i_adc_val + current4; props_bat4.v_adc_val = props_bat4.v_adc_val + voltage4; /* Over-current protection */ if(current4>3950 || current4<100) { u8_oc4++; if(u8_oc4 > 15) { conv_init(battery4); bat_stat4 = OVERCURRENT; } } else { u8_oc4 = 0; } i4++; #endif HAL_SYSTICK_IRQHandler(); } }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); MX_TIM2_Init(); MX_TIM21_Init(); HAL_TIM_Encoder_Start(&htim21, TIM_CHANNEL_ALL); HAL_TIM_OC_Start(&htim2, TIM_CHANNEL_1); HAL_TIM_OC_Start(&htim2, TIM_CHANNEL_2); cnt1 = __HAL_TIM_GET_COUNTER(&htim21); tick = HAL_GetTick(); while (1) { if (HAL_GetTick() - tick > 1000L) { cnt2 = __HAL_TIM_GET_COUNTER(&htim21); if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&htim21)) { if (cnt2 < cnt1) /* Check for counter underflow */ diff = cnt1 - cnt2; else diff = (65535 - cnt2) + cnt1; } else { if (cnt2 > cnt1) /* Check for counter overflow */ diff = cnt2 - cnt1; else diff = (65535 - cnt1) + cnt2; } sprintf(msg, "Difference: %d\r\n", diff); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); speed = ((diff / PULSES_PER_REVOLUTION) / 60); /* If the first three bits of SMCR register are set to 0x3 * then the timer is set in X4 mode (TIM_ENCODERMODE_TI12) * and we need to divide the pulses counter by two, because * they include the pulses for both the channels */ if ((TIM21->SMCR & 0x3) == 0x3) speed /= 2; sprintf(msg, "Speed: %d RPM\r\n", speed); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); dir = __HAL_TIM_IS_TIM_COUNTING_DOWN(&htim21); sprintf(msg, "Direction: %d\r\n", dir); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); tick = HAL_GetTick(); cnt1 = __HAL_TIM_GET_COUNTER(&htim21); } if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_RESET) { /* Invert rotation by swapping CH1 and CH2 CCR value */ tim1_ch1_pulse = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1); tim1_ch2_pulse = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2); __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, tim1_ch2_pulse); __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, tim1_ch1_pulse); } } }