Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is a code created on CCS for a ti MSP-432 lauchpad. Port 2(LED2) is showin

ID: 3881985 • Letter: B

Question

Below is a code created on CCS for a ti MSP-432 lauchpad. Port 2(LED2) is showing a total of 7 colors instead of the 3 colors wanted. What can I change on the code, so it only displays RED, GREEN, and BLUE one after another?

#include <stdint.h> //exact-width integer types
#include <tidevicesmsp432p4xxdriverlibdriverlib.h> //driver library

#define DCO_FREQ    48e6 //unit: Hz

void main(void)
{
    uint32_t n;
    uint16_t p2_data = 0;

    WDT_A_holdTimer(); //stop watchdog timer

    //Enable FPU for DCO Frequency calculation.
    FPU_enableModule();

    //Change VCORE to 1 to support a frequency larger than 24MHz.
    //See data sheet for Flash wait-state requirement for a given frequency.
    PCM_setCoreVoltageLevel(PCM_VCORE1);
    FlashCtl_setWaitState(FLASH_BANK0, 1);
    FlashCtl_setWaitState(FLASH_BANK1, 1);

    //Set the DCO Frequency.
    //DCO nominal frequencies: 1.5, 3, 6, 12, 24, 48 MHz.
    CS_setDCOFrequency(DCO_FREQ);

    //Init clock signals: MCLK, HSMCLK, SMCLK.
    //Can be divided by 1, 2, 4, 8, 16, 32, 64, or 128.
    CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1);
    CS_initClockSignal(CS_HSMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_8);
    CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_16);

    //Configure P1.0, P2.0, P2.1, P2.2 as output.
    //P1.0 is connected to a red LED on LaunchPad.
    //P2.0, P2.1, P2.2 are connected to a RGB tri-color LED on LaunchPad.
    GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
    GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2);

    while(1)
    {
        //simple delay loop
        for(n=0; n<1000000; n++)
        {
            //do nothing
        }

        GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0);
        GPIO_setOutputLowOnPin(GPIO_PORT_P2, (~p2_data)&0x07);
        GPIO_setOutputHighOnPin(GPIO_PORT_P2, p2_data);

        p2_data++;
        if(p2_data >= 0x08)
        {
            p2_data = 0;
        }
    } //end of while
}

Explanation / Answer

#include <stdint.h> //exact-width integer types
#include <tidevicesmsp432p4xxdriverlibdriverlib.h> //driver library
#define DCO_FREQ 48e6 //unit: Hz
void main(void)
{
uint32_t n;
uint16_t p2_data = 1; //make here initial value as 1 instead of zero
WDT_A_holdTimer(); //stop watchdog timer
//Enable FPU for DCO Frequency calculation.
FPU_enableModule();
//Change VCORE to 1 to support a frequency larger than 24MHz.
//See data sheet for Flash wait-state requirement for a given frequency.
PCM_setCoreVoltageLevel(PCM_VCORE1);
FlashCtl_setWaitState(FLASH_BANK0, 1);
FlashCtl_setWaitState(FLASH_BANK1, 1);
//Set the DCO Frequency.
//DCO nominal frequencies: 1.5, 3, 6, 12, 24, 48 MHz.
CS_setDCOFrequency(DCO_FREQ);
//Init clock signals: MCLK, HSMCLK, SMCLK.
//Can be divided by 1, 2, 4, 8, 16, 32, 64, or 128.
CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1);
CS_initClockSignal(CS_HSMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_8);
CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_16);
//Configure P2.0, P2.1, P2.2 as output.
//P2.0, P2.1, P2.2 are connected to a RGB tri-color LED on LaunchPad.
/* GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0); */ //not required
GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2); //only this is required now for RGB
while(1)
{ //simple delay loop
for(n=0; n<1000000; n++)
{
//do nothing
}
/* GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0); */ //not required now
/* GPIO_setOutputLowOnPin(GPIO_PORT_P2, (~p2_data)&0x07); */ //not required
GPIO_setOutputHighOnPin(GPIO_PORT_P2, p2_data);
p2_data *= 2;
if(p2_data >= 0x04)
{
p2_data = 1;
}
} //end of while
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote