Serial Communication - Using printf Function

Publisher:bobojrtLatest update time:2016-11-21 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

For boards without emulators, the serial port is a very good debugging tool. You can set the variables you need to observe

Send it to the PC through the serial port for subsequent analysis and debugging. Here we mainly introduce how to use the serial port in Keil C

Debugger.

The program code is as follows:

#include

#include

void serial_init(void);

void main()

{

    serial_init();

    while(1)

    {

        printf("Hello,Serial Port\n"Serial communication - using printf function - Wanderer - Wanderer Map;

    }

}

void serial_init(void) //Serial port initialization function

{

   TMOD = 0x20; //T1 working mode 1, used to set the baud rate

   TH1 = 0xF3; //Baud rate 4800Bps, 12MHz crystal

   TL1  = 0xF3;

   PCON |= 0x80; //SMOD = 1, baud rate doubled

   SCON = 0x50; //Allow sending and receiving

   ES = 0; //Disable serial port interrupt

   IF = 1;

   TR1  = 1;

}

It should be noted that the microcontroller crystal oscillator is 12MHz (otherwise the baud rate setting will be incorrect).

Create a project, compile, enter the debugging interface, select "Peripheraals"-"Serial" in the menu, and you will see the serial port settings.

In the interface, we can see that before the serial port is initialized, the parameters of the serial port are all 0x00, and other options are not set.
Press F10 to enter single-step debugging and execute the serial_init function. We can see that the serial port has been initialized and the baud rate is 4806. After the serial port is initialized, we can use the printf function to output the information we need (note that the stdio.h file is required). Here, we only output a simple string information example. Select the menu "View"-"Serial Window #1", you can see a blank
area, which is where the serial port information is displayed. Execute the printf function and you can see the words "Hello, Serial Port" printed on the screen.

When debugging a program on actual hardware, we can use this method to observe the values ​​of related variables and debug the program when we encounter problems. Once the program is OK, we can remove the printf function.


Reference address:Serial Communication - Using printf Function

Previous article:12864 Chinese character library serial port test program
Next article:A Brief Discussion on Displaying Chinese Characters in 1602

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号