Hi,
Can we use TimerValueGet () to obtain the HIGH pulse which act like arduino pulseIn()?
This is used for HC-SR04 Ultrasonic Sensor.
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hello 311Nice,
The Timer only captures edges. A call of TimerValueGet will not give the High Pulse Width.
To get the pulse width, you will have to set it rising edge capture. On Interrupt get the timer value and set it for falling edge. On second interrupt again get the Timer Value.and set it back to Rising edge for the next pulse.
The difference of the two timer values will give you the pulse width
Please do make sure that the Timer in Edge Time mode is a 16-bit timer so the events of rise and fall should happen within 2^16 system clocks. Otherwise pre scalar would be needed as well.
Regards
Amit
//initialise system
void main(){
//configure oscillator & timer
SysCtlClockSet(SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN|SYSCTL_USE_PLL|SYSCTL_SYSDIV_5); //set timer to 40Mhz
SysCtlPeripheralEnable(echo_timer_en); //enable timer
TimerConfigure(echo_timer,echo_con); //configure timer
TimerPrescaleSet(echo_timer,TIMER_A,40); //adjust timer speed
TimerLoadSet(echo_timer,TIMER_A,65000); //set period
TimerDisable(echo_timer,TIMER_A); //disable clock
//configure pin
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF|sensor_port_en); //enable port e for sensor & port f for LED
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, 0x0E); //configure led output
GPIOPinTypeGPIOOutput(sensor_port,trig_pin); //configure trig output
//configure interrupt
GPIOIntEnable(sensor_port, echo_pin); //configure echo as gpio interrupt
GPIOIntTypeSet(sensor_port, echo_pin, GPIO_BOTH_EDGES); //to detect both edges for echo to measure pulse
IntEnable(sensor_port); //enable int
IntMasterEnable();
while(1){
trigger();
//calculation
distance = (65000 - pulse)/58;
if (distance < 2){
GPIOPinWrite(GPIO_PORTF_BASE,0x0E,0x02);
}else {
GPIOPinWrite(GPIO_PORTF_BASE,0x0E,0x04);
}
}
}
//initialise trigger pin
void trigger(){
//check echo status
if (checkecho ==0){
GPIOPinWrite(sensor_port,trig_pin,1); //set trig high
SysCtlDelay(trig_delay*SysCtlClockGet()/400); //delay 10us based on datasheet
}
}
//check echo pulse
void GPIOEIntHandler(){
GPIOIntClear(sensor_port,echo_pin); //clear interrupt flag
echo = GPIOPinRead(sensor_port,echo_pin);
if (echo==1){
TimerEnable(echo_timer,TIMER_A); //start timer to record
checkecho = 1; //set checkecho 1 to prevent new trigger signal
}else{
TimerDisable(echo_timer,TIMER_A); //stop timer
pulse = TimerValueGet(echo_timer,TIMER_A); //record value
}
}
This is my testing code. Did I configure the timer coding correctly?
Any mistake on it?
Hello 311Nice
Yes, there are a few.
1. GPIOPinRead on return will return the value in the Data Register. So the comparison has to be made as echo == echo_pin
2. GPIOPinWrite for setting and clearing a pin has to be done
GPIOPinWrite(sensor_port,trig_pin,trig_pin);
GPIOPinWrite(sensor_port,trig_pin,~(trig_pin));
3.What is the div by 58 factor for?
4. IntEnable will not have the Port Address but the Port Interrupt
5. SysCtlPeripheralEnable can accept only one peripheral at a time. Logical operations are prohibited.
Regards
Amit
Hello 311Nice,
The UART has to be initialized. That is not done in the code. Include the header file and the code below. After that UARTprintf should work.
#include "utils/uartstdio.h"
void
InitConsole(void)
{
//
// Enable GPIO port A which is used for UART0 pins.
// TODO: change this to whichever GPIO port you are using.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Configure the pin muxing for UART0 functions on port A0 and A1.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
//
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
//
// Enable UART0 so that we can configure the clock.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Use the internal 16MHz oscillator as the UART clock source.
//
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
//
// Select the alternate (UART) function for these pins.
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Initialize the UART for console I/O.
//
UARTStdioConfig(0, 115200, 16000000);
}
Regards
Amit
Hello 311Nice,
Can you please share the code so that I can give points on troubleshooting?
Regards
Amit
//initialise system
void main(){
//configure oscillator & timer
SysCtlClockSet(SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN|SYSCTL_USE_PLL|SYSCTL_SYSDIV_5); //set timer to 40Mhz
SysCtlPeripheralEnable(echo_timer_en); //enable timer
TimerConfigure(echo_timer,echo_con); //configure timer
TimerPrescaleSet(echo_timer,TIMER_A,40); //adjust timer speed
TimerLoadSet(echo_timer,TIMER_A,65000); //set period
TimerDisable(echo_timer,TIMER_A); //disable clock
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTStdioConfig(0, 9600, SysCtlClockGet());
//configure pin
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //enable port e for sensor & port f for LED
SysCtlPeripheralEnable(sensor_port_en);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, 0x0E); //configure led output
GPIOPinTypeGPIOOutput(sensor_port,trig_pin); //configure trig output
GPIOPinTypeGPIOInput(sensor_port,echo_pin); //configure echo input
//configure interrupt
GPIOIntEnable(sensr_port, echo_pin); //configure echo as gpio interrupt
GPIOIntTypeSet(sensor_port, echo_pin, GPIO_BOTH_EDGES); //to detect both edges for echo to measure pulse
IntEnable(IN_GPIOE); //enable int
IntMasterEnable();
while(1){
trigger();
//calculation
distance = (65000 - pulse)/58;
if (distance < 2){
GPIOPinWrite(GPIO_PORTF_BASE,0x0E,0x02);
}else {
GPIOPinWrite(GPIO_PORTF_BASE,0x0E,0x04);
}
UARTprintf("Hello I am Tiva C Series LaunchPad!\n",distance);
}
}
//initialise trigger pin
void trigger(){
//check echo status
if (checkecho ==0){
GPIOPinWrite(sensor_port,trig_pin,trig_pin); //set trig high
SysCtlDelay(trig_delay*SysCtlClockGet()/60); //delay 10us based on datasheet
}
}
//check echo pulse
void GPIOEIntHandler(){
GPIOIntClear(sensor_port,echo_pin); //clear interrupt flag
echo = GPIOPinRead(sensor_port,echo_pin);
if (echo==echo_pin){
TimerEnable(echo_timer,TIMER_A); //start timer to record
checkecho = 1; //set checkecho 1 to prevent new trigger signal
}else{
TimerDisable(echo_timer,TIMER_A); //stop timer
pulse = TimerValueGet(echo_timer,TIMER_A); //record value
}
}
Here you go.
Hello 311Nice,
You need to add %d as shown below.
UARTprintf("Hello I am Tiva C Series LaunchPad! %d\n",distance);
Also you are using Baud Rate of 9600. Please make sure that device manager of the PC and the Terminal application are also configured for 9600.
Regards
Amit
Hello 311Nice,
I am not sure of Arduino setup to emulate it. But if the pulseIn is coming from Arduino, then you may want to make sure that the trigger function is over before the Input Pulse comes in, otherwise there may be some problem with the timing calculated.
Regards
Amit
Hello 311Nice,
The trigger function is in a while loop and it keeps on executing and it could be possible the trigger and the input pulse at some point may come together and messing up the computation.
Basically after the trigger do make sure you wait for the interrupt to fire before doing anything else.
Regards
Amit