I am trying to capture the time between falling and rising edge of a switch, the following code gets stuck on the line :
while((GPTMRIS_R & 0x04)==0){}; //Wait till captured
Any help ?
Sorry for the messy code
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC)) #define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400)) #define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420)) #define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025510)) #define GPIO_PORTF_PDR_R (*((volatile unsigned long *)0x40025514)) #define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C)) #define GPIO_PORTF_LOCK_R (*((volatile unsigned long *)0x40025520)) #define GPIO_PORTF_CR_R (*((volatile unsigned long *)0x40025524)) #define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528)) #define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C)) #define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108)) #define NVIC_ST_CTRL_R (*((volatile unsigned long *)0xE000E010)) #define NVIC_ST_RELOAD_R (*((volatile unsigned long *)0xE000E014)) #define NVIC_ST_CURRENT_R (*((volatile unsigned long *)0xE000E018)) #define RCGCTIMER_R (*((volatile unsigned long *)0x400FE604)) #define GPTMCTL_R (*((volatile unsigned long *)0x4003000C)) #define GPTMCFG_R (*((volatile unsigned long *)0x40030000)) #define GPTMTAMR_R (*((volatile unsigned long *)0x40030004)) #define GPTMICR_R (*((volatile unsigned long *)0x40030024)) #define GPTMRIS_R (*((volatile unsigned long *)0x4003001C)) #define GPTMTAR_R (*((volatile unsigned long *)0x40030048)) void Timer0_Init(void); void SysTick_Init(void); void SysTick_Wait(unsigned long delay); void SysTick_Wait10ms(unsigned long delay); // Function Prototypes void PortF_Init(void); void Delay(void); void EnableInterrupts(void); // 3. Subroutines Section int main(void){ PortF_Init(); SysTick_Init(); unsigned long RisingEdge; unsigned long FallingEdge; unsigned long time; while(1){ GPTMICR_R = 0x04; while((GPTMRIS_R & 0x04)==0){}; //Wait till captured RisingEdge = GPTMTAR_R; //Time of rising edge GPTMICR_R =4; //clear timer capture flag while((GPTMRIS_R & 4)==0){}; //Wait till captured FallingEdge = GPTMTAR_R; //Time of falling edge time = FallingEdge - RisingEdge; time = time * 80000000; } } void PortF_Init(void){ volatile unsigned long delay; SYSCTL_RCGC2_R |= 0x00000020; // 1) F clock delay = SYSCTL_RCGC2_R; // delay GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock PortF PF0 GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0 GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog function GPIO_PORTF_PCTL_R = 0x00000000; // 4) GPIO clear bit PCTL GPIO_PORTF_DIR_R = 0x0E; // 5) GPIO_PORTF_AFSEL_R = 0x00; // 6) no alternate function GPIO_PORTF_PUR_R = 0x11; // enable pullup resistors on PF4,PF0 GPIO_PORTF_AFSEL_R |= 0x01; // 5) alternate for PF0 GPIO_PORTF_PCTL_R &=~0x0000000F; GPIO_PORTF_PCTL_R |= 0x00000007; GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital pins PF4-PF0 } void SysTick_Init(void){ NVIC_ST_CTRL_R = 0; // disable SysTick during setup NVIC_ST_CTRL_R = 0x00000005; // enable SysTick with core clock } // The delay parameter is in units of the 80 MHz core clock. (12.5 ns) void SysTick_Wait(unsigned long delay){ NVIC_ST_RELOAD_R = 80*delay-1; // number of counts to wait NVIC_ST_CURRENT_R = 0; // any value written to CURRENT clears while((NVIC_ST_CTRL_R&0x00010000)==0){ // wait for count flag } } void SysTick_Wait10ms(unsigned long delay){ unsigned long i; for(i=0; i<delay; i++){ SysTick_Wait(800000); // wait 10ms } } void Timer0_Init(void) { RCGCTIMER_R |= 0x01; //Enable and provide a clock to 16/32-bit general-purpose timer module 0 in Run mode GPTMCTL_R &= ~0x01; //Timer A is disabled GPTMCFG_R = 0x4; //For a 16/32-bit timer, this value selects the 16-bit timer configuration GPTMTAMR_R |= 0x17; //Capture mode , Edge-Time mode , Capture or compare mode is enabled, The timer counts up. When counting up, the timer starts from a //value of 0x00 GPTMCTL_R |= 0x0C; //Timer is sensetive to both edges GPTMCTL_R |= 0x01; //Enable timerA }