[MCU] [Learning Log] 51 MCU Learning Log [Day1, 2022.1.09]

Publisher:SerendipityLoveLatest update time:2022-07-29 Source: csdnKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


3. STC89C52 microcontroller

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

}

}

Keywords:MCU Reference address:[MCU] [Learning Log] 51 MCU Learning Log [Day1, 2022.1.09]

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

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号