Other Parts Discussed in Thread: TM4C123GH6PM
I'm trying to set up external interrupt on SW1 to count the number of button presses and (depending on if the counter equals 0 or 7) increment the counter variable and turn on a combination of LEDs, but pressing the button seem to have no effect - setting counter to 1 turns on the green LED, but stays there no mather how many times I presses the button
Although I've been giving a different header file to use in the course I'm taking, I've double checked the registers and should match what is written in the datasheet, so can anyone see what may be wrong with my code?
The code:
#include <stdint.h>
#include "tm4c123gh6pm.h"
#if 0
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <inc/hw_types.h>
#include <inc/hw_gpio.h>
#include <inc/hw_memmap.h>
#include <driverlib/gpio.h>
#include <driverlib/rom.h>
#include <driverlib/sysctl.h>
#include <inc/hw_sysctl.h>
#include <driverlib/pin_map.h>
#include <driverlib/timer.h>
#include "clib.h"
#endif
int counter = 0, state = 0, mode = 0;
void init_registers() {
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOF; // From datasheet: "When set, indicates that GPIO Port F is present"
int wasting_time = SYSCTL_RCGC2_R;
SYSCTL_RCGCGPIO_R |= 0x20; // From datasheet (when the 5th bit is set to 1): "Enable and provide a clock to GPIO Port F in Run mode"
GPIO_PORTF_DIR_R |= 0x0E; // LED as output; E = 1110 -> 1st bit (red), 2nd bit (blue) & 3rd bit (green)
GPIO_PORTF_DEN_R |= 0x1E; // DEN = Digital ENable, here enabling GPIO pins PF1 - PF4
GPIO_PORTF_PUR_R |= 0x10; // Pull-up resistor enabled for switch, PF4
// Table 10-4 have a good overview of the interrupt registers
GPIO_PORTF_IS_R &= ~(0x10); // Enabling interrupt sense for switch, PF4; by default it detects edges (rising/falling), but by setting the bit, it detects levels (high/low)
GPIO_PORTF_IBE_R &= ~(0x10); // Cleared by default, only included here to mention that if the bit is not cleared for the pin, it ignores configuring the register below
// and instead interrupt on both rising/falling edges (high/low levels)
GPIO_PORTF_IEV_R &= ~(0x10); // Interrupt on falling edge / low levels for switch (thought it should be rising levels at first, but that just always triggered the interrupt)
GPIO_PORTF_IM_R |= 0x10; // Setting the bit allows interrupts to "be sent to the interrupt controller on the combined interrupt signal" (from datasheet)
GPIO_PORTF_ICR_R |= 0x10; // Clears any previous interrupts on pin PF4
// Check table 2-9 on the datasheet for the interrupt table, there you can see that the interrupt number for PORTF is 30
NVIC_PRI7_R |= (3 << 21); // PDF page 151 in datasheet; setting interrupt priority to 3
NVIC_EN0_R |= (1 << 30); // Shifts 00000....1 (30 bits in total) 30 times to the left, setting the 30th bit for enabling PORTF interrupt
}
void delay_timer() {
}
void counter_check() {
if (counter == 0) {
GPIO_PORTF_DATA_R = 0x00;
}
else if (counter == 1) {
GPIO_PORTF_DATA_R = 0x08;
}
else if (counter == 2) {
GPIO_PORTF_DATA_R = 0x04;
}
else if (counter == 3) {
GPIO_PORTF_DATA_R = 0x0C;
}
else if (counter == 4) {
GPIO_PORTF_DATA_R = 0x02;
}
else if (counter == 5) {
GPIO_PORTF_DATA_R = 0x0A;
}
else if (counter == 6) {
GPIO_PORTF_DATA_R = 0x06;
}
else if (counter == 7) {
GPIO_PORTF_DATA_R = 0x0E;
}
}
int main(void) {
init_registers();
while(1) {
if (mode == 1) {
delay_timer();
}
counter_check();
}
return 0;
}
void GPIOF_Handler(void) {
if (state == 0) {
++counter;
}
else if (state == 1) {
--counter;
}
if (counter >= 7 && state == 0) {
state = 1;
}
else if (counter == 0 && state == 1) {
state = 0;
}
GPIO_PORTF_ICR_R |= 0x10;
}
The header file: https://ufile.io/5dy40ac1 (thought it would take too much space pasting it here)