|
#include
#include "BoardConfig.h"
unsigned int overflow=0;
unsigned int width[]={0};
unsigned int i=0,j=0;
float temp=0.000000,sum=0.000000;
void sys_init(void);
void main()
{
WDTCTL = WDTPW + WDTHOLD; // 关狗
BoardConfig(0x00);
sys_init();
while(1)
{
CCR0=330;
CCTL1 = OUTMOD_7;
CCR1= 150;
CCTL2 = OUTMOD_7;
CCR2=150;
if(overflow==40)
{
if(temp<0.050000)
{
while(1)
{
CCR0=330;
CCTL1 = OUTMOD_7;
CCR1=0;
CCTL2 = OUTMOD_7;
CCR2=0;
if(overflow==40)
break;
}
}
else if(temp>0.050000&&temp<0.100000)
{
while(1)
{
CCR0=330;
CCTL1 = OUTMOD_7;
CCR1=180;
CCTL2 = OUTMOD_7;
CCR2=180;
if(overflow==40)
break;
}
}
else if(temp>0.100000&&temp<0.150000)
{
while(1)
{
CCR0=330;
CCTL1 = OUTMOD_7;
CCR1=210;
CCTL2 = OUTMOD_7;
CCR2=210;
if(overflow==40)
break;
}
}
else if(temp>0.150000&&temp<0.200000)
{
while(1)
{
CCR0=330;
CCTL1 = OUTMOD_7;
CCR1=240;
CCTL2 = OUTMOD_7;
CCR2=240;
if(overflow==40)
break;
}
}
else if(temp>0.200000&&temp<0.250000)
{
while(1)
{
CCR0=330;
CCTL1 = OUTMOD_7;
CCR1=270;
CCTL2 = OUTMOD_7;
CCR2=270;
if(overflow==40)
break;
}
}
else while(1)
{
CCR0=330;
CCTL1 = OUTMOD_7;
CCR1=330;
CCTL2 = OUTMOD_7;
CCR2=330;
if(overflow==40)
break;
}
overflow=0;
}
}
}
void sys_init(void)
{
//P5DIR = 0xff; //P5设置为输出
//P5OUT = 0x00;
P1DIR |= 0x0C; // P1.2 and P1.3 output
P1SEL |= 0x0C;
TACTL = TASSEL_2 + MC_1;
P4SEL =BIT0; //P4.0作为捕获模块功能的输入端输入方波
//-------开晶振XT2---------
BCSCTL1&=~XT2OFF; //打开XT2振荡器
do
{
IFG1 &= ~OFIFG; // 清除振荡器失效标志
for (i=256;i>0;i--); // 延时,等待XT2起振
}
while ((IFG1 & OFIFG) != 0); // 判断XT2是否起振
BCSCTL2 =SELM_2+SELS; //选择MCLK=SMCLK为XT2
//-----------------------------
TBCCTL0&=~(CCIS1+CCIS0); // 捕获源为P4.0,即CCI0A(也是CCI0B)
TBCCTL0 =CM_2+SCS+CAP; //下降沿捕获,同步捕获,工作在捕获模式
TBCCTL0 =CCIE; //允许捕获比较模块提出中断请求
TBCTL =ID_3+MC_2+TBSSEL_2+TBCLR+TBIE;
_EINT();
}
#pragma vector=TIMERB0_VECTOR
__interrupt void TimerB0(void)
{
if(TBCCTL0&CM1) //捕获到下降沿
{
TBCTL =TBCLR;
TBCCTL0=(TBCCTL0&(~CM1))| CM0; //改为上升沿捕获:CM1置零,CM0置一
}
else if(TBCCTL0&CM0) //捕获到上升沿
{
width[i++]=TBCCR0; //记录下结束时间
TBCCTL0=(TBCCTL0&(~CM0)) |CM1; //改为下降沿捕获:CM0置零,CM1置一
}
}
#pragma vector=TIMERB1_VECTOR
__interrupt void TimerB1(void)
{
switch(TAIV)
{
case 2: break;
case 4: break;
case 6: break;
case 8: break;
case 10: break;
case 12: break;
case 14: overflow++;
if(overflow==40)
{
for(j=0;j
{
sum+=width[j++];
}
temp=sum/26214000;
i=0;
}
}
}
捕获PWM波的低脉冲率。好像是定时器定时的问题,进入主循环后,只执行CCR0=330;
CCTL1 = OUTMOD_7;
CCR1= 150;
CCTL2 = OUTMOD_7;
CCR2=150;
大家帮忙看看是什么原因,谢谢
|
|