time.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "time.h"
  2. #include "delay.h"
  3. void tim4_config(void)
  4. {
  5. /* TIM4 configuration:
  6. - TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 128 so the TIM1 counter
  7. clock used is 16 MHz / 128 = 125 000 Hz
  8. - With 125 000 Hz we can generate time base:
  9. max time base is 2.048 ms if TIM4_PERIOD = 255 --> (255 + 1) / 125000 = 2.048 ms
  10. min time base is 0.016 ms if TIM4_PERIOD = 1 --> ( 1 + 1) / 125000 = 0.016 ms
  11. - In this example we need to generate a time base equal to 1 ms
  12. so TIM4_PERIOD = (0.001 * 125000 - 1) = 124 */
  13. //CLK_PeripheralClockConfig((CLK_Peripheral_TypeDef)CLK_Peripheral_TIM4, ENABLE);
  14. CLK->PCKENR1 |= CLK_PCKENR1_TIM4;//
  15. /* Time base configuration */
  16. //TIM4_TimeBaseInit(TIM4_Prescaler_128, 124);//1ms period
  17. /* Set the Autoreload value */
  18. TIM4->ARR = (uint8_t)(124);
  19. /* Set the Prescaler value */
  20. TIM4->PSCR = (uint8_t)(TIM4_Prescaler_128);
  21. /* Generate an update event to reload the Prescaler value immediately */
  22. TIM4->EGR = TIM4_EventSource_Update;
  23. /* Clear TIM4 update flag */
  24. //TIM4_ClearFlag(TIM4_FLAG_Update);
  25. TIM4->SR1 = (uint8_t)(~((uint8_t)TIM4_FLAG_Update));
  26. /* Enable update interrupt */
  27. //TIM4_ITConfig(TIM4_IT_Update, ENABLE);
  28. TIM4->IER |= (uint8_t)TIM4_IT_Update;
  29. /* enable interrupts */
  30. enableInterrupts();
  31. /* Enable TIM4 */
  32. //TIM4_Cmd(ENABLE);
  33. TIM4->CR1 |= TIM4_CR1_CEN ;
  34. }
  35. /**
  36. * @brief INTERRUPT_HANDLER(TIM4_UPD_OVF_TRG_IRQHandler,25)
  37. * @param ¶¨Ê±Æ÷Òç³ö´¦Àíº¯Êý
  38. * @retval None
  39. */
  40. INTERRUPT_HANDLER(TIM4_UPD_OVF_TRG_IRQHandler,25)
  41. {
  42. /* In order to detect unexpected events during development,
  43. it is recommended to set a breakpoint on the following instruction.
  44. */
  45. if(milli_second==0xFFFFFFFF)milli_second=0;
  46. milli_second++;
  47. /* Cleat Interrupt Pending bit */
  48. TIM4->SR1 = (uint8_t)(~(uint8_t)TIM4_IT_Update);
  49. }