MSP430G2553 MCU uses printf function to print out serial port

Publisher:彩虹微笑Latest update time:2016-08-18 Source: eefocusKeywords:MSP430G2553 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
When I used Keil to write 51 MCU programs, I often used the printf function to print out some key process data to the computer to monitor the running status of the program. Recently, when I used IAR for MSP430 to debug the MSP430G2553 program, I found some small problems. The MSP430G2553 MCU did not output data to the computer as I expected.
With doubts, I looked at the description of the printf function in the keil help file. It turned out that the printf function ultimately calls the putchar function to print out characters.
MSP430G2553 MCU uses printf function to print serial output - Longcheng Electronics - Longcheng Electronics' Blog
putchar, this function outputs the character corresponding to the value of the specified expression to the standard output terminal. The expression can be a character or an integer, and it can only output one character at a time. Let's look at the body of the putchar function in the keil standard function library.
 MSP430G2553 MCU uses printf function to print serial output - Longcheng Electronics - Longcheng Electronics' Blog
  According to the instructions, I found the putchar.c file in the D:\Keil\C51\LIB folder.
#include
 
#define XON  0x11
#define XOFF 0x13
 
/*
 * putchar (full version):  expands '\n' into CR LF and handles
 *                          XON/XOFF (Ctrl+S/Ctrl+Q) protocol
 */
char putchar (char c)  {
 
  if (c == '\n')  {
    if (RI)  {
      if (SBUF == XOFF)  {
        do  {
          RI = 0;
          while (!RI);
        }
        while (SBUF != XON);
        RI = 0; 
      }
    }
    while (!IF);
    IF = 0;
    SBUF = 0x0d;                         /* output CR  */
  }
  if (RI)  {
    if (SBUF == XOFF)  {
      do  {
        RI = 0;
        while (!RI);
      }
      while (SBUF != XON);
      RI = 0; 
    }
  }
  while (!IF);
  IF = 0;
  return (SBUF = c);
}
 
#if 0         // comment out versions below
 
/*
 * putchar (basic version): expands '\n' into CR LF
 */
char putchar (char c)  {
  if (c == '\n')  {
    while (!IF);
    IF = 0;
    SBUF = 0x0d;                         /* output CR  */
  }
  while (!IF);
  IF = 0;
  return (SBUF = c);
}
 
/*
 * putchar (mini version): outputs charcter only
 */
char putchar (char c)  {
  while (!IF);
  IF = 0;
  return (SBUF = c);
}
 
#endif
 
As can be seen from the description file, we can rewrite this underlying putchar function to adapt to different hardware. The putchar function in Keil uses the serial port to output information by default. We can freely define it as another output module, such as customizing IO to output information to the 1602 LCD.
I roughly understood the printf function of keil, and then went back to study IAR for MSP430. Unfortunately, I was not able to view printf.c and putchar.c in the standard function library of the software. However, I think the program did not print information to the computer through the serial port because the underlying putchar function was not defined to output through the UART of MSP430G2553. If I redirect a putchar function myself and overwrite the putchar in the standard function library, will it be able to output? So I wrote the putchar function as follows:
/*********************************************************************
 
 * Function name: putchar, function redirection, automatically overwriting standard library functions
 
 * Function: Send a character to the serial terminal
 
 * Parameter: c is the character to be sent
 
 * Return value: c
 
*********************************************************************/
 
int putchar(int c)
 
{
 
   if(c == '\n')
{
 
       while(UCA0STAT & UCBUSY);
 
       UCA0TXBUF = '\r';
 
    }
 
    while(UCA0STAT & UCBUSY);
 
    UCA0TXBUF = c;
 
return c;
 
}
After compiling, the output is completely correct.
I will show you the test program, I hope it will be helpful to you.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
uart.c
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include
 
/**********************************************************************
 
 * Function name: UartInit
 
 * Function: Initialize the USCI register of msp430g2553 to make it work in UART mode
 
 * Parameters: None
 
 * Return value: None
 
 * ********************************************************************/
 
void UartInit()
{
 
    BCSCTL1 = CALBC1_1MHZ;              // Set DCO
   DCOCTL = CALDCO_1MHZ;
   BCSCTL2 &= ~(DIVS_3);
 
   P1SEL = BIT1 + BIT2 ;               // P1.1 = RXD, P1.2=TXD
   P1SEL2 = BIT1 + BIT2;      // P1.1 = RXD, P1.2=TXD
 
   UCA0CTL1 |= UCSSEL_2;               // SMCLK
   UCA0BR0 = 104;                      // 1MHz 9600
   UCA0BR1 = 0;                        // 1MHz 9600
   UCA0MCTL = UCBRS0;                  // Modulation UCBRSx = 1
   UCA0CTL1 &= ~UCSWRST;               // **Initialize USCI state machine**
 
    UC0IE |= UCA0RXIE;                  // Enable RX int
 
}
 
/*********************************************************************
 
 * Function name: putchar, function redirection, automatically overwriting standard library functions
 
 * Function: Send a character to the serial terminal
 
 * Parameter: c is the character to be sent
 
 * Return value: c
 
*********************************************************************/
 
int putchar(int c)
 
{
 
   if(c == '\n')
{
 
       while(UCA0STAT & UCBUSY);
 
       UCA0TXBUF = '\r';
 
    }
 
    while(UCA0STAT & UCBUSY);
 
    UCA0TXBUF = c;
 
return c;
 
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
main.c
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include
#include "uart.h" 
#include "stdio.h"
 
void main()
 
{  
float value = 123.123456789;
 
char *string="http://www.hao123.com";
 
    WDTCTL = WDTPW + WDTHOLD;
 
    UartInit();
 
printf("value = %f\n%s\n",value,string);
 
while(1);
 
}
The information is printed out to the computer's HyperTerminal, and the screenshot is as follows:
MSP430G2553 MCU uses printf function to print serial output - Longcheng Electronics - Longcheng Electronics' Blog

Keywords:MSP430G2553 Reference address:MSP430G2553 MCU uses printf function to print out serial port

Previous article:MSP430 digital filter design sharing
Next article:MSP430 learning core reset signal

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号