C51: Transmitting data via serial port interruption

Publisher:innovator8Latest update time:2016-10-13 Source: elecfansKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/* 
 
1. Serial port mode, baud rate setting:
      1.1 T1 working mode 2, automatic reload 8-bit counting mode, as baud rate generator, baud rate calculation:
      1.1 Baud rate calculation:
  Baud rate = 2^SMOD * T1 overflow rate / 32 
  T1 overflow rate = f/(12*(256-TH1))
  Result: Baud rate = 2^SMOD * f/32 * 1/[12*(256-TH1)]  
  For example: f=11.0592, SMOD=0, baud rate=9600, then: TH1=0xfd. 
 
  1.2 T1 working mode 2 (timer T1 interrupt disabled) setting:
  TMOD=0x20;
  TR1=1; 
 
  1.3 Serial port mode 1, allow receiving settings
  SCON = 0x50;
  If you use printf() to send data from the serial port, you must set TI=1.
 
2. Serial port interrupt setting
  2.1 Open the general interrupt, EA=1;
  2.2 Enable serial port interrupt, ES=1;  
   
*/
 
/*
Programming features:
1. The serial port uses interrupt mode: after receiving a character, it sends the character immediately.
 
*/
 
#include "reg52.h"
 
 
#define uchar unsigned char
 
uchar mybuf;
 
 
 
//Declare the serial port initialization function
void init();
 
//
 
 
void main()
{
init();
//Open interrupt
EA=1;
ES=1;
while(1);
 
}
 
//Serial port interrupt response function
void serial() interrupt 4
{
//Disable interrupt
ES=0;
 
//Serial port receives characters
if(RI==1)
{
RI=0;
mybuf=SBUF;
 
 
SBUF=mybuf;
while(TI==0);
TI=0;
}
 
//Open interrupt
ES=1;
 
 
}
 
//Serial port initialization
void init()
{
//T1 mode 2 as baud rate,
TMOD=0x20;
//Serial port mode 1, allow receiving settings
SCON=0x50;
//f=11.0592,baud=9600,calculate constant
TH1=0xfd;
TL1=0xfd;
//start up
TR1=1;
 
 
 
}
Keywords:C51 Reference address:C51: Transmitting data via serial port interruption

Previous article:c51: Sending strings with a 16-bit keyboard
Next article:c51: pulse period measurement

Recommended ReadingLatest update time:2024-11-16 15:43

C51 single chip microcomputer uses 74hc138 to expand the simulation circuit diagram + program of the water lamp
The C51 microcontroller uses 74hc138 to expand the water lamp program, which includes source code. The simulation circuit can be expanded to multiple LED circuits at will. The schematic diagram is as follows: The microcontroller source program is as follows: #define uint unsigned int #define uchar unsigned char vo
[Microcontroller]
C51 single chip microcomputer uses 74hc138 to expand the simulation circuit diagram + program of the water lamp
static, const, code keywords in C51 development
static Keyword Static can be used to modify variables in C, and can also be used to modify functions. static modifier variable Variables can exist in three places in C: the global data area, the stack, and the heap. The stack and the heap are different. int a ; int main() {   int b ;   int c* = (int *)malloc(sizeo
[Microcontroller]
Understanding of C51 external interrupts and program calls
First, let's introduce the basic concept of C51 external interrupt Advantages of interrupts: 1. Real-time processing. The CPU is required to respond or process immediately. 2. Exception handling: If power outages, program errors, and other problems occur during operation, the CPU needs to handle the source of the faul
[Microcontroller]
Understanding of C51 external interrupts and program calls
C51---4.1 Static digital tube display
Digital tube pin definition Schematic 74HC138 Through the 74HC138 decoder P2_2 P2_3 P2_4 control LED1-LED8 output 0 is selected For example P2_2=1//low position P2_3=0 P2_4=1//High 101 is 5 in decimal, so the 5th bit is valid and the low level is effective. The digital tube is connected with a common cathode
[Microcontroller]
C51---4.1 Static digital tube display
Detailed explanation of Keil C51 software usage
Section 1 Keil C51 Compiler Control Instructions  The control instructions of the C51 compiler are divided into three categories: source file control, target file control and list control.  1. Source file control  NOEXTEND: C51 source files are not allowed to use ANSI C extension functions.  DEFINE (DF): define prepro
[Microcontroller]
Single chip infrared TV remote control C51 program code
//************************************************ ****************  //Name: Single-chip infrared TV remote control C51 program code() /*-------------------------------------------------------------- Description: The output of general infrared TV remote control is generated by pulse amplitude modulation of 38-40kHz
[Microcontroller]
KEIL C51 printf formatted output special usage
/******************************************* KEIL has expanded the b, h, and l to set the input byte width: (1) b is 8 bits (2) h is 16 bits (default) (3) l is 32 bits In Keil C51, when using printf to output a single-byte variable, you need to use %bd, such as unsigned char counter; printf("Current count: %bd\n", cou
[Microcontroller]
C51 MCU C language function editing
In fact, main(), which always appears in the examples, is also a function, but it is special. It is used as the beginning of the program when compiling. With functions, C language has the advantage of modularization. Generally, for programs with more functions, each individual function will be divided into several sub
[Microcontroller]
C51 MCU C language function editing
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号