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.
According to the instructions, I found the putchar.c file in the D:\Keil\C51\LIB folder.
Keywords:MSP430G2553
Reference address:MSP430G2553 MCU uses printf function to print out serial port
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.
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.
#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:
Previous article:MSP430 digital filter design sharing
Next article:MSP430 learning core reset signal
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
Guess you like
- I am looking for the electronic version of "Mentor Xpedition Engineering from Scratch: High-Speed PCB Design"!
- Award-winning live broadcast: TI's new generation Sitara AM62 processor revolutionizes human-computer interaction - product introduction and related resources
- The list of materials for the 2022 provincial competition has been released. What do you think?
- How to use C2000 CSM
- Tianjin in my eyes in 2020
- 6678 I am debugging the phy network. If I encounter any problems, please help me. Thank you.
- RSL10 Development Board Circuit Overview
- TI DSP C6000 structural knowledge
- LSD-FET430UIF wiring diagram
- Grab the post! A wave of high-quality tutorials is coming, comment and forward the tutorials to get gifts! Cheer for the 2019 National Competition~