武林教你學(xué)PIC32(五)定時(shí)器
今天我們用定時(shí)器實(shí)現(xiàn)LED燈的1s閃爍。
首先我們看看要用的幾個(gè)庫函數(shù):
1 OpenTimer1配置16位定時(shí)器函數(shù),函數(shù)定義為:
void OpenTimer1(unsigned int config,unsigned int period);
參數(shù)有:
Timer Module On/Off
Tx_ON
Tx_OFF
(These bit fields are mutually exclusive)
Asynchronous Timer Write Disable
T1_TMWDIS_ON
T1_TMWDIS_OFF
(These bit fields are mutually exclusive)
Timer Module Idle mode On/Off
Tx_IDLE_CON
Tx_IDLE_STOP
(These bit fields are mutually exclusive)
Timer Gate time accumulation enable
Tx_GATE_ON
Tx_GATE_OFF
(These bit fields are mutually exclusive)
Timer Prescaler(1)
T1_PS_1_1
T1_PS_1_8
T1_PS_1_64
T1_PS_1_256
Timer Prescaler
Tx_PS_1_1
Tx_PS_1_2
Tx_PS_1_4
Tx_PS_1_8
Tx_PS_1_16
Tx_PS_1_32
Tx_PS_1_64
Tx_PS_1_256
(These bit fields are mutually exclusive)
Timer Synchronous clock enable(1)
Tx_SYNC_EXT_ON
(These bit fields are mutually exclusive)
Timer Clock source
Tx_SOURCE_EXT
(These bit fields are mutually exclusive)
我需要設(shè)置的是T1_ON(開啟TIMER1模塊)、T1_SOURCE_INT(內(nèi)部時(shí)鐘)、T1_PS_1_256(256分頻)和period(初值)
2ConfigIntTimer1設(shè)置定時(shí)器1的中斷,函數(shù)原型為:
void ConfigIntTimer1(unsigned int config);
Timer interrupt enable/disable
Tx_INT_ON
Tx_INT_OFF
(These bit fields are mutually exclusive)
Timer interrupt priorities
Tx_INT_PRIOR_7
Tx_INT_PRIOR_6
Tx_INT_PRIOR_5
Tx_INT_PRIOR_4
Tx_INT_PRIOR_3
Tx_INT_PRIOR_2
Tx_INT_PRIOR_1
Tx_INT_PRIOR_0
(These bit fields are mutually exclusive)
Timer interrupt sub- priorities
Tx_INT_SUB_PRIOR_3
Tx_INT_SUB_PRIOR_2
Tx_INT_SUB_PRIOR_1
Tx_INT_SUB_PRIOR_0
(These bit fields are mutually exclusive)
這里我們設(shè)置T1_INT_ON(開中斷)和T1_INT_PRIOR_2(2優(yōu)先級)
3定時(shí)器的中斷函數(shù)
void __ISR(_TIMER_1_VECTOR, ipl2) Timer1Handler(void)
{
//清除中斷
mT1ClearIntFlag();
//LED1閃爍
mPORTBToggleBits(BIT_10);
}
還記得第四講的時(shí)鐘配置吧,我們這里把系統(tǒng)時(shí)鐘配置為80M,外設(shè)時(shí)鐘配置為10M,具體配置如圖:
我們算算定時(shí)1s,TIME1的初值怎么算?應(yīng)該是外設(shè)時(shí)鐘10M/256