Practical analysis of 8051 microcontroller (taking STC89C52RC as an example) | 02 - LED flashes with a delay of about 5 seconds

Publisher:BlissfulBlissLatest update time:2022-05-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The code is modified as follows:


#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;



sbit led=P2^2; //Define the P2.2 port of the microcontroller as led


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

* Function name: delay

* Function    : Delay function, when i=1, the delay is about 10us

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

void delay(u16 i)

{

while(i--);

}


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

 * Function name: main

 * Function : Main function

 * Input: None

 * Output    : None

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

void main()

{

while(1)

{

led=0;

delay(50000); //About 450ms delay

led=1;

delay(50000); //About 450ms delay

}

}


Typedef is used in the code to declare and define data types. This is a syntax of C language, which is easy to understand. Here, u16 is used instead of unsigned int. In an 8-bit microcontroller, 16-bit data can be represented, which can represent numbers from 0 to 65535 (2^16-1). Similarly, u8 is used instead of unsigned char.


Add a delay function, which implements a self-decrementing infinite loop to achieve the purpose of delay. We will know later that this method is not recommended, but here it can roughly achieve a delay of about 5s for the LED to flash.

Reference address:Practical analysis of 8051 microcontroller (taking STC89C52RC as an example) | 02 - LED flashes with a delay of about 5 seconds

Previous article:Practical Analysis of 8051 MCU (Taking STC89C52RC as an Example) | 03 - LED Flowing Light
Next article:Practical Analysis of 8051 MCU (Taking STC89C52RC as an Example) | 01 - Lighting up an LED

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

Design and Implementation of a Function Generator Based on Single Chip Microcomputer
1 Introduction In the process of designing and debugging automatic control systems, sine waves, triangle waves and square waves of different frequencies are often used as signal sources, which are very convenient to use. In the past, oscillators were often composed of discrete components and integrated operational amp
[Microcontroller]
Design and Implementation of a Function Generator Based on Single Chip Microcomputer
C8051F320AD conversion and temperature sensor
   The C8051F320 has a 10-bit successive approximation ADC that can operate in single-ended or differential mode. 1. Brief Principle     Two multiplexers are integrated in the microcontroller, which serve as the positive input signal and negative input of the ADC respectively.         The positive input terminal i
[Microcontroller]
The address space of 8051 microcontroller program memory 27128
Write down the address space occupied by each of the four program memories 27128 of the 8051 microcontroller in the following figure. Picture 1 Picture 1 Answer: (1) 0000H~3FFFH (2)4000H~7FFFH (3)8000H~BFFFH (4)C000H~FFFFH 
[Microcontroller]
The address space of 8051 microcontroller program memory 27128
Design of distributed static measurement system using 8051 series microcontroller
This article introduces a CAN bus distributed measurement and control system based on the single-chip microcomputer AT89C52. It mainly explains the overall design scheme of the system, as well as the software and hardware design of the control module and acquisition module. The focus is on the CAN bus interface with t
[Microcontroller]
Design of distributed static measurement system using 8051 series microcontroller
Notes 01 of "8051 MCU C Language Innovation Tutorial"
Internal memory structure: RAM and ROM address overlap, distinguished by selection signal PSEN Certain memory locations that users should avoid entering Entry address of interrupt service function: What users actually use is 30H-7FH------------------------------80 bytes
[Microcontroller]
Notes 01 of
C8051F320-Clock and 4in1 8-segment tube
       The C8051F320 contains an internal oscillator, but other methods are possible. I don't have any components on hand, so I'll just consider using the internal oscillator. 1. Clock Setting        When the system is reset, the internal oscillator is used as the system clock by default. The base frequency has been
[Microcontroller]
C8051F320-Clock and 4in1 8-segment tube
Microcontroller Tutorial 1: Introduction to the pin functions of 8051 microcontroller
First, let’s connect the pin diagram of the microcontroller. If so, the specific functions are introduced below. The 40 pins of the microcontroller can be roughly divided into 4 categories: power, clock, control and I/O pins. ⒈ Power supply: ⑴ VCC - chip power supply, connected to +5V; ⑵ VSS - ground terminal; ⒉ C
[Microcontroller]
Microcontroller Tutorial 1: Introduction to the pin functions of 8051 microcontroller
Design and system testing of M8051 debugger based on SOPC
In the development process of embedded systems, the host computer completes the development, downloading and debugging of the target computer software through the debugger. Early debuggers communicated with the host computer through serial ports or parallel ports, which had shortcomings such as slow speed and poor ver
[Microcontroller]
Design and system testing of M8051 debugger based on SOPC
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号