• Home
  • Blog
  • Add Logic to modify the functionality as follows

Add Logic to modify the functionality as follows

0 comments

//

//

// Lab 1: Startup code

// 1 – Initialize Core

// 2 – Initialize LEDs

// 3 – Initialize buttons

// 4 – logic to control the LEDs based on buttons:

// If Right_button is pressed, activate the RED LED

// If Left_button is pressed, activate the BLUE LED

// If Both_buttons are pressed, activate the GREEN LED

// Assignment:

// 1-Add Logic to modify the functionality as follows:

// 2-Press the button once, then the corresponding LED functionality will be in effect, i.e. press the right button one time, then the Green LED will start flashing with incremental delay,

// 3-Press the right button once the RED will start flashing in decremental delay

// 4-Press both Buttons, the BLUE LED latches

#include <stdint.h>

#include <stdbool.h>

#include “driverlib/sysctl.h”

#include “driverlib/gpio.h”

#include “inc/hw_gpio.h”

#include “inc/hw_memmap.h” // for GPIO Port Base

#include “inc/hw_types.h” // needed for HWREG for locking HW

#define TRUE 1

#define FALSE 0

// Hardware Abstraction

// LEDs Pin HW Positions

#define RED_LED_IO GPIO_PIN_1 // PF1

#define GREEN_LED_IO GPIO_PIN_3 // PF3

#define BLUE_LED_IO GPIO_PIN_2 // PF2

#define RED_LED 0x02 // PF1

#define GREEN_LED 0x08 // PF3

#define BLUE_LED 0x04 // PF2

// Other LED colors derived from RGB:

// Color LED(s) PortF

// red R– 0x02

// green -G- 0x08

// blue –B 0x04

// yellow RG- 0x0A

// sky blue -GB 0x0C

// white RGB 0x0E

// pink R-B 0x06

#define YELLOW_LED (RED_LED | GREEN_LED)

#define SKY_BLUE_LED (GREEN_LED | BLUE_LED)

#define WHITE_LED (RED_LED | GREEN_LED | BLUE_LED)

#define PINK_LED (RED_LED | BLUE_LED)

#define LED_OFF FALSE

// Buttons HW Position

#define LEFT_BUTTON GPIO_PIN_0 // PF0

#define RIGHT_BUTTON GPIO_PIN_4 // PF4

#define BUTTON_OFF FALSE

// Button states:

uint8_t ButtonState_L = BUTTON_OFF;

uint8_t ButtonState_R = BUTTON_OFF;

uint8_t ButtonState_RL = BUTTON_OFF;

uint8_t ButtonStates = BUTTON_OFF;

// LED commands:

uint8_t LED_Command_R = LED_OFF;

uint8_t LED_Command_G = LED_OFF;

uint8_t LED_Command_B = LED_OFF;

// Program variables

uint32_t ui32Loop;

uint32_t MaxDelay = 0; //16MZ, 62ns/instruction, 12 instructions 1M times = 744ms, 500000

/*

* Assignment hints:

* Create an enumerated structure type

*/

typedef enum {

ALL_LEDS_Off = 0, /* When no button is pressed */

RED_LED_Control = 1, /* Controlled by the RIGHT Button */

GREEN_LED_Control = 2, /* Controlled by the LEFT Button */

BLUE_LED_Control = 3 /* Controlled by the RL (both) Buttons */

} LED_Control_States_t;

// Create an LED control state based on the above type

LED_Control_States_t LED_Control_State = ALL_LEDS_Off;

/*

* Assignment hints:

* Alternatively, also we can discrete states as follows:

*

*/

#define ALL_LEDS_OFF 0

#define RED_LED_CONTROL 1

#define GREEN_LED_CONTROL 2

#define BLUE_LED_CONTROL 3

int main(void)

{

// This function enables a peripheral. At power-up, all peripherals are

// disabled; they must be enabled in order to operate or respond to register

// reads/writes.

//

// 1- Enable the GPIO port that is used for the on-board LED and for the Switch

// LEDs & the Switch are both connected to PORTF:

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

// 2- SET THE LEDS AS OUTPUTS:

// Enable the GPIO pin for the LED (PF1, PF2 and PF3)

GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, RED_LED_IO|BLUE_LED_IO|GREEN_LED_IO);

//

// 3-Set each of the button GPIO pins as an input with a pull-up.

//

GPIODirModeSet(GPIO_PORTF_BASE, (LEFT_BUTTON | RIGHT_BUTTON), GPIO_DIR_MODE_IN);

GPIOPadConfigSet(GPIO_PORTF_BASE, (LEFT_BUTTON | RIGHT_BUTTON), GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

// 4- Loop forever:

while(1)

{

// 5- Read button states:

ButtonStates = GPIOPinRead(GPIO_PORTF_BASE, (LEFT_BUTTON | RIGHT_BUTTON));

// Check if RIGHT button, LEFT button, or BOTH(RL) buttons are pressed:

ButtonState_R = ((ButtonStates == RIGHT_BUTTON) ? TRUE : FALSE);

ButtonState_L = ((ButtonStates == LEFT_BUTTON) ? TRUE : FALSE);

ButtonState_RL = ((ButtonStates == 0x00) ? TRUE : FALSE);

// 6- control the LED flashing delay based on the pressed button

// MaxDelay is used as the ‘for’ loop terminator

if(ButtonState_R)

{

//slow the delay,

MaxDelay+= 30000;

}

else if (ButtonState_L)

{

if(MaxDelay > 30000) //clip to the min delay limit

{

MaxDelay-= 30000;

}

}

else if (ButtonState_RL)

{

MaxDelay = 0;

}

else

{

MaxDelay = 500000;

}

// 7-Based on which button is pressed, set individual LEDs command:

LED_Command_R = ((ButtonState_R) ? RED_LED : LED_OFF);

LED_Command_G = ((ButtonState_L) ? GREEN_LED : LED_OFF);

LED_Command_B = ((ButtonState_RL) ? BLUE_LED : LED_OFF);

// Assignment hint:

// Based on which button is pressed, set individual LEDs command:

/*

LED_Command_R = ((LED_Control_State == RED_LED_Control) ? RED_LED : LED_OFF);

LED_Command_G = ((LED_Control_State == GREEN_LED_Control) ? GREEN_LED : LED_OFF);

LED_Command_B = ((LED_Control_State == BLUE_LED_Control) ? BLUE_LED : LED_OFF);

*/

// 8-Turn ON the corresponding LED

// Write to LED HW Port: 0x0E = ALL LEDS Mask

GPIOPinWrite(GPIO_PORTF_BASE, 0x0E, (LED_Command_R | LED_Command_G | LED_Command_B));

// 9- Create a delay based on the value of the loop counter:

for(ui32Loop = 0; ui32Loop < MaxDelay; ui32Loop++) {;} // spinloop wait delay

// 10- Turn OFF all the LEDs after the spin delay

GPIOPinWrite(GPIO_PORTF_BASE, 0x0E, !(LED_Command_R | LED_Command_G | LED_Command_B));

for(ui32Loop = 0; ui32Loop < MaxDelay; ui32Loop++) {;} // spinloop wait delay

// Note: Of course Spin loop delay IS NOT the preferred way to create delays, it is only used to demonstrate the LED functionality

// Timers will be used instead (Hardware and/or Software timers are used to manage time-based delays

}

}

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}