×
单片机 > 单片机程序设计 > 详情

武林教你学PIC32(五)定时器

发布时间:2020-05-27 发布时间:
|

今天我们用定时器实现LED灯的1s闪烁。

首先我们看看要用的几个库函数:

1 OpenTimer1配置16位定时器函数,函数定义为:

void OpenTimer1(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)

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)

我需要设置的是T1_ON(开启TIMER1模块)、T1_SOURCE_INT(内部时钟)、T1_PS_1_256(256分频)和period(初值)

2 ConfigIntTimer1设置定时器1的中断,函数原型为:

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)

这里我们设置T1_INT_ON (开中断)和 T1_INT_PRIOR_2(2优先级)

3定时器的中断函数

void __ISR(_TIMER_1_VECTOR, ipl2) Timer1Handler(void)

{

    // 清除中断

mT1ClearIntFlag();

//LED1闪烁

    mPORTBToggleBits(BIT_10);

}

还记得第四讲的时钟配置吧,我们这里把系统时钟配置为80M,外设时钟配置为10M,具体配置如图:


我们算算定时1s,TIME1的初值怎么算?应该是外设时钟10M/256


关键字:武林  PIC32  定时器 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
51单片机的数码管动态扫描方法