找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4186|回復(fù): 0
收起左側(cè)

MSP430交通燈源程序

[復(fù)制鏈接]
ID:270665 發(fā)表于 2018-1-3 11:07 | 顯示全部樓層 |閱讀模式
#include <MSP430f249.h>
#define uchar unsigned char
#define uint  unsigned int   
uchar const led_tab[]={  0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,   
                        0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};   //共陰數(shù)碼管段選碼表,無小數(shù)點   
uchar disp_buf[4];        //顯示緩沖區(qū)   

void delayus(uint t)
{      
  uint i;     
  while(t--)        
    for(i=1300;i>0;i--);
}  

void  timecount(uint count){
  uint i;
  disp_buf[1] = count / 10;    //對主干道計數(shù)值取十位數(shù)
  disp_buf[0] = count % 10;    //對主干道計數(shù)值取個位數(shù)   
  disp_buf[3] = count / 10;    //對支干道計數(shù)值取十位數(shù)
  disp_buf[2] = count % 10;    //對支干道計數(shù)值取個位數(shù)   
  for(i = 0;i < 25; i++) //總共循環(huán)25次,計時1s         
  {               
     P1OUT = 0;  
     P4OUT = 0;                        
     P1OUT = led_tab[disp_buf[1]];   
     P4OUT = led_tab[disp_buf[3]];            
     P2OUT = ~(BIT0+BIT2); //顯示十位數(shù)              
     delayus(2);     //軟件仿真時,取20不閃爍            
     P1OUT = 0;  
     P4OUT = 0;            
     P1OUT = led_tab[disp_buf[0]];  
     P4OUT = led_tab[disp_buf[2]];           
     P2OUT = ~(BIT1+BIT3); //顯示個位數(shù)              
     delayus(2);     //軟件仿真時,取20不閃爍         
  }
}

void system_Initial(void)
{   
  P3SEL &= 0x00;                                //選擇P3為輸入/輸出口
  P3DIR |= 0Xff;                                //選擇P3為輸出功能

  P1DIR = 0xFF;            // 設(shè)置方向為輸出   
  P4DIR = 0xFF;            // 設(shè)置方向為輸出  
  P2DIR = BIT0 + BIT1+BIT2+BIT3;               
  P1OUT = 0x00;            //LED輸出全部關(guān)閉
  P4OUT = 0x00;            //LED輸出全部關(guān)閉
  P2OUT = BIT0 + BIT1+BIT2+BIT3;      
  P2IE |= BIT4+BIT5+BIT6;    //P2.2中斷使能
  P2IES |= BIT4+BIT5+BIT6;   //P2.2下降沿中斷
  P2IFG &=~(BIT4+BIT5+BIT6);  //P2.2清除中斷標(biāo)志
  _EINT();
}  

// 端口2中斷服務(wù)程序  
#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void)
{      

    P2IFG &= ~(BIT4+BIT5+BIT6);   //清除中斷標(biāo)志   
}  

//**********************************************************************
int main(void)
{      
  uchar count = 12;          //計數(shù)值      
  WDTCTL = WDTPW + WDTHOLD; // 關(guān)閉看門狗   
  system_Initial();
  while(1)   
   {      
      if(!(0x10 & P2IN))  {
        while(1){
          for(count=15;count>=1;count--){
             P3OUT=0x04+0x08;
             timecount(count);                                          
          }

          for(count=5;count>=1;count--){
             P3OUT=0x02+0x08;
             timecount(count);      
          }

          for(count=15;count>=1;count--){
            P3OUT=0x01+0x20;
            timecount(count);      
          }

          for(count=5;count>=1;count--){
            P3OUT=0x01+0x10;
            timecount(count);      
        }      
       }
     }   
   }      
}

回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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