Hallo community,
Sine its my first time using the Launchpad im trying to figure out how interrupt controled timing works.
So basically i want to measure time from when I turn on a Led to where i press a button.
I have understood the basics of timers and I cant get it to calculate the time properly.
Here is what I did in Code:
Note, The turning on and off of the LEDS is for debug reasons only.
---------------------------------------------------------------------------------------------------------------------------------------
#include <stdint.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include "inc/hw_ssi.h" #include "inc/hw_types.h" #include "driverlib/ssi.h" #include "inc/tm4c1294ncpdt.h" #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "inc/hw_memmap.h" #include "driverlib/sysctl.h" #include "driverlib/interrupt.h" #include "driverlib/systick.h" #include "driverlib/sysctl.h" #include "driverlib/adc.h" #include "driverlib/uart.h" #include "time.h" #include "driverlib/timer.h" #define TIMEOUT 0xFFFFFFFF #define F_CPU 16000000 // 16MHz volatile uint32_t ButtonTime = 0; volatile uint32_t ButtonTimeinms = 0; void ISR_Button_interrupt(void) { GPIOIntClear(GPIO_PORTJ_BASE,GPIO_PIN_0); TimerDisable(TIMER1_BASE,TIMER_BOTH); ButtonTime=TIMEOUT-TimerValueGet(TIMER1_BASE, TIMER_A); ButtonTimeinms = (ButtonTime*1000) / F_CPU; TimerLoadSet(TIMER1_BASE,TIMER_A,TIMEOUT); } void init_interrupt(void) { GPIOIntRegister(GPIO_PORTJ_BASE, ISR_Button_interrupt); GPIOIntTypeSet(GPIO_PORTJ_BASE, GPIO_PIN_0, GPIO_FALLING_EDGE); GPIOIntEnable(GPIO_PORTJ_BASE, GPIO_PIN_0); } void init(void) { uint32_t oldpadconfig_strength; uint32_t oldpadconfig_type; SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); TimerClockSourceSet(TIMER1_BASE,TIMER_CLOCK_SYSTEM); TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC); TimerUpdateMode(TIMER1_BASE,TIMER_A,TIMER_UP_LOAD_IMMEDIATE); TimerLoadSet(TIMER1_BASE,TIMER_A,TIMEOUT); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ); GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, GPIO_PIN_0); GPIOPadConfigGet(GPIO_PORTJ_BASE, GPIO_PIN_0,&oldpadconfig_strength,&oldpadconfig_type); GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_0,oldpadconfig_strength,GPIO_PIN_TYPE_STD_WPU); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4); } uint8_t GetUsrSW1(void) { return (uint8_t) GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_0); } void delay(int s) { SysCtlDelay(s*40000); /*in milliseconds*/ } void main(void) { int status = 0; init(); init_interrupt(); if (GetUsrSW1()== 1) { status = 1; GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_0,1); while (GetUsrSW1()==1); } while (status == 1){ GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_0,0); delay(250); GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_0,1); TimerEnable(TIMER1_BASE,TIMER_BOTH); delay(1000); GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_0,0); delay(100); GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_0,1); delay(100); } }