#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}; //共阴数码管段选码表,无小数点
uchar disp_buf[4]; //显示缓冲区
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; //对主干道计数值取十位数
disp_buf[0] = count % 10; //对主干道计数值取个位数
disp_buf[3] = count / 10; //对支干道计数值取十位数
disp_buf[2] = count % 10; //对支干道计数值取个位数
for(i = 0;i < 25; i++) //总共循环25次,计时1s
{
P1OUT = 0;
P4OUT = 0;
P1OUT = led_tab[disp_buf[1]];
P4OUT = led_tab[disp_buf[3]];
P2OUT = ~(BIT0+BIT2); //显示十位数
delayus(2); //软件仿真时,取20不闪烁
P1OUT = 0;
P4OUT = 0;
P1OUT = led_tab[disp_buf[0]];
P4OUT = led_tab[disp_buf[2]];
P2OUT = ~(BIT1+BIT3); //显示个位数
delayus(2); //软件仿真时,取20不闪烁
}
}
void system_Initial(void)
{
P3SEL &= 0x00; //选择P3为输入/输出口
P3DIR |= 0Xff; //选择P3为输出功能
P1DIR = 0xFF; // 设置方向为输出
P4DIR = 0xFF; // 设置方向为输出
P2DIR = BIT0 + BIT1+BIT2+BIT3;
P1OUT = 0x00; //LED输出全部关闭
P4OUT = 0x00; //LED输出全部关闭
P2OUT = BIT0 + BIT1+BIT2+BIT3;
P2IE |= BIT4+BIT5+BIT6; //P2.2中断使能
P2IES |= BIT4+BIT5+BIT6; //P2.2下降沿中断
P2IFG &=~(BIT4+BIT5+BIT6); //P2.2清除中断标志
_EINT();
}
// 端口2中断服务程序
#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void)
{
P2IFG &= ~(BIT4+BIT5+BIT6); //清除中断标志
}
//**********************************************************************
int main(void)
{
uchar count = 12; //计数值
WDTCTL = WDTPW + WDTHOLD; // 关闭看门狗
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);
}
}
}
}
}
|