⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 MSP430 UART 初始化
💻 C
字号:


/***************************************** Private functions ******************/


/******************************************************************************/


//#include "main.h"
/*  UART
 *  Copyright 2006 by Indigo Manufacturing Inc.
 *  All rights reserved. Property of Indigo Manufacturing Inc.
 *
 *  Created by Frank Tu
 *
 *  April 12, 2007
 */
#include  <msp430x22x4.h>
//#include "msp430x22x4.h"
#include "flash.h"
#include "UART.h"
unsigned char IRpulseBuff1[129];
int UartRcvCnt=0;
int UartCMD=0;
int UartRcvbyte=0;
unsigned int iflashsize=0;
unsigned int ichecksum=0;
void Uart_init(void);
 void usart0_tx (void);
 void usart0_rx (void);
extern void prepareToWrite_Seg (int pointer);
extern unsigned int UartTimer;
void vInitUART(void)
{ _DINT();
//UCTL0 |= SWRST;
UCA0CTL0|= 1;
// BCSCTL2 |= SELM1+SELM0;               // MCLK = LFXT1 (safe)
  //UCTL0 = 0x10;                         // 8-bit character
  UCA0CTL0=0x00;
  UCA0CTL1 = 0x80;                       // UCLK = SMCLK=DCO = 735K
  UCA0BR0 = 0x42;                         // 19600 = 735000/0x25
  UCA0BR1 = 0x00;                         //
  UCA0MCTL = 0x00;                        // no modulation
  //ME2 |= UTXE0 + URXE0;                 // Enabled USART0 TXD/RXD
    IE2 |= 1;// + UTXIE0;                   // Enable USART0 RX/TX interrupt
//  IE2 |= URXIE0;                        // Enabled USART0 RX interrupt
//IE2 &= ~URXIE0;
//IE2 &= ~UTXIE0; 
   P3SEL |= 0x30;                        // P3.4,5 = USART0 TXD/RXD
   P3DIR |= 0x10;                        // P3.4 output direction
IE2 |= UCA0RXIE;
 _EINT();    
}


void Uart_init(void)
{ 
DCOCTL = CALDCO_8MHZ;                     // Set DCO for 1MHz using
  BCSCTL1 = CALBC1_8MHZ| XTS;;                    // calibration registers
//  BCSCTL2=0xd8;

P3DIR &= ~0x06;
//return;
 /* P3DIR&=~BIT5;*/
//  P3DIR|=BIT4;
  P3SEL = 0x30;                             // P3.4,5 = USCI_A0 TXD/RXD
  UCA0CTL1 |=0x40;// ACLK
//  UCA0BR0 = 0x80;                           // 12.288M/19200=640
//  UCA0BR1 = 0x02;
  UCA0BR0 = 0;                           // 12.288M/9600=500
  UCA0BR1 =5;// 0x14;
  UCA0MCTL = UCBRS1 + UCBRS0;               // Modulation UCBRSx = 3
  UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
  IE2 |= UCA0RXIE ;//+ UCA0TXIE;               // Enable USCI_A0 TX/RX interrupt
  UartRcvCnt=0;
}


void usart0_tx (void) __interrupt[USCIAB0TX_VECTOR]
{
  IE2 &= ~UCA0TXIE;
}

// USCI A0/B0 Receive ISR
#define iPod_DET_bit BIT2 
#define iPod_DET_port_in    P4IN
#define iPod_DET_port_ren   P4REN
 void usart0_rx (void) __interrupt[USCIAB0RX_VECTOR]
{int i;

  _DINT();

iPod_DET_port_ren |= iPod_DET_bit;
   if ((iPod_DET_port_in & iPod_DET_bit)==0) 
   {
    if (UartRcvCnt<127)
    //if (UartRcvCnt<9)
    {
    IRpulseBuff1[UartRcvCnt]=UCA0RXBUF;
    if (UartRcvCnt==1 & (IRpulseBuff1[0]!=0xff | IRpulseBuff1[1]!=0x55))
      {
      UartRcvCnt=0;
      _EINT();
      return;
      }
    UartTimer=32000;
    UartRcvCnt++;
    if (UartRcvCnt-4 ==IRpulseBuff1[2])
      {
      UartRcvCnt=0;
      command_processing();
      }
   }
   else
   {
   i=UCA0RXBUF;
   }
   _EINT();
    return;
  }

#ifdef gui_switch
  //from GUI
        UartRcvCnt++;
        UartTimer=32000;
    if (UartRcvCnt<9)
    {
      if (UartRcvCnt<7)
      {
        UartRcvbyte=0x62 & UCA0RXBUF;       // GUI depended
        if ((UartRcvbyte!=0x62))
        {
        UartRcvCnt=0;
        UartCMD=0;
        }
      }
      if ((UartRcvbyte==0x62)&&(UartRcvCnt==7))
      {
        iflashsize=UCA0RXBUF;
        //UCA0TXBUF=0x55;
      }

      if ((UartRcvbyte==0x62)&&(UartRcvCnt==8))
      { //UCA0TXBUF=0x55;
        iflashsize=iflashsize+UCA0RXBUF*256;
        UartCMD=0x62;
        prepareToWrite_Seg (0xfa00);
        prepareToWrite_Seg (0xf800);

      }
    _EINT();
      return;
    }
    
    flashoverflowchec();
mFlashWriteEnable;
  *Flash_ptr++ = UCA0RXBUF;                   // Write value to flash
  mFlashWriteDisable;
  ichecksum+=UCA0RXBUF;
//UCA0TXBUF=0x55;
  if (UartRcvCnt>(iflashsize+8))
  {
    //Write_GUI_data_size(iflashsize);
    //Write_GUI_data_check_sum(ichecksum);
        for (i=0;i<1600;i++);
    UCA0TXBUF=ichecksum>>8;     //send back check sum back to PC
    for (i=0;i<1600;i++);
    UCA0TXBUF=ichecksum&0xff;
/*    for (i=0;i<100;i++);
    UCA0TXBUF=0x55;
    for (i=0;i<100;i++);
    UCA0TXBUF=0x55;
    for (i=0;i<100;i++);
    UCA0TXBUF=0x55;
    P3SEL&=~0x30;
    P3OUT&=~BIT4;*/
    UartRcvCnt=0;
    UartCMD=0;
    UartRcvbyte=0;
    ichecksum=0;
    //for (i=0;i<100;i++)
    //TXBUF0=0xa5;
    //Uart_init();
  }
#endif
_EINT();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -