0% found this document useful (0 votes)
6 views15 pages

EMS lab1[1]

The document outlines a laboratory exercise on interfacing LEDs and switches using the GPIO module of the TIVA TM4C123GH6PM Launch Pad. It includes objectives, code examples, and results for toggling an onboard LED, controlling an LED with a switch, changing LED colors based on switch states, and controlling external LEDs using onboard switches. The exercises demonstrate practical applications of GPIO programming and hardware interaction.

Uploaded by

mandalamharini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

EMS lab1[1]

The document outlines a laboratory exercise on interfacing LEDs and switches using the GPIO module of the TIVA TM4C123GH6PM Launch Pad. It includes objectives, code examples, and results for toggling an onboard LED, controlling an LED with a switch, changing LED colors based on switch states, and controlling external LEDs using onboard switches. The exercises demonstrate practical applications of GPIO programming and hardware interaction.

Uploaded by

mandalamharini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

ESD LAB-1

LED AND SWITCH


INTERFACE

HARINI MANDALAM(EC23I2012)
13/01/2025
THEORY :
GPIO MODULE :

The GPIO namely, General-Purpose Input Output module gives the access of outside world to the
processor and vice versa. Be it sensors or actuators or switches are connected to the processor through
this module.

In TIVA TM4C123GH6PM Launch Pad there are 43 programmable input/output pins divided into 6 ports.

The Port F is special as it has an onboard LED connected and two onboard switches connected in
negative logic.

SWITCH INTERFACE :

When a normally open (NO) switch with infinite resistance is closed, its resistance is about 0.1 ohms.

Used with a pull-up resistor to +3.3V or a pull-down resistor to ground.

RUN MODE CLOCK GATING CONTROL REGISTER 2 (RCGC2) :

This register controls the clock gating logic in normal Run mode. Each bit controls a clock enable for a
given interface, function, or module. If set, the module receives a clock and functions.
Otherwise, the module is unclocked and disabled.

1
// Color LED(s) PortF

// dark --- 0

// red R-- 0x02

// blue --B 0x04

// green -G- 0x08

// yellow RG- 0x0A

// sky blue -GB 0x0C

// white RGB 0x0E

// pink R-B 0x06

2
1.TOGGLE ONBOARD LED :

OBJECTIVE : To toggle an onboard LED and to observe its waveform.

CODE:

#include "tm4c123gh6pm.h”

#include <stdint.h>

void PortF_Init(void){ volatile unsigned long delay;

SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F delay =

SYSCTL_RCGC2_R; // allow time for clock to start

GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F

GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0

// only PF0 needs to be unlocked, other bits can't be locked

GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF

GPIO_PORTF_PCTL_R = 0x00000000; // 4) PCTL GPIO on PF4-0

GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 in, PF3-1 out

GPIO_PORTF_AFSEL_R = 0x00; // 6) disable alt funct on PF7-0

GPIO_PORTF_PUR_R = 0x11; // enable pull-up on PF0 and PF4

GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0

unsigned long Led; void Delay(void){unsigned

long volatile time;

time =145448; // 0.1sec while(time)

3
time--;

int main(void){
PortF_Init(); // make PF1 out (PF1 built-in LED) while(1){

GPIO_PORTF_DATA_R ^= 0x02;

Delay();

}}

OUTPUT:

RESULT :

A LED and switch was created. The Waveform was observed in bit form and the LED was made to toggle.

4
2.INTERFACE SW1 AND CONTROL LED :

OBJECTIVE : To set up switch interface 1 to control the LED.

Reading Rainbow Tip: Find an image online that resembles the setting of this story, then replace the
image above.

CODE:

#include "tm4c123gh6pm.h" #include <stdint.h>

unsigned long Switch; void PortF_Init(void){ volatile

unsigned long delay;

SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F delay =

SYSCTL_RCGC2_R; // allow time for clock to start

GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F

GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0

// only PF0 and PF3 needs to be unlocked, other bits can't be locked

GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF

GPIO_PORTF_PCTL_R = 0x00000000; // 4) PCTL GPIO on PF4-0

GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 in, PF3-1 out

GPIO_PORTF_AFSEL_R = 0x00; // 6) disable alt funct on PF7-0

GPIO_PORTF_PUR_R = 0x11; // enable pull-up on PF0 and PF4

GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0

int main(void)

5
{

PortF_Init();

while(1)

Switch=GPIO_PORTF_DATA_R&0x11;

if(Switch==0x01)

GPIO_PORTF_DATA_R = 0x02; else

GPIO_PORTF_DATA_R = 0x00;

OUTPUT:

When switch is off :

When switch is turned on :

6
RESULT :

The switch 1 has been configured successfully to control LED.

7
3. LED G,B,R :
OBJECTIVE :To control on board LED switches to perform the following actions.

CODE :

#include "tm4c123gh6pm.h" #include <stdint.h>

unsigned long Switch; void PortF_Init(void){ volatile

unsigned long delay;

SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F delay =

SYSCTL_RCGC2_R; // allow time for clock to start

GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F

GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0

// only PF0 and PF3 needs to be unlocked, other bits can't be locked

GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF

GPIO_PORTF_PCTL_R = 0x00000000; // 4) PCTL GPIO on PF4-0

GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 in, PF3-1 out

GPIO_PORTF_AFSEL_R = 0x00; // 6) disable alt funct on PF7-0

GPIO_PORTF_PUR_R = 0x11; // enable pull-up on PF0 and PF4

GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0

8
int main(void)

PortF_Init();

while(1)

Switch=GPIO_PORTF_DATA_R&0x11;

if(Switch==0x11)

GPIO_PORTF_DATA_R = 0x02;

else if (Switch==0x01)

GPIO_PORTF_DATA_R = 0x08;

else if (Switch==0x10)

GPIO_PORTF_DATA_R = 0x04;

else if (Switch==0x00)

GPIO_PORTF_DATA_R = 0x0E; else

GPIO_PORTF_DATA_R = 0x00;

9
OUTPUT :

When both the switches are turned on RED LED glows :

When switch 1 is on and switch 2 is off BLUE LED GLOWS :

10
When switch 1 is off and switch 2 is on YELLOW LED GLOWS :

When both switches are turned off WHITE LED GLOWS :

11
RESULT :

When the onboard LED switches have been controlled to observe the required colors.

4. CONTROL EXTERNAL LED USING ONBOARD SWITCHES:


OBJECTIVE : The switches in port F have to be configured to control the port D LEDs.

CODE :

#include "tm4c123gh6pm.h" #include <stdint.h>

unsigned long SW; void PortF_Init(void){ volatile

unsigned long delay;

SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F delay =

SYSCTL_RCGC2_R; // allow time for clock to start GPIO_PORTF_LOCK_R =

0x4C4F434B; // 2) unlock GPIO Port F

GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0

// only PF0 needs to be unlocked, other bits can't be locked

GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF

GPIO_PORTF_PCTL_R = 0x00000000; // 4) PCTL GPIO on PF4-0

GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 in, PF3-1 out

GPIO_PORTF_AFSEL_R = 0x00; // 6) disable alt funct on PF7-0

GPIO_PORTF_PUR_R = 0x11; // enable pull-up on PF0 and PF4

GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0

void PortD_Init(void){ volatile unsigned long delay;

12
SYSCTL_RCGC2_R |= 0x00000008; // 1) activate clock for Port D

delay = SYSCTL_RCGC2_R; // allow time for clock to start

GPIO_PORTD_AMSEL_R &= ~(0x09); // 3) disable analog on PD0 and PD3

GPIO_PORTD_PCTL_R &= ~(0x09); // 4) PCTL GPIO on PD0 and PD3

GPIO_PORTD_DIR_R |= 0x09; // 5) enable PD0 and PD3 as output

GPIO_PORTD_AFSEL_R&= ~(0x09); // 6) disable alt funct on PD0 and PD3

GPIO_PORTD_DEN_R |= 0x09; // 7) enable digital I/O on PD0 and PD3

int main(void) { PortF_Init();

PortD_Init();

while(1) {

SW =GPIO_PORTF_DATA_R&0x11;

if(SW==0x11)

GPIO_PORTD_DATA_R = 0x00;

else if (SW==0x01)

GPIO_PORTD_DATA_R = 0x08;

else if (SW==0x10)

GPIO_PORTD_DATA_R = 0x01; else if

(SW==0x00)

GPIO_PORTD_DATA_R = 0x09;

else

GPIO_PORTD_DATA_R = 0x00;

}}

13
OUTPUT :
When both the switches are turned off:

When both the switch 2 is turned on :

RESULT :

Port F has been successfully used to configure the LEDs in Port D.

14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy