Capturing PWM

Create timer for capture PWM via interrupts
This commit is contained in:
2024-09-02 09:28:38 +03:00
parent 4ad21d4102
commit 78e0a88604
5 changed files with 132 additions and 49 deletions

85
main.c
View File

@@ -51,8 +51,16 @@
/* USER CODE BEGIN PV */
uint8_t transmitBuffer[BUFFER_SIZE];
uint8_t receiveBuffer[BUFFER_SIZE];
uint8_t cnt2_PA3_level = -1;
uint32_t period = 0;
uint32_t pulseWidth = 0;
uint32_t cnt2_PA3_freqcnt = 0;
uint32_t cnt2_PA3_freqcnt0 = 0;
float k = 0;
uint32_t cnt2_ch4_0 = 0;
uint32_t cnt2_ch4_1 = 0;
uint32_t cnt2_ch3_1 = 0;
uint32_t cnt2_ch3_0 = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
@@ -105,6 +113,7 @@ int main(void)
MX_USART3_UART_Init();
MX_TIM4_Init();
/* USER CODE BEGIN 2 */
// uint8_t button_down;
extern float MAX6675_Temperature; //<2F>?змеренная температура
@@ -114,8 +123,12 @@ extern float MAX6675_Temperature; //<2F>?змеренная температур
for (unsigned char i = 0; i < BUFFER_SIZE; i++)
{
transmitBuffer[i] = i + 1;
// transmitBuffer[0] = 0x69;
receiveBuffer[i] = 0;
}
//HAL_UART_Receive_IT(&huart3, receiveBuffer, BUFFER_SIZE);
// HAL_UART_Transmit_IT(&huart3, transmitBuffer, BUFFER_SIZE);
/* USER CODE END 2 */
@@ -123,13 +136,14 @@ for (unsigned char i = 0; i < BUFFER_SIZE; i++)
/* Infinite loop */
/* USER CODE BEGIN WHILE */
HAL_UART_Receive_IT(&huart3, receiveBuffer, BUFFER_SIZE);
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_3);
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_4);
while (1)
{
MAX6675_Temperature = MAX6675_GetTemperature(SPI1);
// Delay_ms(220);
// MAX6675_Temperature = MAX6675_GetTemperature(SPI1);
HAL_Delay(1000);
HAL_UART_Transmit_IT(&huart3, transmitBuffer, BUFFER_SIZE);
}
/* USER CODE END WHILE */
@@ -143,6 +157,8 @@ HAL_UART_Receive_IT(&huart3, receiveBuffer, BUFFER_SIZE);
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
@@ -187,12 +203,14 @@ void SystemClock_Config(void)
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART3)
{
// USART3 завершил отправку данных
HAL_UART_Transmit_IT(&huart3, receiveBuffer, BUFFER_SIZE);
HAL_UART_Transmit_IT(&huart3, transmitBuffer, BUFFER_SIZE);
}
}
@@ -224,6 +242,59 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
the HAL_GPIO_EXTI_Callback could be implemented in the user file
*/
}
void HAL_TIM_PeriodElapsedCallback (TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
{
if(cnt2_PA3_freqcnt0 == cnt2_PA3_freqcnt)
{
switch (cnt2_PA3_level)
{
case 0: transmitBuffer[0] = 0x98;
case 1: transmitBuffer[0] = 0x99;
}
}
cnt2_PA3_freqcnt0 = cnt2_PA3_freqcnt;
}
}
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)
{
cnt2_ch3_1 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_3);
cnt2_PA3_level = 0; //PA3 low
}
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
{
// TIM2->CNT = 0;
cnt2_PA3_level = 1; //PA3 high
cnt2_PA3_freqcnt++;
cnt2_ch4_1 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_4);
cnt2_ch3_1 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_3);
if(cnt2_ch4_0 < cnt2_ch4_1) period = cnt2_ch4_1 - cnt2_ch4_0;
else period = htim2.Init.Period - cnt2_ch4_0 + cnt2_ch4_1;
if(cnt2_ch4_0 < cnt2_ch3_1)pulseWidth = cnt2_ch3_1 - cnt2_ch4_0;
else pulseWidth = htim2.Init.Period - cnt2_ch4_0 + cnt2_ch3_1;
transmitBuffer[0] = (uint8_t)(100*(((float)pulseWidth)/((float)period)));
transmitBuffer[1] = (uint8_t)(255*pulseWidth/30000);
transmitBuffer[2] = (uint8_t)(255*period/30000);
if(period == 0) transmitBuffer[0] = 0;
cnt2_ch4_0 = cnt2_ch4_1;
// cnt_ch3_0 = cnt_ch3_1;
}
}
}
/* USER CODE END 4 */