【GD32F350開發分享三】定時器T0中斷:內部高速時鐘源



GD32F350定時器有TIMER0~TIMER5,絕對夠用,我是用的是TIMER0,向上計數模式
在這種模式,計數器的計數方向是向上計數。計數器從0開始向上連續計數到自動加載值(定 義在TIMERx_CAR寄存器中),一旦計數器計數到自動加載值,會重新從0開始向上計數。如果 設置了重複計數器,在(TIMERx_CREP+1)次上溢後產生更新事件,否則在每次上溢時都會產 生更新事件。在向上計數模式中,TIMERx_CTL0寄存器中的計數方向控制位DIR應該被設置成 0。 
當通過TIMERx_SWEVG寄存器的UPG位置1來設置更新事件時,計數值會被清0,併產生更新 事件。 
如果TIMERx_CTL0寄存器的UPDIS置1,則禁止更新事件。 
當發生更新事件時,所有的寄存器(重複計數器,自動重載寄存器,預分頻寄存器)都將被更新。 
下面這些圖給出了一些例子,當TIMERx_CAR=0x63時,計數器在不同預分頻因子下的行爲。

 
  1. void nvic_config(void)
  2. {
  3.     nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  4.     nvic_irq_enable(TIMER0_BRK_UP_TRG_COM_IRQn, 0, 1);
  5. }
  6. /*!
  7.     \brief      configure the TIMER peripheral
  8.     \param[in]  none
  9.     \param[out] none
  10.     \retval     none
  11.   */
  12. void timer_config(void)
  13. {
  14.     /* -----------------------------------------------------------------------
  15.     TIMER0 configuration:
  16.     generate 3 complementary PWM signal.
  17.     TIMER0CLK is fixed to systemcoreclock, the TIMER0 prescaler is equal to 84(GD32F330)
  18.     or 107(GD32F350) so the TIMER0 counter clock used is 1MHz.
  19.     insert a dead time equal to 1us 
  20.     configure the break feature, active at high level, and using the automatic
  21.     output enable feature.
  22.     use the locking parameters level0.
  23.     ----------------------------------------------------------------------- */
  24.     timer_parameter_struct timer_initpara;
  25.  
  26.  
  27.     rcu_periph_clock_enable(RCU_TIMER0);
  28.  
  29.     timer_deinit(TIMER0);
  30.  
  31.     timer_initpara.prescaler         = 4000/50-1;
  32.     timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
  33.     timer_initpara.counterdirection  = TIMER_COUNTER_UP;
  34.     timer_initpara.period            = 270/5;
  35.     timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
  36. //    timer_initpara.repetitioncounter = 0;
  37.     timer_init(TIMER0,&timer_initpara);
  38.  
  39.     /* TIMER0 channel control update interrupt enable */
  40.     timer_interrupt_enable(TIMER0,TIMER_INT_UP);
  41.  
  42.     /* TIMER0 counter enable */
  43.     timer_enable(TIMER0);
  44. }
複製代碼
採用內部時鐘源108MHz,定時4ms,定時中斷如下
  1. extern uint16_t Timer_Count;
  2. extern bool Timer_flag;
  3. void TIMER0_BRK_UP_TRG_COM_IRQHandler(void)
  4. {
  5.  
  6.     timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_UP);
  7.                 Timer_Count++;
  8.                 if(Timer_Count==1)
  9.                 {
  10.                         Timer_Count = 0;
  11.                         Timer_flag = 1;
  12. //                        printf("Timer0\n");
  13.                 }
  14.  
  15.  
  16. }
複製代碼



使用內部時鐘源,時間久了,時間會便宜一點,下次我自己焊接外部晶振8MHz,獲取高精度的定時器,敬請期待
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章