alex31 wrote:I have implemented a driver (chibios 2.6) for the HC-SR04 based on PWM for pulse generation and ICU for response measure,
it works well without hacking CH_FREQUENCY, and with minimal cpu usage.
Can post the source if interested.
Alexandre
We will appreciate!
Can you check also my code?
for PWM configuration i've used
Code: Select all
static PWMConfig pwmcfg = {
100000, /* 100kHz=10us PWM clock frequency. */
2, /* Initial PWM period 20uS. */
pwmpcb,
{
{PWM_OUTPUT_ACTIVE_HIGH, pwmc1cb},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL}
},
0,
0
};for ICU
Code: Select all
static ICUConfig icucfg = {
ICU_INPUT_ACTIVE_HIGH,
100000, /* 100kHz ICU clock frequency. */
icuwidthcb,
icuperiodcb,
NULL,
ICU_CHANNEL_1,
0
};
and in the main
Code: Select all
pwmStart(&PWMD1, &pwmcfg);
palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(1));
icuStart(&ICUD3, &icucfg);
palSetPadMode(GPIOC, 6, PAL_MODE_ALTERNATE(2));
pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
icuStartCapture(&ICUD3);
pwmDisableChannel(&PWMD1, 0);
pwmStop(&PWMD1);
icuStopCapture(&ICUD3);
icuStop(&ICUD3);
Giovanni wrote:Then there some other problem that is not immediately apparent to me. I can suggest to keep the code simple and add things one by one, that code looks very convoluted for the simple thing you are trying to accomplish.
If you need to measure an interval then you just need the system time.
1) Generate the pulse.
2) Read time.
3) Wait for the raising front (polling, not EXT).
4) Read time again and make the difference.
Once the simple thing works then you may try a more complex approach.
Giovanni
You gave me the same suggestion, but i used get time function and get elapsed time function in this way:
Code: Select all
palSetPad(port, pad);
chThdSleepMicroseconds(10); //this time is taken by sensor datasheet
palClearPad(port, pad);
deltatime=chVTGetSystemTime();
while(Signal echo is low)
chVTTimeElapsedSinceX(deltatime);
and which is the difference between chVTGetSystemTime and chVTGetSystemTimeX