Tiva Lab 03: Police LED Light Bar Simulation
Objective
- Develop and code an electronic setup using LEDs and a microcontroller to imitate the police LED light bar.
Components Required
Component/Device | Description | Quantity |
---|---|---|
220 Ω (red red brn gld / red red blk blk brn) | × 6 | |
![]() |
Red LED | × 3 |
![]() |
Blue LED | × 3 |
![]() |
Push Button (Switch) | × 1 |
Circuit Diagram
- Position the six LEDs on your breadboard, and pair each one with a 220-ohm resistor. These resistors ensure safe current levels for the LEDs, preventing potential damage.
- Link every LED-resistor combo to a digital output terminal on the microcontroller.
- Attach the push button switch to a digital input on the microcontroller, configuring the pins to function in input mode with an accompanying pull-up resistor.
Pin configurations
Device | Port.Pin | Signal Type | Module | Direction | Drive Mode |
---|---|---|---|---|---|
Example Firmware Code
Copy-paste the following code to your main.c file.
Example Firmware Code
EK-TM4C123GXL LaunchPad - main.c
Sample Firmware Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include "TM4C123GH6PM.h"
#include "MyDefines.h"
void DelayMs(int ms); // Software Delay Function
void Setup_GPIO(void);
int main(void)
{
// Place your initialization/startup code here (e.g. Setup_GPIO() )
Setup_GPIO();
while (1) {
// Place your application code here
}
}
//------------------------------------------------------------------------------
/*
Device Port.Pins DIR DriveMode
LEDR0
LEDR1
LEDR2
LEDB0
LEDB1
LEDB2
Port ____
*/
void Setup_GPIO(void)
{
// Configure GPIOs
// 1. Enable Clock to the GPIO Modules (SYSCTL->RCGCGPIO |= (_PORTs);)
SYSCTL->RCGCGPIO |= (__);
// allow time for clock to stabilize (SYSCTL->PRGPIO & (_PORTs))
while ((SYSCTL->PRGPIO & (__)) != (__)) {};
// 2. Unlock GPIO only PD7, PF0 on TM4C123G; PD7, PE7 on TM4C1294 (GPIOx->LOCK = 0x4C4F434B; and GPIOx->CR = _PINs;)
//GPIOF->LOCK = 0x4C4F434B; // Unlock for GPIOF
//GPIOF->CR |= _PIN0; // Commit for PIN0
//GPIOF->LOCK = 0;
// 3. Set Analog Mode Select bits for each Port (GPIOx->AMSEL = _PINs; 0=digital, 1=analog)
// 4. Set Port Control Register for each Port (GPIOx->PCTL = PMCn << _PTCL_PINn, check the PCTL table)
// 5. Set Alternate Function Select bits for each Port (GPIOx->AFSEL = _PINs; 0=regular I/O, 1=PCTL peripheral)
// 6. Set Output pins for each Port (Direction of the Pins: GPIOx->DIR = _PINs; 0=input, 1=output)
// 7. Set PUR bits (internal Pull-Up Resistor), PDR (Pull-Down Resistor), ODR (Open Drain) for each Port (0: disable, 1=enable)
// 8. Set Digital ENable register on all port.pins (GPIOx->DEN = _PINs; 0=disable, 1=enable)
}
//------------------------------------------------------------------------------
// Delay ms milliseconds (4167:50MHz TM4C123G CPU, 1605:16MHz TM4C123G CPU Clock)
void DelayMs(int ms)
{
volatile int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 4167; j++) {} // Do nothing for 1ms
}
EK-TM4C1294XL LaunchPad - main.c
Sample Firmware Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include "TM4C1294NCPDT.h"
#include "MyDefines.h"
void DelayMs(int ms); // Software Delay Function
void Setup_GPIO(void);
int main(void)
{
// Place your initialization/startup code here (e.g. Setup_GPIO() )
Setup_GPIO();
while (1) {
// Place your application code here
}
}
//------------------------------------------------------------------------------
/*
Device Port.Pins DIR DriveMode
LEDR0
LEDR1
LEDR2
LEDB0
LEDB1
LEDB2
Port ____
*/
void Setup_GPIO(void)
{
// Configure GPIOs
// 1. Enable Clock to the GPIO Modules (SYSCTL->RCGCGPIO |= (_PORTs);)
SYSCTL->RCGCGPIO |= (__);
// allow time for clock to stabilize (SYSCTL->PRGPIO & (_PORTs))
while ((SYSCTL->PRGPIO & (__)) != (__)) {};
// 2. Unlock GPIO only PD7, PF0 on TM4C123G; PD7, PE7 on TM4C1294 (GPIOx->LOCK = 0x4C4F434B; and GPIOx->CR = _PINs;)
// 3. Set Analog Mode Select bits for each Port (GPIOx->AMSEL = _PINs; 0=digital, 1=analog)
// 4. Set Port Control Register for each Port (GPIOx->PCTL = PMCn << _PTCL_PINn, check the PCTL table)
// 5. Set Alternate Function Select bits for each Port (GPIOx->AFSEL = _PINs; 0=regular I/O, 1=PCTL peripheral)
// 6. Set Output pins for each Port (Direction of the Pins: GPIOx->DIR = _PINs; 0=input, 1=output)
// 7. Set PUR bits (internal Pull-Up Resistor), PDR (Pull-Down Resistor), ODR (Open Drain) for each Port (0: disable, 1=enable)
// 8. Set Digital ENable register on all port.pins (GPIOx->DEN = _PINs; 0=disable, 1=enable)
}
//------------------------------------------------------------------------------
// Delay ms milliseconds (1605: 16MHz TM4C1294 CPU Clock)
void DelayMs(int ms)
{
volatile int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 1605; j++) {} // // Do nothing for 1ms
}
Implementation
You are tasked with crafting a minimum of three distinctive police LED light sequences. Consider utilizing a struct to structure the LED sequence as demonstrated::
typedef struct LEDPAT{
uint8_t leds;
uint8_t duration;
} LEDPAT;
Structure three pattern arrays as shown:
LEDPAT Pattern1[] = {
{ , }, // First item
...
};
LEDPAT Pattern2[] = {
{ , }, // First item
...
};
LEDPAT Pattern3[] = {
{ , }, // First item
...
};
Within the "main()" function, iterate through your pattern arrays. Illuminate the LEDs as directed by the "leds" parameter in the pattern, and introduce a pause (in milliseconds) as dictated by the "duration" parameter. When the switch (SW1) is activated, shift to the subsequent pattern. Employ software edge-detection techniques to ascertain SW1's state.
Note: Feel free to modify LED sequences and their respective durations to suit your preferences.