MCB1700 Hardware
MCB1700 Hardware
For example, the five button joystick is connected to pins P1.20, P1.23, P1.24, P1.25 and P1.26. To configure these pins as GPIO normally is not required since they are by default are set to GPIO. However it is a good programming practice to configure it. Please refer page 109, Section 8.5.4, Table 82 in LPC17xx Users Manual for more detailed information. LPC_PINCON->PINSEL3 &= ~((3<< 8)|(3<<14)|(3<<16)|(3<<18)|(3<<20)); /* P1.20, P1.23..26 is GPIO (Joystick) */
2. The second step of programming GPIO pin is to set the I/O direction (i.e. Is the
pin for input or output?) through FIODIR register (page 122 of LPC17xx Uers Manual). There are five LPC_GPIOx, where x=0,1,2,3,4, macros defined in LPC17xx.h file. The FIODIR is a member variable in LPC_GPIO_TypeDef C struct in LPC17xx.h. To set a pin as input, set the corresponding bit in FIODIR to 0. All I/Os default to input (i.e. all bits in FIODIR are default to 0s). To set a pin as output, set the corresponding bit in FIODIR to 1. For example, to set pins connected to the joystick as input, we write the following C code: LPC_GPIO1->FIODIR &= ~((1<<20)|(1<<23)|(1<<24)|(1<<25)|(1<<26)); /* P1.20, P1.23..26 is input (Joystick) */ To set pins connected to the LEDs as output, we write the following C code: LPC_GPIO1->FIODIR |= 0xB0000000; // P1.28..29,P1.31 is output LPC_GPIO2->FIODIR |= 0x0000007C; // P2.2..6 is output
3. The third step of programming GPIO depends on the pin direction setting. (1) The pin is set to output. We turn a pin to HIGH (i.e. digital 1) by setting the corresponding bit in FIOSET register. We turn a pin to LOW (i.e. digital 0) by setting the corresponding bit in FIOCLR register. Both FIOSET and FIOCLR are member variables defined in the LPC_GPIO_TypeDef C struct. To set a pin to digital 1, set the corresponding bit of LPC_GPIOx->FIOSET to 1. To turn a pin to digital 0, set the corresponding bit of LPC_GPIOx->FIOCLR to 1. . See sections 9.5.2 and 9.5.3 of LPC17xx Users manual for details. For example, to turn on a particular LED, the following code is used const U8 led_pos[8] = { 28, 29, 31, 2, 3, 4, 5, 6 }; mask = 1 << led_pos[led]; if (led < 3) { // P1.28..29 and P1.31 are at Port1 LPC_GPIO1->FIOSET = mask; } else { // P2.2..6 are at Port2 LPC_GPIO2->FIOSET = mask; }
To turn off a particular LED, the following code can be used. const U8 led_pos[8] = { 28, 29, 31, 2, 3, 4, 5, 6 }; mask = 1 << led_pos[led]; if (led < 3) { // P1.28..29 and P1.31 are at Port1 LPC_GPIO1->FIOCLR = mask; } else { // P2.2..6 are at Port2 LPC_GPIO2->FIOCLR = mask; }
Note: The eight LEDs are split over two separate GPIO ports, with the first three on GPIO port 1 and the remaining five on GPIO port 2. (2) The pin is set to input. We read the current pin state from FIOPIN register. The corresponding bit being 1 indicates that the pin is driven high. The corresponding bit being 0 indicates that the pin is driven low. The FIOPIN is a member variable defined in LPC_GPIO_TypeDef C struct. One normally uses bit shift operations to shift the LPC_GPIOx->FIOPIN value to obtain pin value(s). Please refer to Section 9.5.4 in the LPC17xx Users manual for details. For example, to read the joystick position, the following code can be used #define KBD_MASK 0x79 uint32_t kbd_val; kbd_val = (LPC_GPIO1->FIOPIN >> 20) & KBD_MASK; When the joystick buttons are inactive, the bits at P1.23..26 are 1s. When one of the joystick buttons is active, the bit corresponding to that pin is 0. Note: Writing to the FIOPIN register stores the value in the port output register, bypassing the need to use both the FIOSET and FIOCLR registers to obtain the entire written value. This feature should be used carefully in an application since it affects the entire port. 4. GPIO Interrupts on Port 0 and Port2 (Not required in lab3)