用5529进行脉宽测量,中断貌似进了但是没法判断,求解读~
#include <msp430.h>
#include "Header_Config.h"
#include "sys.h"
unsigned char overflow = 0,Sum_High_Index = 0,Sum_Rising_Index = 0,CCR_Flag = 0;
long Wide_Rising_Old = 0,Wide_Rising_Cur = 0,Wide_Rising = 0,Wide_Rising_Sum = 0,Wide_Rising_Result = 0;
long Wide_High_Cur = 0,Wide_High = 0,Wide_High_Sum = 0,Wide_High_Result = 0;
void main(void)
{
vola
tile unsigned int i;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
Board_init();
LCD_Init();
SetVCore(3);
LFXT_Start(XT1DRIVE_0); // Use 32.768kHz XTAL as reference
Init_FLL_Settle(12000, 762); // Set system clock to (12MHz)
SFRIFG1 = 0;
SFRIE1 |= OFIE;
P1DIR |= 0x01; // P1.0 output
P2DIR &= ~BIT1;
P2SEL |= BIT1;
//P2REN |= BIT1; // Enable P1.4 internal resistance
P2IFG &= ~BIT1;
P2IE |= BIT1;
TA1CCTL1 = CM_3 + SCS + CCIS_0 + CAP + CCIE; // 上升沿和下降沿都捕获
TA1CTL = TASSEL_2 + MC_2 + TAIE; // SMCLK, contmode, interrupt
_EINT();
while(1)
{
if(CCR_Flag)
{
CCR_Flag = 0; // 捕获结果处理
Dogs102x6_numDisplay(0, 0, Wide_Rising_Result, 0); // 周期结果
Dogs102x6_numDisplay(1, 0, Wide_High_Result, 0); // 高电平宽度
}
}
}
#pragma vector=TIMER1_A1_VECTOR
__interrupt void TIMER1_A1_ISR(void)
{
P1OUT ^= 0x01; // Toggle P1.0
switch(__even_in_range(TA1IV,10))
{
case 0: break; // No interrupt
case 2:
TA1CCTL1 &= ~CCIFG;
if((P2IN&BIT0)==BIT0) // 上升沿引发的中断
{
Wide_Rising_Cur = TA1CCR1; // 读取当前所得的最新捕获值
Wide_Rising = 65536*overflow - Wide_Rising_Old + Wide_Rising_Cur; // 防止出现溢出
Wide_Rising_Old = Wide_Rising_Cur;
overflow = 0;
Sum_Rising_Index++;
if(Sum_Rising_Index>4) // 丢掉前几次不稳定数据
{
Wide_Rising_Sum += Wide_Rising;
if(Sum_Rising_Index==12)
{
Sum_Rising_Index = 0;
Wide_Rising_Result = Wide_Rising_Sum>>3; // 得出8次捕获平均值
Wide_Rising_Sum = 0;
Wide_High_Result = Wide_High_Sum/7; // 得出7次高点平捕获平均值,服务于上升沿捕获
Wide_High_Sum = 0;
CCR_Flag = 1; // 置捕获完成标志位
}
}
}
else // 下降沿引发的中断,记录高电平宽度
{
Wide_High_Cur = TA1CCR1; // 读取当前所得的最新捕获值
Wide_High = 65536*overflow - Wide_Rising_Old + Wide_High_Cur; // 防止出现溢出,计算出高电平宽度
//overflow = 0; // 此处不清overflow,一个周期后清除
if(Sum_Rising_Index>4) // 丢掉前几次不稳定数据
{
Wide_High_Sum += Wide_High;
}
}
break; // CCR1 not used
case 4: break; // CCR2 not used
case 6: break; // reserved
case 8: break; // reserved
case 10:
overflow++; // overflow
TA1CTL &= ~CCIFG;
break; // reserved
default: break;
}
}
一周热门 更多>