Page 1 of 1

Generate us pulse

Posted: Sat Dec 05, 2015 12:17 am
by tfAteba
Hello All, hello Giovanni,

First of all here is my working conditions:
ChibiOS version 3.3
Board: STM32F401RE-Nucleo.

My goal:
I want to generate a pulse of 10us width for a distance sensor(srf04) so that I can read the distance every half second for example.

What I did:
Create a thread to generate the pulse(10us) every 500ms

My code:

Code: Select all

 63 /*
 64  * HC-SR04 Trigger thread
 65  */
 66 static THD_WORKING_AREA(waThread2, 128);
 67 static THD_FUNCTION(Thread2, arg){
 68   (void)arg;
 69   
 70   chRegSetThreadName("trigger");
 71   
 72   while(true){
 73     palSetPad(TRIGGER_PORT, TRIGGER_PIN);
 74     chThdSleepMicroseconds(10);
 75     palClearPad(TRIGGER_PORT, TRIGGER_PIN);
 76     chThdSleepMilliseconds(500);
 77  }
 78 }


The result:
I can read the distance with the sensor but my trigger signal ( the pulse of 10us) is larger than 10us. The measure with an oscilloscope, gave me 200us.

Is it really the right method to generate a pulse width on a thread?
Is there another way to generate that signal for example by using timers?
Why did the function chThdSleepMicrosecond don't give me the right width?

Thanks you for your help

Theo.

Re: Generate us pulse

Posted: Sat Dec 05, 2015 8:22 am
by Giovanni
Hi,

chThdSleepMicroseconds() is rounded up to the next tick boundary, if your system tick is set to 10kHz then resolution is set to 100uS. You may increase system tick to 1MHz but you will have jitter of several microseconds, you will never get exactly 10uS.

Alternatively use the PWM driver.

Giovanni

Re: Generate us pulse

Posted: Sat Dec 05, 2015 10:55 am
by tfAteba
Hello Giovanni,

I changed the System Tick frequency, and I had a better result even if there was a jitter of 2us maximum.

Now the chThdSleepMicroseconds(10) give me maximum 12us, It is better than the 200us I had before.

Later I will test by using the PWM Driver as you suggest.

Why did you set the system tick frequency at 10KHz by default? is it a particular reason?

Thank you very much

Theo

Re: Generate us pulse

Posted: Sat Dec 05, 2015 11:08 am
by Giovanni
Increasing it reduces the maximum time specifiable in timeouts, the system time is only 32 bits wide.

Giovanni

Re: Generate us pulse

Posted: Sun Dec 06, 2015 12:47 pm
by tfAteba
I understand now, thank you for your help.