Accessing GPIOin LPC812

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Accessing GPIOin LPC812

735 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Karthik Venkatesh on Wed Mar 02 00:31:57 MST 2016
Hi,

Following is a simple GPIO access code which I am trying, to learn 812 chip programming.

/*
===============================================================================
 Name        : lcd_display.c
 Author      : $(author)
 Version     :
 Copyright   : $(copyright)
 Description : main definition
===============================================================================
*/


#include "LPC8xx.h"

#define SYSAHBCLKCTRL(*(unsigned long *)0x40048080)
#define DIR0(*(unsigned long *)0xA0002000)
#define PIN0(*(unsigned long *)0xA0002100)
#define SET0(*(unsigned long *)0xA0002200)

int main(void) {
/* Enable GPIO peripheral clock */
SYSAHBCLKCTRL=(1<<6);
DIR0=0xffffffff;
PIN0=0xa5a5a5a5;

    while(1) {

    }
    return 0 ;
}


My Setup:
    IDE -> LPCxpresso
    Flash Magic



If you see the code,
SYSAHBCLKCTRL=(1<<6);  -> This enables the clock for GPIO peripheral
DIR0 = 0xffffffff;  -> This configures all the GPIOs as output
PIN0 = 0xa5a5a5a5; -> Making few pins HIGH and few pins LOW.

I am not getting the expected output. I observed all the GPIOs are HIGH (3.3V) instead.

Note:
To start with, I dont want to make use of the CMSIS or redlib or lpcopen libraries. That is why I myself mapped variables to corresponding 32 bit register addresses using #define.

Please let me know what am doing wrong.

Thanks and regards,
Karthik
Labels (1)
0 Kudos
6 Replies

471 Views
lpcware
NXP Employee
NXP Employee
bump
0 Kudos

471 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by starblue on Wed Mar 02 02:45:19 MST 2016

Quote: Karthik Venkatesh

/* Enable GPIO peripheral clock */
SYSAHBCLKCTRL=(1<<6);


You are disabling the clock for everything else except GPIO (and SYS, which can't be disabled).

So  better make that
/* Enable GPIO peripheral clock */
SYSAHBCLKCTRL|=(1<<6);


Or remove it, as GPIO should be enabled on reset.
0 Kudos

471 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by starblue on Tue Mar 08 05:42:07 MST 2016
You are right, normally you wouldn't need them, as you can use SET and CLR to set the value of any single pin, and PIN to read the value.
Maybe it could be useful if you want to use a pointer to choose between pins, but that would be very rare.
0 Kudos

471 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Karthik Venkatesh on Tue Mar 08 05:18:20 MST 2016
Dear starblue,

Thanks for your suggestions. yes it works now after replacing with SYSAHBCLKCTRL|=(1<<6);

Now I am able to set/Reset the GPIOs of LPC812 using the following code:

/*
===============================================================================
 Name        : gpio.c
 Author      : Karthik
 Description : This program does the following:
     1. Configures all the GPIO pins as output
     2. Makes alternate pins HIGH
      The objective of this code is to show how to control the GPIOs
      in LPC812.
 Library : No Libraries are used.
 Test setup : LPC812 Cerebro EVK
===============================================================================
*/

/*
 * Registers to be accessed:
 * SYSAHBCLKCTRL->System Clock Control Register->Address 0x40048080
 * DIR0->GPIO Direction control register->Address 0xA0002000
 * SET0->GPIO pin setting register->  Address 0xA0002200
 * PINENABLE0->Switch Matrix Register to ->Address 0x4000C1C0
 * Disable SWD function on PIO0_2
 * & 3 pins
*/

//Maps the Register at address 0x40048080 (which is SYSAHBCLKCTRL) to the variable SYSAHBCLKCTRL;
//SYSAHBCLKCTRL is defined as the pointer variable of type "volatile unsigned long" which points
//to the address 0x40048080;
//(*(volatile unsigned long *)0x40048080) points to the content of register at address 0x40048080;
//This applies to all other register mapping in #define;
#define SYSAHBCLKCTRL(*(volatile unsigned long *)0x40048080)
#define DIR0(*(volatile unsigned long *)0xA0002000)
#define SET0(*(volatile unsigned long *)0xA0002200)
#define PINENABLE0(*(volatile unsigned long *)0x4000C1C0)

int main(void) {
/*
 * The SYSAHBCLKCTRL register enables the clocks to individual system and peripheral blocks.
 * Making the bit 6 as '1' enables the clock to GPIO block.
 * By default, all the pins except PIO0_2 & 3, are GPIO pins upon power_on.
 * In order to access the GPIOs, we need to enable the clock for GPIO block.
 * Upon reset, GPIO clock is enabled. But to be on safer side we are enabling
 * the GPIO clock in SYSAHBCLKCTRL register as below.
 */
SYSAHBCLKCTRL|=(1<<6);

/*
 * DIR0 register is used to set the direction of the GPIO pins.
 * 1 makes it output; 0 makes it input.
 */
DIR0|=0xffffffff;

/*
 * Here PINENABLE0 register is accessed in order to disable SWD function in
 * PIO0_2 and 3 pins. Only then we can access them as any other GPIO.
 */
PINENABLE0|=(1<<2);
PINENABLE0|=(1<<3);

/*
 * SET0 register is used to make the GPIO pins HIGH or LOW.
 */
SET0= 0xaaaaaaaa;

//Continuous loop to keep the program running infinitely.
    while(1);

    return 0;
}


But one thing is not clear to me, which is
   "What is the purpose of having Port Byte pin Registers B0,B1....B17 ?"

The datasheet says, we can control multiple pins using half-word or word.

But we can do the same using SET0 register, as I have done in the above code.

So what is the real reason of having B0...B17 Registers ? I am confused on this.


Thanks and regards,
Karthik

0 Kudos

471 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Karthik Venkatesh on Wed Mar 02 01:50:41 MST 2016
Hi wmues,

Thanks for your suggestion.

I tried but in vain.

#include "LPC8xx.h"

#define SYSAHBCLKCTRL(*(volatile unsigned long *)0x40048080)
#define DIR0(*(volatile unsigned long *)0xA0002000)
#define PIN0(*(volatile unsigned long *)0xA0002100)
#define SET0(*(volatile unsigned long *)0xA0002200)

int main(void) {
/* Enable GPIO peripheral clock */
SYSAHBCLKCTRL=(1<<6);
DIR0=0xffffffff;
PIN0=0xa5a5a5a5;

    while(1) {

    }
    return 0 ;
}
0 Kudos

471 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by wmues on Wed Mar 02 01:31:22 MST 2016
The register variables have to be declared "volatile".
0 Kudos