Expt 2
Expt 2
Expt 2
Experiment No. 2
Name:-
MIS :-
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);