Micro bit Basic Programming Guide Input Function Group Special Analysis 7

Mondo Technology Updated on 2024-01-30

Start planning my 2024 micro:bit basic programming guide Input Function Group (Special Analysis)7

Analysis of the four working modes of the GPIO port of a microcontroller

The following ——

Copyright Notice: This article is the original article of CSDN blogger Xirui, following CC 40 BY-SA copyright agreement, **please attach the original source link and this statement.

Original link:The series of single-chip microcomputer GPIO port four modes

Preface. How to configure the port mode.

Port mode configuration registers (pxm0, pxm1).

1.Quasi-two-way port mode.

Characteristics of quasi-two-way port.

2.Open-drain output mode.

Open-drain output mode function.

3.Push-pull output mode.

4.High-impedance input mode.

Preface.

The four basic working modes of the GPIO port of STC51 series microcontroller are: quasi-bidirectional port, push-pull output, high-impedance input and open-drain output.

gpioThe port operates in four modes

With the update and development of technology, the performance of the single-chip microcomputer continues to be enhanced, and the working mode of GPIO is more, taking the STC8G microcontroller as an example, all GPIO ports have 4 most basic working modes, to be precise, every bit (every pin) of all IO ports has 4 working modes: quasi-bidirectional port, push-pull output, high-impedance input, open-drain output. It can also be that different pins have different modes, and each pin operates independently.

After the single-chip microcomputer is powered on: except for P30 and p31, all other IO ports are high-impedance states, and the user must set the working mode of the IO port before using the IO port, that is, in the initialization program design, write and set the corresponding special function register unit. Otherwise, after the program is powered on, the default state is a high-impedance state, and the functions do not match, so there will be no corresponding input and output functions (this is the requirement, so that I have suffered many losses. Obviously there is no problem with the program, but there is no output).

Therefore, in order to develop good programming habits, I wrote a starting architecture and original architecture model at the beginning of program design, so as to remind myself to consider, design, and set the program content part in advance at the beginning of the program.

As follows (configure the IO port working mode), no matter how you use it, you don't need to set it to the quasi-bidirectional port mode first. That said, the first thing that comes to mind is the ability to configure the GPIO port.

How to configure the port mode.

Of course, my program architecture is a bit problematic, I am aligning the io ports to both directions, what should I do when I encounter different pins to set different modes?The function setting of the GPIO port pin is operated according to the bytes in the special function registers, and the operation of the registers is performed on a byte-by-byte basis, so if you want to set the port pins to different applications, you must first perform a logical conversion of the relevant bytes to or, or not, and write the final result into the corresponding control register.

Port mode configuration registers (pxm0, pxm1).

This is the data sheet of the STC15 series microcontroller, and the relevant content is as follows

It can be seen that each group of IO ports is jointly controlled by two sets of special function registers (taking P1 port as an example).

Two registers for each bit to form a two-digit control code, 00-01-10-11 to correspond to the control mode of each bit (pin).

For example, the control P1 port bus, that is, SFR:P1M1 P1M0;If you control the P5 port bus, i.e. SFR: P5M1 P5M0.

In this combination of two registers, there are a total of 5 sets of IO ports in the STC microcontroller, and each group of IO ports corresponds to two control registers, that is, there are a total of 10 sets of SFR port control mode registers.

It is important to note that none of these register addresses support bit addressing, so they can only be configured directly to SFR in hexadecimal format.

In other words, the function setting of the GPIO port pins is operated according to the bytes in the special function registers, and the operation of the registers is carried out on a byte-by-byte basis, so if you want to set the port pins to different applications, you must first perform a logical conversion of the relevant bytes to or, or not, and then write the final result into the corresponding control register.

For example, p10 requires open-drain output mode, p11 is the high-impedance input mode, etc. (as shown in the figure)....

then the corresponding register p1m1=0x03;p1m0=0x09;The corresponding program can be assigned in this way (as are the other ports).

Related Pages