- /****测脉冲宽度并用1602显示出来****/
- #include <msp430x14x.h>
- #include "cry1602.c"
- #define uint unsigned int
- #define uchar unsigned char
- uint pwm_star,pwm_end;
- uint width;
- uint i=0;
- uchar *s="The width is :";
- uchar b[]={'0','1','2','3','4','5','6','7','8','9'};
- uint m,n,p,q;
- void main(void)
- {
- WDTCTL=WDTPW+WDTHOLD;
-
- P6DIR |= BIT2;P6OUT |= BIT2; //关闭电平转换
-
- P6DIR |= BIT3;P6OUT |= BIT3;
-
- BCSCTL1 &= ~XT2OFF; //打开XT2高频晶体振荡器
- do
- {
- IFG1 &= ~OFIFG; //清除晶振失败标志
- for (i = 0xFF; i > 0; i--); //等待8MHz晶体起振
- }
- while ((IFG1 & OFIFG)); //晶振失效标志仍然存在?
- BCSCTL2 |= SELM_2 + SELS; //MCLK和SMCLK选择高频晶振
-
- P1DIR|=BIT2;
- P1SEL|=BIT2; //捕获源为P1.2
-
- TACTL=TACLR; //定时器清零
- TACTL |= TASSEL_2 + ID_3; //计数时钟选择SMLK=8MHz,1/8分频后为1MHz
-
- TACCTL1 =CM_3+SCS+CAP+CCIS_0+CCIE; //上升沿下降沿都同步捕获
-
- TACTL = MC_2; //定时器计数开始 连续计数模式
-
- _EINT();
-
- LcdReset();
-
- DispStr(0,0,s);
-
- while(1)
- {
-
- m=width/1000;
- n=(width/100)%10;
- p=(width/10)%10;
- q=width%10;
-
- Disp1Char(4,1,b[m]);
- Disp1Char(5,1,b[n]);
- Disp1Char(6,1,b[p]);
- Disp1Char(7,1,b[q]);
-
- }
- }
-
- /******定时器TA的CCR1的中断 用于检测脉冲上升沿和下降沿******/
- #pragma vector=TIMERA1_VECTOR
- __interrupt void Timer_A1(void)
- {
- switch(TAIV)
- { case 0x02:
- if(CCI==1)
- {
- pwm_star = TACCR1;
- break;
- }
- else
- {
- pwm_end = TACCR1;
- break;
- }
-
- default:
- break;
- }
-
- width = pwm_end-pwm_star;
-
- }
复制代码
此帖出自
小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
首先真的谢谢你,但我还是有一些不明白。在捕获模式,当满足捕获条件时,硬件不是自动将计数器TAR中的数据写到TACCRx中吗?至于如何减小误差,我没有考虑到这点。
一周热门 更多>