10042-基于msp430f249串口通信仿真

资料

  1. C语言源程序
  2. Protues仿真文件

环境

  1. 程序代码:IAR 
  2. 单片机:msp430f249
  3. 仿真环境:Protues8.15

功能

  1. 串口显示打印内容。

演示

20240106231046246-image

代码

#include <MSP430x24x.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
uchar const table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,  //共阴数码管段选码表,无小数点
                               0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

uchar Disbuf[8],Index;
uchar RXbbuf[4];
uchar data1;
void delayms(uint t)
{
    uint i;
    while(t--)
      //for(i=1330;i>0;i--);//进过参数的调整 
      for(i=150;i>0;i--);//进过参数的调整
}




void InitUART0(void)
{
  
if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)                                     
  {  
    while(1);                               // If calibration constants erased
                                            // do not load, trap CPU!!
  }   
  BCSCTL1 = CALBC1_1MHZ;                    // Set DCO
  DCOCTL = CALDCO_1MHZ;
  P3SEL = 0x30;                             // P3.4,5 = USCI_A0 TXD/RXD
  UCA0CTL1 |= UCSSEL_2;                     // SMCLK
  UCA0BR0 = 104;                            // 1MHz 9600; (104)decimal = 0x068h
  UCA0BR1 = 0;                              // 1MHz 9600
  UCA0MCTL = UCBRS0;                        // Modulation UCBRSx = 1
  UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
  IE2 |= UCA0RXIE;                          // Enable USCI_A0 RX interrupt

}
void SendData(uchar Data)
{
   UCA0TXBUF = Data;                         // TX -> RXed character 
   while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
    
}
void SendChars(char *p)
{
     uchar len;
     len=strlen(p);
     while(len)
     {
       SendData(*p);
       p++;
       len--;
     }  
}
void RxDataProcess(uchar Data)
{
    signed char i;
    for(i=0;i<3;i++)
    {
        RXbbuf[i]=RXbbuf[i+1];
    }
    RXbbuf[3]=Data;
    
   for(i=0;i<4;i++)
   {
       Disbuf[2*i]=table[RXbbuf[i]/16];
       Disbuf[2*i+1]=table[RXbbuf[i]%16]; 
   }
}
void main(void)
{
    
    WDTCTL=WDTPW + WDTHOLD; // 关闭看门狗
    P4DIR=0x01; 

    P1DIR=0xFF;            // 设置方向
    P1SEL=0;            // 设置为普通I/O 口
    
    P2DIR=0xFF;            // 设置方向
    P2SEL=0;            // 设置为普通I/O 口
   
    P1OUT=0x00;
    P2OUT=0xFF;
   
    
   
    InitUART0();

    SendChars("Press any Key.....\r");
    _BIS_SR(LPM0_bits+ GIE);//进入低功耗睡眠模式
   
    while(1)
    {
       
          P2OUT=0xFF;
          P1OUT=Disbuf[Index];
          P2OUT=~(1<<Index); 
          if(++Index==8) Index=0;
          delayms(1);
          
    }

    
}




#pragma vector=USCIAB0RX_VECTOR
__interrupt void usart0_rx (void)
{
  
 while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  UCA0TXBUF = UCA0RXBUF;                    // TX -> RXed character
  RxDataProcess(UCA0RXBUF);
  LPM0_EXIT; 
  
  
}

 

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享