MSP430 流水灯 按键控制LED灯 数码管静态显示

1.流水灯

#include "msp430f149.h"
#include "config.h"
void delay(int i)
{
  while(i--);
}
void main( void )
{
    WDTCTL = WDTPW + WDTHOLD;//关闭看门狗
    BoardConfig();
    unsigned int io=0x01,i=0;//用来控制灯亮灭,现在最低位为1,其余为为0
    //初始化
    P2DIR|=0xff;//设置P2所有端口为输出方向
    P2OUT=0xff;
    
    while(1)
    {
        io=0x01;
        for(i=0;i<8;i++)
        {
            P2OUT=~io;
            delay(10000);
            io<<=1;
        }
    }
}

2.按键控制LED灯

#include "msp430f149.h"
#include "config.h"

void delay(int i)
{
  while(i--);
}
void main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  BoardConfig();
  _EINT();
  P1IE|=0X01;
  P1IES!=0X01;
  P1IFG&=~0X01; //清除中断标志位
  //P1REN|=0X01;  //使能上下拉电阻
  P1OUT|=0X01;   //设置为上拉电阻
  
  P2DIR|=0X01; //设置P2.0口为输出
  P2OUT|=0X01;
  
  while(1)
  {;
  }
}

#pragma vector=PORT1_VECTOR
__interrupt void PORT2_ISR(void)//端口2中断
{
  delay(100);
  if(P1IFG&0X01)
  {
    while(P1IN&0X01==0);//等待
    delay(500);
    P2OUT^=0X01;//反转灯的亮灭状态
  }
  //清除中断标志位
  P1IFG&=~0X01;
}

3.数码管静态显示

#include "msp430f149.h"
#include "config.h"

void delay(int i)
{
  while(i--); 
}
void main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  unsigned int i=0;
  BoardConfig();
  unsigned int data[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
  P5DIR|=BIT5;
  P6DIR|=BIT6;//P5.5 P5.6设置为输出方向
  P6OUT&=~BIT6;//P5.6初始为低电平
  P5OUT|=BIT5;//P5.5初始为高电平
  P4DIR|=0XFF;//P4所有端口设置为输出方向
  P4OUT=0X00;
  P4OUT|=0XEF;//选择Y4;
  P5OUT&=~BIT5;//关闭位选
  P6OUT|=BIT6;//打开段选
  P4OUT=0X3F;//首先为0

  while(1)
  {
    for(i=0;i<=15;i++)
    {
      P4OUT=data[i];
      delay(10000);
      delay(10000);
      delay(10000);
      if(i>15)
      {
        i=0;
      }
    }
  }
}

 

posted @ 2020-02-06 15:52  阳光中的影子  阅读(1552)  评论(0编辑  收藏  举报