找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1882|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

秒表程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:325084 發(fā)表于 2018-5-8 15:24 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
/*
****************************************************************************
* 程序說(shuō)明 ;使數(shù)碼管依次顯示0-8
****************************************************************************
*/
#include "io430.h"
#define uint unsigned int
#define uchar unsigned char
#define KeyPort         P1IN    //獨(dú)立鍵盤(pán)接在P60~P63
#define CPU_F ((double)1000000)
#define DelayUs(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define DelayMs(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
#define KEY1  1
#define KEY2  2
#define KEY3  3
#define KEY4  4
#define KEY5  5
uchar KeyScan(void);
#define SMG_DATA    P4OUT           //573位選信號(hào)的輸入管腳
#define SMG_WEI  P5OUT
//#define CLK_H       P6OUT|=BIT6     //595時(shí)鐘信號(hào)的輸入置高
//#define CLK_L       P6OUT&=~BIT6    //595時(shí)鐘信號(hào)的輸入置低
//#define ST_H        P6OUT|=BIT5     //595鎖存信號(hào)置高
//#define ST_L        P6OUT&=~BIT5    //595鎖存信號(hào)置低
//#define DATA_H      P6OUT|=BIT4    //595數(shù)據(jù)信號(hào)輸入置高
//#define DATA_L      P6OUT&=~BIT4    //595數(shù)據(jù)信號(hào)置低
//#define LED_E0      P5OUT&=~BIT7     //數(shù)碼管 573使能置低
//#define LED_E1      P5OUT|=BIT6     //點(diǎn)陣 573使能置高
uchar uc7leds[17]={0x3f,0x06,0x5b,0x4f,   //0,1,2,3,
                   0x66,0x6d,0x7d,0x07,          //4,5,6,7,
                   0x7f,0x6f,0x77,0x7c,          //8,9,A,b,
                   0x39,0x5e,0x79,0x71,0x00};  //C,d,E,F
uchar jishu=0;
uchar mode=1;
uchar downtime=1;
void PortInit();
uchar number[4] = {0,0,0,0};
void ClockInit();
void display();
void TimerAInit();
void fentime(int num);
void fenshu(int num);
void main()
{
  uint b;
  WDTCTL=WDTPW+WDTHOLD;
  ClockInit();    //時(shí)鐘初始化
  TimerAInit();
  PortInit();
  uchar jishi=0;
  uchar keyvalue=0;
  uchar zhu=1;  
  uchar wait=1;
  uchar mode2start=0;
  
  while(1)
  {
    if(b==0)  
      keyvalue=KeyScan();
    //*
    if(zhu==1)
    {
      if(keyvalue==5)
        zhu=0;        
      if(keyvalue==4)
        mode++;
      if(mode==4)
        mode=1;
        number[0]=mode+9;
        number[1]=16;
        number[2]=16;
        number[3]=16;      
    }

    else if(mode==1)
    {
      if(wait)
      {
        fentime(0);
        wait=0;
      }
      if(keyvalue==1)
      {
        jishi=!jishi;
        if(jishi)
          TACTL=TASSEL_1+MC_1+ID_3+TAIE;
        else
          TACTL=TASSEL_1+MC_0+ID_3+TAIE;        
      }
   }
   else if(mode==2)
   {
     if(!mode2start)
     {
       if(wait)
       {
         fentime(60);
         wait=0;
       }
       else
       {  
         if(keyvalue==4)
         {
         downtime++;
         fentime(downtime*60);
         }
         if(keyvalue==1)
         {
           mode2start=1;
           TACTL=TASSEL_1+MC_1+ID_3+TAIE;
         }
       }
     }
     else
     {
      
     }      
   }
   if(keyvalue==2)
   {
      jishi=0;
      keyvalue=0;
      zhu=1;  
      wait=1;
      mode2start=0;
      jishu=0;
      mode=1;
      downtime=1;
      TACTL=TASSEL_1+MC_0+ID_3+TAIE+TACLR;
      P2OUT_bit.P0=0;   
     
   }
   


    display();
    if(keyvalue!=0)
    {
      b=0x8fff;
      keyvalue=0;      
    }
    if(b!=0)
      b--;
  }
}

/*
****************************************************************************
*                         PortInit()
* 功能說(shuō)明 ;IO初始化
*   參數(shù)   :無(wú)
* 返回值   :無(wú)
****************************************************************************
*/
void PortInit()
{
  P4SEL=0X00;//普通
  P4DIR=0XFF;//輸出
  P5SEL=0X00;//普通
  P5DIR=0XF0;//輸出  
  P5OUT=0XFF;
  P2SEL = 0x00;   //設(shè)置IO口為普通I/O模式
  P2DIR = 0x01;   //設(shè)置IO口方向?yàn)檩敵?br />   P2OUT = 0x00;   //初始設(shè)置為00,即都為低電平
  P1SEL = 0x00;
  P1DIR = 0x00;
  P4OUT=uc7leds[0];
  //P6SEL=0X00;
  //P6DIR=0XFF;
  //P5SEL=0X00;
  //P5DIR=0XFF;
  //LED_E1;
  //LED_E0;
}


/*
****************************************************************************
*                         TimerAInit()
* 功能說(shuō)明 ;定時(shí)器A初始化
*   參數(shù)   :無(wú)
* 返回值   :無(wú)
****************************************************************************
*/

void TimerAInit()
{
TACTL=TASSEL_1+MC_0+ID_3+TAIE;//選擇ACK,增計(jì)數(shù)模式,8分頻,使能定時(shí)器中斷
CCR0=4095;
  __enable_interrupt();
}

void ClockInit()
{
  uchar i;

  BCSCTL1&=~XT2OFF;   //設(shè)置XT2為有效
  BCSCTL2|=SELM1+SELS;
  do{
    IFG1&=~OFIFG;   //清除振蕩器失效標(biāo)志
    for(i=0;i<100;i++)
       __no_operation();
  }while((IFG1&OFIFG)!=0);    //如果振蕩失效標(biāo)志存在則繼續(xù)循環(huán)
  IFG1&=~OFIFG;
}





/*
****************************************************************************
*                         TimerA()
* 功能說(shuō)明 ;定時(shí)器A中斷處理程序
*   參數(shù)   :無(wú)
* 返回值   :無(wú)
****************************************************************************
*/
#pragma vector = TIMERA1_VECTOR
__interrupt void TimerA(void)
{
  switch(TAIV)
  {
case 2:break;
case 4:break;
case 10:
        jishu++;
        if(mode==1)
        {
          fentime(jishu);
        }
        else if(mode==2)
        {
          fentime(downtime*60-jishu);
          if(!(downtime*60-jishu))
          {
            P2OUT_bit.P0=1;
            TACTL=TASSEL_1+MC_0+ID_3+TAIE;            
          }
        }
        break;
  }
}
/*
****************************************************************************
*                       uchar KeyScan(void)
* 功能說(shuō)明 ;按鍵檢測(cè)
*   參數(shù)   :無(wú)
* 返回值   :KeyNum:按下的按鍵值
****************************************************************************
*/
uchar KeyScan(void)
{
  uchar keycode;
    static uchar sta=0;       //不支持連續(xù)按
    //uchar sta=0;                //支持連按
    uchar tmp=0;
   
    P1DIR  |= 0X1f;
    P1OUT = 0X1f;
    P1DIR  &=  0xe0;    //設(shè)置P10 P11 P12為輸入
    if(sta==0)                  //sta=0 時(shí)檢測(cè)按鍵是否按下
    {
         if((P1IN&0X1f)!=0X1f)     // 掃描按鍵
         {
            DelayMs(10);
            if((P1IN&0X1f)!=0X1f)  //確認(rèn)按鍵按下
            {
              
              keycode=(P1IN&0X1f);
              switch(keycode)           //確認(rèn)按鍵
              {               
              case 0x1e:tmp=1;;break;
              case 0x1d:tmp=2;;break;
              case 0x1b:tmp=3;;break;
              case 0x17:tmp=4;;break;
              case 0x0f:tmp=5;;break;
              default : ;break;
            }
            sta=1;
            }
            
         }
    }
   
   else if((P1IN&0X1f)!=0X1f)   // 確定按鍵是否松開(kāi)
    {
      DelayMs(10);
      if((P1IN&0X1f)!=0X1f)     //若是按鍵松開(kāi),sta = 0;
        sta=0;
    }
   
   return tmp;          //返回按下的按鍵數(shù)

}

void fenshu(int num)
{
  number[0]=num%10;
  number[1]=(num%100)/10;
  number[2]=(num%1000)/10;
  number[3]=num/1000;  
}
void fentime(int num)
{
  number[0]=(num%60)%10;
  number[1]=(num%60)/10;
  number[2]=(num/60)%10;
  number[3]=(num/60)/10;   
}

void display()           //數(shù)碼管顯示
{
        static uchar i=0;
        i++;
        if(i >= 4)
                i = 0;        
        P4OUT =0x00;                //消隱
        switch(i)               //位選
        {
                case 3: SMG_WEI=0x10; break;
                case 2: SMG_WEI=0x20; break;
                case 1: SMG_WEI=0x40; break;
                case 0: SMG_WEI=0x80; break;
        }        
        P4OUT = uc7leds[number];
}
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表