当前位置:网站首页>Embedded Systems: Basic Timers
Embedded Systems: Basic Timers
2022-08-05 07:54:00 【TUTUATM】
定时器

(一)工作原理
1.定时器:计数器,时钟脉冲计数
2.工作原理:
(1)Automatic counting mode:Under the condition of the clock,The system automatically count to the maximum overflow,Represents a timing to complete
- The clock a maximum count is determined by the timer of digit 2 n 2^n 2n
(2)Counting modulation mode:用户Custom timing time,System clock to the custom time,Represents a timing to complete
由上可知,Have more flexibility in modulation counting mode.
向上计数:After counting up to the set point to count,周期循环


连续计数:After counting up to a maximum count again,周期循环


上/下计数:After counting up to the set value,From the set point counting down again

(二)分类
1.In the nuclear timer
(1)SysTick定时器:系统时钟滴答定时器(Timing function)
- Can be used as an operating system scheduling time slice
(2)RTC:实时时钟
(3)WatchDog:看门狗时钟,For abnormal reset
2.Peripherals timer
(1)基本定时器:Timing function,For other time base timer function
(2)通用定时器
- 输入捕获
- 输出比较
- PWM(A special case of our output compare mode)
(3)高级定时器
- 通用定时器
- With dead zone controlPWM
- The road three phase shift waveform(Three-phase motor control)
(三)编程
1.编程流程
(1)时钟配置
- 时钟源
- 分频系数
(2)功能配置
- 计数模式
- 重装载值
- 计数值清零
(3)定时器使能
(4)中断配置
- 中断源
- 中断优先级
- 中断使能
(5)中断处理函数
2.编程实例
(1)寄存器版本
#include "msp.h"
#include "driverlib.h"
int main()
{
//关闭看门狗
WDTCTL = WDTPW | WDTHOLD;
//GPIO复用为HFXT
PJ->SEL1 &=~(BIT2 | BIT3);
PJ->SEL0 |= (BIT2 | BIT3);
//Unlock the clock register(0x695A)
CS->KEY = CS_KEY;
//HCLK 16MHz
CS->CTL2 |= CS_CTL2_HFXTFREQ_2 | CS_CTL2_HFXTDRIVE | CS_CTL2_HFXT_EN;
//SMCLK 4MHz
CS->CTL1 |= CS_CTL1_DIVS_2 | CS_CTL1_SELS_5;
//SMCLK 时钟源使能
CS->CLKEN |= CS_CLKEN_SMCLK_EN;
//Lock the clock register(0xA569)
CS->KEY = CS_KEY_KEY_OFS;
//时钟选择(1MHz)
TIMER_A0->CTL |= TIMER_A_CTL_SSEL__SMCLK | TIMER_A_CTL_ID__4;
//计数值1ms
TIMER_A0->R = 999;
//Rising counting mode,计数值清零
TIMER_A0->CTL |= TIMER_A_CTL_MC__UP | TIMER_A_CTL_CLR;
//清除中断标志位
TIMER_A0->CTL &= ~TIMER_A_CTL_IFG;
//Timer中断使能
TIMER_A0->CTL |= TIMER_A_CTL_IE;
P1->SEL0 &=~BIT0;
P1->SEL1 &=~BIT0;
P1->DIR |= BIT0;
P1->OUT |= BIT0;
while(1);
return 0;
}
uint8_t timecount = 0;
void TA0_N_IRQHandler(void)
{
//判断中断是否发生
if(TIMER_A_CTL_IFG & TIMER_A0->CTL)
{
//中断标志位清零
TIMER_A0->CTL &= ~TIMER_A_CTL_IFG;
timecount++;
if(timecount == 10)
{
P1->OUT ^= BIT0;
}
}
}
(2)库函数版本
#include "msp.h"
#include "driverlib.h"
int main()
{
//关闭看门狗
WDT_A_holdTimer();
//GPIO复用为HFXT
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_PJ,GPIO_PIN3 | GPIO_PIN2, GPIO_PRIMARY_MODULE_FUNCTION);
//HCLK 16MHz
CS_setExternalClockSourceFrequency(32000,16000000);
CS_startHFXT(false);
//SMCLK 4MHz
CS_initClockSignal(CS_SMCLK,CS_HFXTCLK_SELECT, CS_CLOCK_DIVIDER_4);
CS_enableClockRequest(CS_SMCLK);
//The timer function configuration
Timer_A_UpModeConfig Timer_A_UpMode;
Timer_A_UpMode.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
Timer_A_UpMode.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_4;
Timer_A_UpMode.timerPeriod = 999;
Timer_A_UpMode.timerClear = TIMER_A_DO_CLEAR;
Timer_A_UpMode.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
Timer_A_UpMode.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE;
//The timer function configuration
Timer_A_configureUpMode(TIMER_A0_BASE,&Timer_A_UpMode);
//清除中断标志位
Timer_A_clearInterruptFlag(TIMER_A0_BASE);
//使能中断
Timer_A_enableInterrupt(TIMER_A0_BASE);
//Can make the timer count
Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE);
while(1);
return 0;
}
uint8_t timecount = 0;
void TA0_N_IRQHandler(void)
{
uint32_t status = Timer_A_getInterruptStatus(TIMER_A0_BASE);
//判断中断是否发生
if(TIMER_A_CTL_IFG & status)
{
//中断标志位清零
Timer_A_clearInterruptFlag(TIMER_A0_BASE);
timecount++;
if(timecount == 10)
{
GPIO_toggleOutputOnPin(GPIO_PORT_P1,GPIO_PIN0);
}
}
}
边栏推荐
猜你喜欢
随机推荐
Cannot compare or sort text, ntext, and image data types
Mysql 死锁和死锁的解决方案
php向mysql写入数据失败
v-if/v-else determines whether to display according to the calculation
Redis实现分布式锁-原理-问题详解
Discourse 清理存储空间的方法
微信 小程序 之PC端 不支持 wx.previewMedia 方法 故用自定义轮播图进行 模拟照片视频的播放
Antdesign a-select 下拉框超出长度换行显示
Liunx教程超详细(完整)
nn.unfold和nn.fold
Use of thread pool (combined with Future/Callable)
Hash these knowledge you should also know
常用的遍历map的方法
2022.7.29好题选讲(计数专题)
Illegal key size 报错问题
cmake 学习使用笔记(三)
window.open 全屏展示
SVG big fish eat small fish animation js special effects
Does Libpq support read-write separation configuration?
re正则表达式








