中断向量
TIMERx_A0_VECTOR 是 CCR0 的中断向量 (第一个引脚)
TIMERx_A1_VECTOR 是 TAIV 的中断向量
#define TIMER2_A1_VECTOR (43 * 1u) /* 0xFFD6 Timer2_A5 CC1-4, TA */
#define TIMER2_A0_VECTOR (44 * 1u) /* 0xFFD8 Timer2_A5 CC0 */
#define TIMER1_A1_VECTOR (48 * 1u) /* 0xFFE0 Timer1_A3 CC1-2, TA1 */
#define TIMER1_A0_VECTOR (49 * 1u) /* 0xFFE2 Timer1_A3 CC0 */
#define TIMER0_A1_VECTOR (52 * 1u) /* 0xFFE8 Timer0_A5 CC1-4, TA */
#define TIMER0_A0_VECTOR (53 * 1u) /* 0xFFEA Timer0_A5 CC0 */
定时器A 增计数模式CCR0中断:
定时器初始化:
void Timer_A_Init(void)
{
Timer_A_initUpModeParam htim = {0};
htim.clockSource = TIMER_A_CLOCKSOURCE_ACLK; //时钟源选为ACLK = 32768Hz
htim.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_64; //64分频
htim.timerPeriod = 512 - 1; //计数值设为512 - 1
htim.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
htim.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE; //使能CCR0中断
htim.timerClear = TIMER_A_DO_CLEAR; //把定时器的定时计数器,分频计数器的计数值清零
htim.startTimer = true; //初始化后立即启动定时器
//中断频率=32768Hz / 64 / 512 = 1Hz
Timer_A_initUpMode(TIMER_A0_BASE, &htim);
//配置定时器A0为增计数模式
}
//中断频率=32768Hz / 64 / 512 = 1Hz 也就是1秒进一次中断
中断函数:
#pragma vector=TIMER0_A0_VECTOR
__interrupt
void TIMER0_A0_ISR (void)
{
GPIO_toggleOutputOnPin(GPIO_PORT_P4, GPIO_PIN7); //翻转P4.7
}
主函数:
#include "driverlib.h"
#define MCLK_IN_HZ 25000000
#define delay_us(x) __delay_cycles((MCLK_IN_HZ/1000000*(x)))
#define delay_ms(x) __delay_cycles((MCLK_IN_HZ/1000*(x)))
void SystemClock_Init(void)
{
PMM_setVCore(PMM_CORE_LEVEL_3); //高主频工作需要较高的核心电压
//XT1引脚复用
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN4);
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P5, GPIO_PIN5);
//起振XT1
UCS_turnOnLFXT1(UCS_XT1_DRIVE_3,UCS_XCAP_3);
//XT2引脚复用
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN2);
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P5, GPIO_PIN3);
//起振XT2
UCS_turnOnXT2(UCS_XT2_DRIVE_4MHZ_8MHZ);
//XT2作为FLL参考时钟,先8分频,再50倍频 4MHz / 8 * 50 = 25MHz
UCS_initClockSignal(UCS_FLLREF, UCS_XT2CLK_SELECT, UCS_CLOCK_DIVIDER_8);
UCS_initFLLSettle(25000, 50);
//XT1作为ACLK时钟源 = 32768Hz
UCS_initClockSignal(UCS_ACLK, UCS_XT1CLK_SELECT, UCS_CLOCK_DIVIDER_1);
//DCOCLK作为MCLK时钟源 = 25MHz
UCS_initClockSignal(UCS_MCLK, UCS_DCOCLK_SELECT, UCS_CLOCK_DIVIDER_1);
//DCOCLK作为SMCLK时钟源 = 25MHz
UCS_initClockSignal(UCS_SMCLK, UCS_DCOCLK_SELECT, UCS_CLOCK_DIVIDER_1);
//设置外部时钟源的频率,使得在调用UCS_getMCLK, UCS_getSMCLK 或 UCS_getACLK时可得到正确值
UCS_setExternalClockSource(32768, 4000000);
}
void Timer_A_Init(void)
{
Timer_A_initUpModeParam htim = {0};
htim.clockSource = TIMER_A_CLOCKSOURCE_ACLK;
htim.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_64;
htim.timerPeriod = 512 - 1;
htim.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
htim.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE;
htim.timerClear = TIMER_A_DO_CLEAR;
htim.startTimer = true;
Timer_A_initUpMode(TIMER_A0_BASE, &htim);
//配置定时器A为增计数模式
}
int main(void)
{
WDT_A_hold(WDT_A_BASE);
SystemClock_Init();
GPIO_setAsOutputPin(GPIO_PORT_P4, GPIO_PIN7);
Timer_A_Init();
//interrupts enabled
__bis_SR_register(GIE);
while(1)
{
}
}
#pragma vector=TIMER0_A0_VECTOR
__interrupt
void TIMER0_A0_ISR (void)
{
GPIO_toggleOutputOnPin(GPIO_PORT_P4, GPIO_PIN7);
}
定时器A 增计数模式TAIE中断 :
定时器初始化:
void Timer_A_Init(void)
{
Timer_A_initUpModeParam htim = {0};
htim.clockSource = TIMER_A_CLOCKSOURCE_SMCLK; //时钟源选为SMCLK = 25MHz
htim.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_5; //5分频
htim.timerPeriod = 25000 - 1; //计数值设为25000 - 1
htim.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_ENABLE; //使能TALE中断
htim.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE;
htim.timerClear = TIMER_A_DO_CLEAR; //把定时器的定时计数器,分频计数器的计数值清零
htim.startTimer = true; //初始化后立即启动定时器
Timer_A_initUpMode(TIMER_A0_BASE, &htim);
//配置定时器A为增计数模式
}
//设得中断频率为25MHz / 5 / 25000 = 200Hz 也就是0.005秒进一次中断
中断服务函数:
#pragma vector=TIMER0_A1_VECTOR
__interrupt
void TIMER0_A1_ISR (void)
{
switch(TA0IV)
{
case TA0IV_NONE:
break;
case TA0IV_TACCR1:
break;
case TA0IV_TACCR2:
break;
case TA0IV_TACCR3:
break;
case TA0IV_TACCR4:
break;
case TA0IV_5:
break;
case TA0IV_6:
break;
case TA0IV_TAIFG:
GPIO_toggleOutputOnPin(GPIO_PORT_P4, GPIO_PIN7);
break;
default:
break;
}
}
TAxIV寄存器的值有:
00h =没有中断等待
02h =中断源:捕获/比较1;中断标志:TAxCCR1 CCIFG; 中断优先级:最高
04h =中断源:捕获/比较2;中断标志:TAxCCR2 CCIFG
06h =中断源:捕获/比较3;中断标志:TAxCCR3 CCIFG
08h =中断源:捕获/比较4;中断标志:TAxCCR4 CCIFG
0ah =中断源:捕获/比较5;中断标志:TAxCCR5 CCIFG
0ch =中断源:捕获/比较6;中断标志:TAxCCR6 CCIFG
0Eh =中断源:定时器溢出;中断标志:TAxCTL TAIFG; 中断优先级:最低
这里是要等定时器溢出进入中断,所以选择最后一个
主函数:
#include "driverlib.h"
#define MCLK_IN_HZ 25000000
#define delay_us(x) __delay_cycles((MCLK_IN_HZ/1000000*(x)))
#define delay_ms(x) __delay_cycles((MCLK_IN_HZ/1000*(x)))
void SystemClock_Init(void)
{
PMM_setVCore(PMM_CORE_LEVEL_3); //高主频工作需要较高的核心电压
//XT1引脚复用
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN4);
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P5, GPIO_PIN5);
//起振XT1
UCS_turnOnLFXT1(UCS_XT1_DRIVE_3,UCS_XCAP_3);
//XT2引脚复用
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN2);
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P5, GPIO_PIN3);
//起振XT2
UCS_turnOnXT2(UCS_XT2_DRIVE_4MHZ_8MHZ);
//XT2作为FLL参考时钟,先8分频,再50倍频 4MHz / 8 * 50 = 25MHz
UCS_initClockSignal(UCS_FLLREF, UCS_XT2CLK_SELECT, UCS_CLOCK_DIVIDER_8);
UCS_initFLLSettle(25000, 50);
//XT1作为ACLK时钟源 = 32768Hz
UCS_initClockSignal(UCS_ACLK, UCS_XT1CLK_SELECT, UCS_CLOCK_DIVIDER_1);
//DCOCLK作为MCLK时钟源 = 25MHz
UCS_initClockSignal(UCS_MCLK, UCS_DCOCLK_SELECT, UCS_CLOCK_DIVIDER_1);
//DCOCLK作为SMCLK时钟源 = 25MHz
UCS_initClockSignal(UCS_SMCLK, UCS_DCOCLK_SELECT, UCS_CLOCK_DIVIDER_1);
//设置外部时钟源的频率,使得在调用UCS_getMCLK, UCS_getSMCLK 或 UCS_getACLK时可得到正确值
UCS_setExternalClockSource(32768, 4000000);
}
void Timer_A_Init(void)
{
Timer_A_initUpModeParam htim = {0};
htim.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
htim.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_5;
htim.timerPeriod = 25000 - 1;
htim.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_ENABLE;
htim.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE;
htim.timerClear = TIMER_A_DO_CLEAR;
htim.startTimer = true;
//设得中断频率为25MHz / 5 / 25000 = 200Hz
Timer_A_initUpMode(TIMER_A0_BASE, &htim);
//配置定时器A为增计数模式
}
int main(void)
{
WDT_A_hold(WDT_A_BASE);
SystemClock_Init();
GPIO_setAsOutputPin(GPIO_PORT_P4, GPIO_PIN7);
Timer_A_Init();
//interrupts enabled
__bis_SR_register(GIE);
while(1)
{
}
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt
void TIMER0_A1_ISR (void)
{
switch(TA0IV)
{
case TA0IV_NONE:
break;
case TA0IV_TACCR1:
break;
case TA0IV_TACCR2:
break;
case TA0IV_TACCR3:
break;
case TA0IV_TACCR4:
break;
case TA0IV_5:
break;
case TA0IV_6:
break;
case TA0IV_TAIFG:
GPIO_toggleOutputOnPin(GPIO_PORT_P4, GPIO_PIN7);
break;
default:
break;
}
}
标签:UCS,电赛,中断,void,htim,CCSMSP430F5529,TIMER,GPIO,定时
From: https://blog.csdn.net/2302_80529671/article/details/139220403