Serial port programming

Publisher:q13358975046Latest update time:2022-07-28 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Serial port programming

Before the serial port works, it should be initialized, mainly to set the timer 1 that generates the baud rate, serial port control and interrupt control. The specific steps are as follows:

Determine the working mode of T1 (program the TMOD register), calculate the initial value of T1, and load TH1 and TL1;

Start T1 (program the TR1 bit in TCON). Determine the serial port control (program the SCON register);

When the serial port works in interrupt mode, interrupt settings must be performed (programming the EIP register).


#include "reg52.h" //This file defines some special function registers of the microcontroller


typedef unsigned int u16;   //declare and define the data type

typedef unsigned char u8;



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

* Function name: UsartInit()

* Function    : Set the serial port

* Input : None

* Output          : None

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

void UsartInit()

{

SCON=0X50; //Set to work mode 1

TMOD=0X20; //Set the counter working mode 2

PCON=0X80; //Double the baud rate

TH1=0XF3; //Set the initial value of the counter, note that the baud rate is 4800

TL1=0XF3;

ES=1; //Open receive interrupt

EA=1; //Open the general interrupt

TR1=1; //Open the counter

}


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

* Function name: main

* Function : Main function

* Input: None

* Output    : None

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

void main()

{

UsartInit(); // Serial port initialization

while(1);

}


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

* Function name: Usart() interrupt 4

* Function   : Serial communication interrupt function

* Input : None

* Output          : None

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

void Usart() interrupt 4

{

u8 receiveData;


receiveData=SBUF; //Output received data

RI = 0; // Clear the receive interrupt flag

SBUF=receiveData; //Put the received data into the send register

while(!TI); //Wait for sending data to complete

TI=0; //Clear the send completion flag

}


Reference address:Serial port programming

Previous article:[51 Single Chip Microcomputer] My roommate learned how to use digital tubes using a King of Time computer.
Next article:How 80C51 serial port works

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号