用输出触发中断,C

时间:2016-08-31 22:33:01

标签: c function interrupt msp430

我在现有代码中实现和中断函数时遇到问题。基本上我希望中断在触发某个输出或触发输出的计数器达到阈值时触发。

设备:

Micro:MSP430F5522

CCS:5.5.0.00077

编译器:TI v4.1.9

设备应该扫描一个灯,如果连续检测到一定次数的灯,则打开输出,点亮绿色LED。如果连续失败一定次数,则应该打开红色LED。

此代码存在的问题是红色LED指示灯长时间不足以显示扫描灯光时连续丢失的次数很多。解决方案是提出一个中断功能,该功能在处理器内使用不同的时钟,因此测试周期不会延迟,红色LED保持足够长的时间以便操作员了解未命中。

RED / GREEN_LED_ON / OFF是文件中先前定义的按位输出控制器。

在将中断函数放入其中之前,这是原始函数:

#define PASSCOUNTERTHRESHOLD  40//20

   void display( char  event , char test )
    {
        int j,i;

        static int maxConsecPassingCount=0;
        static int consecPassingCount=0; 
        static int passingCount=0; 
        static int timeLimit=0;   // amount of time allowed for non detection before failure is asserted.
        static int consecFails=0;

        if ( event == DETECTED )
        {


            if ( stardata.outputOptions & DUALED )
            {

                if( ++consecPassingCount > 99  && !(EXT_INPUT)) // if no detection for long period , assume no motion and just reset counter
                {
                    RED_LED_OFF;
                    GREEN_LED_ON;
                    return;
                }

                if( timeLimit > 0  && !(EXT_INPUT))
                {
                    // evaluate whether the time limit has been violated by stopping timer and reading counter, reset counter for next time. if limit exceeded turn on red led
                    // else turn on green led
                    TA0CCR0 = 0;
                    i=TA0R;

                    if ( i  > timeLimit )
                    {
                        RED_LED_ON;
                        GREEN_LED_OFF;
                    }
                    else
                    {
                        GREEN_LED_ON;
                        RED_LED_OFF;
                    }
                }
                else
                {
                    RED_LED_OFF;
                    GREEN_LED_ON;
                    timer_us(50);
                    GREEN_LED_OFF;
                }

                consecFails=0;
            }


        }
        else if ( event==NOT_DETECTED)
        {
            passingCount+=consecPassingCount;
            if ( consecPassingCount > maxConsecPassingCount )   // find maximum string of consecutive passing tests out of a group <PASSCOUNTERTHRESHOLD> of tests to estimate the speed.
            {
                maxConsecPassingCount = consecPassingCount;
            }
            consecPassingCount=0;


            if ( passingCount > PASSCOUNTERTHRESHOLD)  // we should have enough data to predict the speed, so now calculate the maximum failing time allowed before we set the fail led.
            {
                passingCount=0;
                timeLimit= maxConsecPassingCount * stardata.starLimits[test].hiTestLimit2; //150;  // amount of time which is allowed between non detect intervals.
                maxConsecPassingCount=0;
            }
            if ( timeLimit > 0 && consecFails == 0 )                // start timer
            {
                TA0R=0;
                TA0CCR0 = 5000;
            }

            if ( consecFails > 100)
            {
                GREEN_LED_OFF;
                RED_LED_ON;
                return;
            }
                //consecFails=0;
            else ++consecFails;
        }


    }

我在main.c文件中定义了中断函数中使用的定时器,如下所示:

 TA2CCTL0=CCIE;

 TA2CTL=TASSEL_2+MC_3+ID__8; 

 TA2CCR0=125000;

这是带有中断功能的代码替换RED_LED_ON输出:

   void display( char  event , char test )

 {

 int j,i;

 static int maxConsecPassingCount=0;

 static int consecPassingCount=0; //kg 8/16

 static int passingCount=0; //kg 8/16

 static int timeLimit=0;   // amount of time allowed for non detection before failure is asserted.

 static int consecFails=0;

 if ( event == DETECTED )

 {

 if ( stardata.outputOptions & DUALED )

 {

 if( ++consecPassingCount > 99  && !(EXT_INPUT)) // if no detection for long period , assume no motion and just reset counter

 {

 RED_LED_OFF;

 GREEN_LED_ON;

 return;

 }

 if( timeLimit > 0  && !(EXT_INPUT))

 {

 // evaluate whether the time limit has been violated by stopping timer and reading counter, reset counter for next time. if limit exceeded turn on red led

 // else turn on green led

 TA0CCR0 = 0;

 i=TA0R;

 if ( i  > timeLimit )

 {

 #pragma vector=TIMER1_A1_VECTOR  //kg

 __interrupt void TIMER1_A1ISR (void)

 {

 RED_LED_ON;

 GREEN_LED_OFF;

 }

 }

 else

 {

 GREEN_LED_ON;

 RED_LED_OFF;

 }

 }

 else

 {

 RED_LED_OFF;

 GREEN_LED_ON;

 timer_us(50);

 GREEN_LED_OFF;

 }

 consecFails=0;

 }

 }

 else if ( event==NOT_DETECTED)

 {

 passingCount+=consecPassingCount;

 if ( consecPassingCount > maxConsecPassingCount )   // find maximum string of consecutive passing tests out of a group <PASSCOUNTERTHRESHOLD> of tests to estimate the speed.

 {

 maxConsecPassingCount = consecPassingCount;

 }

 consecPassingCount=0;

 if ( passingCount > PASSCOUNTERTHRESHOLD)  // we should have enough data to predict the speed, so now calculate the maximum failing time allowed before we set the fail led.

 {

 passingCount=0;

 timeLimit= maxConsecPassingCount * stardata.starLimits[test].hiTestLimit2; //150;  // amount of time which is allowed between non detect intervals.

 maxConsecPassingCount=0;

 }

 if ( timeLimit > 0 && consecFails == 0 ) // start timer

 {

 TA0R=0;

 TA0CCR0 = 5000;

 }

 if ( consecFails > 100)

 {

 #pragma vector=TIMER1_A1_VECTOR  //kg

 __interrupt void TIMER1_A1ISR (void)

 {

 RED_LED_ON;

 GREEN_LED_OFF;

 }

 }

 //consecFails=0;

 else ++consecFails;

 }

    }

我无法获得编译代码,我找不到讨论添加中断的论坛,这些中断的控制方式与我使用它们的方式相同。有没有人对我做错了什么有任何想法?当红色LED亮起以触发中断时,我可以设置全局标志吗?

0 个答案:

没有答案
相关问题