使用MCC学习PIC单片机 – Timer2 定时器2(带硬件限制HLT)
Timer2 是带硬件限制的定时器(HLT)。同一个器件里往往有还有Timer4和Timer6,它们具有相同的功能。
MCC配置界面
模块功能结构图:
硬件限制定时器HLT
硬件限定信号通过外部复位源引入。根据不同的模式,复位源信号上边沿或电平,能够对定时器进行控制,具体的有:
- 门控运行,高电平有效或低电平有效
- 上升沿启动,或下降沿启动
- 上升沿复位,或下降沿复位
- 低电平复位,或高电平复位
对定时器的控制,具体解释如下:
- 启动:定时器开始计数
- 复位:将定时器的计数寄存器清零,如条件满足,复位后会从0继续计数
- 停止:定时器计数停止,不再工作
定时器运行方式
- 自由运行周期:连续工作
- 单触发:定时器计数到预设的周期值时,ON清零,即停止工作;
- 单稳态:与单触发类似,区别是ON不清零,定时器可由外部复位时间重启
MCC代码
Tmr2.c 中,
初始化
void TMR2_Initialize(void);
模式设置,共有21中有效模式可供选择,具体参见数据手册。
void TMR2_ModeSet(TMR2_HLT_MODE mode);
外部复位源设定
void TMR2_ExtResetSourceSet(TMR2_HLT_EXT_RESET_SOURCE reset);
启动/停止定时器
void TMR2_StartTimer(void);
void TMR2_StopTimer(void);
读取/写入计数寄存器
uint8_t TMR2_ReadTimer(void);
void TMR2_WriteTimer(uint8_t timerVal);
写入周期寄存器
void TMR2_LoadPeriodRegister(uint8_t periodVal);
中断服务程序
void TMR2_ISR(void);
void TMR2_SetInterruptHandler(void (* InterruptHandler);
void TMR2_DefaultInterruptHandler(void);
由于使用了中断,别忘了在main.c中启动全局中断控制和外设中断控制。
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
INTERRUPT_PeripheralInterruptEnable();
定时器好几个,每个 用法不太一样吧 598330983 发表于 2019-4-30 20:27
定时器好几个,每个 用法不太一样吧
Timer 2/4/6 功能、用法相同
Timer 1/3/5 功能、用法相同 用定时器中断显示一个口的led闪烁
弄了几天没成功
不用查询 在中断 程序里取反
能给演示一下?
手机+微信 18603693136
void TMR2_ISR(void);
void TMR2_SetInterruptHandler(void (* InterruptHandler);
什么区别 为什么我用MCC配置时,不会自动生成全局中断以及外围中断使能函数 竹杖芒鞋888 发表于 2020-4-26 22:55
为什么我用MCC配置时,不会自动生成全局中断以及外围中断使能函数
有个是能中断需要打钩 竹杖芒鞋888 发表于 2020-4-26 22:55
为什么我用MCC配置时,不会自动生成全局中断以及外围中断使能函数
你需要的函数在main.c里,只要取消注释,就能使用了
void main(void)
{
// Initialize the device
SYSTEM_Initialize();
// If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
// If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
// Use the following macros to:
// Enable the Global Interrupts
//INTERRUPT_GlobalInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Enable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptEnable();
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
谢谢分享,学习了 谢谢分享,学习了 pic单片机在pwm时的时钟源? PIC单片机的Timer2 感觉没有使能位吗?
页:
[1]