技术说明
摘录1:MSP430x2xx Family User's Guide Version.SLAU144J page.363
以上是datasheet中对定时器比较功能的说明,意思是在计数器TAR和TACCR0发生匹配时,会有以下动作:
- CCIFG中断标志置位
- 相应的匹配符EQUx置位
- 和EQUx对应的输出模式发生动作
- CCI信号被锁存到SCCI
摘录2:MSP430G2x53/MSP430G2x13用户手册中文 Version.SLAS735E page.7
比较功能有对应的输出管脚,例如:
TA0.0表示当TACCR0发生匹配的时候,该管脚发生设置的转换动作。
代码1:
CCTL0 = OUTMOD_4; // CCR0 toggle mode
CCR0 = 500-1;
TACTL = TASSEL_2 + MC_1; // SMCLK, upmode
以上配置,CCR0设置为500个时钟周期且工作模式是开关模式,即TAR每计数到500个时钟P1.1就会翻转一次,也就是输出的方波周期是1000个时钟周期,outclk = SMCLK/1000。
代码2:
CCR0 = 512-1; // PWM Period
CCTL1 = OUTMOD_7; // CCR1 reset/set
CCR1 = 384; // CCR1 PWM duty cycle
TACTL = TASSEL_2 + MC_1; // SMCLK, up mode
结合程序比较器模式为reset/set模式、CCR0配置为511、CCR1配置为384,结合图形可知属于Output Mode 7。
即在0--383时钟周期内P1.1为高、384 -- 511时钟周期内P1.1为低。
完整例程
说明:P2.1经TA1产生一个周期为400Hz、占空比为25%的方波,并将P2.1 ----连接到--- P1.2作为TA0的捕获输入,
P1.0接LED灯,在P1.2测得输入的方波是25%占空比的方波时,LED将会
点亮否则是灭的!!!!
#include <msp430.h>
unsigned char Count, First_Time;
unsigned int REdge1, REdge2, FEdge;
int main(void)
{
unsigned int Period, ON_Period;
unsigned char DutyCycle;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
// P1SEL |= BIT0;
P1DIR |= BIT0; // P1.0/LED Output
P1OUT &= ~BIT0; // LED off
if (CALBC1_8MHZ==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_8MHZ; // Set DCO to 8MHz
DCOCTL = CALDCO_8MHZ;
// Configure Port Pins
P2DIR |= BIT1; // P2.1/TA1.1 Output
P2SEL |= BIT1; // TA1.1 Option select
P1DIR &= ~BIT2; // P1.1/TA0.1 Input Capture
P1SEL |= BIT2; // TA0.1 option select
// Configure TA1.1 to output PWM signal
// Period = 82/32khz = 2.5ms ~ 400Hz Freq
TA1CCR0 = 82-1; // Period Register
TA1CCR1 = 21; // TA1.1 25% dutycycle
TA1CCTL1 |= OUTMOD_7; // TA1CCR1, Reset/Set
TA1CTL = TASSEL_1 + MC_1 + TACLR; // ACLK, upmode, clear TAR
// Configure the TA0CCR1 to do input capture
TA0CCTL1 = CAP + CM_3 + CCIE + SCS + CCIS_0;
// TA0CCR1 Capture mode; CCI1A; Both
// Rising and Falling Edge; interrupt enable
TA0CTL |= TASSEL_2 + MC_2 + TACLR; // SMCLK, Cont Mode; start timer
// Variable Initialization
Count = 0x0;
First_Time = 0x01;
while(1)
{
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0
__no_operation(); // For debugger
// On exiting LPM0
if (TA0CCTL1 & COV) // Check for Capture Overflow
while(1); // Loop Forever
Period = REdge2 - REdge1; // Calculate Period
ON_Period = FEdge-REdge1; // On period
DutyCycle = ((unsigned long)ON_Period*100/Period);
if(DutyCycle!= 25)
{
P1OUT |= BIT0;
}
else
{
P1OUT &= ~BIT0;
}
}
}
// TA0_A1 Interrupt vector
#pragma vector = TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR (void)
{
switch(__even_in_range(TA0IV,0x0A))
{
case TA0IV_NONE: break; // Vector 0: No interrupt
case TA0IV_TACCR1: // Vector 2: TACCR1 CCIFG
if (TA0CCTL1 & CCI) // Capture Input Pin Status
{
// Rising Edge was captured
if (!Count)
{
REdge1 = TA0CCR1;
Count++;
}
else
{
REdge2 = TA0CCR1;
Count=0x0;
__bic_SR_register_on_exit(LPM0_bits + GIE); // Exit LPM0 on return to main
}
if (First_Time)
First_Time = 0x0;
}
else
{
// Falling Edge was captured
if(!First_Time)
{
FEdge = TA0CCR1;
}
}
break;
case TA0IV_TACCR2: break; // Vector 4: TACCR2 CCIFG
case TA0IV_6: break; // Vector 6: Reserved CCIFG
case TA0IV_8: break; // Vector 8: Reserved CCIFG
case TA0IV_TAIFG: break; // Vector 10: TAIFG
default: break;
}
}
注释:
14 -- 15 :设置P1.0为输出,作为LED的控制信号
16 -- 22 :设置DCO的时钟为8MHz,因此SMCLK = MCLK = 8MHz,前面已经学习了!!!
25 -- 28 :设置P2.1为输出、P1.2为输入
32 -- 35 :设置TA1为reset/set,这样TA1CCR1代表高电平、TA1CCR0代表低电平。和代码2类似!!!
38 -- 41 :设置捕获
49 -- 65 :等待检测完毕,然后计算出占空比DutyCycle
70 -- 110 :中断程序写法参照系列1,在捕获上升沿和下降沿的时间!!!