STC89C52 serial port application example

Publisher:码梦狂人Latest update time:2018-06-20 Source: eefocusKeywords:STC89C52 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1) Hardware Design
Generally, the serial port communication of a microcontroller needs to be converted by MAX232 for level conversion before data communication. Of course, the STC89C52RC microcontroller is no exception. The connection method in the figure is the simplest connection of the commonly used zero-modem method, that is, the 3-wire connection method: only the RXD, TXD and GND lines are used, as shown below.

 


STC89C52 serial port application example

Since the logic "0" level of RS232 is specified as +5~+15V, and the logic "1" level is specified as -15~-5V, it cannot be directly connected
to the TTL/CMOS circuit and must be converted.
Level conversion can be achieved using discrete devices such as transistors, or a dedicated level conversion chip, MAX232 is a typical
one. MAX232 can not only achieve level conversion, but also realize mutual conversion of logic, that is, positive logic to negative logic.
#include "stc.h" //Load "stc.h" header file
void Delay(void) //Define Delay function, delay 500ms
{
unsigned char i,j; //Declare variables i,j
for(i=0;i<255;i++) //Perform loop operation to achieve the delay effect
for(j=0;j<255;j++);
for(i=0;i<255;i++) //Perform loop operation to achieve the delay effect
for(j=0;j<255;j++); for(i=0;i<255;i++) //Perform loop operation to achieve the delay effect for(j=0;j<255;j++);
for(i=0;i<255;i++) //Perform loop operation to achieve the delay effect
for(j=0;j<140;j++);
}


void UARTInit(void) //Define serial port initialization function
{
SCON =0x40; //8-bit data bit
T2CON=0x34; //T/C2 is used as the baud rate generator
RCAP2L=0xD9; //The baud rate is 9600 The lower 8 bits of
RCAP2H=0xFF; //The higher 8 bits of the baud rate is 9600
}


void UARTSendByte(unsigned char byte)//Serial port sends a single byte function
{
SBUF=byte; //The buffer loads the byte to be sentwhile
(TI==0); //Wait for the sending to be completed, the TI flag will be set to 1
TI=0; //Clear the sending completion flag
}
void main(void) //Enter the Main function
{
unsigned char i=0; //Declare variable i
UARTInit(); //Serial port initializationwhile
(1) //Enter an infinite loop
{
UARTSendByte(i); //Serial port sends a single byte of dataDelay
(); //Delay 500ms
i++; //i adds 1
if(i>255)i=0; //If i>255,i=0
}
}


Keywords:STC89C52 Reference address:STC89C52 serial port application example

Previous article:STC51 MCU serial port sending program
Next article:The single chip microcomputer collects real-time temperature and uploads it to the computer display through serial communication

Recommended ReadingLatest update time:2024-11-16 22:55

Design of automotive LED center grid scanning light using STC89C52RC
This article introduces the design and production method of a car grille scanning light. It mainly uses the common STC89C52RC microcontroller as the main controller, uses a photocoupler for signal conversion, and uses the piranha LED light strip purchased on the market to modify eight groups of LED scanning lights. It
[Microcontroller]
Design of automotive LED center grid scanning light using STC89C52RC
8051 MCU (STC89C52) - Changing the buzzer sound frequency
Since the interrupt handler of timer 0 is triggered once every 50ms, the frequency of the buzzer sound can be changed by changing the critical value when tcount returns to zero.  When tcount = 40 and the level is flipped, the sound produced is similar to the effect sound when the bus turn signal is turned on. When
[Microcontroller]
STC89C52 and AT24C02 write by byte (C51)
#include #include intrins.h #define uchar unsigned char uchar i,a; uchar code table ={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8};    sbit wp=P1^0; sbit scl=P1^1; sbit sda=P1^2; sbit led=P2^0; void delay() {  _nop_();  _nop_();  _nop_(); } void de1s()   //误差 0us {     unsigned char a,b,c;     for(c=16
[Microcontroller]
STC89C52 microcontroller timer
How to set the timer? For example: Timing 50 microseconds First of all, 50000 ÷ 1.085 = 46,082.94930875576. Starting from 0 and adding 19,453 times will overflow.     This calculation will have errors, but the impact is relatively small and can be ignored. TH0 = (65535 - 46082) / 256; // Place the quotient in
[Microcontroller]
Research on the Control System of Pulsating Vacuum Sterilizer Based on STC89C52
    Now that people's living standards have improved, they are more concerned about medical sterilization and disinfection. In the mid-1990s, a new type of sterilization equipment, the pulsating vacuum sterilization system, was developed in China. The system uses advanced pulsating vacuum technology and has good steril
[Microcontroller]
Research on the Control System of Pulsating Vacuum Sterilizer Based on STC89C52
Design of temperature and humidity remote monitoring system based on STC89C52 and LabVIEW
  Abstract: In order to solve the problem of being unable to monitor the temperature and humidity of the equipment cabin of the unmanned aerial platform in real time, the design of the remote monitoring system of temperature and humidity of the unmanned aerial platform is proposed, using the STC89C52 single-chip micro
[Test Measurement]
Design of temperature and humidity remote monitoring system based on STC89C52 and LabVIEW
Interface Design between STC89C52 and 8-bit D/A Converter DAC0832
1. DAC0832 chip - current output D/A converter 8-bit parallel input mode Resolution 19.5mV (VREF = 5V) Current build-up time 1μS Input compatible with TTL level Single power supply (+5V~+15V) Low power consumption, 20mw 1. Pin function: DI0~DI7: 8-bit digital signal input terminal, connected to the data bus P0 port
[Microcontroller]
Interface Design between STC89C52 and 8-bit D/A Converter DAC0832
STC89C52RC features and pin introduction
STC89C52RC is a small single-chip microcomputer launched by Hongjing Company. It is a commonly used device for electronic engineers . Its main features are the use of Flash memory technology, which reduces the manufacturing cost. Its software and hardware are fully compatible with MCS-51, and it is manufactured using h
[Microcontroller]
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号