[LaunchPad] 超声波测试,数码管显示

  1 #include "io430g2553.h"

  2 

  3 unsigned char    Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};  //段码控制 

  4 unsigned char    dispbit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F};    //位选控制 

  5 

  6 unsigned char LedOut[4];

  7 

  8 unsigned int num =1234;

  9 unsigned int time;

 10 unsigned char i;

 11 

 12 unsigned char RxFlag;

 13 unsigned char Byte_H;

 14 unsigned char Byte_L;

 15 

 16 void selectled(unsigned char led)

 17  {

 18    switch(led)

 19    {

 20    case 0:

 21      P2OUT_bit.P5=0;

 22      P1OUT_bit.P6=1; 

 23      P1OUT_bit.P7=1;

 24      break;

 25    case 1:

 26      P2OUT_bit.P5=1;

 27      P1OUT_bit.P6=0; 

 28      P1OUT_bit.P7=1;

 29      break;

 30    case 2:

 31      P2OUT_bit.P5=1;

 32      P1OUT_bit.P6=1; 

 33      P1OUT_bit.P7=0;

 34      break;

 35    default:

 36      P2OUT_bit.P5=1;

 37      P1OUT_bit.P6=1; 

 38      P1OUT_bit.P7=1;

 39      break;  

 40    }

 41  }

 42  

 43  void showled(unsigned char led)

 44  {

 45  

 46    if(led & 0x01)    P1OUT_bit.P3=1;

 47     else P1OUT_bit.P3=0;

 48     

 49    if(led & 0x02)    P1OUT_bit.P4=1;

 50     else P1OUT_bit.P4=0;

 51     

 52    if(led & 0x04)    P1OUT_bit.P5=1;

 53     else P1OUT_bit.P5=0;

 54     

 55    if(led & 0x08)    P2OUT_bit.P0=1;

 56     else P2OUT_bit.P0=0;

 57     

 58    if(led & 0x10)    P2OUT_bit.P1=1;

 59     else P2OUT_bit.P1=0; 

 60     

 61    if(led & 0x20)    P2OUT_bit.P2=1;

 62     else P2OUT_bit.P2=0; 

 63     

 64    if(led & 0x40)    P2OUT_bit.P3=1;

 65     else P2OUT_bit.P3=0; 

 66     

 67    if(led & 0x80)    P2OUT_bit.P4=1;

 68     else P2OUT_bit.P4=0; 

 69  

 70  }

 71  

 72   int putchar(int ch)

 73  {

 74    while(!(IFG2&UCA0TXIFG));

 75     UCA0TXBUF=ch;

 76     return ch;

 77  }

 78  

 79 

 80   void sendChar(unsigned char c)

 81   {

 82     while(!(IFG2&UCA0TXIFG));

 83     UCA0TXBUF=c;

 84   }

 85 

 86 void main()

 87 {

 88   

 89 WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer

 90   

 91 P1DIR=0xff;

 92 P2DIR=0xff;

 93 

 94   P1SEL = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD

 95   P1SEL2 = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD

 96   UCA0CTL1 |= UCSSEL_2;                     // SMCLK

 97   UCA0BR0 = 104;                            // 1MHz 9600

 98   UCA0BR1 = 0;                              // 1MHz 9600

 99   UCA0MCTL = UCBRS0;                        // Modulation   UCBRSx = 1

100   UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine** 

101   IE2 |= UCA0RXIE;   // Enable USCI_A0 RX interrupt 

102   __bis_SR_register(GIE);

103   

104   

105 while(1)

106 {

107   

108 time++;

109 if(time>50)

110 {

111   time=0;

112   sendChar(0x55);

113   __delay_cycles(1000);

114   if(num>500) P1OUT_bit.P0=1;

115   else P1OUT_bit.P0=0;

116 }

117      LedOut[3]=Disp_Tab[num%10000/1000];       //千位

118      LedOut[2]=Disp_Tab[num%1000/100];

119      LedOut[1]=Disp_Tab[num%100/10];           //十位

120      LedOut[0]=Disp_Tab[num%10];             //个位

121   

122 for(i=0;i<4;i++)

123 {

124   showled(LedOut[i]);

125   selectled(i);

126     __delay_cycles(1000);

127 }

128 }

129 

130 }

131 

132 #pragma vector=USCIAB0RX_VECTOR

133   __interrupt void USCI0RX_ISR(void)

134   {

135  

136   while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?

137   

138    if(RxFlag == 0)

139    {

140      Byte_H=UCA0RXBUF;

141      RxFlag=1;

142    }

143    else

144    { 

145        Byte_L=UCA0RXBUF;

146        num=Byte_H*256+Byte_L;

147        RxFlag = 0;

148    }

149 }

 

你可能感兴趣的:(HP)