PIC18 Series Single Chip microcomputer I/O port operation Register and its application

Source: Internet
Author: User
Tags bitwise

Test chip:Microchip PIC 18f4550

Integrated development environment:MPLAB IDE v8.53

Compiler:Microchip C18

PIC18 Series MCU is the American micro-core company (Microchip)8-bit single-chip series of high-end series, any one of itsI/O pins allow maximum sink current or maximum pull current up toThe LEDs and relays can be driven directly . the maximumsink or maximum pull current of the PORTA, PORTB, and PORTE is a sum of 200 MA, the maximum sink or maximum pull current of PORTC and PORTD Ma, the maximum sink current or maximum pull current of the portf and PORTG is at the sum of (note:pic18f4550 does not have these two ports).

the interaction between MCU and peripherals is I/O ports have three operation registers per I/O port:

1,TRISx ——— Data Direction register

Used to control the direction of the I/O pin, which is used to control whether the Portx is input or output.

2.portx ——— Port Register

Used to latch output data. When reading portx , the device reads the I/O pin level (not the latch value) directly.

3,LATx ——— Output data latches

write port is write the latch ( latx). The data latch can also be read and written directly. If the peripheral is not using the pin, and the TRISX bit is configured with the PIN as output, the data inside the latch is output to the pin.  

in the reset state, The reset value of the TRISx is 0xFF, i.e. the value of 8 bits (D0 to D7) of The TRISx register is 1 . at this point the corresponding portx pin is defined as input and the corresponding output driver is in a high-impedance state. A setting of 0 indicates that the corresponding PIN is defined as output.

It should be noted that the writing Port is written in lat, but reading port differs from read lat. read Port reads the status of the pin, regardless of whether the pin is set to an input pin or an output pin. The read lat gets the stored value of the output data latch, and the value read LAT may be different from the value read port.

In Microchip C18, theI/O Port three operation registers can be bitwise (bit), or byte (byte) operation.

For example , the direction register for Port B is represented by TRISB (or DDRB), one with trisbbits.trisb0 (or DDRB bits. RB0) said. Bytes are represented by TRISB (or DDRB).

such as Port b port Register with portb said that one with portbbits.rb0. Bytes are denoted by portb.

such as Port b output data latches latb, one with lat< Span lang= "en-us" xml:lang= "en-us" >bbits.  lat b0. Bytes are denoted by latb.

since the chip is reset,the value of the LATx (portx) latch is random, in order to eliminate the possibility of Burr at the I/O pin level, when initializing the port , first initialize the data latch (LAT or port register) of the port , and then initialize the data direction register TRIS.

Following an example to illustrate the specific application, pic18f4550 and power supply, crystal and light-emitting diodes constitute the simplest 8-bit microcontroller system, the need to light up 8 light-emitting diodes.    

 

First, you can choose the bitwise operation method to implement. It is not difficult to see that the bitwise operation is actually not the real realization of the simultaneous light and PORTB connected to the 8 light-emitting diodes, but the light-emitting diode light-emitting delay effect masked the fact that light, so that the final effect is lit at the same time. The following is the implementation code for bitwise operation.

#include <p18f4550.h>

void Main (void)

{

Portbbits.rb0=1;

trisbbits.trisb0=0; Light 1th LED

Portbbits.rb1=1;

trisbbits.trisb1=0; Light 2nd LED

Portbbits.rb2=1;

trisbbits.trisb2=0; Light 3rd LED

Portbbits.rb3=1;

trisbbits.trisb3=0; Light 4th LED

Portbbits.rb4=1;

trisbbits.trisb4=0; Light 5th LED

Portbbits.rb5=1;

trisbbits.trisb5=0; Light 6th LED

Portbbits.rb6=1;

trisbbits.trisb6=0; Light 7th LED

Portbbits.rb7=1;

trisbbits.trisb7=0; Light 8th LED

while (1);

}

second, it can be implemented by byte operation, the code is much simpler than the bitwise operation, and it really realizes the requirement to be lit at the same time. The following is the implementation code by byte operation.

#include <p18f4550.h>

void Main (void)

{

Portb=0xff;

trisb=0x00; 8 LEDs lit

while (1);

}

PIC18 Series Single chip microcomputer I/O port operation Register and application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.