基于MSP430F249的电子钟仿真(源码+仿真)

发布于:2024-04-28 ⋅ 阅读:(29) ⋅ 点赞:(0)

目录

1、前言

2、仿真

3、程序

资料下载地址:基于MSP430F249的电子钟仿真(源码+仿真)

 1、前言

        基于MSP430F249的电子钟仿真,数码管显示时分秒,并可以通过按键调节时间。

2、仿真

 3、程序

#include <MSP430x24x.h>
#define uchar unsigned char
#define uint unsigned int
uchar const table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,  //共阴数码管段选码表,无小数点
                               0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
struct {
   uchar hou;
   uchar min;
   uchar sec;
}time;
struct {
   uchar hou:1;
   uchar min:1;
   uchar sec:1;
}Flashflag;
uchar Disbuf[8],Index;
uint count1ms,FlashCnt;
uchar Keytimes=0;
void delayms(uint t)
{
    uint i;
    while(t--)
      for(i=1330;i>0;i--);//进过参数的调整 
}
void Init_Timer_A(void)
{
    CCTL0 = CCIE; // 使能CCR0 中断
    TAR=0xFC18;  // 计数装入初值
   TACTL=TASSEL_2+MC_2+TAIE+ID_3;//设置时钟源和计数模式 采用SMCLK/8 =1Mhz                          

}
void Refreshtime(void)
{
    if(Flashflag.hou)
    {
      Disbuf[1]=table[time.hou%10];
      Disbuf[0]=table[time.hou/10]; 
    }
    else
    {
      Disbuf[1]=0x00;
      Disbuf[0]=0x00; 
    }
     if(Flashflag.min)
    {
       Disbuf[3]=table[time.min/10];
       Disbuf[4]=table[time.min%10];
    }
    else
    {
      Disbuf[3]=0x00;
      Disbuf[4]=0x00; 
    }
     if(Flashflag.sec)
    {
      Disbuf[6]=table[time.sec/10];
      Disbuf[7]=table[time.sec%10]; 
    }
    else
    {
      Disbuf[6]=0x00;
      Disbuf[7]=0x00; 
    }
 
   
}
uchar ReadKey(void)
{
   static uchar count;
   uchar temp;
   temp=P2IN&0x07;
   if(temp!=0x07)
   {
      if(++count==10)
      {
          return temp;
          
      }else if(count>=200)
      {
          count=180;
          return temp;
      }else
        return 0xFF;
   }else
   {    
     count=0;
     return 0xFF;
   }  
}
void KeyProcess(void)
{
    
    uchar temp;
    temp=ReadKey();
    if(temp!=0xFF)
    {
        temp=temp^0x07;
        if(temp&0x01)
        {
            if(++Keytimes==4)
            {
              Keytimes=0;
               Flashflag.hou=1;
               Flashflag.min=1;
               Flashflag.sec=1;
               FlashCnt=0;
            } 
        }
         if(temp&0x02)
        {
            if(Keytimes==1)
            {
                if(++time.hou>=24)
                  time.hou=0;
            }
            else if(Keytimes==2)
            {
                if(++time.min>=60)
                  time.min=0;
            }
            else if(Keytimes==3)
            {
                 if(++time.sec>=60)
                  time.sec=0;
            }
        }
         if(temp&0x04)
        {
            if(Keytimes==1)
            {
                if(time.hou--==0)
                  time.hou=23;
            }
            else if(Keytimes==2)
            {
                if(time.min--==0)
                  time.min=59;
            }
            else if(Keytimes==3)
            {
                 if(time.sec--==60)
                  time.sec=59;
            }
        }
       
    }
}

void main(void)
{
    
    WDTCTL=WDTPW + WDTHOLD; // 关闭看门狗
    P4DIR=0x01; 

    P1DIR=0xFF;            // 设置方向
    P1SEL=0;            // 设置为普通I/O 口
    
    P3DIR=0xFF;            // 设置方向
    P3SEL=0;            // 设置为普通I/O 口
   
    P1OUT=0x00;
    P3OUT=0xFF;
    
    P2DIR=0x0;
    P2OUT=0x07;
    P2SEL=0x00;  
    
    time.hou=12;
    Flashflag.hou=1;
    Flashflag.min=1;
    Flashflag.sec=1;
    Disbuf[2]=0x40;
    Disbuf[5]=0x40;
    Refreshtime();
    Init_Timer_A();
    _BIS_SR(LPM0_bits+ GIE);//进入低功耗睡眠模式
   
    while(1)
    {
       
          P3OUT=0xFF;
          if(++Index==8) Index=0;
          delayms(1);
          
    }

    
}
#pragma vector=TIMERA0_VECTOR //定时器A 中断服务程序
__interrupt void Timer_a(void) 
{ 
  
  switch(TAIV)         //TAIV 表示中断向量号  
  { 
  case 2:break; 
  case 4:break; 
  case 10:              //TAIV=10 表示中断计数器溢出中断  
  TAR=0xFC18;// (65536-1000) 
  P4OUT=P4OUT^0x01;
  if(++count1ms==220)
  {
    time.sec++;
    count1ms=0;
    if(time.sec>=60)
    {
      time.sec=0;
      time.min++;
      if(time.min>=60)
      {
         time.min=0;
         if(++time.hou>=24)
           time.hou=0;
       
      }
    }
  }
  if(Keytimes)
  {
    if(++FlashCnt>=80)
    {
   
      if(Keytimes==1)
      {
         Flashflag.hou=!Flashflag.hou;
         Flashflag.min=1;
         Flashflag.sec=1;
      }
      else if(Keytimes==2)
      {
         Flashflag.min=!Flashflag.min;
         Flashflag.hou=1;
         Flashflag.sec=1;
      }
      else if(Keytimes==3)
      {
         Flashflag.sec=!Flashflag.sec;
         Flashflag.hou=1;
         Flashflag.min=1;
      }
      FlashCnt=0;
    }
  }
  KeyProcess();
  Refreshtime(); 
           
 //TACCR0=0xF000;
  break; 
  }
  LPM0_EXIT;

}


网站公告

今日签到

点亮在社区的每一天
去签到