Most of the function oriented pins on lpc214x Microcontrollers are
grouped into ports. lpc2148 has 2 ports viz. Port 0 and Port 1.
- Port 0 is a 32 bit wide I/O port (i.e it can be used for max 32 pins where each pin refers to a corresponding bit) and has dedicated direction bits for each of the pins present in the port. 28 out of the 32 pins can be used as bi-directional I/O (digital) pins. Pins P0.24 , P0.26 & P0.27 are unavailable for use and Pin P0.30 can be used as output pin only.
- Port 1 is also a 32 bit wide I/0 port but Pins 0 to 15 i.e P1.0 – P1.15 are unavailable for use and this port too has a dedicated direction bit for each of the usable pins.
Note #1: The naming convention for Pins on MCU is ‘Px.yz’ where ‘x’ is the port number , 0 or 1 in our case since we have only 2 ports to play with in lpc214x , and ‘yz’ is simply the pin number in port ‘x’. For example : P0.2 refers to Pin number 2 of Port 0 , P1.13 refers to Pin number 13 in Port 1.
In lpc214x MCUs most of the PINS are Multiplexed i.e. these pins can be configured to provide different functions.
I’ll explain this in upcoming tutorial. For now Just keep in mind that
by default : all functional pins i.e pins in port 0 & 1 are set as
GPIO so we can directly use them when learning GPIO usage.
Note #2: The functions of the Pins in Port 0 & 1 can be selected by manipulating appropriate bits in PINSEL0/1/2
registers. Explaining this is outside the scope of this article and
will be dealt in detail in another article. Just remember that assigning
’0′ to these registers forces the corresponding pins to be used as
GPIO. Since by default all pins are configured as GPIOs we don't need to
explicitly assign zero value to PINSELx registers in our programming examples.
Now , lets go through the registers used for GPIO programming.
1. IOxPIN (x=port number) :
This register can be used to Read or Write values directly to the pins.
Regardless of the direction set for the particular pins it gives the
current start of the GPIO pin when read.
2. IOxDIR : This
is the GPIO direction control register. Setting a bit to 0 in this
register will configure the corresponding pin to be used as an Input
while setting it to 1 will configure it as Output.
3. IOxSET : This register can be used to drive an ‘output’ configured pin to Logic 1 i.e HIGH. Writing Zero does NOT have any effect and hence it cannot be used to drive a pin to Logic 0 i.e LOW. For driving pins LOW IOxCLR is used which is explained below.
4. IOxCLR : This register can be used to drive an ‘output’ configured pin to Logic 0 i.e LOW. Writing Zero does NOT have any effect and hence it cannot be used to drive a pin to Logic 1.
Note #3: Naming convention for GPIO related registers – Replace ‘x’ with the port number to get the register name. For e.g IOxPIN becomes IO0PIN when used for Port 0 and IO1PIN when used to port 1. These are defined in ‘lpc214x.h’ header file for KIEL IDE.3. IOxSET : This register can be used to drive an ‘output’ configured pin to Logic 1 i.e HIGH. Writing Zero does NOT have any effect and hence it cannot be used to drive a pin to Logic 0 i.e LOW. For driving pins LOW IOxCLR is used which is explained below.
4. IOxCLR : This register can be used to drive an ‘output’ configured pin to Logic 0 i.e LOW. Writing Zero does NOT have any effect and hence it cannot be used to drive a pin to Logic 1.
Registers Names defined in ‘lpc214x.h’ header file are basically pointers which point to actual register in Hardware. Since lpc214x MCUs are 32 bit , the size of the pointer is also 32 bits. Each bit in these registers mentioned above is directly linked to a corresponding Pin. Manipulating these bits changes the behavior or state of the pins. For e.g consider IOxDIR register. Bit 0 of IO0DIR corresponds to pin 0 of port 0 hence bit ‘y’ in IOxDIR corresponds to pin ‘y’ in port ‘x’.
Now setting PIN 2 of Port 0 i.e P0.2 as output can be done in various ways as show :
CASE 1. IO0DIR = (1<<2); //(binary – direct assign: other pins set to 0)
CASE 2. IO0DIR |= 0×0000004; // or 0×4; (hexadecimal – OR and assign: other pins not affected)
CASE 3. IO0DIR |= (1<<2); //(binary – OR and assign: other pins not affected)
First thing is to note that preceding Zeros in Hexadecimal Notation can be ignored since bcoz have no meaning since we are working with unsigned values here (positive only) which are assigned to Registers. For eg. ’0×32′ and ’0×032′ and ’0×0032′ all mean the same.
Note #4: Note that bit 31 is MSB on extreme left and bit 0 is LSB on extreme right i.e Big Endian Format. Hence bit 0 is the 1st bit from right , bit 1 is the 2nd bit from right and so on.Also
note that the BIT and PIN Numbers are Zero(0) indexed which is quite
evident since Bit ‘x’ refers to (x-1)th location in the corresponding
register.
Case 1 must be avoided since we are directly assigning a value to the register. So while we are making P0.2 ’1′ others are forced to be assigned a ’0′ which can be avoided by ORing and then assigning Value.
Case 2 can be used when bits need to be changed in bulk and
Case 3 when some or single bit needs to be changed.
Also Note: All GPIO pins are configured as Input after Reset by default!
IO0DIR |= (1<<19); // Config P0.19 as Output
Case 1 must be avoided since we are directly assigning a value to the register. So while we are making P0.2 ’1′ others are forced to be assigned a ’0′ which can be avoided by ORing and then assigning Value.
Case 2 can be used when bits need to be changed in bulk and
Case 3 when some or single bit needs to be changed.
Also Note: All GPIO pins are configured as Input after Reset by default!
Example #1)
Consider that we want to configure Pin 19 of Port 0 i.e P0.19 as Ouput and want to drive it High(Logic 1). This can be done as :IO0DIR |= (1<<19); // Config P0.19 as Output
IO0SET |= (1<<19); // Make output High for P0.19
Example #2)
Making output configured Pin 15 High of Port 0 i.e P0.15 and then Low can be does as follows:
IO0DIR |= (1<<15); // P0.15 is Output pin
IO0SET |= (1<<15); // Output for P0.15 becomes High
IO0CLR |= (1<<15); // Output for P0.15 becomes Low
IO0SET |= (1<<15); // Output for P0.15 becomes High
IO0CLR |= (1<<15); // Output for P0.15 becomes Low
Example #3)
Configuring P0.13 and P0.19 as Ouput and Setting them High:
IO0DIR |= (1<<13) | (1<<19); // Config P0.13 and P0.19 as Output
IO0SET |= (1<<13) | (1<<19); // Make output High for P0.13 and P0.19
IO0SET |= (1<<13) | (1<<19); // Make output High for P0.13 and P0.19
Example #4)
Configuring 1st 16 Pins of Port 0 (P0.0 to P0.15) as Ouput and Setting them High:
IO0DIR |= 0x0000FFFF; // Config P0.0 to P0.15 as Output
IO0SET |= 0x0000FFFF; // Make output High for P0.0 to P0.15
IO0SET |= 0x0000FFFF; // Make output High for P0.0 to P0.15
No comments:
Post a Comment