first part
1. Introduction to MCU:
1. Basic knowledge
Micro Controller Unit, or MCU for short;
It integrates a series of common computer hardware functions such as CPU, RAM, ROM, timer, interrupt system, communication interface, etc.
The tasks of the microcontroller: information collection (relying on sensors), processing (relying on CPU) and control of hardware devices (such as motors, LEDs, etc.)
Compared with a computer, a single-chip microcomputer is a pocket-sized computer. One chip can form a complete computer system.
In terms of performance, it is far from a computer, but the single-chip microcomputer has low cost, small size, simple structure, and is very useful in the fields of life and industrial control;
At the same time, learning to use single-chip microcomputers is the best choice to understand the principles and structure of computers.
2. Application fields of single chip microcomputer
The application areas of single-chip microcomputers are very wide, such as smart instruments, real-time industrial control, communication equipment, navigation systems, household appliances, etc.
Once a single-chip microcomputer is used in various products, it can play a role in upgrading the products. The adjective "smart" is often added before the product name, such as smart washing machine, etc.
Series: 51 single chip microcomputer
Company: STC
Number of digits: 8
RAM: 512 bytes (bit) equivalent to the computer's memory stick (usually 4G, 8G)
ROM: 8K (Flash) Equivalent to a computer's hard disk (hundreds of GB, thousands of GB)
Operating frequency: 12MHz
2. Practical Operation
1. Turn on the first LED
Chinese name: light emitting diode
English name: Light Emitting Diode
Abbreviation: LED
Application: lighting, advertising lights, guide lights, screens
1.1 Microcontroller Specifications: TTL
That is, the high level is 5V and the low level is 0V
1.2 Control principle of high and low levels
The CPU controls the hardware circuit by controlling the registers configured for each IO port to execute the hardware functions we want.
For example: we want the LED of the P20 port
1.3 How to light up the first LED
A C program cannot write binary directly like the following, it will think you are writing decimal:
It should be written in hexadecimal. Hexadecimal is actually the abbreviation of binary (remember this picture. You will remember it after writing it more often)
So it should be written as:
1 is off, 0 is on;
And the arrangement order is opposite to that on the microcontroller.
But at this time, the compiler does not recognize the P2 port, so: add the header file before the main function
The execution process of this code is that the microcontroller starts from main, and when P2 = 0xFE is executed, it continues to execute from main and executes in an infinite loop.
So how do you make it stop?
After execution, the phenomenon has not changed, but now it actually no longer configures the IO port.
1.4 On and off On and off On and off
1.5 Cyclic on and off
(1. You can use STC-ISP to automatically generate delay code
(2. The overall code is as follows
(3. Write a delay function that is easy to modify
A small box for storing data
unsigned int 16 bits (MCU) 0~65535
int 16 bits (MCU) -32768~32767 (half represents negative numbers and half represents integers)
(3.1) Use STC-ISP to generate a function with a delay of 1ms
(3.2) Add function parameters, while loop, and decrement statement
2. Independent buttons
Touch button: It is equivalent to an electronic switch. When pressed, the switch is turned on, and when released, the switch is turned off. The principle of implementation is that the metal spring inside the touch button is forced to bounce to achieve connection and disconnection.
The functions of the two sentences are the same, both of which make D1 light up.
### 2.1 View the pin data of the button
2.1.1 Key1 connected to RXD
2.1.2 RXD is connected to P3.1 again
2.1.3 Long press Key1 D1 lights up, release it to turn off
2.2 C51 Data Operation
2.3 Key jitter
For mechanical switches, when the mechanical contacts are opened and closed, due to the elastic effect of the mechanical contacts, a switch will not be immediately and stably connected when closed, nor will it be disconnected all at once when opened, so there will be a series of jitters at the moment the switch is closed and opened.
Long press Key1, no change, D1 lights up after release
2.4 Independent button control LED display binary
Initial state P2 is all high level 1111 1111
P2++ overflows to 0000 0000
~P2 then 1111 1111
It's always 1111 1111 so the light is always off
(1. Define an unsigned character, unsigned char LEDNum = 0;
[Because it is an eight-bit binary number from 0 to 255, it just meets the characteristics of the 51 microcontroller register]
2.5 Independent button control LED shift
#include void Delay1ms(unsigned int xms); unsigned char LEDNum; void main() { P2=~0x01; //Let D1 light up first while(1) { if(P3_1==0) //Press Key1 (see HC6800-ES+V2.0 new version pdf) { Delay1ms(20); while(P3_1==0); Delay1ms(20); LEDNum++; if(LEDNum >= 8) LEDNum=0; P2=~(0x01< if(P3_0==0) //Press Key2 (see HC6800-ES+V2.0 new version pdf) { Delay1ms(20); while(P3_0==0); Delay1ms(20); if(LEDNum==0) { LEDNum=7; } else { LEDNum--; } P2=~(0x01< } } } void Delay1ms(unsigned int xms) { unsigned char i, j; while(xms) { i = 2; j = 239; do { while (--j); } while (--i); xms--; } }
Previous article:[MCU] [Learning Log] 51 MCU Learning Log [Day2, 2022.1.10]
Next article:【51 MCU】Ordinary I/O port simulates SPI port C language program
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- 【NXP Rapid IoT Review】+ GUI Usage
- N32WB452 asked: Does N32WB452 have a basically complete ported RT-Thread development kit?
- IoT Gateways – A Simple Guide to the Internet of Things
- [Bluesun AB32VG1 RISC-V evaluation board] IDE installation and use
- It's New Year's Eve soon. I wish you all a happy New Year.
- Using Ginkgo USB-ADC and heart rate sensor to implement a heart rate tester with Android APP source code
- 【DIY Creative LED】Add touch button and install it on LED lamp
- [RISC-V MCU CH32V103 Review] - 2: Making an LED flash is not easy
- SensorTile.box uses air pressure sensors to analyze environmental changes
- When using an oscilloscope probe to measure the AC waveform between the two ends of a component, how should the probe ground wire be connected?