Introduction to Microcontrollers

What is a microcontroller? According to the internet: “A microcontroller is a device that integrates a CPU, memory (RAM, ROM, EPROM), and various I/O interfaces (timers, counters, parallel I/O ports, etc.) on a single silicon chip, functioning like a computer, also known as a single board computer, microcontroller, or embedded controller.” I think this statement is very good, but here I want to say more.

Introduction to Microcontrollers

I don’t understand/(ㄒoㄒ)/~~

So, let’s first talk about microcontrollers and remember one thing: the pins of a microcontroller can receive or emit high and low levels. The principle seems to be “not seeking large scale, but aiming for small and complete.” n(*≧▽≦*)n

Introduction to Microcontrollers

The left image is a physical picture of the 51 microcontroller, and the right image is a pin diagram. What are pins? Pins, also known as leads, are the connections that extend from the internal circuit of the integrated circuit (chip) to the external circuit. That is the small white rectangles in the left image. We first need to control ourselves with our thoughts and not wonder what is inside this black block; we just need to know that these pins can receive or emit high and low levels. The received levels can influence the output, and the output can also affect the input. To understand the mystery of the mutual influence between the two, we need to know the principles of this black block. For those who are just getting acquainted with microcontrollers, let’s first get to know the visible components. Pins. Pin 20 connects to the negative terminal, and pin 40 connects to the positive terminal; this is for power supply, with 5V voltage being the best. Ports P1 and P2 have a total of 16 pins; they are the same, both output or receive the so-called high and low levels. Pin P3 is quite busy; it not only has the same functions as P1 and P2 but also has a second function, which I won’t discuss now. Port P0 is somewhat deficient; it needs to be provided with driving force to function like P1, and this driving force is called a pull-up resistor. Of course, this design serves a purpose, but I won’t elaborate on that for now. The ports we mainly use are these P ports.

Learning C language or Java, the first thing is to say “hello world”; similarly, when learning microcontrollers, the first task is to light up a lamp. We learned how to do this in middle school: take one or two batteries, connect one end of the lamp to the negative terminal and the other end to the positive terminal, and it will light up. Just like this.

Introduction to Microcontrollers

Now we want to use a microcontroller to light a lamp; first, let’s talk about this lamp, which we call an LED, or light-emitting diode (I remember at first I didn’t know LED meant light-emitting diode (⊙﹏⊙)). We all know about diodes, which have unidirectional conductivity, as shown in the diagram below.

Introduction to Microcontrollers

Connect A to the positive terminal and B to the negative terminal, and it will conduct. If reversed, it won’t work; of course, you can forcibly break it, making it cease to exist. Now, what about light-emitting diodes? As the name suggests, when A is connected to the positive terminal and B to the negative terminal, it conducts and the light turns on; otherwise, it won’t light up. Here, we have new terms to describe the positive and negative terminals: high level and low level. In physical terms, the positive terminal refers to the end with a higher potential (voltage) in the power source, while the negative terminal is the opposite. High level corresponds to the positive terminal, while the negative terminal corresponds to the low level. When we talk about “level,” the dictionary explains it as being parallel to the horizontal plane or reaching a certain height in some aspect, extending to mean the comparative conclusion of things under the same conditions.

Introduction to Microcontrollers

Look at this picture; water flows from a high elevation to a lower one. Water flows from a high level to a low level. Now, think about high level and low level, and it will all become clear. Electric charges flow from high level to low level, just like flowing from the positive terminal to the negative terminal. That’s the principle. In digital systems, we use 1 to represent high level and 0 to represent low level. Why? Using 0 to represent high level is also possible, but doesn’t it feel awkward that 0 is larger than 1? Next, let’s talk about light-emitting diodes; see the diagram below.

Introduction to Microcontrollers

When A is at high level and B is at low level, it lights up. So what does this have to do with microcontrollers? Remember how I said earlier? The pins of the microcontroller can receive or emit high and low levels. Therefore, when the high level connected to A is provided by the microcontroller, and B is connected to the negative terminal, the light will turn on. If A is connected to an external high level and B is connected to the low level emitted by the microcontroller, the light will also turn on. In fact, these two connection methods are also called common anode and common cathode, which we will discuss later. So how does the microcontroller emit high and low levels? This requires software to achieve. I am using Keil 3; I won’t elaborate on how the software works~( ̄0 ̄)/. I must say that any program written without looking at the circuit diagram is nonsense, so let’s assume the LED is connected to pin P1.1. #include<reg52.h> To know what this header file does is simple: select the header file, right-click, and select open document #include<reg52.h> to see some content. Introduction to Microcontrollers document means file. include means to include, and reg52.h is a header file defined for 51/52 microcontrollers, mainly including register definitions. So we can roughly understand what this header file means. SFR = special function register. For example, if you write P0=0x01, the software will assign 0x01 to address 0x80. This is how the bridge between C language and microcontrollers is built. If you have learned assembly, you will be familiar with this content. If you are not clear, I think there is no need to get stuck; just write these when programming, and you will understand later. It is essential to know that the essence of a program is the data transfer between registers. #include<reg52.h> sbit led = P1^1; // Define the bit variable of the special function register. In functions, bit operations on ports P0 to P3 are not allowed, so you need to define bits beforehand. main() { while(1) { led=1; }} Introduction to Microcontrollers Why do we write while(1)? main is the main program, while(1) means an infinite loop. Those who have learned C language know that keeping led=1; going on is necessary. Why do we need to add while(1) to keep it going? The main program executes sequentially; led=1; is not its last statement. If we don’t add while(1), led=1 is indeed the last statement; yes, that’s the case for C language, but assembly is not like that. For microcontrollers, it is not that after executing led=1 it stops working; we need to explore the internal structure to understand this concept. I hope you now understand this concept. Additionally, I forgot to add a current limiting resistor. A stream of water is not yet finished; I will continue next time with more nonsense. My knowledge is shallow, and if there are any mistakes, please teach me. I am here, feeling grateful.

Introduction to Microcontrollers

For more comments from netizens, please click Read Original below.

Introduction to Microcontrollers

Recent Exciting Content Recommendations:

Rectification Discussion (1)

Rectification Discussion (2)

Rectification Discussion (3)

Introduction to Microcontrollers

Introduction to Microcontrollers

Introduction to Microcontrollers

Leave a Comment

×