// ParallelKeypad.c // Runs on LM3S811 // Use a busy-wait loop to wait for a rising edge and then return the // parallel data from the keypad. // Daniel Valvano // June 16, 2011 /* This example accompanies the book "Embedded Systems: Real Time Interfacing to the Arm Cortex M3", ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2011 Program 4.7, Example 4.2, Figure 4.32, Section 4.8 Copyright 2011 by Jonathan W. Valvano, valvano@mail.utexas.edu You may use, edit, run or distribute this file as long as the above copyright notice remains THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. For more information about my classes, my research, and my books, see http://users.ece.utexas.edu/~valvano/ */ // strobe connected to PC4 (rising edge means keypad pressed) // keypad data bit 6 connected to PD6 // keypad data bit 5 connected to PD5 // keypad data bit 4 connected to PD4 // keypad data bit 3 connected to PD3 // keypad data bit 2 connected to PD2 // keypad data bit 1 connected to PD1 // keypad data bit 0 connected to PD0 #define GPIO_PORTC_DIR_R (*((volatile unsigned long *)0x40006400)) #define GPIO_PORTC_IEV_R (*((volatile unsigned long *)0x4000640C)) #define GPIO_PORTC_RIS_R (*((volatile unsigned long *)0x40006414)) #define GPIO_PORTC_ICR_R (*((volatile unsigned long *)0x4000641C)) #define GPIO_PORTC_DEN_R (*((volatile unsigned long *)0x4000651C)) #define GPIO_PORTD_DATA_R (*((volatile unsigned long *)0x400073FC)) #define GPIO_PORTD_DIR_R (*((volatile unsigned long *)0x40007400)) #define GPIO_PORTD_DEN_R (*((volatile unsigned long *)0x4000751C)) #define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108)) #define SYSCTL_RCGC2_GPIOD 0x00000008 // port D Clock Gating Control #define SYSCTL_RCGC2_GPIOC 0x00000004 // port C Clock Gating Control void Keypad_Init(void){ volatile unsigned long delay; // activate port C and port D SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOC+SYSCTL_RCGC2_GPIOD; delay = SYSCTL_RCGC2_R; // allow time to finish activating GPIO_PORTC_DIR_R &= ~0x10;// make PC4 in (PC4 built-in button) GPIO_PORTC_DEN_R |= 0x10; // enable digital I/O on PC4 GPIO_PORTD_DIR_R &= ~0x7F;// make PD6-0 in (PD6-0 data) GPIO_PORTD_DEN_R |= 0x7F; // enable digital I/O on PD6-0 GPIO_PORTC_IEV_R |= 0x10; // rise on PC4 GPIO_PORTC_ICR_R = 0x10; // clear flag4 } unsigned long Keypad_In(void){ while((GPIO_PORTC_RIS_R&0x10)==0); // 1)wait GPIO_PORTC_ICR_R = 0x10; // clear flag4 return(GPIO_PORTD_DATA_R&0x7F); // 2) read Data } //debug code #define GPIO_PORTC5 (*((volatile unsigned long *)0x40006080)) volatile unsigned long input; int main(void){ Keypad_Init(); // initialize ports GPIO_PORTC_DIR_R |= 0x20; // make PC5 out (PC5 built-in LED) GPIO_PORTC_DEN_R |= 0x20; // enable digital I/O on PC5 while(1){ GPIO_PORTC5 = 0x00; // turn off LED input = Keypad_In(); GPIO_PORTC5 = 0x20; // turn on LED input = Keypad_In(); } }