What MCU does in most applications is input high and low level, output high and low level. It is through this simple control level to achieve most of the application control.
What is an I/O port?
What is I/O is whether IN/OUT is input/output.
What can I/O do specifically?
If you are new to electronics, then you better know what the I/O port can do. A perceptual understanding is very helpful for your study. The I/O port outputs a high (low) level that can be used to illuminate an LED. It can be connected to a relay to turn on the motor to turn on the sound. Open a lot of things, can also be used for communication and so on. Entering a high (low) level can be used to detect buttons, detect infrared inputs, detect signals, and more.
Correspondence between I/O port and register
First look at the schematic of the next chip. Pay attention to the mouths of RA5~RA0
Look at the direction control register TRISA of the RA port
Look at the RA port data control register PORTA
It can be seen that the second pin RA5 in the chip schematic and the TRISA5 bit in the TRISA register correspond to the RA5 bit in the PORTA register, and RA4~RA0 correspond to this.
This means that 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 the I/O port?
In fact, controlling the microcontroller, is to control the various bits on the register, set the high setting to low. It's like giving you a button with 8 machines. Then according to the instructions, press or release the corresponding button according to your needs to get the function you want. But we are not using the hand but the C language to press and release these buttons.
The TRISA register is the direction control register. Is to control the I / O input or output. For example, the TRISA5 bit in the TRISA register is set to 0. The RA5 port is set to 1 and the RA5 port is input.
The specific implementation of the C language:
Several ways to set the RA5 port as input
The first TRISAbits.TRISA5=1; // Simple and straightforward, it only makes it easier to set a single I/O for one bit.
The second TRISA |= 0x20; / / is not clear enough, only affect one bit
The third TRISA = 0x20; / / is not clear enough, affecting other bits, setting up multiple I / O is more convenient
Several ways to set the RA5 port to output
The first type TRISAbits.TRISA5=0;
The second TRISA &= ~0x20;
The third TRISA = 0x00;
The PORTA register is a data control register. If it is the output state: the RA5 bit in the PORTA register is 0, then the RA5 port outputs a low level, and the RA5 bit in the PORTA register is 1 the RA5 port outputs a high level.
If it is the input state: the RA5 bit in the PORTA register is 0, indicating that the RA5 port is currently low, and the RA5 bit in the PORTA register is 1, indicating that the RA5 port is currently high.
Example program:
If an LED is connected to the RA5 port. How will we light him?
/*
The development environment is MPLAB X IDE, the chip model is PIC16LF1823
*/
#include
&CLKOUTEN_OFF&IESO_ON&FCMEN_ON);//This should be placed on the previous line to __CONFIG(PLLEN_OFF&LVP_OFF) ;//Configuration bit int main(int argc, char** argv) {
ANSELA=0;//Set all RA ports to digital port, TRISAbits.TRISA5=0; //Set RA5 as output PORTAbits.RA5=1; //Set output high level to light LED
While(1);//Program in this infinite loop}
Header file: as long as #include in the program
Configuration bit: This is very important even if the program does not have any error configuration bits that are not configured. How to configure it can be seen (based on the MPLAB X IDE configuration bit settings).
Analog digital port: When using the I/O port, make sure that the corresponding I/O port is set to digital port.
ARD series ARD2 series start timeout motor protector (hereinafter referred to as protector) can perform various conditions such as start timeout,over load, phase failure, unbalance, under load, ground/leakage, blockage, short circuit and external fault during motor operation. Protection, and SOE fault event recording function, convenient for on-site maintenance personnel to find the cause of the fault. Suitable for coal petrochemical,smelting, electric power, ships, and civil construction. The protector has RS485 remote communication interface, DC 4 ~ 20mA analog output, and PLC, PC and other control machines form a network system. Remote monitoring of motor operation.
Start Timeout Motor Protector,Short Circuit Motor Protector,Residual Current Motor Protector,Motor Comprehensive Protection Device
Jiangsu Acrel Electrical Manufacturing Co., LTD. , https://www.acrel.com.pk