【MSP430F149】按键控制数码管实现电子表功能

#include 
void second();      //秒函数 
void minute();          //分函数 
void hour();          //时函数 
int h1 =0,h2 = 0,m1 = 0,m2 = 0,s1 = 0,s2 = 0;        //初始时间 
int temp=0;//时钟暂定标记 1代表时间暂停 0表示时间流动
int Temp=0;//时钟开始流动 按键按两次 temp转换为0

#define duan_l P6OUT &= ~BIT6   //段拉低 
#define duan_h P6OUT |= BIT6     //段拉高 
#define wei_l P5OUT &= ~BIT5       //位拉低 
#define wei_h P5OUT |= BIT5        //位拉高 
char duan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};     //0-9 
char wei[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};     //位数 
void main( )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;//关狗 
  TACTL = TASSEL_1 + TACLR + MC_1;    // 辅助时钟+计数内容清零+增计数 
  CCTL0 |= CCIE;//CCR0中断允许 
  CCR0 = 32767;
  P1DIR = 0XF0;
  P3DIR = 0XFF;
  P3OUT = 0X00;
  P5DIR = 0XFF;
  P5OUT = 0XFF;
  P6DIR = 0XFF;
  P6OUT = 0XFF;
  P4DIR = 0XFF;
  P4OUT = 0XFF;
  P1DIR=0xf0;//连通所有按键
  P1OUT=0x0f;//连通所有按键
  P1IE=0x0f;
  P1IES=0x0f;
  P1IFG=0x00;
 
  while(1)
  {
    _BIS_SR(CPUOFF + GIE);//进入CPU低功耗模式 
  }
}
      
#pragma vector=PORT1_VECTOR
__interrupt void PORT1()
{
  switch(P1IFG)
  {
  case 0x01://控制小时数码管
  {  
       h1++;
       if(h1 >= 10)
       {
         h1 = 0;
         h2++;
       }
       if(h2>=2&&h1==4)
       {
     

h1= h2 = 0;           
       }
       P1IFG=0x00;
       break;
  }
  case 0x02://控制分钟
  {     
       m1++;
       if(m1 >= 10)
       {
         m1 = 0;
         m2++;
       }
       if(m2>=6)
       {
         m2 = 0;
         h1++;
       }
    P1IFG=0x00;
    break;
  }
  case 0x04://控制秒
  {
    s1++;
    if(s1 >= 10)
    {
      s1 = 0;
      s2++;
    }
    if(s2 >= 6)
    {
      s2=0;
      m1++;
    }
    P1IFG=0x00;
    break;
  }
  case 0x08://控制时间暂停与启动
  {
    Temp++;
    if(Temp==2)
    {
      Temp=0;
      temp=0;
    }
    else
      temp=1;
    P1IFG=0x00;
    break;
  }
  }
}  

//timer_A0中断服务程序 
#pragma vector = TIMERA0_VECTOR
__interrupt void smlight(void)
{ 
    for(int i=0;i<2000;i++) //数码管显示时间
    {
    P4OUT = duan[s1];
    duan_h;
    duan_l;
    P4OUT =wei[0];
    wei_h;
    wei_l;                         //0位显示初始值 
    P4OUT = 0XFF;
    wei_h;
    wei_l;
    P4OUT = duan[s2];
    duan_h;
    duan_l;
    P4OUT =wei[1];
    wei_h;
    wei_l;                          //1位初始 
    P4OUT = 0XFF;
    wei_h;
    wei_l;
    P4OUT = 0X40;                  //短杠 
    duan_h;
    duan_l;
    P4OUT = wei[2];
    wei_h;
    wei_l;
    P4OUT = 0XFF;
    wei_h;
    wei_l;
    P4OUT = duan[m1];
    duan_h;
    duan_l;
    P4OUT =wei[3];
    wei_h;
    wei_l;
    P4OUT = 0XFF;
    wei_h;
    wei_l;
    P4OUT = duan[m2];
    duan_h;
    duan_l;
    P4OUT =wei[4];
    wei_h;
    wei_l;
    P4OUT = 0XFF;
    wei_h;
    wei_l;
    P4OUT = 0X40;
    duan_h;
    duan_l;
    P4OUT = wei[5];
    wei_h;
    wei_l;
    P4OUT = 0XFF;
    wei_h;
    wei_l;
    P4OUT = duan[h1];
    duan_h;
    duan_l;
    P4OUT = wei[6];
    wei_h;
    wei_l;
    P4OUT = 0XFF;
    wei_h;
    wei_l;
    P4OUT = duan[h2];
    duan_h;
    duan_l;
    P4OUT =wei[7];
    wei_h;
    wei_l;
    P4OUT = 0XFF;
    wei_h;
    wei_l;
    }
    if(temp != 1)//控制时间是否流动
    {
      second();
      minute();
      hour();
    } 
}

void second()//秒函数
{   
    s1++;  
    if(s1 == 10)
	{
      s1 = 0;
      s2++;
    }  
}

void minute()//分钟函数
{ 
    if(s2==6)
    {
      s2 = 0;
      m1++;
    }   
    if(m1 == 10)
    {
      m1 = 0;
      m2++;
    } 
}

void hour()//小时函数
{   
    if(m2==6)
    {
      m2 = 0;
      h1++;
    }   
    if(h1 ==10)
	{
      h1 = 0;
      h2++;
    }
    if(h2 == 2&&h1==4)
      h2 = 0;
}

你可能感兴趣的:(MSP430,单片机)