PIC MCU I/O Control

Publisher:qpb1234Latest update time:2018-11-21 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 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.


Keywords:MCU Reference address:PIC MCU I/O Control

Previous article:PIC microcontroller basic knowledge AD conversion
Next article:Example to explain the interrupt program of PIC microcontroller

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号