其中,按键按一次数码管变化一次
#include "msp430g2553.h" #define uint unsigned int #define uchar unsigned char void delay(uint z); int num,wei; unsigned long i; uchar table[11]={ 0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f, 0x00 }; uchar table1[8]={ 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; void keyscan(); void main() { // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; //主系统时钟切换为外部高速晶振 if (CALBC1_8MHZ == 0xFF || CALDCO_8MHZ == 0xFF) { while(1); // If calibration constants erased, trap CPU!! } // Configure Basic Clock BCSCTL1 = CALBC1_8MHZ; // Set range DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation BCSCTL3 |= LFXT1S_2; // Set LFXT1 P1DIR=0XFF;//p4作为输出 P1DIR&=~BIT3;//P1.3按键输入 P2DIR=0XFF;//数码管段输出
P1IE |=BIT3; //使能按键P1.3引脚中断 P1IES&=~BIT3; //中断触发方式为上升降沿 P1IFG= 0x00; //清除中断标志位 _EINT();//开总中断 while(1) { P2OUT=table[10]; P1OUT|=BIT5;//设置p5.5为高电平 P2OUT=table1[wei]; P1OUT^=BIT5;//设置p5.5为低电平 P1OUT|=BIT6; P2OUT=table[num]; P1OUT^=BIT6; for(i=0;i<100000;i++); P2OUT=table[10]; } } #pragma vector=PORT1_VECTOR __interrupt void PORT1_ISR(void) { for(i=0;i<1000;i++);//消抖 if(P1IFG&BIT3) { num++; wei++; if(num==10) num=0; if(wei==8) wei=0; } P1IFG=0; //清除中断标志位 }
|