20
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

Lab 2_5

Embed Size (px)

Citation preview

Page 1: Lab 2_5

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

Page 2: Lab 2_5

IntroductionLab 2_5 continues the study of the TivaTM C Series EK-TM4C123GXL and CCS software. Lab 2_5 's aspects consist of system controls, timing, uart and different modules.

Results and Discussions

Problem 5

#include <stdint.h>

This line contains the header file for variable definitions. The header file is already defined in the CCS software. Examples of the values are bit size, precision, and signed or unsigned integers.

#include <stdbool.h>

This line contains the header file for Boolean definitions. The header file is already defined in the CCS software. The file definitions are used for operations, comparing variables, and program control flow.

#include "inc/tm4c123gh6pm.h"

This line contains the header file for the TM4C123GH6PM microcontroller. The header file is part-specific and contains macros and definitions to simplify programming the peripheral's registers and interrupt control. This is contained in the inc directory. The .c file for the TM4C123GH6PM microcontroller is included in the project folder for direct access.

#include "inc/hw_memmap.h"

This line contains the header file for the Tiva C Series device memory map definitions. The header file contains many of the same definitions used for direct register access. The header file contains macros and definitions to simplify programming the peripheral's registers and interrupt control. This is contained in the inc directory. The definitions are used by driver libraries and are hidden from the programmer.

#include "inc/hw_types.h"

This line contains the header file for common types and macros. The header file contains many of the same definitions used for direct register access. This is contained in the inc directory. The definitions are used by driver libraries and are hidden from the programmer. The values are a wide variety of general use for items such as arithmetic, clock timing, file recognition, and device recognition.

#include "inc/hw_gpio.h"

This line contains the header file for common types and macros. The header file contains many of the same definitions used for direct register access. This is contained in the inc directory. The definitions are

Page 3: Lab 2_5

used by driver libraries and are hidden from the programmer. The values are a wide variety of general use for items such as arithmetic, clock timing, file recognition, and device recognition.

#include "driverlib/sysctl.h"

This line contains the header file for the System Control API definitions and macros. The header file is in the directory "driverlib" of the CCS software. The drivers provide guided control of the peripherals and allows for quick application. Enabling peripherals is an example of this control.

#include "driverlib/interrupt.h"

This line contains the header file for the System Control API definitions and macros. The header file is in the directory "driverlib" of the CCS software. The drivers provide guided control of the peripherals and allows for quick application. Enabling peripherals is an example of this control.

#include "driverlib/gpio.h"

This line contains the header file for GPIO API definitions and macros. The header file is in the directory "driverlib" of the CCS software. The driver provides a set of functions to control the input and output module.

#include "driverlib/timer.h"

This line contains the header file for Timer API definitions and macros. The header file is in the directory "driverlib" of the CCS software. The drivers provide quick control of the peripherals and allows for quick application. The timer API provides a set of functions for using the timer module.

#include "driverlib/pin_map.h"

This line contains the header file for pin_map API definitions and macros. The header file is in the directory "driverlib" of the CCS software. The drivers provide quick control of the peripherals and allows for quick application. The pin_map API provides a map of the peripheral to pin configuration.

#include "driverlib/uart.h"

This line contains the header file for UART (Universal Asynchronous Receiver/Transmitter) API definitions and macros. The header file is in the directory "driverlib" of the CCS software. The drivers provide quick control of the peripherals and allows for quick application. The UART API provides a set of functions for using the TIVA UART module.

#include "utils/uartstdio.h"

This line contains the header file for UART. The header file is in the directory "utils" of the CCS software. The utility provides quick control of the peripherals and allows for quick application. The file contains prototypes for the UART console functions. On building the project, the .c file had to be added manually to link the files together.

#define RED_LED GPIO_PIN_1

Page 4: Lab 2_5

Creates a constant for pin 1 in the GPIO API module that displays the red LED.

#define BLUE_LED GPIO_PIN_2

Creates a constant for pin 2 in the GPIO API module that displays the blue LED.

#define GREEN_LED GPIO_PIN_3

Creates a constant for pin 3 in the GPIO API module that displays the green LED.

#define NUM_BUTTONS 2

Creates a constant used for the two switch buttons.

#define LEFT_BUTTON GPIO_PIN_4

Creates a constant for pin 4 in the GPIO API module for left switch button.

#define RIGHT_BUTTON GPIO_PIN_0

Creates a constant for pin 0 in the GPIO API module right switch button.

#define NUM_DEBOUNCE_CHECKS 10

It checks before a switch is debounced. Creates a constant used for 50msec debounce time.

#define TIMER0_FREQ 2

Creates a constant used for frequency in the Timer API macro with a value of 2.

#define TIMER1_FREQ 200

Creates a constant used for frequency in the Timer API macro with a value of 200.

#define UART0_BAUDRATE 115200

Creates a constant used for baud rate in the UART API macro with a of value 115200 bps.

#define NUM_DISP_TEXT_LINE 4

Creates a constant for the number of lines of text display in the character pointer "disp_text" with a value of 4.

void init_LEDs(void);

This line declares the function prototype "init_LEDs". Declaration at this point gives the actual function a global scope. Any other function, header, or .c file contained in this source file is given access to the function and accessible by the function.

void init_timer(void);

Page 5: Lab 2_5

This line declares the function prototype " init_timer ". Declaration at this point gives the actual function a global scope. Any other function, header, or .c file contained in this source file is given access to the function and accessible by the function.

void init_UART(void);

This line declares the function prototype " init_UART ". Declaration at this point gives the actual function a global scope. Any other function, header, or .c file contained in this source file is given access to the function and accessible by the function.

void init_buttons(void);

This line declares the function prototype " init_buttons ". Declaration at this point gives the actual function a global scope. Any other function, header, or .c file contained in this source file is given access to the function and accessible by the function.

void set_button_states(void);

This line declares the function prototype " set_button_states ". Declaration at this point gives the actual function a global scope. Any other function, header, or .c file contained in this source file is given access to the function and accessible by the function.

void Timer0_ISR(void);

This line declares the function prototype " Timer0_ISR ". Declaration at this point gives the actual function a global scope. Any other function, header, or .c file contained in this source file is given access to the function and accessible by the function.

void Timer1_ISR(void);

This line declares the function prototype " Timer1_ISR ". Declaration at this point gives the actual function a global scope. Any other function, header, or .c file contained in this source file is given access to the function and accessible by the function.

extern void UARTStdioIntHandler(void);

This line declares a function prototype of type "extern". This informs the microprocessor that it needs to access a function that exists outside of this source file. The uartstdio.c file is included in the project folder for direct access. The uartstdio.c file has additional functions for the UART console. This particular function handles interrupts from the UART. The UART has a transmit and receive buffer that is used for data transfers between the microprocessor and the putty console.

uint32_t sys_clock;

Creates an unsigned 32-bit global variable for the system clock.

uint8_t cur_LED = RED_LED;

Page 6: Lab 2_5

Creates an unsigned 8-bit global variable for the system clock.

const char *disp_text[NUM_DISP_TEXT_LINE] = {

"\n","UART and LED Demo\n","H: help, R: red, G: green, B: blue.\n","> " };

This line creates a pointer for a system message communicated to the user via putty console.

volatile uint8_t raw_button_states[NUM_DEBOUNCE_CHECKS]; Creates an unsigned 8-bit global array for debounce.

volatile uint32_t raw_button_states_index=0;

Creates an unsigned 32-bit global variable for initializing index pointer of state.

volatile uint8_t button_states=0;

Creates an unsigned 8-bit global variable for initializing debounced state.

int main(void){

The main process calls and acts on variables, macros, definitions, and other system functions.

uint32_t i;

Creates an unsigned 32-bit variable for loops.

unsigned char user_cmd;

Creates an unsigned character variable for user input.

uint8_t saved_button_states=0, cur_button_states;

SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);

System Control Clock Set sets the clocking of the device. Osc_Main configures the oscillator source. XTAL_16MHZ uses a 16 MHZ crystal clock. USE_PLL is a phase locked loop at 400MHZ. SYSDIV_5 divides the clock by the number specified giving control of the frequency.

sys_clock = SysCtlClockGet();

This line retrieves the processor clock rate and assigns it to a variable.

init_LEDs();

Page 7: Lab 2_5

Initializes LED configuration and enables by calling the function definition.

init_buttons();

Initializes buttons configuration and enables by calling the function definition.

init_UART();

Initializes UART configuration and enables by calling the function definition.

init_timer();

Initializes Timer configuration and enables by calling the function definition.

IntMasterEnable();

This NVIC API function enables interrupts from the microprocessor to interrupt controller.

TimerEnable(TIMER0_BASE, TIMER_A);

This TIMER API function enables operation of the timer module.

TimerEnable(TIMER1_BASE, TIMER_A);

This TIMER API function enables operation of the timer module.

for(i=0; i<NUM_DISP_TEXT_LINE; i++)

This line is a for loop to display program message on console.

UARTprintf(disp_text[i]);

This function prints the message on the console.

while(1) {

The while loop provides a continuous loop when set to "1".

if(UARTRxBytesAvail())

If the UART has available user input.

user_cmd = UARTgetc();

This line retrieves the user input and stores it in a variable.

else

user_cmd = 0;

Else, the user command variable is set to 0.

Page 8: Lab 2_5

switch(user_cmd){

Switch case for the user input data.

case '\r':

Switch case for red.

case ' ':

Switch case for empty.

case 'H':

Switch case for H.

case 'h':

Switch case for h.

for(i=0; i<NUM_DISP_TEXT_LINE; i++)

Switch case display for lines 87-90 is a for loop to display console message to user via putty console.

UARTprintf(disp_text[i]);

This function prints the message on the putty console.

break;

Switch case break.

case 'R':

Switch case for red led.

case 'r':

Switch case for red led.

cur_LED = RED_LED;

Stores the red led in current led variable.

UARTprintf("\n> ");

This function prints a new line with a > on the console. break;

Switch case break.

Page 9: Lab 2_5

case 'B':

Switch case for blue led.

case 'b':

Switch case for blue led.

cur_LED = BLUE_LED;

Stores the blue led in current led variable.

UARTprintf("\n> ");

This function prints a new line with a > on the console.

break;

Switch case break.

case 'G':

Switch case for green led.

case 'g':

Switch case for green led.

cur_LED = GREEN_LED;

Stores the green led in current led variable.

UARTprintf("\n> ");

This function prints a new line with a > on the console.

break;

Switch case 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> ");

}

Page 10: Lab 2_5

if((~saved_button_states & RIGHT_BUTTON) && (cur_button_states & RIGHT_BUTTON)) {

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 program compares incoming data to saved data. When the data is not the same, a nested IF statement ANDs all entries of the array to configure the switch button output display on the UART.

void init_LEDs(void)

User-defined function definition for initializing LEDs. The function returns a value of 0.

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

The System Control API function enables the GPIO port F peripheral.

GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);

The GPIO API function configures pins 1, 2, and 3 of port F for use as GPIO outputs.

void init_timer(void)

User-defined function definition for initializing Timer. The function returns a value of 0.

SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);

The System Control API function enables the timer0 peripheral.

SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);

The System Control API function enables the timer1 peripheral.

TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);

The Timer API function configures the timer as a 32-bit full-width periodic timer.

TimerLoadSet(TIMER0_BASE, TIMER_A, sys_clock/TIMER0_FREQ -1);

The Timer API function specifies the base address for configuration, the correct name for a full-width timer, and the load value specified by user-defined constants.

TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);

Page 11: Lab 2_5

The Timer API function configures the timer as a 32-bit full-width periodic timer.

TimerLoadSet(TIMER1_BASE, TIMER_A, sys_clock/TIMER0_FREQ -1);

The Timer API function specifies the base address for configuration, the correct name for a full-width timer, and the load value specified by user-defined constants.

IntRegister(INT_TIMER0A, Timer0_ISR);

The NVIC API function registers the "TIMER0_ISR" function to be called when the "TIMER0A" interrupt is enabled.

IntEnable(INT_TIMER0A);

The NVIC API function enables "TIMER0A" as a full-width timer to be used for the interrupt function process.

TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

The Timer API function enables the specific timer module(TIMER0_Base) for the timer interrupt source. The bit mask of the interrupt source to be enabled is "TIMER_TIMA_TIMEOUT".

IntRegister(INT_TIMER1A, Timer1_ISR);

The NVIC API function registers the "TIMER1_ISR" function to be called when the "TIMER0A" interrupt is enabled.

IntEnable(INT_TIMER1A);

The NVIC API function enables "TIMER1A" as a full-width timer to be used for the interrupt function process.

TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);

The Timer API function enables the specific timer module(TIMER1_Base) for the timer interrupt source. The bit mask of the interrupt source to be enabled is "TIMER_TIMA_TIMEOUT".

void init_UART(void)

User-defined function definition for initializing UART. The function returns a value of 0.

SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

The System Control API function enables the UART0 peripheral.

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

The System Control API function enables the GPIOA peripheral.

Page 12: Lab 2_5

GPIOPinConfigure(GPIO_PA0_U0RX);

The GPIO API function configures the alternate function of a GPIO pin. GPIO Port A's pin mux is configured for the UART peripheral. Pin 0 is configured for the UART receiver buffer.

GPIOPinConfigure(GPIO_PA1_U0TX);

The GPIO API function configures the alternate function of a GPIO pin. GPIO Port A's pin mux is configured for the UART0 peripheral. Pin 1 is configured for the UART transmitter buffer.

GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

The GPIO API function configures pins for use by the UART peripheral. GPIO Port A will be the peripheral used for UART communication. Pins 0 and 1 of port A are available.

IntRegister(INT_UART0, UARTStdioIntHandler);

The NVIC API function registers the "UARTStdioIntHandler" function to be called when the "UART0" interrupt is enabled.

UARTStdioConfig(0, UART0_BAUDRATE, sys_clock);

The UART driver function configures the UART for input/output. UART Port 0 is the base that will be used. The user-defined constant "UART0_BAUDRATE" is the bit rate for the UART and the user-defined constant "sys_clock" is the frequency used for the source clock of the UART module.

void Timer0_ISR(void)

User-defined function definition for interrupt handler. The function returns a value of 0.

TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

The Timer API function clears the timer interrupt sources. TIMER0 is the base specified and "TIMER_TIMA_TIMEOUT" is the bit mask for the interrupt source cleared.

if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3)) {

If the GPIO API function reads pins 1,2, or 3 from port F.

GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);

The GPIO API function writes a 0 to pins 1,2, or 3.

else {

Else the GPIO API function ...

GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, cur_LED);

Writes the value of the variable "cur_LED" to the correct pin.

Page 13: Lab 2_5

void Timer1_ISR(void)

User-defined function definition for interrupt handler. The function returns a value of 0.

TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);

The Timer API function clears the timer interrupt sources. TIMER1 is the base specified and "TIMER_TIMA_TIMEOUT" is the bit mask for the interrupt source cleared.

set_button_states();

void init_buttons(void)

User-defined function definition for switch buttons.

uint32_t i

Creates an unsigned 32-bit local index variable.

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

The System Control API function enables the GPIO peripheral port F.

HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;

Unlock port F from NMI as GPIO input pin.

Page 14: Lab 2_5

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 API function configures pins 0 and 4 of port F for use as GPIO inputs.

GPIOPadConfigSet(GPIO_PORTF_BASE, LEFT_BUTTON|RIGHT_BUTTON, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

The GPIO API function configures pins 0 and 4 of port F for weak pull-up button type.

for(i=0; i<NUM_DEBOUNCE_CHECKS; i++) raw_button_states[i] = 0;

The for loop maintains bounce status from user interface.

void set_button_states(void)

User-defined function definition for button states.

uint32_t i

Creates an unsigned 32-bit local index variable.

uint8_t states = LEFT_BUTTON|RIGHT_BUTTON;

Creates an unsigned 8-bit local GPIO port F pin number variable.

raw_button_states[raw_button_states_index] = ~ GPIOPinRead(GPIO_PORTF_BASE,

LEFT_BUTTON|RIGHT_BUTTON);

GPIO API function reads pins 0 and 4 from port F and stores state in volatile memory.

if(raw_button_states_index >= NUM_DEBOUNCE_CHECKS-1)raw_button_states_index = 0;

If the function reads a larger index than the (debouncer - 1) checks for, it starts from 0 again.

elseraw_button_states_index ++;

else the function reads a smaller index and increments.

for(i=0; i<NUM_DEBOUNCE_CHECKS; i++) states = states & raw_button_states[i];

Page 15: Lab 2_5

A for loop compares raw button state values and current switch button values .

button_states = states;

Updates the left and right switch button states.

Problem 6

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> ");GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 2);}

}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)) {UARTprintf("Right button pushed down.\n> ");}if((saved_button_states & RIGHT_BUTTON) && (~cur_button_states &

RIGHT_BUTTON)) {UARTprintf("Right button released.\n> ");GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 4);}if(cur_button_states == (LEFT_BUTTON | RIGHT_BUTTON)) {UARTprintf("Both buttons held down.\n> ");GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 8);}

saved_button_states = cur_button_states;}

// Timer0 interrupt service routinevoid Timer0_ISR(void){

// Clear the timer interrupt.TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

Page 16: Lab 2_5

// Blink blue LED.// Read the current state of GPIO pins and write back the opposite

state.if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3)) {

GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);

Summary and ConclusionsThe Tiva C Series TM4C123G microcontroller has attained better precision, durability, power management, and interrupt control. 32-bit architecture and unaligned data access delivers a fast processing environment. These improvements set a new tone for micro processing and creative environments.

References Texas Instruments. (2014). TivaWare Peripheral Driver Library: User's Guide. Austin, Texas: TI.