PIC 18XXX PORT LAT

Publisher:HarmoniousSoulLatest update time:2016-11-04 Source: eefocusKeywords:PIC  18XXX  PORT  LAT Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
PIC18 series microcontrollers are high-end series of 8-bit microcontrollers of Microchip, USA. The maximum sink current or maximum source current allowed by any I/O pin is 25mA, which can directly drive LEDs and relays. The maximum sink current or maximum source current of PORTA, PORTB and PORTE is 200mA, the maximum sink current or maximum source current of PORTC and PORTD is 200mA, and the maximum sink current or maximum source current of PORTF and PORTG is 100mA (Note: PIC18F4550 does not have these two ports). 

           

The interaction between the microcontroller and peripherals is carried out through the I/O port. Each I/O port has three operation registers:

1. TRISx ——— Data direction register

Used to control the direction of the I/O pin, that is, to control whether 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 directly (not the latched value).

3. LATx ——— Output data latch

Writing to the port is writing to this latch (LATx). The data latch can also be read and written directly. If the peripheral does not use the pin and the TRISx bit configures the pin as an output, the data in the latch is output to the pin. 

    In the reset state, the reset value of TRISx is 0xff, that is, the values ​​of the 8 bits (D0 ~ D7) of the TRISx register are all 1. At this time, the corresponding PORTx pin is defined as input, and the corresponding output driver is in high impedance state. When set to 0, it means that the corresponding pin is defined as output.

 

It should be noted here that writing PORT is writing LAT, but reading PORT is different from reading LAT. Reading PORT reads the state of the pin, whether the pin is set as an input pin or an output pin. Reading LAT, on the other hand, gets the stored value of the output data latch. The value obtained by reading LAT may be different from the value obtained by reading PORT.

                     

In Microchip C18, the three operation registers of the I/O port can be operated bit by bit or byte by byte.

For example, the direction register of port B is represented by TRISB (or DDRB), and a certain bit is represented by TRISBbits.TRISB0 (or DDRB bits.RB0). Bytes are represented by TRISB (or DDRB).

For example, the PORT register of port B is represented by PORTB, and a certain bit is represented by PORTBbits.RB0. The byte is represented by PORTB.

For example, the output data latch of port B is represented by LATB, a certain bit is represented by LATBbits. LATB0. Bytes are represented by LATB.

    After the chip is reset, the value of the LATx (PORTx) latch is random. In order to eliminate the possibility of glitches on the I/O pin level, when initializing the port, first initialize the PORT data latch (LAT or PORT register) and then initialize the data direction register TRIS.

    The following example illustrates a specific application. In the figure below, PIC18F4550, power supply, crystal oscillator and light-emitting diodes form the simplest 8-bit microcontroller system, which requires lighting up 8 light-emitting diodes at the same time.   

  

    First, we can choose the bitwise operation method to implement it. It is not difficult to see that the bitwise operation does not actually realize the simultaneous lighting of the 8 LEDs connected to PORTB. It is just that the delay effect of the LEDs covering up the fact of sequential lighting makes the final effect reach the simultaneous lighting. The following is the implementation code of the bitwise operation method.

#include

void main(void)
{
 PORTBbits.RB0=1;
 TRISBbits.TRISB0=0; //Turn on the 1st LED
 PORTBbits.RB1=1;
 TRISBbits.TRISB1=0; //Turn on the 2nd LED
 PORTBbits.RB2=1;
 TRISBbits.TRISB2=0; //Turn on the 3rd LED
 PORTBbits.RB3=1;
 TRISBbits.TRISB3=0; //Turn on the 4th LED
 PORTBbits.RB4=1;
 TRISBbits.TRISB4=0; //Turn on the 5th LED
 PORTBbits.RB5=1;
 TRISBbits.TRISB5=0; //Turn on the 6th LED
 PORTBbits.RB6=1;
 TRISBbits.TRISB6=0; //Turn on the 7th LED
 PORTBbits.RB7=1;
 TRISBbits.TRISB7=0; //Light up the 8th LED
 
 while(1); 

    Secondly, it can be implemented by byte operation. The code is much simpler than bit operation, and it can truly achieve the requirement of lighting up at the same time. The following is the implementation code of byte operation.
#include

void main(void)
{
 PORTB=0xff;
 TRISB=0x00; //Light up 8 LEDs
 
 while(1); 
}

Keywords:PIC  18XXX  PORT  LAT Reference address:PIC 18XXX PORT LAT

Previous article:Use of PIC Timer
Next article:Four reasons to choose PIC microcontroller

Recommended ReadingLatest update time:2024-11-16 13:34

51+PIC microcontroller compatible program 1602 LCD display welcome interface
//Mistake: In 51, RW (L command, H data) is directly soldered to ground //∴ lcd_enable() is missing; this function {lcdrs = 0; lcdrw = 0; lcden = 0; delay(5); lcden = 1;} //Without this function setting, it cannot be displayed #define PIC // Description, if it is 51, you can delete this sentence #define ww //Instruct
[Microcontroller]
51+PIC microcontroller compatible program 1602 LCD display welcome interface
Application of Serial LED Decoder/Driver TEC9607 in PIC16C5X Series Microcontrollers
PIC16C5X series microcontrollers have the characteristics of high reliability, low power consumption, practicality, low price, and easy learning. In particular, OTP products have almost the same performance and price as mask products, so they are widely used in industrial and civilian products. In the design of appl
[Microcontroller]
Application of Serial LED Decoder/Driver TEC9607 in PIC16C5X Series Microcontrollers
PIC microcontroller reset system module
The reset function of the PIC16F87X series microcontrollers is relatively well designed. The conditions and reasons that cause the internal reset of the microcontroller can be roughly summarized into the following five types.   1. Power-on reset   Every time the microcontroller is powered on, the power-on reset circ
[Microcontroller]
PIC microcontroller read and write 24LC02 routine
I2C bus features      The main advantages of the I2C bus are its simplicity and effectiveness. Since the interface is directly on the components, the I2C bus takes up very little space, reducing the space on the circuit board and the number of chip pins, reducing the cost of interconnection. The bus can be up to 25 fe
[Microcontroller]
Implementation of wireless self-service ordering system based on PIC32, including schematic diagram and circuit diagram
0. Design Summary Today's society is an era of information explosion, but in most catering companies, the traditional way of ordering is still used, that is, when guests walk into a restaurant or bar, the waiter will come up with a menu and ask the guests to order. This traditional way of ordering brings a lot of in
[Microcontroller]
Implementation of wireless self-service ordering system based on PIC32, including schematic diagram and circuit diagram
How to calculate the execution time of AB program segment during PIC microcontroller software simulation
In MPLAB IDE, when you select MPLAB SIM as the debugger, there is a simulator logic analyzer under the view menu. Before and after the program segment to be calculated in the program, add the same IO port to set the output signal to 0 or 1. For example, to calculate the delay of delaynus(160), you can set RC0=1;RC0=0;
[Microcontroller]
Analysis and understanding of ARM position-independent code PIC
The characteristics of PIC are:         It can be correctly executed when loaded into any address space. The principle is that PIC's operations on constants and function entry addresses are based on the addressing mode of PC + offset. Even if the program is moved, the PC also changes, but the offset remains unchanged,
[Microcontroller]
Several Issues in PIC Microcontroller Development
The PIC series of microcontrollers produced by Microchip Corporation of the United States have been widely used in industrial control, instruments, meters, communications, home appliances, toys and other fields due to their ultra-small size, low power consumption, low cost and wide variety. This article summarizes som
[Microcontroller]
Several Issues in PIC Microcontroller Development
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号