仿真原理图如下
单片机源程序如下:
#include<msp430.h>
void delay_1s(void);
void seg7s(unsigned char data_seg7);
void seg7s2(unsigned char data_seg7);
const unsigned char decoder_seg7[10]
={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void main(void)
{
WDTCTL=WDTPW+WDTHOLD;
P1SEL=0x00;
P1SEL2=0x00;
P1DIR=0xff;
P2SEL=0x00;
P2SEL2=0x00;
P2DIR=0xff;
while(1)
{
P1OUT=0x81;
seg7s(6);
seg7s2(9);
delay_1s();
P1OUT=0x81;
seg7s(5);
seg7s2(8);
delay_1s();
P1OUT=0x81;
seg7s(4);
seg7s2(7);
delay_1s();
P1OUT=0x81;
seg7s(3);
seg7s2(6);
delay_1s();
P1OUT=0x81;
seg7s(2);
seg7s2(5);
delay_1s();
P1OUT=0x81;
seg7s(1);
seg7s2(4);
delay_1s();
P1OUT=0x82;
seg7s(3);
seg7s2(3);
delay_1s();
P1OUT=0x82;
seg7s(2);
seg7s2(2);
delay_1s();
P1OUT=0x82;
seg7s(1);
seg7s2(1);
delay_1s();
P1OUT=0x24;
seg7s(9);
seg7s2(6);
delay_1s();
P1OUT=0x24;
seg7s(8);
seg7s2(5);
delay_1s();
P1OUT=0x24;
seg7s(7);
seg7s2(4);
delay_1s();
P1OUT=0x24;
seg7s(6);
seg7s2(3);
delay_1s();
P1OUT=0x24;
seg7s(5);
seg7s2(2);
delay_1s();
P1OUT=0x24;
seg7s(4);
seg7s2(1);
delay_1s();
P1OUT=0x44;
seg7s(3);
seg7s2(3);
delay_1s();
P1OUT=0x44;
seg7s(2);
seg7s2(2);
delay_1s();
P1OUT=0x44;
seg7s(1);
seg7s2(1);
delay_1s();
}
}
void seg7s(unsigned char data_seg7)
{
unsigned char code_seg7;
code_seg7=decoder_seg7[data_seg7];
P2OUT&=~BIT0;
if(code_seg7&0x80) //D7
{
P2OUT|=BIT1;
}
else
{
P2OUT&=~BIT1;
}
P2OUT|=BIT0;
P2OUT&=~BIT0;
if(code_seg7&0x40) //D6
{
P2OUT|=BIT1;
}
else
{
P2OUT&=~BIT1;
}
P2OUT|=BIT0;
P2OUT&=~BIT0;
if(code_seg7&0x20) //D5
{
P2OUT|=BIT1;
}
else
{
P2OUT&=~BIT1;
}
P2OUT|=BIT0;
P2OUT&=~BIT0;
if(code_seg7&0x10) //D4
{
P2OUT|=BIT1;
}
else
{
P2OUT&=~BIT1;
}
P2OUT|=BIT0;
P2OUT&=~BIT0;
if(code_seg7&0x08) //D3
{
P2OUT|=BIT1;
}
else
{
P2OUT&=~BIT1;
}
P2OUT|=BIT0;
P2OUT&=~BIT0;
if(code_seg7&0x04) //D2
{
P2OUT|=BIT1;
}
else
{
P2OUT&=~BIT1;
}
P2OUT|=BIT0;
P2OUT&=~BIT0;
if(code_seg7&0x02) //D1
{
P2OUT|=BIT1;
}
else
{
P2OUT&=~BIT1;
}
P2OUT|=BIT0;
P2OUT&=~BIT0;
if(code_seg7&0x01) //D0
{
P2OUT|=BIT1;
}
else
{
P2OUT&=~BIT1;
}
P2OUT|=BIT0;
P2OUT&=~BIT0;
}
void seg7s2(unsigned char data_seg7)
{
unsigned char code_seg7;
code_seg7=decoder_seg7[data_seg7];
P2OUT&=~BIT0;
if(code_seg7&0x80) //D7
{
P2OUT|=BIT3;
}
else
{
P2OUT&=~BIT3;
}
P2OUT|=BIT2;
P2OUT&=~BIT2;
if(code_seg7&0x40) //D6
{
P2OUT|=BIT3;
}
else
{
P2OUT&=~BIT3;
}
P2OUT|=BIT2;
P2OUT&=~BIT2;
if(code_seg7&0x20) //D5
{
P2OUT|=BIT3;
}
else
{
P2OUT&=~BIT3;
}
P2OUT|=BIT2;
P2OUT&=~BIT2;
if(code_seg7&0x10) //D4
{
P2OUT|=BIT3;
}
else
{
P2OUT&=~BIT3;
}
P2OUT|=BIT2;
P2OUT&=~BIT2;
if(code_seg7&0x08) //D3
{
P2OUT|=BIT3;
}
else
{
P2OUT&=~BIT3;
}
P2OUT|=BIT2;
P2OUT&=~BIT2;
if(code_seg7&0x04) //D2
{
P2OUT|=BIT3;
}
else
{
P2OUT&=~BIT3;
}
P2OUT|=BIT2;
P2OUT&=~BIT2;
if(code_seg7&0x02) //D1
{
P2OUT|=BIT3;
}
else
{
P2OUT&=~BIT3;
}
P2OUT|=BIT2;
P2OUT&=~BIT2;
if(code_seg7&0x01) //D0
{
P2OUT|=BIT3;
}
else
{
P2OUT&=~BIT3;
}
P2OUT|=BIT2;
P2OUT&=~BIT2;
}
void delay_1s(void)
{
unsigned long data_delay1s;
for(data_delay1s=0;data_delay1s<6654;data_delay1s++);
}