Reading GPIO

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

Reading GPIO

443 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by boris on Wed Mar 21 05:35:27 MST 2012
I would like to do the polling on a particular GPIO.
meaning just the read from the GPIO not interrupt.
But  the in the user manual of lpc1343 and exampls there is only interrupt metod. 
can  i do it by enather (poling )metod.
thanks.

Boris:)
0 Kudos
11 Replies

415 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by boris on Fri Mar 23 13:55:31 MST 2012
Thanks for all.

Its very helped.
0 Kudos

415 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by stalisman on Fri Mar 23 02:57:27 MST 2012
here is some stuff to play with on the expresso IDE.

 
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]#define [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]PORT_MASK 0xFFF [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//12 bit port on LPC1114[/COLOR][/SIZE][/COLOR][/SIZE]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][B]GPIOSetMultiValue[/B]( [/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] portNum, [/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] MultiBitMask, [/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Value )[/SIZE][/LEFT]
[SIZE=2]{[/SIZE]
 [SIZE=2]      LPC_GPIO[portNum]->MASKED_ACCESS[(PORT_MASK) & MultiBitMask] = Value;[/SIZE]
[SIZE=2]}[/SIZE]
 
 
[LEFT][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t  [/COLOR][/SIZE][/COLOR][/SIZE] [SIZE=2][B]GPIOGetMultiValue[/B]( [/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] portNum, [/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] MultiBitMask )[/SIZE]
[SIZE=2]{[/SIZE][/LEFT]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])((PORT_MASK)&( LPC_GPIO[portNum]->MASKED_ACCESS[(PORT_MASK)&MultiBitMask]));[/SIZE]
[SIZE=2]}[/SIZE]
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void  [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][B]GPIOSetDaftValue[/B]( [/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] portNum, [/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] bitPosi, [/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] bitVal )[/SIZE][/LEFT]
[SIZE=2]{[/SIZE]
[SIZE=2]LPC_GPIO[portNum]->MASKED_ACCESS[(1<<bitPosi)] = (bitVal<<bitPosi)+7 ; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//7 is ignored![/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]}[/SIZE]
 
 
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]_Bool [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][B]getbit[/B]([/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] portNum, [/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] bit)            //@gbm[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][B][COLOR=#7f0055][COLOR=#7f0055]return[/COLOR][/COLOR][/B] !! LPC_GPIO[portNum]->MASKED_ACCESS[1 << bit];[/SIZE]
[LEFT][SIZE=2]}[/SIZE][/LEFT]
 




I then insetted the following lines in 'main' and monitered the (uint32_t) bitVal as I single stepped through ...

[LEFT][SIZE=2]GPIOSetValue(2,6,0);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]bitVal = ([/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])getbit(2,6);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]GPIOSetValue(2,6,1);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]bitVal = ([/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])getbit(2,6);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]GPIOSetDaftValue(2,6,0);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]bitVal = ([/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])getbit(2,6);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]GPIOSetDaftValue(2,6,1);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]bitVal = ([/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])getbit(2,6);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]bitVal = ([/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])getbit(2,2);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]GPIOSetValue(2,6,0);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]bitVal = ([/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])getbit(2,6);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]GPIOSetDaftValue(2,6,1);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]&#12288;[/SIZE]
[SIZE=2]bitVal = ([/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])getbit(2,2);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]bitVal = ([/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])getbit(2,6);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]&#12288;[/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]/*[/COLOR][/SIZE]
[SIZE=2][COLOR=#3f7f5f]void GPIOSetMultiValue( uint32_t portNum, uint32_t bitMask, uint32_t Value );[/COLOR][/SIZE]
[SIZE=2][COLOR=#3f7f5f]uint32_t GPIOGetMultiValue( uint32_t portNum, uint32_t bitMask );[/COLOR][/SIZE]
[SIZE=2][COLOR=#3f7f5f]*/[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]GPIOSetMultiValue( 2, 0xA4, 0x1A4 );[/SIZE][/LEFT]
 
[LEFT][SIZE=2]bitVal = GPIOGetMultiValue( 2, 0x84 );[/SIZE][/LEFT]
 
[LEFT][SIZE=2]bitVal = ([/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])getbit( 2, 5); [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//note:0x20 is the mask not the [U]bitno[/U] which is 5[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2]bitVal = GPIOGetMultiValue( 2, 0x04 );[/SIZE]
[SIZE=2]bitVal = GPIOGetMultiValue( 2, 0xA0 );[/SIZE]
[SIZE=2]bitVal = GPIOGetMultiValue( 2, 0xA4 );[/SIZE][/LEFT]
 


[LEFT][SIZE=2]most iluminating.[/SIZE][/LEFT]

[LEFT][SIZE=2]Stali[/SIZE][/LEFT]
0 Kudos

415 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Thu Mar 22 06:57:20 MST 2012
If you search the forum (eg for GPIOReadValue), then you'll find the following post...

http://knowledgebase.nxp.com/showpost.php?p=1276&postcount=2

Regards,
CodeRedSupport
0 Kudos

415 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by gbm on Thu Mar 22 03:38:25 MST 2012
Of course not - should be "port". Also, the exact syntax of GPIO masked access depends on the declarations of GPIO stuff in header files. The above code was just an idea.
0 Kudos

415 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by serge on Thu Mar 22 03:04:39 MST 2012

Quote:

Code:
inline static _Bool getbit(uint32_t port, uint32_t bit)
{
[SIZE=2]    return !! LPC_GPIO[port[COLOR=red]Num[/COLOR]]->MASKED_ACCESS[1 << bit];[/SIZE]
}



Is the Num part of portNum there correct?:confused:
0 Kudos

415 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by stalisman on Thu Mar 22 02:55:52 MST 2012
elegant ...

.. but is it proof against someone inadvertently writing a value other than (1<<bit) at some earlier point?

I looked in http://infocenter.arm.com/help/topic/com.arm.doc.ddi0479b/DDI0479B_cortex_m_system_design_kit_r0p0_t... but could not find a definitive answer.

edit: brilliant idea ..why not code it up as see duhhh!

so I did and it's fine. :-)

( um with Serge's edit too)
0 Kudos

415 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by gbm on Wed Mar 21 13:08:37 MST 2012
Actually if you must make a function:

inline static _Bool getbit(uint32_t port, uint32_t bit)
{
[SIZE=2]    return !! LPC_GPIO[portNum]->MASKED_ACCESS[1 << bit];[/SIZE]
}
0 Kudos

415 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by boris on Wed Mar 21 09:03:38 MST 2012
[SIZE=2][LEFT]uint8_t [B]GPIOGetValue[/B]( uint32_t portNum, uint32_t bitPosi )
{
uint8_t mybitstate;
mybitstate = ( (LPC_GPIO[portNum]->DATA & (1<<bitPosi)) >> bitPosi);
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] mybitstate;[/LEFT]
}:)
[/SIZE]
0 Kudos

415 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by boris on Wed Mar 21 07:42:57 MST 2012
Exactly!!!:):)

Thank!

what about :

unsigned char GPIOGetValue(unit32_t portNum, uint_32t bitPosi) ;

[SIZE=2][SIZE=3]Can Help me?[/SIZE]
[/SIZE]
0 Kudos

415 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by gbm on Wed Mar 21 07:07:28 MST 2012
Do you mean this highly sophisticated and top secret statement?

mypinstate = LPC_GPIO0->DATA;
0 Kudos

415 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by boris on Wed Mar 21 06:04:57 MST 2012
Can somebody help with example code,but not interrut.
0 Kudos