Abstract: I heard that there are a lot of microcontroller friends will not use the structure? Pointer and structure is to learn microcontroller must master, if you don’t grasp the C language, microcontroller can not learn the essence, can only complete some low-level projects. To understand the structure and be able to flexibly use the structure to say that you have entered the single-chip microcomputer. This article will describe the use of the structure in the most popular way with STM32 monolithic. Solve your study C language, passed the computer level two or do not understand the microcontroller structure of the distress. Treasure article, remember to like forwarding collection.
We know that pointer and structure is the difficulty of SCM, so to learn C language, find a video to read……
Every single one of these videos gets a lot of play. For pure LEARNING C, this is very clear. You can’t help but comment: Wow! Speak really too clear! But when you really learn microcontroller, you will find that I have not learned C language? Computer level two I also passed ah! Why don’t you understand the pointer and structure? Did I learn a fake C language?
In fact, this is not your fault, is not the fault of the MCU, but in C language and the need for a transition between the MCU! This transition point is not explained in many SCM video tutorials. Since the educational institutions assume that you know it, they will not explain GPIO initialization when they talk about water lights, because by default you know how to do it.
Declare a GPIO_InitTypeDef structure, and then define a GPIO_InitStructure variable in LED_Init(void), This variable then sets the member in the structure of the GPIO_InitTypeDef. Here to understand, please continue to read.
1. Why structure
So instead of saying what a structure is, why do we need a structure? Only when you know why you need it, can you learn according to your needs, so that efficiency will be high. You know when we need to write a structure and how to use a structure.
Here we take a smart home project as an example.
Let’s start with a practical problem
One project had four sensors: light sensors, smoke sensors, alcohol sensors, and humidity sensors. And then these four sensors also set the alarm threshold range.
It’s usually written like this
#include "sys.h"
#include "delay.h"
#include "usart.h"
/* Record the sensor value */
float temperature; / / temperature
char humidity; / / humidity
char alcohol; // Alcohol concentration
int illumination; // Light intensity
/* Records the high and low sensor thresholds */
float temperature_threshold[2];
float humidity_threshold[2];
float alcohol_threshold[2];
float illumination_threshold[2];
int main(void)
{
uart_init(115200);// Initialize the serial port
delay_init();
while(1) {}}Copy the code
Of course you have to define a lot of other variables in a project, and you need to keep track of other variables
Then a carbon monoxide sensor was added a few days later
Then, a few days later, each sensor would have to add a bit to indicate whether it was working properly
Because of the needs of the project, four more same sensors were added: temperature and humidity, light intensity, smoke concentration and alcohol concentration.
Then four more identical sensors were added for temperature and humidity, light intensity, smoke concentration and alcohol concentration.
Screenshots can not be opened….
Full screen variables……
Full screen variables……
Full screen variables……
If you can’t plan for a rainy day at the beginning of a project and keep going, the entire code, let alone maintain, is a pain to write!
Full screen of variables…
Full screen of variables…
2. The structure makes its debut
And then the C guys built a functional struct
1. A structure is something that contains variables
Struct means you define a structure and sensors is the name of that structure and then a curly bracket {}
You can define any variable inside the curly braces
How do you use the variables inside?
Notice that the structure is a data type like int and char
It’s a data type
You can then use this data type to define variables
Defines a variable for the structure
Why do you define it that way?
Answer: you ask the gang that builds C language to go! Ask them why they designed it that way!
Then operate on the member variables inside the structure variables. When we define the structure variable, in the initialization of the variable inside the variable will automatically appear in the structure of the variable inside the member, if this code is you one by one out of the words, you will sigh structure is so wonderful in the MCU!
Some people say why do you put a dot in the middle of a structure variable?
Answer: you ask the gang that builds C language to go! Ask them why they designed it that way.
2. Struct variables can be defined as follows
You can also define more than one
See, each struct variable owns all the member variables in the struct individually.
As we said at the beginning, if you add another set of sensors: temperature and humidity, light intensity, smoke concentration, alcohol concentration.
To use a structure, you just need to define another structure variable.
But most of the time we see in the microcontroller structure is not defined as above, but in front of a typedef keyword.
An example of this is often seen in the header file of a library function
Typedef keyword
Take a look at baidu Baike’s definition of typedef
A typedef can give a data type another name
Typedef {data type} {other names}
#include "sys.h"
#include "delay.h"
#include "usart.h"
typedef int zhjiguoxin;/ / zhjiguoxin is int
zhjiguoxin value = 0;
int main(void)
{
uart_init(115200);// Initialize the serial port
delay_init();
printf("value=%d\r\n",value);
while(1) {}}Copy the code
While a typedef can be used to alias a variable, no one ever uses a name like the one above, so I’ll give you an example here.
4. The essence of the structure
Note:
1. The following represents the structure data type
2. Give this data type an alias
Note that there are three parts, typedef {data type} {other names}. So the sensor represents this structure.
It is advisable for beginners to save the following image on your computer, so that you will never forget the use of typedef in structures, and you will quickly remember structures.
3, struct sensors sen is not needed when defining struct variables in the future; Sensors sen; sensors sen; Can be
4. The structure name can be omitted
Note that the sensor is not called the name of the structure. For C, that sensor is not called the name of the structure. The C language structure name is struct keyword + tag. So for the sake of simplicity we see the structure of the MCU is written in the following form.
5. Structure variables can be placed in any variable
1, structure variables can be placed in any variable (int pointer)
#include "sys.h"
#include "delay.h"
#include "usart.h"
typedef struct
{
float temperature; / / temperature
char humidity; / / humidity
char alcohol; // Alcohol concentration
int illumination;// Light intensity
char CO; // Carbon monoxide concentration
int *p; // a pointer variable of type int
} sensor;
sensor sen;
int value =0;
int main(void)
{
uart_init(115200);// Initialize the serial port
delay_init();
sen.p=&value;// Assign the address of value
// Print the value of the address represented by p.
printf("value=%d\r\n",*(sen.p));
while(1) {}}Copy the code
Since it’s a pointer variable, assigning to a pointer variable is of course assigning an address.
2. Structure variables can be placed in any variable (function pointer)
#include "sys.h"
#include "delay.h"
#include "usart.h"
typedef struct
{
float temperature; / / temperature
char humidity; / / humidity
char alcohol; // Alcohol concentration
int illumination;// Light intensity
char CO; // Carbon monoxide concentration
int *p; // a pointer variable of type int
void (*fun)();
} sensor;
sensor sen;
void function(a)
{
printf("zhiguoxin\r\n");
}
int value =0;
int main(void)
{
uart_init(115200);// Initialize the serial port
delay_init();
sen.fun=function;
sen.fun();
while(1) {}}Copy the code
The address of a function is the address of a function, and the name of a function is the address of that function. Hence the following function(); Function assigns the address of function to function pointer fun. So that makes sense to you. If not clear advice to see more than 3 times!
Struct variables can be placed in any variable (struct variables)
This is called structural nesting, where a structure contains another structure as a member. When structure nesting occurs, the structure members must be accessed in a cascading manner, that is, referenced as the lowest member is found through the member selection operator.
#include "sys.h"
#include "delay.h"
#include "usart.h"
typedef struct
{
int i;
}zhiguoxin;
typedef struct
{
float temperature; / / temperature
char humidity; / / humidity
char alcohol; // Alcohol concentration
int illumination;// Light intensity
char CO; // Carbon monoxide concentration
int *p; // a pointer variable of type int
void (*fun)();
zhiguoxin guougo;
}sensor;
sensor sen;
int main(void)
{
uart_init(115200);// Initialize the serial port
delay_init();
sen.guougo.i=100;
printf("i=%d\r\n",sen.guougo.i);
while(1) {}}Copy the code
4. Structure variables can be placed in any variable (structure pointer)
A structure is a data type. Of course, data types can also define corresponding pointer variables.
Just like int can define int *p; The same
So if you find a structure in your code that is accessed through — >, the structure variable must be a pointer variable. Similarly if the structure in your code is passed. The struct variable is not a pointer variable, but a generic variable.
Summary: here the structure in the application of the microcontroller you have almost mastered, we may feel that the content of this talk is too simple, but only you put this simple basic knowledge of the firm, you will progress faster. Otherwise you’ll always feel like your code is lame.