I am struggling to initialize the LEDs and pushbuttons on my MSP432p401R LaunchPad.
The problem:
Write a code that reads input of the two pushbuttons S1 & S2 (P1.1 & P1.4) and changes the state of the LED correspondingly: none - off S1 - RED S2 - GREEN both - BLUE
here's some code:
#define RED BIT0 // Red LED connected to Port 1.0
#define GREEN BIT1
#define BLUE BIT2
#define S1 BIT1
#define S2 BIT4
#define SBOTH (BIT1 | BIT4)
#define PB1_PRESSED() ((P1->IN & S1) == 0)
#define PB2_PRESSED() ((P1->IN & S2) == 0)
#define BOTH_PRESSED() ((P1->IN & SBOTH) == 0)
P2->OUT |= OFF; // turn off LEDs
P2->DIR |= (RED | GREEN | BLUE); // set LEDs for output
P2->REN |= (RED | GREEN | BLUE); // turn on resistors
My if statements look like this:
if (BOTH_PRESSED())
{
P2->REN |= BLUE;
P2->OUT |= BLUE;
}
what am I doing wrong in the initialization?
I´m looking through documentation and will point some details that you might be missing. Could you provide what is the current behavior of the LED? Is only one color being activated (red, green, blue) lightened, no color is activated or a mixture between the RGB components is activated?
1. You need to enable pull up resistors for both P1.1 and P1.4
Going to schematics of the launchpad and looking for the button connections to the microcontroller we can see in Figure 29 Schematics (2 of 6) that the buttons connected to P1.1 and P1.4 are both grounded:
Therefore you should enable the pull up resistor at both I/Os as per Table 12.1 of MSP432P4xx SimpleLink™ Microcontrollers Technical Reference Manual section 12.2.4:
The consequence of this topology wherein the push button is grounded and there is a pull up resistor is negative logic: If the button is pressed, the input is being grounded, thus the MCU reads a 0; If the button is not pressed, the input is getting signal from VCC via pull resistor, thus the MCU reads a 1; In summary: pressed button = 0, open button = 1.
2. You don´t need to enable resistors for the LED outputs
Going back to schematics of the launchpad at Figure 29, in there we can see that the LEDs connect to P2.0, P2.1 and P2.2 already have a resistor in the anodes and I don´t see the need of having pull up or pull down resistors. You should be just fine without the resistors:
3. You might need to enable the high drive strength at output ports
Since the LEDs are arranged in a common cathode manner, the MSP432 I/O pins will need to drive the current for lightening it (as opposed of what would happen in a common anode configuration). Therefore, you will need to activate the high strength driver for pins P2.0, P2.1 and P2.2. Going again back to MSP432P4xx SimpleLink™ Microcontrollers Technical Reference Manual at section 12.2.5 Output Drive Strength Selection Registers (PxDS), we can see that we need to set BIT0, BIT1 and BIT2 in the P2DS register in order to allow them to use the high strength IO driver