Generate us pulse Topic is solved

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
User avatar
tfAteba
Posts: 551
Joined: Fri Oct 16, 2015 11:03 pm
Has thanked: 91 times
Been thanked: 50 times

Generate us pulse

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Generate us pulse

Post 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
User avatar
tfAteba
Posts: 551
Joined: Fri Oct 16, 2015 11:03 pm
Has thanked: 91 times
Been thanked: 50 times

Re: Generate us pulse

Post 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
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Generate us pulse

Post by Giovanni »

Increasing it reduces the maximum time specifiable in timeouts, the system time is only 32 bits wide.

Giovanni
User avatar
tfAteba
Posts: 551
Joined: Fri Oct 16, 2015 11:03 pm
Has thanked: 91 times
Been thanked: 50 times

Re: Generate us pulse

Post by tfAteba »

I understand now, thank you for your help.
Post Reply