目录
第2句:TA1CTL |= TASSEL_2+TACLR+MC_3+ID_3;
第6句:#pragma vector=TIMER1_A0_VECTOR
__interrupt void ta10_isr(void)
先给出框图
随便从百度文库上找了个定时器程序,分析下。


1 #include <msp430g2553.h> 2 void main (void) 3 { 4 WDTCTL = WDTPW + WDTHOLD; 5 6 7 TA1CTL|=TASSEL_2+TACLR+MC_3+ID_3;//采取内部时钟源1.04M,八分频,增减计数模式 8 TA1CCTL0=CCIE;//启用定时器中断 9 TA1CCR0=65535;//计数1S 10 P1DIR|=BIT0; 11 _EINT(); 12 //LPM4; 13 while(1); 14 } 15 #pragma vector=TIMER1_A0_VECTOR 16 __interrupt void ta10_isr(void) 17 { 18 P1OUT^=BIT0; 19 }
测试通过,这个程序可以是LED1按2秒的频率闪烁(1秒亮,一秒暗)下面分析下:
这一句有点不太懂了,明明是定时器,怎么把看门狗定时器给停止了呢?
第2句:TA1CTL |= TASSEL_2+TACLR+MC_3+ID_3;
后面的解释:采取内部时钟源1.04M,八分频,增减计数模式。
先分析程序句,去头文件里找各自的定义:
1 SFR_16BIT(TA1CTL); /* Timer1_A3 Control */
1 #define MC_0 (0*0x10u) /* Timer A mode control: 0 - Stop */ 2 #define MC_1 (1*0x10u) /* Timer A mode control: 1 - Up to CCR0 */ 3 #define MC_2 (2*0x10u) /* Timer A mode control: 2 - Continous up */ 4 #define MC_3 (3*0x10u) /* Timer A mode control: 3 - Up/Down */ 5 #define ID_0 (0*0x40u) /* Timer A input divider: 0 - /1 */ 6 #define ID_1 (1*0x40u) /* Timer A input divider: 1 - /2 */ 7 #define ID_2 (2*0x40u) /* Timer A input divider: 2 - /4 */ 8 #define ID_3 (3*0x40u) /* Timer A input divider: 3 - /8 */ 9 #define TASSEL_0 (0*0x100u) /* Timer A clock source select: 0 - TACLK */ 10 #define TASSEL_1 (1*0x100u) /* Timer A clock source select: 1 - ACLK */ 11 #define TASSEL_2 (2*0x100u) /* Timer A clock source select: 2 - SMCLK */ 12 #define TASSEL_3 (3*0x100u) /* Timer A clock source select: 3 - INCLK */
第1句话表示了在特殊功能寄存器里找16个位置给Timer1_A3 Control;后面12句话定义了各个宏,实际上就是用前面的标示符表示后面的数,没别的意思。
从数据手册上截取:
数据手册对TACTL的结构有说明:
后面对各个有详细的说明,这里只说明遇到的:
(1)TASSELx: (Timer_A Source select,A计时器的激励源选择)
TASSEL_0: 00 代表着 TACLK,外部时钟单元
TASSEL_1: 01 代表着 ACLK ,Auxiliary clock ,辅助时钟单元,可做2、4或者8分频。
TASSEL_2: 10 代表着 SMCLK,Sub-main clock,子时钟单元,也可做2、4或者8分频。
TASSEL_3: 11 代表着 INCLK ,外部时钟单元
(2)IDx: 表示分频的个数
ID_0 | ID_1 | ID_2 | ID_3 |
1分频,也就是不分 | 2分频 | 4分频 | 8分频 |
(3)MCx表示模式
百度文库找个图:
嘿嘿,这个图就是整个的定时器结构图和计数模式了,虽然有点复杂,但对于老爷们来说,这点事不算啥。
(4)TACLR:Timer A clear
后面给出的解释是:启用定时器中断。
从头文件里获取
SFR_16BIT(TA1CCTL0);
#define CCIE (0x0010) /* Capture/compare interrupt enable */
如果TA1CCTL0=CCIE,实际上在置CCIE位为1的同时,把其他位置0,
BIT0在头文件里这么定义的,并不是0的意思:
1 #define BIT0 (0x0001) 2 #define BIT1 (0x0002) 3 #define BIT2 (0x0004) 4 #define BIT3 (0x0008)
所以这句话的意思是把P1.0 的方向设为输出。
enable interuption 打开全局中断。
相反的,_DINT(); disable interruption关闭全局中断。
第6句:#pragma vector=TIMER1_A0_VECTOR
__interrupt void ta10_isr(void)
用关键字来__interrupt来定义一个中断函数。注意前面的下划线是两个,一个会出错的。
用#pragma vector来提供中断函数的入口地址。