Pic microcontroller io port control tutorial c language programming implementation

1. What is I/O? What can I/O do?

I / O control is the most basic and core thing for the microcontroller, in fact, in addition to the AD DA conversion. Most other things I/O can do. I/O control is simple but can be varied.

I/O is the English abbreviation of Input/Output, which is translated as input/output.

I/O is the most basic thing for the MCU. In addition to the AD and DA conversion, most of the I/O can be done.

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.

2. Correspondence between the TRISA register and the io port:

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.

3. 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.

4, the specific implementation of C language:

Several ways to set the RA5 port as input

The first type of TRISAbits.TRISA5=1; //Simple and clear, it is convenient to only affect one bit to set a single I/O.

The second TRISA |= 0x20; / / is not clear enough, only affect one bit

The third type of TRISA = 0x20; / / is not clear enough, affecting other bits, it is convenient to set multiple I / O

Several ways to set the RA5 port to output

The first type of TRISAbits.TRISA5=0;

The second type of TRISA &= ~0x20;

The third type of 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 //Head file

__CONFIG(FOSC_INTOSC&WDTE_OFF&PWRTE_ON&MCLRE_OFF&CP_ON&CPD_OFF&BOREN_ON

&CLKOUTEN_OFF&IESO_ON&FCMEN_ON);//This is going to the previous line.

__CONFIG(PLLEN_OFF&LVP_OFF) ;//Configuration bit

Int main(int argc, char** argv) {

ANSELA=0; / / set the RA port to a digital port,

TRISAbits.TRISA5=0;//Set RA5 as output

PORTAbits.RA5=1;//Set the output high level to light the LED

While(1);//Program in this infinite loop

}

Header file: As long as the #include compiler is in the program, it will automatically find the header file of the corresponding model. The header file defines the address of each register, the address of the bit, and so on.

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.

Let our team of experts design and build your very own soft pvc material, metal material and plastic material custom shaped Cartoon Usb Flash Drive. Such as 3D chickren animal Cartoon USB Flash Drive, Cute Monkey Cartoon USB Flash Drive, Lovely Panda Cartoon USB Flash Drive and so on.

Cartoon USB Flash Drive

All you need to do is send us a photo or sketch, we will then send you a FREE mockup of your design for approval.

Once you have approved your design, you can have your order of custom shaped Cartoon USB Flash Drive in just 1 weeks.


Cartoon USB Flash Drive


For inspiration, feel free to have a quick look at our custom shape Cartoon USB Flash Drive gallery below.

Please note, the minimum order quantity for this service is only 100 UNITS, with memory sizes available from 32MB to 128GB.


Cartoon USB Flash Drive


Cartoon USB Flash Drive is An ideal way to store all your pictures, documents, music and videos. Cartoon USB Flash Drive Can act as a wonderful gift for your friends and families and A great way to distinguish your masses of USB flash drives from each other as our cute USB come in a variety of variations for every day use. 

  • Compatibility: Desktop, Laptop, Macintosh, Tablet, Speakers all with USB1.0 and 2.0.
  • Operating System : Windows7/Vista/XP/2000/ME/NT/98,Linux (Sometimes incompatible with Mac OS 9.X/Linux2.4)
  • Fine choice for advertisement allow to print LOGOs and advertisement.
  • Very Low Power Consumption, durable solid-state storage.
  • Small and exquisite design brings much convenience.
We are a professional Chinese manufacturer of Cartoon USB Flash Drive, and look forward to your cooperation!

Cartoon Usb Flash Drive

Cartoon Usb Flash Drive,Car Shaped Usb Flash Drive,Pvc Cartoon Usb Flash Drive,Cartoon Panda Usb Flash Drive

Reteck Electronic Co., Ltd. , https://www.reteck.com

Posted on