LPC 1227 - how to read value from GPIO input?

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

LPC 1227 - how to read value from GPIO input?

3,181 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ElMoe on Mon Jan 26 07:36:04 MST 2015
Hello to all,

i am very new to embedded programming and so i have a very simple question, which i wasn't able to answer by myself.
I have experiences with JAVA and fundamental knowledge with Python and C by using a RaspberryPi.

For a traineeship i now have a PREV601 blueboard (information to PREV601) witch includes among others a LPC1227, two led and two buttons.
After reading all the datasheets, UMs and so on and after trying the given samplecode i want to write some simple code by my self. At the moment i'm able to define the GPIOs as outputs and change their values, so i can controle the on board LEDs.

My question is: How can i read the value of a GPIO to use him as input?

I use this method to write values to an output GPIO:
GPIOSetValue(LED_PORT, LED_ORANGE, LED_ON);

But there seems to be no mehtod to read the current value of a GPIO.
In examples for other uC i found the following code, but it does not work with the LPC1227:
GPIOGetValue(BUTTON_PORT, BUTTON);


In this moment i want one of the on board LEDs to shine by pressing one of the on board buttons, but at the end the button shall start the reading procedure of a NFC card.

I hope someone can help me solving this problem!

Many thanks in advance!
Labels (1)
0 Kudos
5 Replies

1,553 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by fjrg76 on Fri Apr 17 15:58:20 MST 2015
Hi Nerd Herd,

I can see you work at NXP, so let me ask you again ("again" in the sense that in the past someone from NXP replied me with an answer I didn't satisfied me):

Why haven't you developed a LPC1200 API library, as you've done with all other microcontrollers, from LPC800 family to the LPC4000 one?

What is wrong with these chips? Is there any obscure reason behind the fact that you haven't put some much effort in this family? Recently a newer family have showed up: LPC11E, that seems to superseed the LPC1200, so this later family was only developed while a better one was released?

The guy from NXP told me: "Take something that is similar from other families and try to adapt it to the LPC1227 chip. We are not planning to go far from here with this family". I'm not laying:

http://www.lpcware.com/content/forum/drivers-for-lpc12xx
http://www.lpcware.com/content/forum/lpcopen-lpc12xx

At this point I don't know what is more pathetic: that NXP doesn't give any decent support, or me still being a NXP fan despite those awkward facts.
0 Kudos

1,553 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ElMoe on Mon Feb 02 05:13:19 MST 2015
Thanks nerd herd!

Your solution works without any problems.
There is just a little mistake in your example.
By reading the UM I spotted that GPIO Port 1 has the register location 0x50010004, not 0x50020004.


I implemented your code and only changed
#define GPIOP1 0x50020004 /* GPIO port 1 register location */ 
...
...
int val, temp;

/* Retrieve the entire GPIO1 port values */
temp = *(volatile int*)GPIOP1;

/* We want pin 12, so shift the register value to the right 12, making the bit we want the LSB */
temp = temp >> 12;

/* Retrieve just the LSB by zeroing out the rest of the register -- variable val should now contain the pin state for GPIO port 1, pin 12 */
val = temp & 1;

to
#define GPIOP0 0x50000004 /* GPIO port 0 register location */ 
...
...
int val, temp;

/* Retrieve the entire GPIO0 port values */
temp = *(volatile int*)GPIOP0;

/* We want pin 6, so shift the register value to the right 6, making the bit we want the LSB */
temp = temp >> 6;

/* Retrieve just the LSB by zeroing out the rest of the register -- variable val should now contain the pin state for GPIO port 0, pin 6 */
val = temp & 1;

because the on board button number 1 is soldered to GPIO Port 0, Pin 6.

Thanks for your great help!
0 Kudos

1,553 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by nerd herd on Fri Jan 30 08:59:56 MST 2015
Hi Elmoe,

After looking at the source files you provided, it looks like you are correct -- there are no functions to retrieve the current value of a GPIO pin. There is, however a register to do this which is documented in chapter 8 of the UM (more specifically, section 8.3.2):

http://www.nxp.com/documents/user_manual/UM10441.pdf

I'm not sure if you would want to develop your own API that follows the form of the APIs given to you, but the gist of what you need to do to read this register is use a volatile pointer to point to the piece of memory you would like to read from (depending on which GPIO port you're using). Then, do some bit manipulation to retrieve the pin state you are interested in. For instance, if you were using GPIO1[12] (GPIO port 1, pin 12) this is what the code would look like:

#define GPIOP1 0x50020004 /* GPIO port 1 register location */
...
...
int val, temp;

/* Retrieve the entire GPIO1 port values */
temp = *(volatile int*)GPIOP1;

/* We want pin 12, so shift the register value to the right 12, making the bit we want the LSB */
temp = temp >> 12;

/* Retrieve just the LSB by zeroing out the rest of the register -- variable val should now contain the pin state for GPIO port 1, pin 12 */
val = temp & 1;

I did not check this on a compiler and MCU, but at the very least it should give you the idea of what to do. Let me know if this does not make sense, and I'll be glad to clear up any confusion. :)
0 Kudos

1,553 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ElMoe on Tue Jan 27 23:12:43 MST 2015
Hi nerd herd,

thanks for your quick answer! I had some trouble with the spamfilter.
I wanted to share an URL to an NXP Homepage but the spamfilter wouldn't let me do that...
So here is my post from two days ago:

I use the LPCXpresso IDE and the example code PREV601 MIFARE Ultralight Project from the NXP Homepage.

I found the files gpio.h, gpio.c defining the beahviour of the GPIOs and there is no GetValue() or GetPinState() method mentioned.
So I am of the oppinin (and a bit scared...  :~ ), that no such method exists in the given API.
Even in the UM of the LPC1227 no way to get the state of a GPIO is mentioned.

For completeness, i added the three files gpio.h, gpio.c and hw_config.h, how I use them actually.
0 Kudos

1,553 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by nerd herd on Mon Jan 26 08:35:33 MST 2015
Hi ElMoe,

Welcome to embedded programming! I hope you've been enjoying your time with the LPC1227.

It should be as easy as it sounds to read the value of a GPIO input. Instead of defining a GPIO on a specific port and pin as an output, you would need to set it as an input and then just read a bit in a specific register to get the current value. As for the actual implementation, I would like to think there are APIs you can call to do this without messing with registers and volatile pointers but I am unsure as to what kind of software you were given. What I would do is find where these GPIO functions you have been using are defined and see if there any others you can use. Examples of the functions I would be looking for are: GPIOSetDirection() and GPIOGetPinState().

If there are no APIs in your software to do what you want, we can discuss alternatives :)
0 Kudos