I just started working with the NXP LPC1768 in class and I'm struggling to understand how the internal pullup/down resistors are utilized. We have a program that changes the status(on/off) of one of the LEDs at the push of a button. My question is why does the following line of code cause the LED to light up:
LPC_PINCON->PINMODE3 = LPC_PINCON->PINMODE3 & 0xFFFFFFCF; LPC_GPIO1->FIOSET |= (1<<18);
When the program starts, the LED is on. However, since this pin is set to pull up, I expected it to be off. If setting the 18th bit to 1 is equivalent to closing the switch, then that means that this line:
LPC_GPIO1->FIOSET |= (1<<18);
would cause the current to flow to ground, turning the LED off. I must be misunderstanding something, since the LED is obviously on with the current code. Any insights?
This is how you are expected to use the registers:
LPC_GPIO1->FIOSET = (1<<18);
This is how you waste CPU time and code size by using register explicitly designed for write-only use as if they needed read-modify-write code:
LPC_GPIO1->FIOSET = LPC_GPIO1->FIOSET | (1<<18);
which can be shortened to (but still representes a not needed read-modify-write):
So yes - you should modify all your use of FIxSET and FIxCLR to just assign bits - never any read/modify/write. That's the whole purpose of the processor getting dedicated registers for set and clear. Else the processor could have managed with the FIxPIN register. The FIxPIN register do require the use of "reg |= bit" and "reg &= ~bit", while the FIxSET and FIxCLR only requires "reg = bit".