lpc1114 GPIO problem

4 replies [最新文章]
mshrestha789
离线
Joined: 2012-01-12

I am having problem with masking the bit for reading and writing operation.
Here's what I am doing

LPC_GPIO0->DIR |= (1<<3)|(1<<5);
LPC_GPIO0->MASKED_ACCESS[(1<<3)|(1<<5)] = (1<<3)|(1<<5);

In above code, it is suppose to mask only 3rd and 5th bits of GPIO 0. Bit it masks all bits.
Another strange thing I notice that in above code, replacing
LPC_GPIO0->MASKED_ACCESS[(1<<3)|(1<<5)] = (1<<3)|(1<<5);
with
LPC_GPIO0->MASKED_ACCESS[0] = (1<<3)|(1<<5);
makes no difference. Here in place of 0, writing any number makes no difference.

Can any one help??

0
您的评定:

评论查看选项

选择你喜欢的显示评论的模式,并点选“储存设置”,以启用你所做的变更。
Wouter的头像
Wouter
离线
Joined: 2012-02-01

Hi,

This should work just fine.
What pins do you see changing? All of GPIO0 pins? Did you set these pins as output, or are the pins set as input (default) and do you maybe see a high-level on these pins because of the pull-up resistors (enabled by default)?

You could try the following:

LPC_GPIO0->DIR = 0x0FFF; //All GPIO0 pins as output
LPC_GPIO0->DATA = 0x0000; //All GPIO0 pins low
LPC_GPIO0->MASKED_ACCESS[(1<<3)|(1<<5)] = (1<<3)|(1<<5); //Now set only PIO0.3 & PIO0.5 high
LPC_GPIO0->DATA = 0x0FFF; //All GPIO0 pins high
LPC_GPIO0->MASKED_ACCESS[(1<<3)|(1<<5)] = 0; //Now clear only PIO0.3 & PIO0.5

You can check the behavior by setting two breakpoints, each after writing to "LPC_GPIO0->MASKED_ACCESS".

Please also note that PIO0.5 is an open-drain output.

Regards,
Wouter

TheoVanManen
离线
Joined: 2011-09-02

Using MASKED_ACCESS:

It toke me some time to understand how the 'Masked_Access' worked.
Why not some example C-lines added in the User Manual?

example 1.
LPC_GPIO0->MASKED_ACCESS[(1<<3)|(1<<5)] = (1<<3)|(1<<5); //Now set only PIO0.3 & PIO0.5 high

... means: address '['(1<<3)|(1<<5)']' => only bit 3 and 5 are involved in the bitoperation.
... after the '=' (1<<3)|(1<<5) means: bit 3 is set to '1' and bit 5 = set to '1'.

example 2.
LPC_GPIO0->MASKED_ACCESS[(1<<3)|(1<<5)] = (1<<3)|(0<<5); // is equal to
LPC_GPIO0->MASKED_ACCESS[(1<<3)|(1<<5)] = (1<<3);

... means: [(1<<3)|(1<<5)] => only bit 3 and 5 are involved in the bitoperation.
... after the '=' (1<<3)|(0<<5) means: bit 3 is set to '1' and bit 5 = set to '0'.

Wouter, thanks for your explanation.

alexan.e
离线
Joined: 2012-04-24

In your second example you use = (1<<3)|(0<<5);

Any value ORed with 0 keeps the value it had so what is the purpose of |(0<<5)?

Alex

PhilYoung
离线
Joined: 2011-07-18

I've been using a simple macro for this for a long time with no problems.

#define SET_GPIO(Port,Bits)(Port->MASKED_ACCESS[(Bits)] = (Bits))

#define CLR_GPIO(Port,Bits)(Port->MASKED_ACCESS[(Bits)] = (0))

this agrees with wouters code above, so if things aren't changing I suggest checking first the pin configuration and then the PCB for shorts.

technically

#define SET_GPIO(Port,Bits)(Port->MASKED_ACCESS[(Bits)] = (0xffff))

would also produce the same results, since it is a masked write, but the first may be more efficient as the compiler usually loads (Bits) into a register anyway

a more generic code would be
#define SET_GPIO(Port,Bits,Val)(Port->MASKED_ACCESS[(Bits)] = (Val))

feedback