I/O control is the most basic and core thing for a microcontroller. In fact, apart from AD DA conversion, most other things can be done by the I/O port. I/O control is simple but can be varied.
What a microcontroller does in most applications is to input high and low levels and output high and low levels. It is through this simple control level that most application controls are achieved.
What is an I/O port?
What is I/O? It means IN/OUT, which means input/output.
What exactly can I/O do?
If you are a novice who has just stepped into electronics, then you'd better know what the I/O port can do. Intuitive understanding is very helpful for your learning. The I/O port outputs a high (low) level to light up an LED light, can be connected to a relay to turn on a motor, to turn on a speaker. It can turn on many things and can also be used for communication, etc. Inputting a high (low) level can be used to detect buttons, detect infrared input, detect signals, etc.
The correspondence between I/O ports and registers
First, look at the schematic diagram of the next chip. Note the ports RA5~RA0.
Let's take a look at the direction control register TRISA of the RA port
Let's take a look at the data control register PORTA of port RA.
It can be seen that the second pin RA5 in the chip schematic corresponds to the TRISA5 bit in the TRISA register and the RA5 bit in the PORTA register, and the same is true for RA4~RA0.
That is to say, the TRISA5 bit in the TRISA register and the RA5 bit in the PORTA register control the RA5 port of the second pin.
How to control I/O ports?
In fact, controlling the microcontroller is to control each bit on the register, setting it high or low. It's like giving you a machine with 8 buttons. Then you press or release the corresponding buttons according to the instructions to get the function you want. The only difference is that we use C language instead of hands to press and release these buttons.
The TRISA register is the direction control register. It controls whether the I/O is input or output. For example, if the TRIS A5 bit in the TRISA register is set to 0, the RA5 port is output, and if it is set to 1, the RA5 port is input.
Specific implementation of C language:
Several ways to set RA5 port as input
The first type TRISAbits.TRISA5=1; //Simple and clear, only affects one bit. It is more convenient to set a single I/O
The second type TRISA |= 0x20; // not clear enough, only affects one bit
The third type TRISA = 0x20; // is not clear enough, affects other bits, and is more convenient to set multiple I/Os
Several ways to set RA5 port as output
The first type TRISAbits.TRISA5=0;
The second type TRISA &= ~0x20;
The third type TRISA = 0x00;
The PORTA register is a data control register. If it is in output state: if the RA5 bit in the PORTA register is 0, the RA5 port outputs a low level, and if the RA5 bit in the PORTA register is 1, the RA5 port outputs a high level.
If it is in input state: if the RA5 bit in the PORTA register is 0, it means that the RA5 port is currently at a low level; if the RA5 bit in the PORTA register is 1, it means that the RA5 port is currently at a high level.
Example program:
If an LED light is connected to port RA5, how can we light it up?
/*
The development environment is MPLAB X IDE, and the chip model is PIC16LF1823
*/
#include //header file
__CONFIG(FOSC_INTOSC&WDTE_OFF&PWRTE_ON&MCLRE_OFF&CP_ON&CPD_OFF&BOREN_ON
&CLKOUTEN_OFF&IESO_ON&FCMEN_ON);//This should be put on the previous line
__CONFIG(PLLEN_OFF&LVP_OFF); //Configuration bit
int main(int argc, char** argv) {
ANSELA=0; //Set all RA ports to digital ports.
TRISAbits.TRISA5=0; //Set RA5 to output
PORTAbits.RA5=1; //Set the output high level to light up the LED
while(1); //The program loops infinitely here
}
Header file: As long as there is #include in the program, the compiler will automatically find the header file of the corresponding model. The header file defines the address of each register, the address of the bit, etc.
Configuration bits: This is very important. Even if the program does not have any errors, the program will not work if the configuration bits are not configured properly. For details on how to configure, see (Configuration Bit Settings Based on MPLAB X IDE).
Analog digital port: When using the I/O port, make sure that the corresponding I/O port is set to a digital port.
Previous article:PIC microcontroller basic knowledge AD conversion
Next article:Example to explain the interrupt program of PIC microcontroller
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- [RTT & Renesas high performance CPK-RA6M4] 6. Software simulation I2C driver PCF8574 evaluation
- LPC1768 information release
- Filter capacitor value after bridge rectification
- Please look at this circuit, why is the output signal like this, the DC operating point is fine
- [GD32E503 Evaluation] mig29_Step1: Summary of development environment construction issues
- Mobile station development board CC26X2R1 LaunchPad recommended!
- Embedded System Basics: What is a Microcontroller MCU?
- Evaluation board quick test - based on TI Sitara Cortex-A9(3)
- Three-phase watt-hour meter transformer wiring
- #Idle Market# is open, you sell your idle items and I'll give you gifts!