How to use the input and output ports of the microcomputer

The role and usage of input/output ports

There are a lot of silver wires around the microcomputer. We call them pins. The front ends of these pins are connected to peripheral devices such as LEDs and switches.
Insert picture description here

The pins connected to peripheral devices are connected to the functions of the microcomputer and are called input/output ports (I/O ports). The port is a terminal for connecting a microcomputer and peripheral equipment.

If you want to turn on/off the LED from the microcomputer, you can do so by executing the port control in the program. Port control has become the first hardware control technique for embedded developers to remember.

In high-function microcomputers, the I/O port is called GPIO (General Purpose Input/Output: General Purpose Input/Output), which has the same function as the I/O port!

The relationship between input and output ports and circuit diagrams and structure diagrams

Based on this, various peripheral devices such as a microcomputer and LEDs, switches, and motors are connected.

In order to control these peripheral devices from a microcomputer, you need to know where and what connection on the microcomputer. In order to confirm this connection, a basic circuit diagram is required.

The circuit diagram is made by the person in charge of hardware development of the research target product to connect the peripheral device to which pin of the microcomputer. If you have the ability to interpret circuit diagrams, you can understand what programs you can use to control peripheral devices.

Now, let's take a look at the PDF file in the circuit diagram. The quadrilateral drawn in the center is the H8/36064 microcomputer. The figure on the left below is a circuit diagram of the LED1 part with only the orange LED extracted.
Insert picture description here

The information required for software developers to control peripheral devices is connection information with a microcomputer called a port number. If you prepare a simplified circuit diagram structure diagram, development will become easier.

The relationship between input and output port registers and port numbers

When controlling peripherals from a program, the port number is the key. The port number connected to the orange LED1 is "P60". Let us refer to the H8 data sheet again to understand what this "P60" is. The next picture came out. As you can see, "P60" is written at the bottom.
Insert picture description here

Port 6 is the connection port between the microcomputer and the external pins, and it manages 8 ports in total. "P60" is the number representing terminal 0 of port 6.
For example, if it is the port number of "P74", it is the 4th terminal of port 7.
Insert picture description here

A port number is required to control peripheral devices.

Please note that the terminal number of the port is the same as the bit number, starting from 0!

Register configuration of input/output port 6

I/O port 6 also has registers. Let's look at the data sheet to see which registers exist.

  • Port Control Register 6 (PCR6)
  • Port data register 6 (PDR6)

These two registers are representative registers for I/O port control. Not only in the H8 microcomputer, similar registers are provided on the I/O port in many microcomputers. So, let us look at the role of each register in turn.

Port Control Register 6 (PCR6)

The first register to be suppressed is the port control register. First, start by confirming the explanation of the data sheet.
Insert picture description here

  • You can interpret the following by looking at this register specification
  • Consists of 1Byte (8 bits)
  • Each bit corresponds to 8 ports one-to-one
  • Can only write
  • Usually, 0 is the input port, and 1 is the output port
  • The initial value after system startup is all the input port settings.
    8 ports must select the input port or output port mode.

Input port

When set as an input port, the mode can read the state of the external voltage applied to the target port from the program. You can check whether the voltage is high or low. The typical peripheral device connected to the input port is a switch.

Output port

When it is set as an output port, you can increase and decrease the external voltage of the target port from the program. By raising and lowering the voltage, you can manipulate the connected peripherals. The typical peripheral device connected to the output port is an LED.

How to determine the input/output setting value of the port control register

The port is connected to peripheral devices such as LEDs and switches. Determine the input and output of the port control register according to these peripheral devices. Peripheral devices can be roughly divided into input devices and output devices.
Insert picture description here

Whether it is set as an input port or an output port depends on whether the peripheral device connected to the port is an input system or an output system.
Apart from this example, there are of course many devices in the world, but you can imagine which one it belongs to.

In a microcomputer, the created program runs. For example, it is detected by the program that the movement of the switch being pressed and instructing the motor to rotate is the control of the representative peripheral device using the port.

Note that the port control register is always only a register that determines the direction of input and output control!

Port data register 6 (PDR6)

The port data register controls a specific peripheral device according to the input/output direction determined by the port control register.
Insert picture description here

The characteristics of the register are as follows.

  • Consists of 1Byte (8 bits)
  • Each bit corresponds to 8 ports one-to-one
  • Both read and write
  • Varies according to the corresponding PCR6 input/output settings
  • The initial values ​​after system startup are all set to 0

Port data register control during input setting of port control register

If PCR is the input setting, PDR is the read control.

By looking at the bit value of the PDR, you can check whether the external voltage applied to the target port is Hige or Low. If the switch is connected to a peripheral device, configure the hardware to change the external voltage without pressing or pressing the switch.

This allows you to check whether the switch is pressed or disconnected by reading the voltage status from the program.

Port data register control during port control register output setting

If PCR is an output setting, PDR will be written to control.

By writing 1 in the bit value of the PDR, the external voltage applied to the target port can be written to Hige, thereby making the voltage Low.

If the LED is connected through a peripheral device, the LED can be turned on/off by raising and lowering the voltage.

Summary of the relationship between port control registers and port data registers

Let us conclude these two registers. The hardware-related actions of software registers are as follows.
Insert picture description here
※Note: The above picture is an image diagram for the explanation of the register. The connection port numbers of the LED and the switch in the window are different.
Therefore, registers exist on the boundary line between software and hardware. Controlling hardware from software is to interact through registers.

Guess you like

Origin blog.csdn.net/qq_18191333/article/details/107446536