1. Lab 2_5
EENG 3910: Project V - Digital Signal
Processing System Design
9/21/2015
Joseph Chandler
University of North Texas
College of Engineering
Electrical Engineering
2. Introduction
Lab 2_5 continuesthe studyof the TivaTM
C SeriesEK-TM4C123GXL and CCS software.Lab2_5 's aspects
consistof systemcontrols,timing,uartanddifferentmodules.
Results and Discussions
Problem 5
#include <stdint.h>
Thisline containsthe headerfile forvariabledefinitions.The headerfile isalreadydefinedinthe CCS
software.Examplesof the valuesare bitsize,precision,andsignedorunsignedintegers.
#include <stdbool.h>
Thisline containsthe headerfile forBooleandefinitions.The headerfile isalreadydefinedinthe CCS
software.The file definitionsare usedforoperations,comparingvariables,andprogramcontrol flow.
#include "inc/tm4c123gh6pm.h"
Thisline containsthe headerfile forthe TM4C123GH6PM microcontroller.The headerfile ispart-
specificandcontainsmacrosand definitionstosimplifyprogrammingthe peripheral'sregistersand
interruptcontrol.Thisiscontainedinthe incdirectory. The .c file forthe TM4C123GH6PM
microcontrollerisincludedinthe projectfolderfordirectaccess.
#include "inc/hw_memmap.h"
Thisline containsthe headerfile forthe TivaCSeriesdevice memorymapdefinitions.The headerfile
containsmanyof the same definitionsusedfordirectregisteraccess.The headerfilecontainsmacros
and definitionstosimplifyprogrammingthe peripheral'sregistersandinterruptcontrol.Thisis
containedinthe incdirectory.The definitionsare usedbydriverlibraries andare hiddenfromthe
programmer.
#include "inc/hw_types.h"
Thisline containsthe headerfile forcommontypesandmacros.The headerfile containsmanyof the
same definitionsusedfordirectregisteraccess.Thisiscontainedinthe incdirectory.The definitionsare
usedbydriverlibrariesandare hiddenfromthe programmer.The valuesare a wide varietyof general
use for itemssuchas arithmetic,clocktiming,file recognition,anddevicerecognition.
#include "inc/hw_gpio.h"
Thisline contains the headerfile forcommontypesandmacros.The headerfile containsmanyof the
same definitionsusedfordirectregisteraccess.Thisiscontainedinthe incdirectory.The definitionsare
3. usedbydriverlibrariesandare hiddenfromthe programmer.The valuesare a wide varietyof general
use for itemssuchas arithmetic,clocktiming,file recognition,anddevicerecognition.
#include "driverlib/sysctl.h"
Thisline containsthe headerfile forthe SystemControl APIdefinitionsandmacros.The headerfile isin
the directory"driverlib"of the CCSsoftware.The driversprovideguidedcontrol of the peripheralsand
allowsforquickapplication.Enablingperipheralsisanexampleof thiscontrol.
#include "driverlib/interrupt.h"
Thisline containsthe headerfile forthe SystemControl APIdefinitionsandmacros.The headerfile isin
the directory"driverlib"of the CCSsoftware.The driversprovideguidedcontrol of the peripheralsand
allowsforquickapplication.Enablingperipheralsisanexampleof thiscontrol.
#include "driverlib/gpio.h"
Thisline containsthe headerfile forGPIOAPIdefinitionsandmacros.The headerfile isinthe directory
"driverlib"of the CCSsoftware.The driverprovidesasetof functionstocontrol the inputand output
module.
#include "driverlib/timer.h"
Thisline containsthe headerfile forTimerAPIdefinitionsandmacros.The headerfile isinthe directory
"driverlib"of the CCSsoftware.The driversprovide quickcontrol of the peripheralsandallowsforquick
application.The timerAPIprovidesasetof functionsforusingthe timermodule.
#include "driverlib/pin_map.h"
Thisline containsthe headerfile forpin_mapAPIdefinitionsandmacros.The headerfile isinthe
directory"driverlib"of the CCSsoftware.The driversprovidequickcontrol of the peripheralsandallows
for quickapplication.The pin_map APIprovidesamapof the peripheral topinconfiguration.
#include "driverlib/uart.h"
Thisline containsthe headerfile forUART(Universal AsynchronousReceiver/Transmitter) API
definitionsandmacros.The headerfile isinthe directory"driverlib"of the CCSsoftware.The drivers
provide quickcontrol of the peripheralsandallowsforquickapplication.The UARTAPIprovidesasetof
functionsforusingthe TIVA UART module.
#include "utils/uartstdio.h"
Thisline containsthe header file forUART.The header file isinthe directory"utils"of the CCS
software.The utilityprovidesquickcontrol of the peripheralsandallowsforquickapplication. The file
containsprototypesforthe UART console functions.Onbuildingthe project,the .cfile hadtobe added
manuallytolinkthe filestogether.
#define RED_LED GPIO_PIN_1
4. Createsa constantfor pin1 inthe GPIO APImodule thatdisplaysthe red LED.
#define BLUE_LED GPIO_PIN_2
Createsa constantfor pin2 inthe GPIO APImodule thatdisplaysthe blue LED.
#define GREEN_LED GPIO_PIN_3
Createsa constantfor pin3 inthe GPIO APImodule thatdisplaysthe greenLED.
#define NUM_BUTTONS 2
Createsa constantusedfor the twoswitchbuttons.
#define LEFT_BUTTON GPIO_PIN_4
Createsa constantfor pin4 inthe GPIO APImodule forleftswitchbutton.
#define RIGHT_BUTTON GPIO_PIN_0
Createsa constantfor pin0 inthe GPIO APImodule rightswitchbutton.
#define NUM_DEBOUNCE_CHECKS 10
It checksbefore aswitchis debounced. Createsaconstantusedfor50msec debounce time.
#define TIMER0_FREQ 2
Createsa constantusedfor frequencyinthe TimerAPImacrowitha value of 2.
#define TIMER1_FREQ 200
Createsa constantusedfor frequencyinthe Timer APImacrowitha value of 200.
#define UART0_BAUDRATE 115200
Createsa constantusedfor baudrate inthe UART API macro witha of value 115200 bps.
#define NUM_DISP_TEXT_LINE 4
Createsa constantfor the numberof linesof textdisplayinthe characterpointer"disp_text"witha
value of 4.
void init_LEDs(void);
Thisline declaresthe functionprototype "init_LEDs".Declarationatthispointgivesthe actual functiona
global scope.Anyotherfunction,header,or.c file containedinthissource file isgivenaccesstothe
functionandaccessible bythe function.
void init_timer(void);
5. Thisline declaresthe functionprototype " init_timer".Declarationatthispointgivesthe actual function
a global scope.Anyotherfunction,header,or.cfile containedinthissource fileisgivenaccesstothe
functionandaccessible bythe function.
void init_UART(void);
Thisline declaresthe functionprototype " init_UART".Declarationatthispointgivesthe actual function
a global scope.Anyotherfunction,header,or.cfile containedinthissource fileisgivenaccesstothe
functionandaccessible bythe function.
void init_buttons(void);
Thisline declaresthe functionprototype " init_buttons".Declarationatthispointgivesthe actual
functionaglobal scope.Anyotherfunction,header,or.cfile containedinthissource fileisgivenaccess
to the functionandaccessible bythe function.
void set_button_states(void);
Thisline declaresthe functionprototype " set_button_states ".Declarationatthispointgivesthe actual
functionaglobal scope.Anyotherfunction,header,or.cfile containedinthissource fileisgivenaccess
to the functionandaccessible bythe function.
void Timer0_ISR(void);
Thisline declaresthe functionprototype " Timer0_ISR".Declarationat thispointgivesthe actual
functionaglobal scope.Anyotherfunction,header,or.cfile containedinthissource fileisgivenaccess
to the functionandaccessible bythe function.
void Timer1_ISR(void);
Thisline declaresthe functionprototype " Timer1_ISR".Declarationat thispointgivesthe actual
functionaglobal scope.Anyotherfunction,header,or.cfile containedinthissource fileisgiven access
to the functionandaccessible bythe function.
extern void UARTStdioIntHandler(void);
Thisline declaresafunctionprototype of type "extern".Thisinformsthe microprocessorthatitneedsto
access a functionthatexistsoutsideof thissource file.The uartstdio.cfileisincludedinthe project
folderfordirectaccess.The uartstdio.cfile hasadditional functionsforthe UARTconsole.Thisparticular
functionhandlesinterruptsfromthe UART.The UART has a transmitandreceive bufferthatis usedfor
data transfersbetweenthe microprocessorandthe puttyconsole.
uint32_t sys_clock;
Createsan unsigned32-bitglobal variable forthe systemclock.
uint8_t cur_LED = RED_LED;
6. Createsan unsigned8-bitglobal variable forthe systemclock.
const char *disp_text[NUM_DISP_TEXT_LINE] = {
"n",
"UART and LED Demon",
"H: help, R: red, G: green, B: blue.n",
"> " };
Thisline createsa pointer fora systemmessage communicated tothe userviaputtyconsole.
volatile uint8_t raw_button_states[NUM_DEBOUNCE_CHECKS];
Createsan unsigned8-bitglobal arrayfordebounce.
volatile uint32_t raw_button_states_index=0;
Createsan unsigned32-bitglobal variable forinitializing index pointerof state.
volatile uint8_t button_states=0;
Createsan unsigned8-bitglobal variable forinitializingdebounced state.
int main(void){
The main processcallsandacts on variables,macros,definitions,andothersystemfunctions.
uint32_t i;
Createsan unsigned32-bitvariable forloops.
unsigned char user_cmd;
Createsan unsignedcharactervariable foruserinput.
uint8_t saved_button_states=0, cur_button_states;
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
SystemControl ClockSetsetsthe clockingof the device.Osc_Mainconfiguresthe oscillatorsource.
XTAL_16MHZ usesa 16 MHZ crystal clock. USE_PLL is a phase lockedloopat400MHZ. SYSDIV_5 divides
the clock bythe numberspecifiedgiving control of the frequency.
sys_clock = SysCtlClockGet();
Thisline retrievesthe processorclockrate andassignsit toa variable.
init_LEDs();
InitializesLEDconfigurationandenables by calling the function definition.
7. init_buttons();
Initializesbuttonsconfigurationandenables by calling the function definition.
init_UART();
InitializesUARTconfigurationandenables by calling the function definition.
init_timer();
InitializesTimerconfigurationandenables by calling the function definition.
IntMasterEnable();
ThisNVICAPIfunctionenablesinterruptsfromthe microprocessortointerruptcontroller.
TimerEnable(TIMER0_BASE, TIMER_A);
ThisTIMER API functionenablesoperationof the timer module.
TimerEnable(TIMER1_BASE, TIMER_A);
ThisTIMER API functionenablesoperationof the timer module.
for(i=0; i<NUM_DISP_TEXT_LINE; i++)
Thisline isa for loopto displayprogrammessage onconsole.
UARTprintf(disp_text[i]);
Thisfunctionprintsthe message onthe console.
while(1) {
The while loopprovidesacontinuousloopwhensetto"1".
if(UARTRxBytesAvail())
If the UART has available userinput.
user_cmd = UARTgetc();
Thisline retrievesthe userinputandstoresitina variable.
else
user_cmd = 0;
Else,the usercommandvariable issetto 0.
switch(user_cmd){
8. Switchcase for the userinputdata.
case 'r':
Switchcase for red.
case ' ':
Switchcase for empty.
case 'H':
Switchcase for H.
case 'h':
Switchcase for h.
for(i=0; i<NUM_DISP_TEXT_LINE; i++)
Switchcase display forlines87-90 is a for looptodisplayconsole message touserviaputtyconsole.
UARTprintf(disp_text[i]);
Thisfunctionprintsthe message onthe puttyconsole.
break;
Switchcase break.
case 'R':
Switchcase for redled.
case 'r':
Switchcase for redled.
cur_LED = RED_LED;
Storesthe red ledincurrentledvariable.
UARTprintf("n> ");
Thisfunctionprintsa newline witha> on the console.
break;
Switchcase break.
case 'B':
9. Switchcase for blue led.
case 'b':
Switchcase for blue led.
cur_LED = BLUE_LED;
Storesthe blue ledincurrentledvariable.
UARTprintf("n> ");
Thisfunctionprintsa newline witha> on the console.
break;
Switchcase break.
case 'G':
Switchcase for greenled.
case 'g':
Switchcase for greenled.
cur_LED = GREEN_LED;
Storesthe greenledincurrentledvariable.
UARTprintf("n> ");
Thisfunctionprintsa newline witha> on the console.
break;
Switchcase break.
// Check button states.
cur_button_states = button_states;
if(saved_button_states != cur_button_states){
if((~saved_button_states & LEFT_BUTTON) && (cur_button_states &
LEFT_BUTTON)) {
UARTprintf("Left button pushed down.n> ");
}
if((saved_button_states & LEFT_BUTTON) && (~cur_button_states &
LEFT_BUTTON)) {
UARTprintf("Left button released.n> ");
}
if((~saved_button_states & RIGHT_BUTTON) && (cur_button_states &
RIGHT_BUTTON)) {
10. UARTprintf("Right button pushed down.n> ");
}
if((saved_button_states & RIGHT_BUTTON) && (~cur_button_states &
RIGHT_BUTTON)) {
UARTprintf("Right button released.n> ");
}
if(cur_button_states == (LEFT_BUTTON | RIGHT_BUTTON)) {
UARTprintf("Both buttons held down.n> ");
}
saved_button_states = cur_button_states;
}
*The programcomparesincomingdatato saveddata. Whenthe data is notthe same,a nestedIF
statement ANDsall entriesof the array to configure the switchbuttonoutputdisplayonthe UART.
void init_LEDs(void)
User-definedfunctiondefinitionforinitializingLEDs.The functionreturnsavalue of 0.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
The SystemControl APIfunctionenablesthe GPIOportF peripheral.
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
The GPIO APIfunction configurespins1,2, and3 of portF for use as GPIO outputs.
void init_timer(void)
User-definedfunctiondefinitionforinitializingTimer.The functionreturnsavalue of 0.
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
The SystemControl APIfunction enablesthe timer0peripheral.
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
The SystemControl APIfunctionenablesthe timer1peripheral.
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
The TimerAPI functionconfiguresthe timerasa 32-bitfull-widthperiodictimer.
TimerLoadSet(TIMER0_BASE, TIMER_A, sys_clock/TIMER0_FREQ -1);
The TimerAPI functionspecifiesthe base addressforconfiguration,the correctname fora full-width
timer,andthe load value specifiedbyuser-definedconstants.
TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);
11. The TimerAPI functionconfiguresthe timerasa 32-bitfull-widthperiodictimer.
TimerLoadSet(TIMER1_BASE, TIMER_A, sys_clock/TIMER0_FREQ -1);
The TimerAPI functionspecifiesthe base addressforconfiguration,the correctname fora full-width
timer,andthe load value specifiedbyuser-definedconstants.
IntRegister(INT_TIMER0A, Timer0_ISR);
The NVICAPIfunctionregistersthe "TIMER0_ISR"functiontobe calledwhenthe "TIMER0A"interruptis
enabled.
IntEnable(INT_TIMER0A);
The NVICAPIfunctionenables"TIMER0A"asa full-widthtimer tobe usedforthe interruptfunction
process.
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
The TimerAPI functionenablesthe specifictimermodule(TIMER0_Base) forthe timer interruptsource.
The bit maskof the interruptsource tobe enabledis"TIMER_TIMA_TIMEOUT".
IntRegister(INT_TIMER1A, Timer1_ISR);
The NVICAPIfunctionregistersthe "TIMER1_ISR"functiontobe calledwhenthe "TIMER0A"interruptis
enabled.
IntEnable(INT_TIMER1A);
The NVICAPIfunctionenables"TIMER1A"asa full-widthtimer tobe usedforthe interruptfunction
process.
TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
The TimerAPI functionenablesthe specifictimermodule(TIMER1_Base) forthe timerinterruptsource.
The bit maskof the interruptsource tobe enabledis"TIMER_TIMA_TIMEOUT".
void init_UART(void)
User-definedfunctiondefinitionforinitializingUART.The functionreturnsavalue of 0.
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
The SystemControl APIfunctionenablesthe UART0 peripheral.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
The SystemControl APIfunctionenablesthe GPIOA peripheral.
12. GPIOPinConfigure(GPIO_PA0_U0RX);
The GPIO APIfunctionconfiguresthe alternate functionof aGPIO pin.GPIOPort A's pinmux is
configuredforthe UART peripheral.Pin0is configuredforthe UARTreceiverbuffer.
GPIOPinConfigure(GPIO_PA1_U0TX);
The GPIO APIfunctionconfiguresthe alternate functionof aGPIO pin.GPIOPort A's pinmux is
configuredforthe UART0 peripheral.Pin1isconfiguredforthe UART transmitterbuffer.
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
The GPIO APIfunctionconfigurespinsforuse bythe UART peripheral.GPIOPort A will be the peripheral
usedforUART communication.Pins0and1 of port A are available.
IntRegister(INT_UART0, UARTStdioIntHandler);
The NVICAPIfunctionregistersthe "UARTStdioIntHandler"functiontobe calledwhenthe "UART0"
interruptisenabled.
UARTStdioConfig(0, UART0_BAUDRATE, sys_clock);
The UART driverfunctionconfiguresthe UARTforinput/output.UARTPort0 isthe base that will be
used.The user-definedconstant"UART0_BAUDRATE"isthe bitrate forthe UART and the user-defined
constant "sys_clock"isthe frequencyusedforthe source clockof the UART module.
void Timer0_ISR(void)
User-definedfunctiondefinitionforinterrupthandler.The functionreturnsavalue of 0.
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
The TimerAPI functionclearsthe timerinterruptsources.TIMER0 isthe base specifiedand
"TIMER_TIMA_TIMEOUT" isthe bitmaskfor the interruptsource cleared.
if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3)) {
If the GPIOAPIfunctionreadspins1,2, or 3 fromport F.
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
The GPIO APIfunctionwritesa0 to pins1,2, or 3.
else {
Else the GPIO APIfunction...
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, cur_LED);
Writesthe value of the variable "cur_LED" to the correct pin.
13. void Timer1_ISR(void)
User-definedfunctiondefinitionforinterrupthandler.The functionreturnsavalue of 0.
TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
The TimerAPI functionclearsthe timerinterruptsources.TIMER1 isthe base specifiedand
"TIMER_TIMA_TIMEOUT" isthe bitmaskfor the interruptsource cleared.
set_button_states();
void init_buttons(void)
User-definedfunctiondefinitionfor switchbuttons.
uint32_t i
Createsan unsigned32-bitlocal index variable.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
The SystemControl APIfunctionenablesthe GPIO peripheral portF.
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
Unlock port F from NMI as GPIO input pin.
14. HWREG(GPIO_PORTF_BASE + GPIO_O_CR) |= 0x01;
Configure port F base to enable pins 0 and 4.
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = 0;
Relock port F for permanent configuration.
GPIODirModeSet(GPIO_PORTF_BASE, LEFT_BUTTON|RIGHT_BUTTON, GPIO_DIR_MODE_IN);
The GPIO APIfunctionconfigurespins0and4 of portF for use as GPIO inputs.
GPIOPadConfigSet(GPIO_PORTF_BASE, LEFT_BUTTON|RIGHT_BUTTON,
GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
The GPIO APIfunctionconfigures pins0and4 of portF for weak pull-upbuttontype.
for(i=0; i<NUM_DEBOUNCE_CHECKS; i++)
raw_button_states[i] = 0;
The for loopmaintainsbounce statusfromuserinterface.
void set_button_states(void)
User-definedfunctiondefinitionforbuttonstates.
uint32_t i
Createsan unsigned32-bitlocal index variable.
uint8_t states = LEFT_BUTTON|RIGHT_BUTTON;
Createsan unsigned8-bitlocal GPIOportF pinnumber variable.
raw_button_states[raw_button_states_index] = ~ GPIOPinRead(GPIO_PORTF_BASE,
LEFT_BUTTON|RIGHT_BUTTON);
GPIO APIfunctionreadspins0 and4 from portF and storesstate in volatile memory.
if(raw_button_states_index >= NUM_DEBOUNCE_CHECKS-1)
raw_button_states_index = 0;
If the functionreads alarger index thanthe (debouncer - 1) checksfor, itstarts from0 again.
else
raw_button_states_index ++;
else the functionreadsasmallerindex andincrements.
for(i=0; i<NUM_DEBOUNCE_CHECKS; i++)
states = states & raw_button_states[i];