Part Number: TM4C123GH6PM
Other Parts Discussed in Thread: EK-TM4C1294XL, EK-TM4C123GXL
Tool/software: Code Composer Studio
Hello,
Im try to calculate time duration when pin is on high level. My idea is to use interrupt function which will indicate when pin become high and same interrupt function will indicate when pin go back to low level. I will collect information about time in buffer (first element is time when pin become high and second element is time which is time when pin back to low level). For that purpose I create timer which every 1uS increase variable Microseconds. So for some reason when i disconnect wire from pin, program go in interrupt function some times(program go once in interrupt function). When pin is connected to device (HCSR 04) program always go 2 times in interrupt function whitout calling and then work properly. So my question is what can cause that unpredictable behavior?
/*
* HCSR04.c
*
* Created on: Dec 11, 2019
* Author: suba
*/
#include "Standard_Library.h"
#include "TM4C123GH6PM_API.h"
#include "HCSR04.h"
//*************************************** HARDWARE CONNECTION *****************************************************
//
// PE3 - Trigger pin - ORANGE
// PE4 - Echo pin - YELLOW
//
//*****************************************************************************************************************
volatile double DistanceInCentimeters = 0;
volatile uint32_t BufferForTimes[2];
volatile uint32_t TimeHCSR04 = 0; // timer precision is 1uS
volatile uint8_t EchoPinHowManyTimesChangedState = 0;
volatile uint32_t MicroSeconds = 0;
//****************************************************************************************************************
//
// Configure peripherals for HCSR04
//
//****************************************************************************************************************
void HCSR04_Initialize(void)
{
//
// Configure TRIGGER and ECHO pins
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, TRIGGER_PIN);
GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, ECHO_PIN);
GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, ~TRIGGER_PIN);
GPIOPinWrite(GPIO_PORTE_BASE, ECHO_PIN, ~ECHO_PIN);
GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, ECHO_PIN);
GPIOPadConfigSet(GPIO_PORTE_BASE, ECHO_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPD); // pull down resistor , 2mA max
IntPrioritySet(INT_GPIOE, 1);
GPIOIntEnable(GPIO_PORTE_BASE, ECHO_PIN);
GPIOIntTypeSet(GPIO_PORTE_BASE, ECHO_PIN, GPIO_BOTH_EDGES);
IntRegister(INT_GPIOE, HCSR04_Read_Distance_Int_Handler);
IntEnable(INT_GPIOE);
//
// Configure timer
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
TimerConfigure(TIMER2_BASE, TIMER_CFG_PERIODIC);
TimerLoadSet(TIMER2_BASE, TIMER_A, SysCtlClockGet() / 1000000); // 1uS
TimerEnable(TIMER2_BASE, TIMER_A);
IntPrioritySet(INT_TIMER5A, 4);
TimerIntEnable(TIMER2_BASE, TIMER_TIMA_TIMEOUT);
TimerIntRegister(TIMER2_BASE, TIMER_A, Microseconds_Int_Handler);
IntEnable(INT_TIMER2A);
}
//****************************************************************************************************************
//
// Increase variable MicroSeconds for 1 every 1uS
//
//****************************************************************************************************************v
void Microseconds_Int_Handler(void)
{
uint32_t Timer2InterruptStatus = 0;
Timer2InterruptStatus = TimerIntStatus(TIMER2_BASE, true);
TimerIntClear(TIMER2_BASE, Timer2InterruptStatus);
MicroSeconds++;
}
//****************************************************************************************************************
//
// Trigger HCSR04
// When use this function in burst mode use more then 60mS delay between 2 shoots (Typical delay ~ 75mS )
//
//****************************************************************************************************************
void Trigger_HCSR04(void)
{
GPIOPinWrite(GPIO_PORTE_BASE, TRIGGER_PIN, TRIGGER_PIN);
SysCtlDelay(570/3); // provide 10uS high on trigger pin
GPIOPinWrite(GPIO_PORTE_BASE, TRIGGER_PIN, ~TRIGGER_PIN);
}
//****************************************************************************************************************
//
// This function will be called 2 times for 1 measurement
// Function will be called first time when is detected high on ECHO_PIN
// Function will be called second time when is detected low on ECHO_PIN
// Each time when function is called, collect info about time and move that value in BufferForTimes[]
// Difference between two function calls is time which is used for calculating distance
//
//****************************************************************************************************************
void HCSR04_Read_Distance_Int_Handler(void)
{
uint32_t FlagStatus = 0;
FlagStatus = GPIOIntStatus(GPIO_PORTE_BASE, true);
GPIOIntClear(GPIO_PORTE_BASE, FlagStatus);
BufferForTimes[EchoPinHowManyTimesChangedState] = MicroSeconds;
EchoPinHowManyTimesChangedState++;
//
// Check is limit reached
// If this function is called two times then distance can be calculated
// Reset counter to 0 which count how many times is function called
//
if(EchoPinHowManyTimesChangedState == ECHO_PIN_HOW_MANY_TIMES_CHANGE_STATE_LIMIT)
{
EchoPinHowManyTimesChangedState = 0;
TimeHCSR04 = BufferForTimes[1] - BufferForTimes[0];
DistanceInCentimeters = (SPEED_OF_SOUND * TimeHCSR04) / 2; // formula for calculating distance
}
}
int j = 0;
void main(void)
{
SysCtlClockSet(SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ | SYSCTL_SYSDIV_4); // clock set 50MHz
IntMasterEnable();
HCSR04_Initialize();
while(1)
{
if(j == 1)
{
Trigger_HCSR04();
}
SysCtlDelay(1200000); //72mS delay
}
}