+/EE445L
embedded systems
git clone https://git.julia.blue/+/EE445L
c12dcba
master
1// LED.c 2// Runs on TM4C123 3// Provide functions that initialize a GPIO as an input pin and 4// allow reading of two negative logic switches on PF0 and PF4 5// Output to LEDs 6// Use bit-banded I/O. 7// Daniel and Jonathan Valvano 8// Feb 23, 2015 9#include "../inc/tm4c123gh6pm.h" 10#include <stdint.h> 11 12/* This example accompanies the book 13"Embedded Systems: Introduction to ARM Cortex M Microcontrollers", 14ISBN: 978-1469998749, Jonathan Valvano, copyright (c) 2014 15Section 4.2 Program 4.2 16 17"Embedded Systems: Real Time Interfacing to ARM Cortex M Microcontrollers", 18ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2014 19Example 2.3, Program 2.9, Figure 2.36 20 21Copyright 2015 by Jonathan W. Valvano, valvano@mail.utexas.edu 22You may use, edit, run or distribute this file 23as long as the above copyright notice remains 24THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED 25OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF 26MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. 27VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, 28OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 29For more information about my classes, my research, and my books, see 30http://users.ece.utexas.edu/~valvano/ 31*/ 32 33#define GPIO_LOCK_KEY 0x4C4F434B // Unlocks the GPIO_CR register 34#define PF0 (*((volatile uint32_t*)0x40025004)) 35#define PF4 (*((volatile uint32_t*)0x40025040)) 36#define SWITCHES (*((volatile uint32_t*)0x40025044)) 37#define SW1 0x10 // on the left side of the Launchpad board 38#define SW2 0x01 // on the right side of the Launchpad board 39#define PF1 (*((volatile uint32_t*)0x40025008)) 40#define PF2 (*((volatile uint32_t*)0x40025010)) 41#define PF3 (*((volatile uint32_t*)0x40025020)) 42 43//------------LED_Init------------ 44// Initialize GPIO Port F for negative logic switches on PF0 and 45// PF4 as the Launchpad is wired. Weak internal pull-up 46// resistors are enabled, and the NMI functionality on PF0 is disabled. 47// LEDs on PF3,2,1 are enabled 48// Input: none 49// Output: none 50void LED_Init (void ) { 51SYSCTL_RCGCGPIO_R |=0x20 ;// 1) activate Port F 52while ((SYSCTL_PRGPIO_R & 0x20 )!= 0x20 ) {};// wait to finish activating 53GPIO_PORTF_LOCK_R = GPIO_LOCK_KEY ;// 2a) unlock GPIO Port F Commit Register 54GPIO_PORTF_CR_R = 0x1F ;// 2b) enable commit for PF4-PF0 55GPIO_PORTF_AMSEL_R &= ~0x1F ;// 3) disable analog functionality on PF4-PF0 56GPIO_PORTF_PCTL_R = 0x00000000 ;// 4) configure PF0-PF4 as GPIO 57GPIO_PORTF_DIR_R = 0x0E ;// 5) make PF0 and PF4 in PF3-1 output 58GPIO_PORTF_AFSEL_R = 0 ;// 6) disable alt funct on PF0 and PF4 59GPIO_PORTF_PUR_R = 0x11 ;// enable weak pull-up on PF0 and PF4 60GPIO_PORTF_DEN_R = 0x1F ;// 7) enable digital I/O on PF0-PF4 61} 62 63//------------Board_Input------------ 64// Read and return the status of the switches. 65// Input: none 66// Output: 2 if only Switch 1 is pressed 67// 1 if only Switch 2 is pressed 68// 3 if both switches are pressed 69// 0 if no switches are pressed 70uint32_t Board_Input (void ) { 71return (~((PF4 >>3 )+ PF0 ))& 0x03 ; 72} 73//------------LED_RedOn------------ 74// Turn on red LED 75// Input: none 76// Output: none 77void LED_RedOn (void ) { 78PF1 = 0x02 ; 79} 80//------------LED_RedOff------------ 81// Turn off red LED 82// Input: none 83// Output: none 84void LED_RedOff (void ) { 85PF1 = 0x00 ; 86} 87//------------LED_RedToggle------------ 88// Toggle redv LED 89// Input: none 90// Output: none 91void LED_RedToggle (void ) { 92PF1 ^=0x02 ; 93} 94//------------LED_BlueToggle------------ 95// Toggle blue LED 96// Input: none 97// Output: none 98void LED_BlueToggle (void ) { 99PF2 ^=0x04 ; 100} 101//------------LED_GreenToggle------------ 102// Toggle green LED 103// Input: none 104// Output: none 105void LED_GreenToggle (void ) { 106PF3 ^=0x08 ; 107} 108//------------LED_GreenOn------------ 109// Turn on green LED 110// Input: none 111// Output: none 112void LED_GreenOn (void ) { 113PF3 = 0x08 ; 114} 115//------------LED_GreenOff------------ 116// Turn off green LED 117// Input: none 118// Output: none 119void LED_GreenOff (void ) { 120PF3 = 0x00 ; 121}