Timer PWM 20207031
Timer PWM 20207031
U KHIỂ
N :
- Trần Kiề
u Hảo – Điện TửViễn Thông lớp CLC
Mssv: 20207031
1)
/**
******************************************************************************
* @file : main.c
******************************************************************************
* @attention
* This software is licensed under terms that can be found in the LICENSE
file
******************************************************************************
*/
/* Includes ------------------------------------------------------------------
*/
#include "main.h"
#include <stdlib.h>
#include "lcd.h"
TIM_HandleTypeDef htim2;
void SystemClock_Config(void);
/**
* @retval int
*/
int main(void)
/* MCU Configuration--------------------------------------------------------
*/
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
/* Infinite loop */
while (1)
HAL_Delay(1);
HAL_Delay(1);
}
for(int i = 999; i > 0; i--)
HAL_Delay(1);
HAL_Delay(10);
HAL_Delay(1);
HAL_Delay(1);
{
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 999);
HAL_Delay(1);
HAL_Delay(1);
HAL_Delay(1);
HAL_Delay(1);
/**
* @retval None
*/
void SystemClock_Config(void)
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
Error_Handler();
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
Error_Handler();
/**
* @param None
* @retval None
*/
htim2.Instance = TIM2;
htim2.Init.Prescaler = 7;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 1000;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
Error_Handler();
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
Error_Handler();
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
Error_Handler();
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
Error_Handler();
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 249;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
Error_Handler();
Error_Handler();
Error_Handler();
HAL_TIM_MspPostInit(&htim2);
/**
* @param None
* @retval None
*/
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
|GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);
PC8 PC9 */
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/**
* @retval None
*/
void Error_Handler(void)
/* User can add his own implementation to report the HAL error return state
*/
__disable_irq();
while (1)
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* @retval None
*/
/* User can add his own implementation to report the file name and line
number,
#endif /* USE_FULL_ASSERT */
2)
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0)
{
HAL_Delay(20);
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0)
{
RValue = rand();
GValue = rand();
BValue = rand();
setLed_RGB(RValue,GValue,BValue);
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0);
}
}
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == 0)
{
HAL_Delay(20);
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == 0)
{
TIM2->CCR1 += 5;
TIM2->CCR2 += 5;
TIM2->CCR3 += 5;
while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == 0);
}
}
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == 0)
{
HAL_Delay(20);
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == 0)
{
TIM2->CCR1 -= 5;
TIM2->CCR2 -= 5;
TIM2->CCR3 -= 5;
while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == 0);
}
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/**
* @brief TIM1 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM1_Init(void)
{
/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
3)
int sec = 0;
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
}
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == GPIO_PIN_RESET)
{
HAL_TIM_Base_Stop(&htim1);
}
}
sec++;
}
/*HAL_TIM_Base_Stop(&htim1);
LCD_printf("%d", sec);
}*/
/*
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == GPIO_PIN_RESET){
HAL_Delay(20);
sec++;
}
while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == GPIO_PIN_RESET)
}*/
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/**
* @brief TIM1 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM1_Init(void)
{
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
Het.