from: https://eroro.tistory.com/497
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
uint32_t timers = 0; TIM_HandleTypeDef TimHandle; void main(void) { HAL_Init(); /* Configure the system clock to 48 MHz */ SystemClock_Config(); /* Compute the prescaler value to have TIMx counter clock equal to 10000 Hz */ uwPrescalerValue = (uint32_t)(SystemCoreClock / 1000000) - 1;//48000000 UART1_printf("SystemCoreClock : %d, uwPrescalerValue %d\r\n", SystemCoreClock, uwPrescalerValue); /* Set TIMx instance */ TimHandle.Instance = TIMx; /* Initialize TIMx peripheral as follows: + Period = 10000 - 1 + Prescaler = (SystemCoreClock/10000) - 1 + ClockDivision = 0 + Counter direction = Up */ // about 1000000 = 1s, 100000 = 0.1s, 10000 = 0.01s, 1000 = 0.001s, // 100 = 0.0001s, 10 = 0.00001s, 1 = 0.000001s TimHandle.Init.Period = 2000000; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimHandle.Init.RepetitionCounter = 0; if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } __HAL_TIM_SetCounter(&TimHandle, 0); HAL_TIM_Base_Start(&TimHandle); uint32_t beforetime=0; while(1){ if(__HAL_TIM_GetCounter(&TimHandle) >= 1000000){ __HAL_TIM_SetCounter(&TimHandle, 0); UART1_printf("%d, ", timers); timers++; } } } |