1. 클럭설정
- TIM2 사용을 위해 APB1 Timer clocks 설정 - 72MHz
2. TIM2 설정
- Clock Source : Internal Clock
- Prescaler : 7200-1
- Counter Period : 10000-1
계산식 : 72MHz / 7200 / 10000 = 1
3. 코드
- 프로젝트 생성 후 htim2 타이머 인터럽트 시작 추가
- 정의된 HAL_TIM_PeriodElapsedCallback 사용 LED 토글
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim2);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2) /* 1s */
{
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
}
}