// EdgeInterrupt.c // Runs on LM3S811 // Request an interrupt on the falling edge of PC4 (when the user // button is pressed) and increment a counter in the interrupt. Note // that button bouncing is not addressed. // Daniel Valvano // June 24, 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 5.6, Section 5.5 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/ */ // user button connected to PC4 (increment counter on falling edge) #define NVIC_EN0_INT2 0x00000004 // Interrupt 2 enable #define NVIC_EN0_R (*((volatile unsigned long *)0xE000E100)) // IRQ 0 to 31 Set Enable Register #define NVIC_PRI0_R (*((volatile unsigned long *)0xE000E400)) // IRQ 0 to 3 Priority Register #define GPIO_PORTC_DIR_R (*((volatile unsigned long *)0x40006400)) #define GPIO_PORTC_IS_R (*((volatile unsigned long *)0x40006404)) #define GPIO_PORTC_IBE_R (*((volatile unsigned long *)0x40006408)) #define GPIO_PORTC_IEV_R (*((volatile unsigned long *)0x4000640C)) #define GPIO_PORTC_IM_R (*((volatile unsigned long *)0x40006410)) #define GPIO_PORTC_ICR_R (*((volatile unsigned long *)0x4000641C)) #define GPIO_PORTC_DEN_R (*((volatile unsigned long *)0x4000651C)) #define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108)) #define SYSCTL_RCGC2_GPIOC 0x00000004 // port C Clock Gating Control void DisableInterrupts(void); // Disable interrupts void EnableInterrupts(void); // Enable interrupts long StartCritical (void); // previous I bit, disable interrupts void EndCritical(long sr); // restore I bit to previous value void WaitForInterrupt(void); // low power mode // global variable visible in Watch window of debugger // increments at least once per button press volatile unsigned long FallingEdges = 0; void EdgeCounter_Init(void){ SYSCTL_RCGC2_R |= 0x00000004; // (a) activate port C FallingEdges = 0; // (b) initialize counter GPIO_PORTC_DIR_R &= ~0x10; // (c) make PC4 in GPIO_PORTC_DEN_R |= 0x10; // enable digital I/O on PC4 GPIO_PORTC_IS_R &= ~0x10; // (d) PC4 is edge-sensitive GPIO_PORTC_IBE_R &= ~0x10; // PC4 is not both edges GPIO_PORTC_IEV_R &= ~0x10; // PC4 falling edge event GPIO_PORTC_ICR_R = 0x10; // (e) clear flag4 GPIO_PORTC_IM_R |= 0x10; // (f) arm interrupt on PC4 NVIC_PRI0_R = (NVIC_PRI0_R&0xFF00FFFF)|0x00A00000; // (g) priority 5 NVIC_EN0_R |= 4; // (h) enable interrupt 2 in NVIC EnableInterrupts(); // (i) Program 5.3 } void GPIOPortC_Handler(void){ GPIO_PORTC_ICR_R = 0x10; // acknowledge flag4 FallingEdges = FallingEdges + 1; } //debug code int main(void){ EdgeCounter_Init(); // initialize GPIO Port C interrupt while(1){ WaitForInterrupt(); } }