Compare commits

..

No commits in common. "cd547f855566117a897a69cb709df9464b5dd1dd" and "2c72573241d9e871624f51338d0d16b056770b9b" have entirely different histories.

3 changed files with 49 additions and 260 deletions

View File

@ -42,7 +42,6 @@
/* Private variables ---------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1; ADC_HandleTypeDef hadc1;
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim4; TIM_HandleTypeDef htim4;
/* USER CODE BEGIN PV */ /* USER CODE BEGIN PV */
@ -54,7 +53,6 @@ void SystemClock_Config(void);
static void MX_GPIO_Init(void); static void MX_GPIO_Init(void);
static void MX_TIM4_Init(void); static void MX_TIM4_Init(void);
static void MX_ADC1_Init(void); static void MX_ADC1_Init(void);
static void MX_TIM1_Init(void);
/* USER CODE BEGIN PFP */ /* USER CODE BEGIN PFP */
/* USER CODE END PFP */ /* USER CODE END PFP */
@ -94,11 +92,9 @@ int main(void)
MX_GPIO_Init(); MX_GPIO_Init();
MX_TIM4_Init(); MX_TIM4_Init();
MX_ADC1_Init(); MX_ADC1_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */ /* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim4); HAL_TIM_Base_Start_IT(&htim4);
HAL_TIM_Base_Start_IT(&htim1);
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3); HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
@ -111,28 +107,13 @@ int main(void)
uint32_t adc = 0; uint32_t adc = 0;
uint16_t t = htim1.Instance->CNT; const float Kp = 0.0;
uint16_t dt = 0; const float Ki = 0.0;
uint16_t pt = 0;
const float Kp = 12.5; bool automode = true;
const float Ki = 15;
float error = 0.0;
float integral = 0.0;
bool automode = false;
char change = 0; char change = 0;
char page = 2; bool page = false;
bool convert = true; bool convert = false;
float uTarget = 5.0;
float pi = 0.0;
uint16_t ccr = 0;
#define r1 14820
#define r2 9940
const float ratio = (float)(r1 + r2) / (float)r2;
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Infinite loop */ /* Infinite loop */
@ -151,39 +132,17 @@ int main(void)
unsigned int freq = 84000000 / clkCnt; unsigned int freq = 84000000 / clkCnt;
float dc = ((float)pwmCnt / (float)clkCnt) * 100.0; float dc = ((float)pwmCnt / (float)clkCnt) * 100.0;
// Timer #define r1 14820
t = htim1.Instance->CNT; #define r2 9940
dt = t - pt; const float ratio = (float)(r1 + r2) / (float)r2;
pt = t;
// Read ADC
HAL_ADC_Start(&hadc1); HAL_ADC_Start(&hadc1);
if(HAL_ADC_PollForConversion(&hadc1, 5) == HAL_OK) { if(HAL_ADC_PollForConversion(&hadc1, 5) == HAL_OK) {
adc = HAL_ADC_GetValue(&hadc1); adc = HAL_ADC_GetValue(&hadc1);
} }
// ADC to Volts if (page)
float u = ((float)(adc / 4095.0) * 3.0);
if (automode)
{ {
error = uTarget - (u * ratio);
integral += (float)(error * (float)(dt / 1000000.0));
pi = Kp * error + Ki * integral + 50;
ccr = (float)(pi / 100.0) * clkCnt;
if (ccr >= 0 && ccr <= clkCnt) {
htim4.Instance->CCR1 = ccr;
htim4.Instance->CCR3 = ccr;
}
}
// Display
char ustr[8] = {0};
switch(page){
case 0:
// PWM CCR // PWM CCR
LCD_XY(0, 0); LCD_XY(0, 0);
LCD_puts("CCR "); LCD_puts("CCR ");
@ -193,60 +152,38 @@ int main(void)
LCD_XY(8, 0); LCD_XY(8, 0);
LCD_puts(" AR "); LCD_puts(" AR ");
LCD_putint(clkCnt); LCD_putint(clkCnt);
break; }
else
case 1: {
case 2: // PWM CCR
LCD_XY(0, 0); LCD_XY(0, 0);
if (page == 1) {
// ADC value in spot 1
LCD_puts("ADC "); LCD_puts("ADC ");
LCD_XY(4, 0); LCD_XY(4, 0);
LCD_putint(adc); LCD_putint(adc);
} else {
// Target voltage in spot 1
sprintf(&ustr, "Ut %.2f V", uTarget);
LCD_puts(ustr);
}
memset(&ustr, '0', 8); // ADC to Volts
float u = ((float)(adc / 4095.0) * 3.0);
// Convert with resistor divider in mind if enabled // Convert with resistor divider in mind if enabled
float uC = u;
if (convert) if (convert)
{ {
uC = u * ratio; u = u * ratio;
} }
// Measured voltage in spot 2 char ustr[7] = {0};
sprintf(&ustr, "%.2f V", uC); sprintf(&ustr, "%.2f V", u);
LCD_XY(10, 0); LCD_XY(10, 0);
LCD_puts(ustr); LCD_puts(ustr);
break;
case 3:
sprintf(&ustr, "Out %.2f", pi);
LCD_XY(0, 0);
LCD_puts(ustr);
memset(&ustr, '0', 8);
sprintf(&ustr, "r %d", ccr);
LCD_XY(10, 0);
LCD_puts(ustr);
break;
} }
// Frequency // Frequency
LCD_XY(0, 1); LCD_XY(0, 1);
LCD_putint(freq / 1000); LCD_putint(freq);
LCD_puts("KHz"); LCD_puts(" Hz");
// Dutycycle // Dutycycle
LCD_XY(8, 1); LCD_XY(10, 1);
char dcstr[4] = {0}; LCD_putint((int)dc);
sprintf(&dcstr, "%.1f", dc);
LCD_puts(dcstr);
LCD_putchar('%'); LCD_putchar('%');
// Auto mode enabled // Auto mode enabled
@ -257,7 +194,6 @@ int main(void)
{ {
change = result; change = result;
// Set frequency
if (result == 1) if (result == 1)
{ {
htim4.Instance->ARR = 2799; htim4.Instance->ARR = 2799;
@ -272,8 +208,6 @@ int main(void)
htim4.Instance->CCR3 = (htim4.Instance->ARR + 1) / 2; htim4.Instance->CCR3 = (htim4.Instance->ARR + 1) / 2;
LCD_clear(); LCD_clear();
} }
// Set dutycycle
else if (result == 4) else if (result == 4)
{ {
htim4.Instance->CCR1 = htim4.Instance->CCR1 + (clkCnt / 20); htim4.Instance->CCR1 = htim4.Instance->CCR1 + (clkCnt / 20);
@ -284,59 +218,18 @@ int main(void)
{ {
htim4.Instance->CCR1 = htim4.Instance->CCR1 - (clkCnt / 20); htim4.Instance->CCR1 = htim4.Instance->CCR1 - (clkCnt / 20);
htim4.Instance->CCR3 = htim4.Instance->CCR3 - (clkCnt / 20); htim4.Instance->CCR3 = htim4.Instance->CCR3 - (clkCnt / 20);
}
else if (result == 11)
{
htim4.Instance->CCR1 = htim4.Instance->CCR1 + (clkCnt / 100);
htim4.Instance->CCR3 = htim4.Instance->CCR3 + (clkCnt / 100);
} }
else if (result == 12)
{
htim4.Instance->CCR1 = htim4.Instance->CCR1 - (clkCnt / 100);
htim4.Instance->CCR3 = htim4.Instance->CCR3 - (clkCnt / 100);
}
// Set target voltage (step .1 V)
else if (result == 2)
{
uTarget = uTarget + 0.5;
}
else if (result == 6)
{
uTarget = uTarget - 0.5;
}
// Set target voltage (step .5 V)
else if (result == 3)
{
uTarget = uTarget + 0.1;
}
else if (result == 7)
{
uTarget = uTarget - 0.1;
}
// Convert ADC to actual voltage
else if (result == 13) else if (result == 13)
{ {
convert = !convert; convert = !convert;
LCD_clear(); LCD_clear();
} }
// Switch pages
else if (result == 15) else if (result == 15)
{ {
if (page + 1 == 4) { page = !page;
page = 0;
} else {
page++;
}
LCD_clear(); LCD_clear();
} }
// Switch off PI control
else if (result == 16) else if (result == 16)
{ {
automode = !automode; automode = !automode;
@ -344,6 +237,8 @@ int main(void)
} else { } else {
change = 0; change = 0;
} }
HAL_Delay(100);
} }
/* USER CODE END 3 */ /* USER CODE END 3 */
} }
@ -445,80 +340,6 @@ static void MX_ADC1_Init(void)
} }
/**
* @brief TIM1 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 1680-1;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 65535;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OC_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_TIMING;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM1_Init 2 */
/* USER CODE END TIM1_Init 2 */
}
/** /**
* @brief TIM4 Initialization Function * @brief TIM4 Initialization Function
* @param None * @param None

View File

@ -150,18 +150,7 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
*/ */
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{ {
if(htim_base->Instance==TIM1) if(htim_base->Instance==TIM4)
{
/* USER CODE BEGIN TIM1_MspInit 0 */
/* USER CODE END TIM1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM1_CLK_ENABLE();
/* USER CODE BEGIN TIM1_MspInit 1 */
/* USER CODE END TIM1_MspInit 1 */
}
else if(htim_base->Instance==TIM4)
{ {
/* USER CODE BEGIN TIM4_MspInit 0 */ /* USER CODE BEGIN TIM4_MspInit 0 */
@ -213,18 +202,7 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
*/ */
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base) void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{ {
if(htim_base->Instance==TIM1) if(htim_base->Instance==TIM4)
{
/* USER CODE BEGIN TIM1_MspDeInit 0 */
/* USER CODE END TIM1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM1_CLK_DISABLE();
/* USER CODE BEGIN TIM1_MspDeInit 1 */
/* USER CODE END TIM1_MspDeInit 1 */
}
else if(htim_base->Instance==TIM4)
{ {
/* USER CODE BEGIN TIM4_MspDeInit 0 */ /* USER CODE BEGIN TIM4_MspDeInit 0 */

View File

@ -19,9 +19,8 @@ Mcu.IP0=ADC1
Mcu.IP1=NVIC Mcu.IP1=NVIC
Mcu.IP2=RCC Mcu.IP2=RCC
Mcu.IP3=SYS Mcu.IP3=SYS
Mcu.IP4=TIM1 Mcu.IP4=TIM4
Mcu.IP5=TIM4 Mcu.IPNb=5
Mcu.IPNb=6
Mcu.Name=STM32F407V(E-G)Tx Mcu.Name=STM32F407V(E-G)Tx
Mcu.Package=LQFP100 Mcu.Package=LQFP100
Mcu.Pin0=PE3 Mcu.Pin0=PE3
@ -38,9 +37,7 @@ Mcu.Pin18=PD0
Mcu.Pin19=PE1 Mcu.Pin19=PE1
Mcu.Pin2=PE5 Mcu.Pin2=PE5
Mcu.Pin20=VP_SYS_VS_Systick Mcu.Pin20=VP_SYS_VS_Systick
Mcu.Pin21=VP_TIM1_VS_ClockSourceINT Mcu.Pin21=VP_TIM4_VS_ClockSourceINT
Mcu.Pin22=VP_TIM1_VS_no_output1
Mcu.Pin23=VP_TIM4_VS_ClockSourceINT
Mcu.Pin3=PE6 Mcu.Pin3=PE6
Mcu.Pin4=PC13-ANTI_TAMP Mcu.Pin4=PC13-ANTI_TAMP
Mcu.Pin5=PC14-OSC32_IN Mcu.Pin5=PC14-OSC32_IN
@ -48,7 +45,7 @@ Mcu.Pin6=PC15-OSC32_OUT
Mcu.Pin7=PH0-OSC_IN Mcu.Pin7=PH0-OSC_IN
Mcu.Pin8=PH1-OSC_OUT Mcu.Pin8=PH1-OSC_OUT
Mcu.Pin9=PC1 Mcu.Pin9=PC1
Mcu.PinsNb=24 Mcu.PinsNb=22
Mcu.ThirdPartyNb=0 Mcu.ThirdPartyNb=0
Mcu.UserConstants= Mcu.UserConstants=
Mcu.UserName=STM32F407VGTx Mcu.UserName=STM32F407VGTx
@ -188,7 +185,7 @@ ProjectManager.ToolChainLocation=
ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptAfterPath=
ProjectManager.UAScriptBeforePath= ProjectManager.UAScriptBeforePath=
ProjectManager.UnderRoot=true ProjectManager.UnderRoot=true
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_TIM4_Init-TIM4-false-HAL-true,4-MX_ADC1_Init-ADC1-false-HAL-true,5-MX_TIM1_Init-TIM1-false-HAL-true ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_TIM4_Init-TIM4-false-HAL-true
RCC.48MHZClocksFreq_Value=48000000 RCC.48MHZClocksFreq_Value=48000000
RCC.AHBFreq_Value=168000000 RCC.AHBFreq_Value=168000000
RCC.APB1CLKDivider=RCC_HCLK_DIV4 RCC.APB1CLKDivider=RCC_HCLK_DIV4
@ -234,9 +231,6 @@ SH.S_TIM4_CH1.0=TIM4_CH1,PWM Generation1 CH1
SH.S_TIM4_CH1.ConfNb=1 SH.S_TIM4_CH1.ConfNb=1
SH.S_TIM4_CH3.0=TIM4_CH3,PWM Generation3 CH3 SH.S_TIM4_CH3.0=TIM4_CH3,PWM Generation3 CH3
SH.S_TIM4_CH3.ConfNb=1 SH.S_TIM4_CH3.ConfNb=1
TIM1.Channel-Output\ Compare1\ No\ Output=TIM_CHANNEL_1
TIM1.IPParameters=Channel-Output Compare1 No Output,Prescaler
TIM1.Prescaler=1680-1
TIM4.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE TIM4.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE
TIM4.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 TIM4.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
TIM4.Channel-PWM\ Generation3\ CH3=TIM_CHANNEL_3 TIM4.Channel-PWM\ Generation3\ CH3=TIM_CHANNEL_3
@ -248,10 +242,6 @@ TIM4.Pulse-PWM\ Generation1\ CH1=1400
TIM4.Pulse-PWM\ Generation3\ CH3=1400 TIM4.Pulse-PWM\ Generation3\ CH3=1400
VP_SYS_VS_Systick.Mode=SysTick VP_SYS_VS_Systick.Mode=SysTick
VP_SYS_VS_Systick.Signal=SYS_VS_Systick VP_SYS_VS_Systick.Signal=SYS_VS_Systick
VP_TIM1_VS_ClockSourceINT.Mode=Internal
VP_TIM1_VS_ClockSourceINT.Signal=TIM1_VS_ClockSourceINT
VP_TIM1_VS_no_output1.Mode=Output Compare1 No Output
VP_TIM1_VS_no_output1.Signal=TIM1_VS_no_output1
VP_TIM4_VS_ClockSourceINT.Mode=Internal VP_TIM4_VS_ClockSourceINT.Mode=Internal
VP_TIM4_VS_ClockSourceINT.Signal=TIM4_VS_ClockSourceINT VP_TIM4_VS_ClockSourceINT.Signal=TIM4_VS_ClockSourceINT
board=STM32F407G-DISC1 board=STM32F407G-DISC1