Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Expt 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Embedded System Lab

Experiment No. 2

Name:-
MIS :-

Objective:-To write a program to glow the onboard LED with


delay value.
Theory: Tiva C Launchpad (TM4C123GH6PM)
All the address range of port A to port F are given in the table below. As you can
see from this table, a total of 4K bytes of memory is allocated for each PORT. The
reason for this larger amount of memory for each port is due to many functions and
special functions registers associated with each port of TM4C123G.
Apparatus:
Software required: Code composer studio
Hardware required: TIVA TM4C123GH6PM Launch pad
Procedure:
1. Connect tiva TM4C123GH6PM to PC using USB cable
supplied.
2. Build, Program and Debug the code into launch pad using
ccs to view the status of output.

Program:
#include <stdint.h>
#include <stdbool.h>
#include <inc/hw_memmap.h>
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
Int main(void)
{
volatile uint32_t ui32Loop;
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF))
{
}
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);
while(1)
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
{
}
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0x0);
for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
{
}
}
}

Questions:
1. What is volatile keyword? Why is it used? Elaborate.
Volatile:
In any embedded systems i.e. the systems uses microcontrollers or advanced
controllers, Code language is used to give the command or to perform any
desired task or to get the required output.
While coding, the programmer may decide or choose some letters or signs as a
variable in the program to indicate some quantities for which the program is
written or the device is going to be used.
Volatile is a qualifier that is applied to a variable when it is declared.
It tells the compiler that the value of the variable may change at any time-
without any action being taken by the code the compiler finds nearby.
To declare a variable volatile, include the keyword volatile before or after the
data type in the variable definition.
Use:
A variable should be declared volatile whenever its value could change
unexpectedly.
In practice, only three types of variables could change:
i. Memory-mapped peripheral registers:
Embedded systems contain real hardware, usually with sophisticated
peripherals. These peripherals contain registers whose values may
change asynchronously to the program flow.
ii. Global variables modified by an interrupt service routine:
Interrupt service routines often set variables that are tested in main line
code.
iii. Global variables within a multi-threaded application:
The presence of queues, pipes, and other scheduler -aware
communications mechanisms in real-time operating systems, it is still
fairly common for two tasks to exchange information via a shared
memory location (that is, a global).When you add a pre-emptive
scheduler to your code, your compiler still has no idea what a context
switch is or when one might occur. Thus, another task modifying a
shared global is conceptually identical to the problem of interrupt
service routines discussed previously. So all shared global variables
should be declared volatile.
2. Write code for blink LED in red, blue and green order?
#include <stdint.h>
#include <stdbool.h>
#include <inc/hw_memmap.h>
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
int
main(void)
{
volatile uint32_t ui32Loop;

//
// Enable the GPIO port that is used for the on-board LED.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

//
// Check if the peripheral access is enabled.
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF))
{
}

//
// Enable the GPIO pin for the LED (PF3). Set the direction as output, and
// enable the GPIO pin for digital function.
//
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3);
//
// Loop forever.
//
while(1)
{
//
// Turn on the LED.
//
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);

//
// Delay for a bit.
//
for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
{
}

//
// Turn off the LED.
//
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0x0);

//
// Delay for a bit.
//
for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
{
}

//
// Turn on the LED.
//
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
//
// Delay for a bit.
//
for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
{
}

//
// Turn off the LED.
//
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0x0);

//
// Delay for a bit.
//
for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
{
}

//
// Turn on the LED.
//
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3);

//
// Delay for a bit.
//
for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
{
}

//
// Turn off the LED.
//
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0x0);

// Delay for a bit.


//
for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
{
}
}
}

You might also like