4. Introduction to PIC32 series GPIO

Publisher:幸福满溢Latest update time:2022-07-28 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. PIC32 Reference Resources

PIC32 Series Reference Manual Chinese version Link address: PIC Series Reference Manual - Chinese version Chapter 12 IO Ports.pdf


2. Introduction to GPIO registers

1. TRIS (Tri-state) register


The TRIS register is used to configure the direction of data passing through the IO port pins. The TRIS register determines whether the PORT I/O pin is an input or output.


• When a TRIS bit is set to 1, the corresponding I/O port pin is configured as an input.


• When a TRIS bit is set to 0, the corresponding I/O port pin is configured as an output.


• When reading a TRIS register, the last value written to the TRIS register is read.


• After a power-on reset, all I/O port pins are defined as inputs.


2. PORT register


The PORT register is used to access (read) the I/O pins.


• When writing to a PORT register, the data is written to the corresponding LAT register (PORT data latch). Those I/O port pins configured as outputs are updated.


• Writing to the PORT register is effectively equivalent to writing to the LAT register.


• When reading the PORT register, the synchronization signal applied to the port I/O pins is read.


3. LAT register


The LAT register (PORT Data Latch) is used to hold the data written to the port I/O pins.


• When the LAT register is written, the data is latched into the corresponding port I/O pins. Those I/O port pins configured as outputs are updated.


• When the LAT register is read, the data held in the PORT data latch is read instead of reading the data from the port I/O pins.


4. SET, CLR and INV I/O port registers


In addition to the TRIS, PORT, and LAT base address registers, each port module has associated SET (set), CLR (clear), and INV (invert) registers that provide atomic bit manipulation and allow faster I/O pin operation. As the register names imply, writing a value to a SET, CLR, or INV register effectively performs the operation indicated by its name, but only modifies the bits designated as 1 in the corresponding base address register. Bits designated as 0 are not modified.


• Writing 0x0001 to the TRISASET register only sets bit 0 in the base address register TRISA to 1.


• Writing 0x0020 to the PORTDCLR register will only clear bit 5 in the base address register PORTD.


• Writing 0x9000 to the LATCINV register only inverts bits 15 and 12 in the base address register LATC.


Reading the SET, CLR, and INV registers returns undefined values. To see the effect of a write to the SET, CLR, or INV registers, the base address register must be read.


The SET, CLR, and INV registers are not limited to the TRIS, PORT, and LAT registers. Other I/O port module registers ODC, CNEN, and CNPUE also have these bit manipulation registers.


5. In short


TRS register: Sets whether the digital pin is input or output, set to 1 for input (default), clear to 0 for output;


LAT register: write port value


POR Register: Read Port Value


CLR register: clear


SET register: set to 1


INV register: rollover


Port Combinations


LATCSET |= (1<<0); // PORTC port register bit0 set to 1


LATCCLR |= (1<<0); // PORTC port register bit0 cleared to 0


LATCINV |= (1<<0); //PORTC port register bit0 flip


3. Use registers to configure the IO port output LED to flash

LED pin port RB0


TRISBCLR |= (1<<0); //Set RB0 as output port;

LATBCLR |= (1<<0); //output 0;

LED Flip Program


int main(void)

{

    TRISBCLR |= (1<<0); //Set RB0 as output port;

    LATBCLR |= (1<<0); //output 0;

 

    while(1)

    {

        LATBINV |= (1<<0); //Port level flip

        delay(1000); //delay 1000ms

    }

}

4. Library function encapsulation definition

1. Port definition


typedef enum

{

    GPIO_PORT_A = 0,

    GPIO_PORT_B = 1,

    GPIO_PORT_C = 2,

    GPIO_PORT_D = 3,

    GPIO_PORT_E = 4,

    GPIO_PORT_F = 5,

    GPIO_PORT_G = 6,

} GPIO_PORT;

2. Pin definition


typedef enum

{

    GPIO_PIN_RA0 = 0,

    GPIO_PIN_RA1 = 1,

    GPIO_PIN_RA2 = 2,

    GPIO_PIN_RA3 = 3,

    GPIO_PIN_RA4 = 4,

    GPIO_PIN_RA5 = 5,

    GPIO_PIN_RA6 = 6,

    GPIO_PIN_RA7 = 7,

    GPIO_PIN_RA9 = 9,

    GPIO_PIN_RA10 = 10,

    GPIO_PIN_RA14 = 14,

    GPIO_PIN_RA15 = 15,

    GPIO_PIN_RB0 = 16,

    GPIO_PIN_RB1 = 17,

    GPIO_PIN_RB2 = 18,

    GPIO_PIN_RB3 = 19,

    GPIO_PIN_RB4 = 20,

    GPIO_PIN_RB5 = 21,

    GPIO_PIN_RB6 = 22,

    GPIO_PIN_RB7 = 23,

    GPIO_PIN_RB8 = 24,

    GPIO_PIN_RB9 = 25,

    GPIO_PIN_RB10 = 26,

    GPIO_PIN_RB11 = 27,

    GPIO_PIN_RB12 = 28,

    GPIO_PIN_RB13 = 29,

    GPIO_PIN_RB14 = 30,

    GPIO_PIN_RB15 = 31,

    GPIO_PIN_RC1 = 33,

    GPIO_PIN_RC2 = 34,

    GPIO_PIN_RC3 = 35,

    GPIO_PIN_RC4 = 36,

    GPIO_PIN_RC12 = 44,

    GPIO_PIN_RC13 = 45,

    GPIO_PIN_RC14 = 46,

    GPIO_PIN_RC15 = 47,

    GPIO_PIN_RD0 = 48,

    GPIO_PIN_RD1 = 49,

    GPIO_PIN_RD2 = 50,

    GPIO_PIN_RD3 = 51,

    GPIO_PIN_RD4 = 52,

    GPIO_PIN_RD5 = 53,

    GPIO_PIN_RD6 = 54,

    GPIO_PIN_RD7 = 55,

    GPIO_PIN_RD8 = 56,

    GPIO_PIN_RD9 = 57,

    GPIO_PIN_RD10 = 58,

    GPIO_PIN_RD11 = 59,

    GPIO_PIN_RD12 = 60,

    GPIO_PIN_RD13 = 61,

    GPIO_PIN_RD14 = 62,

    GPIO_PIN_RD15 = 63,

    GPIO_PIN_RE0 = 64,

    GPIO_PIN_RE1 = 65,

    GPIO_PIN_RE2 = 66,

    GPIO_PIN_RE3 = 67,

    GPIO_PIN_RE4 = 68,

    GPIO_PIN_RE5 = 69,

    GPIO_PIN_RE6 = 70,

    GPIO_PIN_RE7 = 71,

    GPIO_PIN_RE8 = 72,

    GPIO_PIN_RE9 = 73,

    GPIO_PIN_RF0 = 80,

    GPIO_PIN_RF1 = 81,

    GPIO_PIN_RF2 = 82,

    GPIO_PIN_RF3 = 83,

    GPIO_PIN_RF4 = 84,

    GPIO_PIN_RF5 = 85,

    GPIO_PIN_RF8 = 88,

    GPIO_PIN_RF12 = 92,

    GPIO_PIN_RF13 = 93,

    GPIO_PIN_RG0 = 96,

    GPIO_PIN_RG1 = 97,

    GPIO_PIN_RG2 = 98,

    GPIO_PIN_RG3 = 99,

    GPIO_PIN_RG6 = 102,

    GPIO_PIN_RG7 = 103,

    GPIO_PIN_RG8 = 104,

    GPIO_PIN_RG9 = 105,

    GPIO_PIN_RG12 = 108,

    GPIO_PIN_RG13 = 109,

    GPIO_PIN_RG14 = 110,

    GPIO_PIN_RG15 = 111,

 

    GPIO_PIN_NONE = -1

 

}GPIO_PIN;


3. Pin number


typedef enum

{

  CN0_PIN = 1 << 0,

  CN1_PIN = 1 << 1,

  CN2_PIN = 1 << 2,

  CN3_PIN = 1 << 3,

  CN4_PIN = 1 << 4,

  CN5_PIN = 1 << 5,

  CN6_PIN = 1 << 6,

  CN7_PIN = 1 << 7,

  CN8_PIN = 1 << 8,

  CN9_PIN = 1 << 9,

  CN10_PIN = 1 << 10,

  CN11_PIN = 1 << 11,

  CN12_PIN = 1 << 12,

  CN13_PIN = 1 << 13,

  CN14_PIN = 1 << 14,

  CN15_PIN = 1 << 15,

  CN16_PIN = 1 << 16,

  CN17_PIN = 1 << 17,

  CN18_PIN = 1 << 18,

  CN19_PIN = 1 << 19,

  CN20_PIN = 1 << 20,

  CN21_PIN = 1 << 21,

}CN_PIN;


5. Library functions

1. Port operation, multiple IO ports can be set at the same time


//Read the port level status

uint32_t GPIO_PortRead(GPIO_PORT port)

{

    return (*(volatile uint32_t *)(&PORTA + (port * 0x10)));

}

 

//Write port level

void GPIO_PortWrite(GPIO_PORT port, uint32_t mask, uint32_t value)

{

    *(volatile uint32_t *)(&LATA + (port * 0x10)) = (*(volatile uint32_t *)(&LATA + (port * 0x10)) & (~mask)) | (mask & value);

}

 

//Read the port latch value

uint32_t GPIO_PortLatchRead(GPIO_PORT port)

{

    return (*(volatile uint32_t *)(&LATA + (port * 0x10)));

}

 

//Set the port

void GPIO_PortSet(GPIO_PORT port, uint32_t mask)

{

    *(volatile uint32_t *)(&LATASET + (port * 0x10)) = mask;

}

 

// Clear the port

void GPIO_PortClear(GPIO_PORT port, uint32_t mask)

{

    *(volatile uint32_t *)(&LATACLR + (port * 0x10)) = mask;

}

 

//Port level flip

void GPIO_PortToggle(GPIO_PORT port, uint32_t mask)

{

    *(volatile uint32_t *)(&LATAINV + (port * 0x10)) = mask;

}

 

//Port input enable

void GPIO_PortInputEnable(GPIO_PORT port, uint32_t mask)

{

    *(volatile uint32_t *)(&TRISASET + (port * 0x10)) = mask;

}

 

//Port output enable

void GPIO_PortOutputEnable(GPIO_PORT port, uint32_t mask)

{

    *(volatile uint32_t *)(&TRISACLR + (port * 0x10)) = mask;

}


2. Operation on a single IO port


//Write pin level

static inline void GPIO_PinWrite(GPIO_PIN pin, bool value)

{

    GPIO_PortWrite((GPIO_PORT)(pin>>4), (uint32_t)(0x1) << (pin & 0xF), (uint32_t)(value) << (pin & 0xF));

}

 

//Read the pin level

static inline bool GPIO_PinRead(GPIO_PIN pin)

{

    return (bool)(((GPIO_PortRead((GPIO_PORT)(pin >> 4))) >> (pin & 0xF)) & 0x1);

}

 

//Read pin register value

static inline bool GPIO_PinLatchRead(GPIO_PIN pin)

{

    return (bool)((GPIO_PortLatchRead((GPIO_PORT)(pin>>4)) >> (pin & 0xF)) & 0x1);

}

 

//Pin level flip

static inline void GPIO_PinToggle(GPIO_PIN pin)

{

    GPIO_PortToggle((GPIO_PORT)(pin>>4), 0x1 << (pin & 0xF));

}

 

//Set up the pins

static inline void GPIO_PinSet(GPIO_PIN pin)

{

    GPIO_PortSet((GPIO_PORT)(pin>>4), 0x1 << (pin & 0xF));

}

 

// Clear pins

static inline void GPIO_PinClear(GPIO_PIN pin)

{

    GPIO_PortClear((GPIO_PORT)(pin>>4), 0x1 << (pin & 0xF));

}

 

//Pin input enable

static inline void GPIO_PinInputEnable(GPIO_PIN pin)

{

    GPIO_PortInputEnable((GPIO_PORT)(pin>>4), 0x1 << (pin & 0xF));

}

 

//Pin output enable

static inline void GPIO_PinOutputEnable(GPIO_PIN pin)

{

    GPIO_PortOutputEnable((GPIO_PORT)(pin>>4), 0x1 << (pin & 0xF));

}


LED Flip Program


int main(void)

{

    GPIO_PortOutputEnable(GPIO_PORT_B, CN0_PIN); //RB0 is set to output

 

    while(1)

    {

        GPIO_PortToggle(GPIO_PORT_B, CN0_PIN);

        delay(1000);

    }

}

6. Experimental verification

After the program is compiled and burned into the development board, the LED light flashes and displays.

Reference address:4. Introduction to PIC32 series GPIO

Previous article:5. PIC32 series timer TMR-16-bit timer usage
Next article:3. PIC Harmony component installation, project creation and burning

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号