[Launchpad] 按键消抖测试

LaunchPad按键消抖测试,推荐定时方式

 

 1 #include  <msp430g2553.h>

 2 #define TYPE 0  //0 定时  1 延时

 3 void main(void)

 4 {

 5   WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer

 6   BCSCTL1 = CALBC1_1MHZ;                    // Set range

 7   DCOCTL = CALDCO_1MHZ;

 8   

 9   BCSCTL2 &= ~(DIVS_3);                     // SMCLK = DCO = 1MHz     

10   P1DIR = 0x01;                             // P1.0 output, else input

11   P1OUT =  0x08;                            // P1.4 set, else reset

12   P1REN |= 0x08;                            // P1.4 pullup

13   P1IE |= 0x08;                             // P1.4 interrupt enabled

14   P1IES |= 0x08;                            // P1.4 Hi/lo edge

15   P1IFG &= ~0x08;                           // P1.4 IFG cleared

16 

17   _BIS_SR(LPM0_bits + GIE);                 // Enter LPM4 w/interrupt

18   

19   

20 }

21 

22 // Port 1 interrupt service routine

23 #pragma vector=PORT1_VECTOR

24 __interrupt void Port_1(void)

25 {

26 

27 # if(TYPE)

28  //----------------------------------------

29  //延时按键消抖 

30   

31   volatile unsigned int i;

32   P1IFG &= ~0x08;                           // P1.4 IFG cleared

33   for(i=500;i>0;i--);

34  

35   if(!(P1IN & 0x08))

36     P1OUT ^= 0x01;                            // P1.0 = toggle

37   

38 

39 #else 

40 //-----------------------------------------

41 //看门狗定时按键消抖

42    

43   P1IFG &= ~0x08;  

44   WDTCTL = WDT_MDLY_32;                    // Set Watchdog Timer interval to ~30ms

45   IFG1 &= ~WDTIFG;

46   IE1 |= WDTIE; 

47 

48 #endif

49 

50 

51 }

52 

53 # if(!TYPE)

54 

55 #pragma vector=WDT_VECTOR

56 __interrupt void watchdog_timer(void)

57 {

58   

59   IE1 &= ~WDTIE;  

60   IFG1 &= ~WDTIFG;                 /* clear interrupt flag */

61   WDTCTL = WDTPW + WDTHOLD; 

62     

63   if(!(P1IN & 0x08))

64       P1OUT ^= 0x01;                            // P1.0 = toggle 

65 }

66 

67 #endif

 

你可能感兴趣的:(HP)