#include "msp430G2553.h"
unsigned int start,end;//定义两个无符号整形变量start和end
unsigned char overflow;//定义一个无符号字符型变量overflow
void main( void )
{
WDTCTL = WDTPW + WDTHOLD;//关闭看门狗
P1DIR|=BIT0+BIT6;//P1.0和P1.6设置为输出
P1DIR&=~BIT2;//P1.2设置为输入
P1SEL|=BIT0+BIT2;//复用功能选择P1.0->ACLK输出,P1.2->捕获输入
P1OUT&=~BIT6;//关闭P1.6LED
BCSCTL1 =DIVA_3;//ACLK8分频(vlo=12Khz)
TACCTL1=CM_1+SCS+CCIS_0+CAP+CCIE;//上升沿捕获+同步捕获+CCI1A(p1.2)输入+捕获功能+捕获中断
TACTL=TASSEL_2+MC_2+ID_3+TAIE+TACLR;//选择SMCLK(DCO=1.1Mhz)时钟源,8分频,连续计数模式、开启定时器中断、定时器清零
while(1)
{
_BIS_SR(LPM0_bits+GIE);//进入低功耗模式0,开启全局中断
}
}
#pragma vector=TIMER0_A1_VECTOR //#pragma 预处理指令,指明引起中断的中断向量类型
__interrupt void Timer_A (void)//中断处理函数
{
switch(TAIV)//向量查询
{
case 2://捕获中断
if(CCTL1&CM_1)//上升沿
{
CCTL1=(CCTL1&(~CM_1))|CM_2;//更变设置为下降沿触发
start=TAR;//记录初始时间
overflow=0;//溢出计数变量复位
}
else if(CCTL1&CM_2)//下降沿
{
CCTL1=(CCTL1&(~CM_2))|CM_1;//更变设置为上升沿触发
end=TAR;//记录结束时间
}
P1OUT|=BIT6;//拉升P1.6电平
break;
case 10://定时器溢出中断
overflow++;//溢出计数加1
break;
default:
break;
}
}
此帖出自
小平头技术问答
时钟配置有没有问题?
给个CCS软件自动配置的例程(ALCK使用的32768晶体,如用VLO需要修改BCSCTL3)
- /* USER CODE START (section: BCSplus_graceInit_prologue) */
- /* User initialization code */
- /* USER CODE END (section: BCSplus_graceInit_prologue) */
-
- /*
- * Basic Clock System Control 2
- *
- * SELM_0 -- DCOCLK
- * DIVM_0 -- Divide by 1
- * ~SELS -- DCOCLK
- * DIVS_3 -- Divide by 8
- * ~DCOR -- DCO uses internal resistor
- *
- * Note: ~
indicates that has value zero
- */
- BCSCTL2 = SELM_0 + DIVM_0 + DIVS_3;
- if (CALBC1_8MHZ != 0xFF) {
- /* Follow recommended flow. First, clear all DCOx and MODx bits. Then
- * apply new RSELx values. Finally, apply new DCOx and MODx bit values.
- */
- __delay_cycles(100000);
- DCOCTL = 0x00;
- BCSCTL1 = CALBC1_8MHZ; /* Set DCO to 1MHz */
- DCOCTL = CALDCO_8MHZ;
- }
- /*
- * Basic Clock System Control 1
- *
- * XT2OFF -- Disable XT2CLK
- * ~XTS -- Low Frequency
- * DIVA_0 -- Divide by 1
- *
- * Note: ~XTS indicates that XTS has value zero
- */
- BCSCTL1 |= XT2OFF + DIVA_0;
- /*
- * Basic Clock System Control 3
- *
- * XT2S_0 -- 0.4 - 1 MHz
- * LFXT1S_0 -- If XTS = 0, XT1 = 32768kHz Crystal ; If XTS = 1, XT1 = 0.4 - 1-MHz crystal or resonator
- * XCAP_1 -- ~6 pF
- */
- BCSCTL3 = XT2S_0 + LFXT1S_0 + XCAP_3;
-
- /* USER CODE START (section: BCSplus_graceInit_epilogue) */
- /* User code */
- /* USER CODE END (section: BCSplus_graceInit_epilogue) */
复制代码先看看这个有没有问题,后面的代码我还没看没看仔细
你为何不用上下沿都捕捉的方式CM_3呢?
一周热门 更多>