代码一:
1 /********************************** 2 程序功能:用按键控制LED灯熄灭 3 ***********************************/ 4 #include <msp430x14x.h> 5 6 void int_clk(); 7 void main() 8 { 9 WDTCTL = WDTPW + WDTHOLD; 10 int_clk(); 11 P1DIR = 0Xf0; 12 P1OUT = 0Xff; 13 P6DIR = 0XFF; 14 P6OUT = 0x00; 15 while(1) 16 { 17 switch(P1IN & 0X0F) 18 { 19 case 0x0e:P6OUT = 0XC0;break; 20 case 0x0d:P6OUT = 0X30;break; 21 case 0x0b:P6OUT = 0X0C;break; 22 case 0x07:P6OUT = 0X03;break; 23 } 24 } 25 } 26 27 void int_clk() 28 { 29 BCSCTL1 &= ~XT2OFF; 30 BCSCTL2 |= SELM_1 + SELS; 31 do 32 { 33 IFG1 &= ~OFIFG; 34 for(int i=100;i>0;i--) 35 _NOP(); 36 }while((IFG1 & OFIFG)!=0); 37 IFG1 &= ~OFIFG; 38 }
代码二:
1 #include <msp430x14x.h> 2 3 void int_clk(); 4 void delay(int i); 5 void main() 6 { 7 int_clk(); 8 //initialize IO 9 P1DIR = 0XF0; 10 P1OUT = 0XFF; 11 P6DIR = 0XFF; 12 P6OUT = 0XFF; 13 14 if(P1IN != 0X0F) 15 { 16 delay(20); 17 if(P1IN != 0X0F) 18 { 19 switch(P1IN & 0X0F) 20 { 21 case 0x0e:P6OUT = 0XFC;break; 22 case 0x0d:P6OUT = 0XF3;break; 23 case 0x0b:P6OUT = 0XCF;break; 24 case 0x07:P6OUT = 0X3F;break; 25 } 26 } 27 } 28 else 29 P6OUT = 0XFF; 30 } 31 32 void int_clk() 33 { 34 BCSCTL1 &= ~XT2OFF; 35 BCSCTL2 |= SELM_1 + SELS; 36 do 37 { 38 IFG1 &= ~OFIFG; 39 for(int i=1;i<100;i++) 40 _NOP(); 41 }while((IFG1 & OFIFG)!=0); 42 IFG1 &= ~OFIFG; 43 } 44 45 void delay(int i) 46 { 47 while(i--); 48 }