单片机源程序如下:
#include "msp430f5529.h"
long unsigned int n;
void delay(unsigned int x)
{
int i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
#define K1 (P2IN & BIT1)
int flag = 0,flag1 = 0;
void main(void){
WDTCTL=WDTPW+WDTHOLD; //关闭看门狗
P1DIR = BIT0;
P1OUT &= ~BIT0;// 灯暗
P4DIR = BIT7;
P4OUT &= ~BIT7;
P2DIR &= ~BIT1; //将P2.1脚设置为输入方式
P2OUT |= BIT1;
P2REN |= BIT1; //设置为内部上拉
P2IE |= BIT1; //P2.1中断使能
P2IES |= BIT1; //置1,设置为下降沿触发
P2IFG &= ~BIT1; //清除P2.1的中断标志位
P1DIR &= ~BIT1; //将P2.1脚设置为输入方式
P1OUT |= BIT1;
P1REN |= BIT1; //设置为内部上拉
P1IE |= BIT1; //P2.1中断使能
P1IES |= BIT1; //置1,设置为下降沿触发
P1IFG &= ~BIT1; //清除P2.1的中断标志位
TA0CTL = TASSEL_2 + TACLR + MC_1;//设置定时器时钟源为SCMLK,8分频,允许置位,增计计数 //
TA0CCTL0 = CCIE; //允许使能中断
TA0CCR0 = 1060; //设置定时周期 1ms
__bis_SR_register(LPM0_bits + GIE); //复位CPUOFF并打开SCMLK,开可屏蔽中断}//进入低功耗模式
__enable_interrupt(); //开总中断
while(1)
{
}
}
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A(void)
{
n++;
if(n == 5000)//5s
{
flag1=1;
n=0;
}
}
#pragma vector=PORT2_VECTOR //固定格式,声明中断向量地址
__interrupt void Port2_ISR(void) //中断子程序
{
if(flag1!=1)
{
__delay_cycles(100);
while(K1 == 0);
P1OUT ^= BIT0;
P2IFG &= ~BIT1;
P1IE &= ~BIT1;
}
else
{
P2IE &= ~BIT1;
}
}
#pragma vector=PORT1_VECTOR //固定格式,声明中断向量地址
__interrupt void Port1_ISR(void) //中断子程序
{
if(flag1!=1)
{
__delay_cycles(100);
while((P1IN&BIT1) == 0);
P4OUT ^= BIT7;
P1IFG &= ~BIT1;
P2IE &= ~BIT1;
}
else
{
P1IE &= ~BIT1;
}
}
|