PSoC 3 Timer Interrupt for ultrasonic sensor

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

 Hi.

   

I've built a circuit which transmits and recieves ultrasonic waves. It has an input (controlling the tx - normally pulsed at 40kHz), and an output (logic high or low - if reflected pulse detected).

   

I am driving the tx from a PWM(1) at 40kHz, I am then controlling the on time of PWM(1) with a second PWM(2).

   

ie, PWM(1) is outputting 40kHz for 0.5ms and is off for 64.5ms.

   

Now, I would like to time the time taken for the reflected signal to occur. I've thought about using an interupt and then reading the PWM value and converting it into a time, or using a timer.

   

Please could you point me in the right direction, as I am unsure wheather this is best treated in software or hardware (trigger and capture pins on the timers/pwms).

   

Many Thanks

0 Likes
4 Replies
Anonymous
Not applicable

Hi Graeme44,

   

 

   

You can use another Timer for measuring the time taken for the signal to reach back after reflection.

   

1) The Timer has to be used in Capture mode.

   

2) Hardware Trigger should be enabled for this Timer. When the PWM(1) starts Transmission, trigger the Timer so that the Timer starts Counting.

   

3) The output pin (reflected pulse indicator) should be connected to the Capture input.

   

4) When the reflected signal is received, the Timer's value is Captured. This can be read using an ISR. The counts elapsed will give an indication of the time taken by the wave to reach the receiver after reflection.

   

5) If the Clock used for Timer is of reasonably high frequency, better accuracy can be obtained. Eg, If Clock = 1MHz, and it takes 100 counts for the relected wave to reach back, then the time taken is 100 micro second.

   

 

   

Distance travesred can be calculated as the speed of sound in air is known.

0 Likes
Anonymous
Not applicable

 Thank you, I will try that.

   

I still think I may be able to just read the PWM count values instead of using a timer - Will try both ways.

   

Standard interupts don't interupt PWM and timer modules, is there anyway of interupting them during a isr?

   

Thanks

0 Likes
Anonymous
Not applicable

Using ISR is also a possibility. But note that for the ISR code execute, there is certain amount of delay due to the PUSH and POP of various registers. And hence the PWM or Timer read out value may not be precise and accurate. As long as you can afford to have lower accuracy this is fine. As told by dasg in post 2, using the Timer capture mode is more accurate way of implementing this. In this case the output from the Ultrasonic module will trigger a Timer capture in hardware and the value captured is stored irrespective of ISR being executed or not. You can later read this value from a ISR or your normal firmware and get the accurate timing information. Also as was told in comment 2 by dasg, higher the frequency of the Timer input clock, better will be the precision of the timing.

0 Likes
Anonymous
Not applicable

Using ISR is also a possibility. But note that for the ISR code execute, there is certain amount of delay due to the PUSH and POP of various registers. And hence the PWM or Timer read out value may not be precise and accurate. As long as you can afford to have lower accuracy this is fine. As told by dasg in post 2, using the Timer capture mode is more accurate way of implementing this. In this case the output from the Ultrasonic module will trigger a Timer capture in hardware and the value captured is stored irrespective of ISR being executed or not. You can later read this value from a ISR or your normal firmware and get the accurate timing information. Also as was told in comment 2 by dasg, higher the frequency of the Timer input clock, better will be the precision of the timing.

0 Likes