2012年8月16日 星期四

[轉] PIC32 庫函數學習筆記3——定時器



根據PIC32MX中文說明介紹,PIC32MX 系列器件共5個定時器,TEMER1~5。

其中,TIMER1採用一個同步/ 異步16 位定時器,它可作為自由運行的時段定時器使用,用於各種計時應用和計數外部事件。此定時器也可與低功耗輔助振盪器(SOSC)結合使用,為應用提供實時時鐘。支持下列模式:
‧ 同步內部定時器
‧ 同步內部門控定時器
‧ 同步外部定時器
‧ 異步外部定時器

其他支持的特性有
‧ 可選的時鐘預分頻比
‧ 定時器可在CPU 空閒和休眠模式下工作
‧ 可使用CLR、SET 和INV寄存器進行快速位操作
‧ 異步模式下,可與SOSC 結合使用以提供實時時鐘(Real-Time Clock, RTC)。

TIMER2~5採用了4 個同步16 位定時器(默認),它可作為自由運行的時段定時器使用,用於各種計時應用和計數外部事件。支持下列模式:
‧ 同步內部 16位定時器
‧ 同步內部 16位門控定時器
‧ 同步外部 16位定時器

Timer2 與Timer3組合以及Timer4與Timer5 組合可提供2個32位同步定時器。這2個32位定時器可工作在3 種模式下:
‧ 同步內部 32位定時器
‧ 同步內部 32位門控定時器
‧ 同步外部 32位定時器

其他支持的特性有:
‧ 可選的時鐘預分頻比
‧ 定時器可工作在CPU 空閒模式下
‧ 為輸入捕捉模塊和輸出比較模塊提供了時基(僅Timer2 和Timer3)
‧ ADC事件觸發信號(僅 Timer3)
‧ 可使用CLR、SET 和INV寄存器進行快速位操作



定時器的庫函數更加的豐富,主要內容如下:

‧ CPU Core Timer Operations //CPU核心定時器
OpenCoreTimer
UpdateCoreTimer
mConfigIntCoreTimer
mEnableIntCoreTimer
mDisableIntCoreTimer
mSetPriorityIntCoreTimer
ReadCoreTimer
WriteCoreTimer
‧ General Purpose Timer Common Operations
OpenTImerx
CloseTimerx
ConfigIntTimerx
SetPriorityIntTx
DisableIntTx
EnableIntTx
‧ General Purpose Timer and Period Read/Write Operations
ReadTimerx
WriteTimerx
ReadPeriodx
WritePeriodx


  • void OpenCoreTimer(unsigned int compare);


This function clears the CPU Core Count register, then loads the CPUCore Compare register with period.

例:OpenCoreTimer(0x00004000);


  • void UpdateCoreTimer(unsigned int period);


This function adds period to the current value in the CPU Core Compare register, effectively creating the next period match.
Note: A simple method for creating periodic interrupts can be achieved by using the CPU Core Timer and an ISR (Interrupt Service Routine) that calls “UpdateCoreTimer() to update the Core Compare value.See Core Timer code example at the end of this chapter

例:void _CoreTimerHandler(void)
{
    mCTClearIntFlag();
    UpdateCoreTimer(CORE_TIMER_PERIOD);
   
    // .. things to do .. add code here
}


  • void mConfigIntCoreTimer(config);


his argument contains one or more bit masks bitwise OR'd
together. Select one or more from the following bit masks.
Note: An absent mask symbol in an argument assumes the
corresponding bit(s) are disabled, or default value, and are
set = 0.
Core Timer On/Off
CT_INT_ON
CT_INT_OFF
Core Timer Priority Interrupt Level
CT_INT_PRIOR_7
CT_INT_PRIOR_6
CT_INT_PRIOR_5
CT_INT_PRIOR_4
CT_INT_PRIOR_3
CT_INT_PRIOR_2
CT_INT_PRIOR_1
CT_INT_PRIOR_0
Core Timer Sub-Priority Interrupt Level
CT_INT_SUB_PRIOR_3
CT_INT_SUB_PRIOR_2
CT_INT_SUB_PRIOR_1
CT_INT_SUB_PRIOR_0

例:mConfigIntCoreTimer(CT_INT_ON | CT_INT_PRIOR_4);


  • mEnableIntCoreTimer();


This macro enables the 32-bit CPU Core Timer interrupt.//應該是打開主定時器一類的


  • void mDisableIntCoreTimer(void);


//關閉主定時器


  • void mCTSetIntPriority(unsigned int priority);//設置定時器優先級


Core Timer Priority Interrupt Levels
CT_INT_PRIOR_7
CT_INT_PRIOR_6
CT_INT_PRIOR_5
CT_INT_PRIOR_4
CT_INT_PRIOR_3
CT_INT_PRIOR_2
CT_INT_PRIOR_1
CT_INT_PRIOR_0

例:mCTSetIntPriority(CT_INT_PRIOR_2);


  • unsigned int ReadCoreTimer(void);


//返回內部定時器值

例:unsigned int t0;
t0 = ReadCoreTimer();


  • void WriteCoreTimer(unsigned int timer);//寫主定時器


例:WriteCoreTimer(0x12345678);



//下面部分是通用定時器設置


  • void OpenTimer1(unsigned int config, unsigned int period);

void OpenTimer2(unsigned int config, unsigned int period);
void OpenTimer3(unsigned int config, unsigned int period);
void OpenTimer4(unsigned int config, unsigned int period);
void OpenTimer5(unsigned int config, unsigned int period);

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)//只有TIMER1可以設置

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)

例:OpenTimer1(T1_ON | T1_SOURCE_EXT | T1_SYNC_EXT_ON |T1_PS_1_8, 0xFFFF);


  • void OpenTimer32(unsigned int config, unsigned long period);


void OpenTimer45(unsigned int config, unsigned long period);//將兩個定時器合併作為一個32位定時器使用

Timer module On/Off
Tx_ON
Tx_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
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)
32-bit Timer Mode enable
Tx_32BIT_MODE_ON
Tx_32BIT_MODE_OFF
(These bit fields are mutually exclusive)
Timer clock source
Tx_SOURCE_EXT
Tx_SOURCE_INT
(These bit fields are mutually exclusive)

例:OpenTimer23(T2_ON | T2_PS_1_256 | T2_32BIT_MODE_ON,
0x00A00000);


  • void CloseTimer1(void);

void CloseTimer2(void);
void CloseTimer3(void);
void CloseTimer4(void);
void CloseTimer5(void);//關閉相應的定時器


  • void CloseTimer23 (void)

void CloseTimer45 (void)//停止32位定時器


  • void ConfigIntTimer1(unsigned int config);

void ConfigIntTimer2(unsigned int config);
void ConfigIntTimer3(unsigned int config);
void ConfigIntTimer4(unsigned int config);
void ConfigIntTimer5(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)

例:ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);


  • void ConfigIntTimer23(unsigned int config);

void ConfigIntTimer45(unsigned int config);//32位定時器中斷配置

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

例:/* Set Timer45 interrupt priority = 3, sub = 2 */
ConfigIntTimer45(T45_INT_ON | T45_INT_PRIOR_3 | T45_INT_SUB_PRIOR_2);


  • void SetPriorityIntT1(unsigned int config);

void SetPriorityIntT2(unsigned int config);
void SetPriorityIntT3(unsigned int config);
void SetPriorityIntT4(unsigned int config);
void SetPriorityIntT5(unsigned int config);//改變中斷優先級

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

例:/* Set Timer3 interrupt priority = 2*/
SetPriorityIntT3(T3_INT_PRIOR_2)


  • void SetPriorityIntT23(unsigned int config);

void SetPriorityIntT45(unsigned int config);

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

例:/* Set Timer23 interrupt priority = 2*/
SetPriorityIntT23(T23_INT_PRIOR_2);

16.void DisableIntT1(void);//禁止相應的定時中斷
void DisableIntT2(void);
void DisableIntT3(void);
void DisableIntT4(void);
void DisableIntT5(void);

例:/* Disable Timer4 interrupt */
DisableIntT4();


  • void DisableIntT23(void);

void DisableIntT45(void);

/* Disable Timer45 interrupt */
DisableIntT45();

18.void EnableIntT1(void);
void EnableIntT2(void);
void EnableIntT3(void);
void EnableIntT4(void);
void EnableIntT5(void);//使能中斷

/* Enable Timer4 interrupt */
EnableIntT4();


  • void EnableIntT23(void);

void EnableIntT45(void);

/* Enable Timer45 interrupt */
EnableIntT45();


  • unsigned int ReadTimer1(void);

unsigned int ReadTimer2(void);
unsigned int ReadTimer3(void);
unsigned int ReadTimer4(void);
unsigned int ReadTimer5(void);//返回16為定時計數器的值

例:/* Read timer 4 */
currentValue = ReadTimer4();


  • unsigned int ReadTimer23(void);

unsigned int ReadTimer45(void);

例:currentValue = ReadTimer45();


  • void WriteTimer1(unsigned int);

void WriteTimer2(unsigned int);
void WriteTimer3(unsigned int);
void WriteTimer4(unsigned int);
void WriteTimer5(unsigned int);//寫16位定時寄存器值

例:WriteTimer1(0x0400);


  • void WriteTimer23(unsigned int);

void WriteTimer45(unsigned int);

WriteTimer45(0x00000000);


  • unsigned int ReadPeriod1(void);

unsigned int ReadPeriod2(void);
unsigned int ReadPeriod3(void);
unsigned int ReadPeriod4(void);
unsigned int ReadPeriod5(void);//

例:currentValue = ReadPeriod4();


  • unsigned int ReadPeriod23(void);

unsigned int ReadPeriod45(void);

例:currentValue = ReadPeriod45();


  • void WritePeriod1(unsigned int);

void WritePeriod2(unsigned int);
void WritePeriod3(unsigned int);
void WritePeriod4(unsigned int);
void WritePeriod5(unsigned int);

例:WritePeriod1(0x0400);


  • void WritePeriod23(unsigned int);

void WritePeriod45(unsigned int);

例:WritePeriod45(0x00000000);

Source : http://hi.baidu.com/lykb/blog/item/803d54cb8d41b20bbf09e677.html

沒有留言:

張貼留言