MSP-EXP430F5529LP开发板006-UART库函数

串口是比较常用的开发中比较常用的部分了,这部分难点就是波特率的设置了,为此还特地下了篇英文手册,硬着头皮看了下,学会如何配置了,下面就是串口初始化部分

void myuart_init()
{
  //P3.3,4 = USCI_A0 TXD/RXD
  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3,GPIO_PIN3 + GPIO_PIN4);

  //Baudrate = 9600, clock freq = 4MHz
  //UCBRx = 26, UCBRFx = 1, UCBRSx = 0, UCOS16 = 1
  USCI_A_UART_initParam param = {0};
  param.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;
  param.clockPrescalar = 26;
  param.firstModReg = 1;
  param.secondModReg = 0;
  param.parity = USCI_A_UART_NO_PARITY;
  param.msborLsbFirst = USCI_A_UART_LSB_FIRST;
  param.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;
  param.uartMode = USCI_A_UART_MODE;
  param.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;

  if (STATUS_FAIL == USCI_A_UART_init(USCI_A0_BASE, ¶m))
  {
      return;
  }

  //Enable UART module for operation
  USCI_A_UART_enable(USCI_A0_BASE);

  //Enable Receive Interrupt
  USCI_A_UART_clearInterrupt(USCI_A0_BASE,USCI_A_UART_RECEIVE_INTERRUPT);
  USCI_A_UART_enableInterrupt(USCI_A0_BASE,USCI_A_UART_RECEIVE_INTERRUPT);
}

看到这一部分大家就应该比较眼熟了,有点类似32的uart初始化,但不同的是波特率部分,需要根据时钟源进行计算。这里我选择的是SMCLK,时钟配置沿袭了上一篇文章的配置,也就是SMCLK为4MHz。那么具体怎么设置呢?这里就只能参看英文手册了,这一部分截图如下

MSP-EXP430F5529LP开发板006-UART库函数_第1张图片

大体思路就是先计算N,然后根据N值进行后面的设置,N大于等于16可以设置为Oversampling,UCOS16 = 1;N小于16可以设置为Low-Frequency,UCOS16 = 0。UCBR, UCBRFx , UCBRSx三个寄存器可以按上述进行计算,但是TI为我们提供了不同时钟源下的取值,还是比较人性化的。下面截图部分,可以很轻松找到4MHz下9600bps的对应设置

MSP-EXP430F5529LP开发板006-UART库函数_第2张图片

整体代码在下面

//*****************************************************************************
#include "driverlib.h"
//*****************************************************************************
//
//Target frequency for MCLK in kHz
//
//*****************************************************************************
#define UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ   24000

//*****************************************************************************
//
//MCLK/FLLRef Ratio
//
//*****************************************************************************
#define UCS_MCLK_FLLREF_RATIO   6

//*****************************************************************************
//
//Variable to store current Clock values
//
//*****************************************************************************
uint32_t clockValue = 0;

//*****************************************************************************
//
//Variable to store status of Oscillator fault flags
//
//*****************************************************************************
//*****************************************************************************
//
//XT1 Crystal Frequency being used
//
//*****************************************************************************
#define UCS_XT1_CRYSTAL_FREQUENCY    32768

//*****************************************************************************
//
//XT2 Crystal Frequency being used
//
//*****************************************************************************
#define UCS_XT2_CRYSTAL_FREQUENCY   4000000
//*****************************************************************************
//
//Desired Timeout for XT1 initialization
//
//*****************************************************************************
#define UCS_XT1_TIMEOUT 50000
#define TIMER_PERIOD 399
#define DUTY_CYCLE  100
#define UCS_XT2_TIMEOUT 50000
uint16_t status;
uint8_t returnValue = 0;
void myclock_init()
{
  //Set VCore = 1 for 12MHz clock
  PMM_setVCore(PMM_CORE_LEVEL_1);//主频提高后,VCore电压也需要随之配置
  
  //Initializes the XT1 and XT2 crystal frequencies being used
  UCS_setExternalClockSource(UCS_XT1_CRYSTAL_FREQUENCY,UCS_XT2_CRYSTAL_FREQUENCY);//设置外部时钟源的频率,没什么实际设定

  //Initialize XT1. Returns STATUS_SUCCESS if initializes successfully
  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5,GPIO_PIN4 + GPIO_PIN5);//XT1口不作为普通IO
  returnValue = UCS_turnOnLFXT1WithTimeout(UCS_XT1_DRIVE_0,UCS_XCAP_3,UCS_XT1_TIMEOUT);//启动XT1

  //Startup HF XT2 crystal Port select XT2
  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5,GPIO_PIN2 + GPIO_PIN3);//XT2口不作为普通IO

  //Initialize XT2. Returns STATUS_SUCCESS if initializes successfully
  returnValue = UCS_turnOnXT2WithTimeout(UCS_XT2_DRIVE_4MHZ_8MHZ,UCS_XT2_TIMEOUT);//启动XT2
  
  //Set DCO FLL reference = REFO
  UCS_initClockSignal(UCS_FLLREF,UCS_XT2CLK_SELECT,UCS_CLOCK_DIVIDER_1);//XT2作为FLL参考
  
  //Set Ratio and Desired MCLK Frequency  and initialize DCO
  UCS_initFLLSettle(UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ,UCS_MCLK_FLLREF_RATIO);//MCLK设置为24MHz
  
  //Set ACLK = REFO
  UCS_initClockSignal(UCS_ACLK,UCS_REFOCLK_SELECT,UCS_CLOCK_DIVIDER_1);//ACLK设置为32.768kHz
  
  UCS_initClockSignal(UCS_SMCLK,UCS_XT2CLK_SELECT,UCS_CLOCK_DIVIDER_1);//SMCLK设置为4MHz
}
void mypwm_init()
{
  //P2.0 as PWM output
  GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2,GPIO_PIN0);

  //Generate PWM - Timer runs in Up mode
  Timer_A_outputPWMParam param = {0};
  param.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
  param.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
  param.timerPeriod = TIMER_PERIOD;
  param.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_1;
  param.compareOutputMode = TIMER_A_OUTPUTMODE_RESET_SET;
  param.dutyCycle = DUTY_CYCLE;
  Timer_A_outputPWM(TIMER_A1_BASE, ¶m);
}
void myuart_init()
{
  //P3.3,4 = USCI_A0 TXD/RXD
  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3,GPIO_PIN3 + GPIO_PIN4);

  //Baudrate = 9600, clock freq = 4MHz
  //UCBRx = 26, UCBRFx = 1, UCBRSx = 0, UCOS16 = 1
  USCI_A_UART_initParam param = {0};
  param.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;
  param.clockPrescalar = 26;
  param.firstModReg = 1;
  param.secondModReg = 0;
  param.parity = USCI_A_UART_NO_PARITY;
  param.msborLsbFirst = USCI_A_UART_LSB_FIRST;
  param.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;
  param.uartMode = USCI_A_UART_MODE;
  param.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;

  if (STATUS_FAIL == USCI_A_UART_init(USCI_A0_BASE, ¶m))
  {
      return;
  }

  //Enable UART module for operation
  USCI_A_UART_enable(USCI_A0_BASE);

  //Enable Receive Interrupt
  USCI_A_UART_clearInterrupt(USCI_A0_BASE,USCI_A_UART_RECEIVE_INTERRUPT);
  USCI_A_UART_enableInterrupt(USCI_A0_BASE,USCI_A_UART_RECEIVE_INTERRUPT);
}
unsigned int i;
uint8_t transmitData = 0x00;
uint8_t receivedData = 0x00;
bool send_ready=0;
void main (void)
{
  //Stop WDT
  WDT_A_hold(WDT_A_BASE);   

  myclock_init();
  mypwm_init();
  myuart_init();
  //Verify if the Clock settings are as expected
  clockValue = UCS_getMCLK();
  clockValue = UCS_getACLK();
  clockValue = UCS_getSMCLK();

  // Enable global interrupt
  __bis_SR_register(GIE);

  //Loop in place
  while (1)
  {
    
  }
}
//******************************************************************************
//
//This is the USCI_A0 interrupt vector service routine.
//
//******************************************************************************
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=USCI_A0_VECTOR
__interrupt
#elif defined(__GNUC__)
__attribute__((interrupt(USCI_A0_VECTOR)))
#endif
void USCI_A0_ISR (void)
{
  switch (__even_in_range(UCA0IV,4))
  {
    //Vector 2 - RXIFG
    case 2: 
      receivedData = USCI_A_UART_receiveData(USCI_A0_BASE);
      USCI_A_UART_transmitData(USCI_A0_BASE,receivedData);
      break;
    default: 
      break;
  }
}

实现上位机向430发送数据,原封不动返回

MSP-EXP430F5529LP开发板006-UART库函数_第3张图片

你可能感兴趣的:(MSP430,IAR,库函数,MSP430)