msp430 MCU realizes constant key and short key plus debounce function

Publisher:平和的心态Latest update time:2018-07-11 Source: eefocusKeywords:msp430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The timer is started through the interrupt of port P1, and then the timer interrupt is used to continuously scan the corresponding port status to determine the key

//Timer settings

void TimerA1Init()

{   

  _UINT data = 0 ; 

  TA1CTL = data;//clear all,diaable interrupt,claer ifg 

  TA1CTL |= TASSEL__ACLK | ID_0 | TACLR ; // ACLK, stop mode, ,div:1 ,clear TAR  

  //TA0CTL |= MC__UP ; //Start timer  

  TA1CCTL0 = CCIE ; //ccr0 compare interrupt enable 

  TA1CCR0 = 327; //ccr0 compare mode, ie disable, ifg clear  

}


//P1 port interrupt program

#pragma vector = PORT1_VECTOR

__interrupt void Port1ADISR(void)

  P1IFG &= 0x0f; 

  switch(P1IFG)

  {

    case Key_Sel:


      TA1CTL |= MC__UP; //Start the timer

      Key_Tmp = Key_Sel; 


      break;

    case Key_Menu:


      TA1CTL |= MC__UP; //Start the timer

      Key_Tmp = Key_Menu;


      break;

  }

  P1IFG &= 0x00;


}



/****************************************************** ********** 

Function name: __interrupt void Timer_A1_Count (void) 

Function:

Entry parameters: None 

Export parameters: None 

Remark: 

*************************************************** *********/

#pragma vector = TIMER1_A0_VECTOR

__interrupt void Timer_A1_Count(void)

{  

  static _UINT i = 0;

  _UBYTE P1_Value;

  P1_Value = P1IN;


  P1_Value &= 0x06;

  switch(Key_Tmp)

  {

  case Key_Menu:

    {

      if( (P1_Value & BIT2) == 0) //Button pressed

      {

        i++;

        if(i >= 300) // Constant key processing

        {


          BUZZER_ON;

          i = 0;


          //Timer stop

          TimerA1_Stop; 

        }

      }

      else if((P1_Value & BIT2) != 0)//button release

      {


        if( (i >= 5) && (i < 300) ) // short key

        {


          LCD_B1_RT_ON;

          TimerA1_Stop;

          i= 0;

        }

        else

        {

          i = 0; //Otherwise it is considered as jitter;

        }

      }


      break;

    }

  case Key_Sel:

    {

      if( (P1_Value & BIT1) == 0) //Button pressed

      {

        i++;

      }

      else if( (P1_Value & BIT1) != 0) //Button released

      {


        if(i > 5) // short key processing

        {

          LCD_B1_RT_OFF;

          TimerA1_Stop;

          i = 0;

        }

        else

        {

          i = 0; //Otherwise it is considered as jitter;

        }

      }


      break;

    }

  }

}


Keywords:msp430 Reference address:msp430 MCU realizes constant key and short key plus debounce function

Previous article:MSP430 MCU Theory Review Knowledge Points
Next article:msp430f149 microcontroller serial port C program

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号