LPC1768 Basic Input Output GPIO usage

Source: Internet
Author: User

The control of the LPC1788 universal IO port contains some basic components, such as setting push-pull output, open-drain output, pull-up resistors, and so on, which we'll look at today.

First use Gpio to open Gpio's system clock

Lpc_sc->pconp |= (1<<15);//gpio clock

Then we need to select the function of the pin we selected, some pins have several functions, through the register can choose a

The external resistor state of the relevant pin is then set Pinmode Register

Then set the open-drain mode pinmode_od

Here, the basic function of the PIN is OK, at this time to operate Gpio also need to set up a few things

    1. Input/output Direction Fiodir

    1. After the direction is set, you can enter the output, note that the Gpio input and output is done through three registers, the output setting register function is to set the IO port level to high

Output clear register with output for low level

Get output status using port value register

In addition, we can use the masking register when we want to disable certain ports

  

We can see that the register access is 32 bit not very convenient, fortunately lpc1768 is support bit segment operation, we can define the bit segment of register to look like this

Bit-band operation for 51 similar gpio control functions

Concrete realization Idea, refer to <<cm3 authoritative guide >> Fifth chapter (87 page ~92 page).

IO port Operation macro definition

#define Bitband (addr, Bitnum) ((Addr & 0xf0000000) +0x2000000+ ((addr &0xfffff) <<5) + (BITNUM<<2))

#define MEM_ADDR (ADDR) * ((volatile unsigned long *) (ADDR))

#define BIT_ADDR (ADDR, Bitnum) mem_addr (Bitband (ADDR, Bitnum))

IO Port Address Mapping

Output registers

#define GPIO0_ODR_ADDR (lpc_gpio0_base+0x18)//0x2009c018

#define GPIO1_ODR_ADDR (lpc_gpio1_base+0x18)//0x2009c038

#define GPIO2_ODR_ADDR (lpc_gpio2_base+0x18)//0x2009c058

#define GPIO3_ODR_ADDR (lpc_gpio3_base+0x18)//0x2009c078

#define GPIO4_ODR_ADDR (lpc_gpio4_base+0x18)//0x2009c098

Input Registers

#define GPIO0_IDR_ADDR (lpc_gpio0_base+0x14)//0x2009c014

#define GPIO1_IDR_ADDR (lpc_gpio1_base+0x14)//0x2009c034

#define GPIO2_IDR_ADDR (lpc_gpio2_base+0x14)//0x2009c054

#define GPIO3_IDR_ADDR (lpc_gpio3_base+0x14)//0x2009c074

#define GPIO4_IDR_ADDR (lpc_gpio4_base+0x14)//0x2009c094

Direction Register

#define GPIO0_DIR_ADDR (lpc_gpio0_base+0x00)//0x2009c000

#define GPIO1_DIR_ADDR (lpc_gpio1_base+0x00)//0x2009c020

#define GPIO2_DIR_ADDR (lpc_gpio2_base+0x00)//0x2009c040

#define GPIO3_DIR_ADDR (lpc_gpio3_base+0x00)//0x2009c060

#define GPIO4_DIR_ADDR (lpc_gpio4_base+0x00)//0x2009c080

Clear 0 Register

#define GPIO0_CLS_ADDR (lpc_gpio0_base+0x1c)//0x2009c01c

#define GPIO1_CLS_ADDR (lpc_gpio1_base+0x1c)//0x2009c03c

#define GPIO2_CLS_ADDR (lpc_gpio2_base+0x1c)//0x2009c05c

#define GPIO3_CLS_ADDR (lpc_gpio3_base+0x1c)//0x2009c07c

#define GPIO4_CLS_ADDR (lpc_gpio4_base+0x1c)//0x2009c09c

IO port operation, only for single IO port!

Make sure the value of n is less than 32!

#define P0HIGH (N) bit_addr (gpio0_odr_addr,n)//Output 0 output unchanged 1 output is 1

#define P0LOW (N) bit_addr (gpio0_cls_addr,n)//Clear 0 Output constant 1 output 0

#define P0IN (N) bit_addr (gpio0_idr_addr,n)//input

#define P0DIR (N) bit_addr (gpio0_dir_addr,n)//direction 0 input 1 output

#define P1HIGH (N) bit_addr (gpio1_odr_addr,n)//Output 0 output unchanged 1 output is 1

#define P1LOW (N) bit_addr (gpio1_cls_addr,n)//Clear 0 Output constant 1 output 0

#define P1IN (N) bit_addr (gpio1_idr_addr,n)//input

#define P1DIR (N) bit_addr (gpio1_dir_addr,n)//direction 0 input 1 output

#define P2HIGH (N) bit_addr (gpio2_odr_addr,n)//Output 0 output unchanged 1 output is 1

#define P2LOW (N) bit_addr (gpio2_cls_addr,n)//Clear 0 Output constant 1 output 0

#define P2IN (N) bit_addr (gpio2_idr_addr,n)//input

#define P2DIR (N) bit_addr (gpio2_dir_addr,n)//direction 0 input 1 output

#define P3HIGH (N) bit_addr (gpio3_odr_addr,n)//Output 0 output unchanged 1 output is 1

#define P3LOW (N) bit_addr (gpio3_cls_addr,n)//Clear 0 Output constant 1 output 0

#define P3IN (N) bit_addr (gpio3_idr_addr,n)//input

#define P3DIR (N) bit_addr (gpio3_dir_addr,n)//direction 0 input 1 output

#define P4HIGH (N) bit_addr (gpio4_odr_addr,n)//Output 0 output unchanged 1 output is 1

#define P4LOW (N) bit_addr (gpio4_cls_addr,n)//Clear 0 Output constant 1 output 0

#define P4IN (N) bit_addr (gpio4_idr_addr,n)//input

#define P4DIR (N) bit_addr (gpio4_dir_addr,n)//Direction 0 output

This makes it easy to control the input and output direction and setting value of the IO port and get the value (otherwise, it is too annoying to be used in some IO ports mimicking IIC).

to a related code.

LED for p2.0

void Ledinit(void)

{

LPC_SC, Pconp |= (1<<); Turn on the Gpio clock

Lpc_pincon-PINSEL4 &= ~ (0x03l<<0); Gpio Features

Lpc_pincon-PINMODE4 &= ~ (0x03l<<0); Pull-up resistor

Lpc_pincon-pinmode_od2 &= ~ (0x01<<0); Push-Pull Mode

P2dir(0) = 1; Output

P2high(0) = 1; Initialize high level, light off

}

void Ledset(U8 set)

{

if (set) P2high(0) = 1;

Else P2low(0) = 1;

}

LPC1768 Basic Input Output GPIO usage

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.