we have a problem with the operation of a sensor HC-SR04.
This is our code:
Code: Select all
/*
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
BaseSequentialStream* chp = (BaseSequentialStream*) &SD2;
static event_source_t HCSR04;
static event_listener_t myel;
/*-----------------------------
* EVENTO CHE VIENE INVOCATO
------------------------------*/
static void extcb1(EXTDriver *extp, expchannel_t channel) {
(void)extp;
(void)channel;
chSysLockFromISR();
chEvtBroadcastFlagsI(&HCSR04, 0);
chSysUnlockFromISR();
}
/*-----------------------------
* STRUTTURA DI CONFIGURAZIONE
------------------------------*/
static const EXTConfig extcfg1 = {
{
{EXT_CH_MODE_DISABLED, NULL}, //0
{EXT_CH_MODE_DISABLED, NULL}, //1
{EXT_CH_MODE_DISABLED, NULL}, //2
{EXT_CH_MODE_DISABLED, NULL}, //3
{EXT_CH_MODE_DISABLED, NULL}, //4
{EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOB, extcb1}, //5
{EXT_CH_MODE_DISABLED, NULL}, //6
{EXT_CH_MODE_DISABLED, NULL}, //7
{EXT_CH_MODE_DISABLED, NULL}, //8
{EXT_CH_MODE_DISABLED, NULL}, //9
{EXT_CH_MODE_DISABLED, NULL}, //10
{EXT_CH_MODE_DISABLED, NULL}, //11
{EXT_CH_MODE_DISABLED, NULL}, //12
{EXT_CH_MODE_DISABLED, NULL}, //13
{EXT_CH_MODE_DISABLED, NULL}, //14
{EXT_CH_MODE_DISABLED, NULL}, //15
{EXT_CH_MODE_DISABLED, NULL}, //16
{EXT_CH_MODE_DISABLED, NULL}, //17
{EXT_CH_MODE_DISABLED, NULL}, //18
{EXT_CH_MODE_DISABLED, NULL}, //19
{EXT_CH_MODE_DISABLED, NULL}, //20
{EXT_CH_MODE_DISABLED, NULL}, //21
{EXT_CH_MODE_DISABLED, NULL} //22
}
};
/*
* LED blinker thread, times are in milliseconds.
*/
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {
(void)arg;
chRegSetThreadName("blinker");
while (true) {
palClearPad(GPIOA, GPIOA_LED_GREEN);
chThdSleepMilliseconds(500);
palSetPad(GPIOA, GPIOA_LED_GREEN);
chThdSleepMilliseconds(500);
}
}
/*
* Application entry point.
*/
/*-------------------------------
* DEFINIZIONE DEI PARAMETRI
* ------------------------------ */
//pin TRIGGER : (D2)
#define T1_PORT GPIOA
#define T1_PIN GPIOA_PIN10
//pin ECO : (D4)
#define E1_PORT GPIOB
#define E1_PIN GPIOB_PIN5
/*-------------------------------
* PROCEDURE PERSONALI
--------------------------------*/
//PROCEDURA DI INIZIALIZZAZIONE
void init_pin(void){
//Definizione delle modalità
palSetPadMode(T1_PORT, T1_PIN, PAL_MODE_OUTPUT_PUSHPULL |
PAL_STM32_OSPEED_HIGHEST);
palSetPadMode(E1_PORT, E1_PIN, PAL_MODE_INPUT_PULLDOWN |
PAL_STM32_OSPEED_HIGHEST);
chEvtObjectInit(&HCSR04);
chEvtRegister(&HCSR04, &myel, 0);
}
//PROCEDURA PER LA LETTURA DEL TEMPO DI PERCORRENZA
float read_time_distance(void){
//definizione delle variabii
systime_t delta_t = 0;
//abbassa il pin
palClearPad(T1_PORT,T1_PIN);
chThdSleepMicroseconds(2);
//registra il tempo di inizio
systime_t start = chVTGetSystemTimeX();
//lancia l'ext
extStart(&EXTD1, &extcfg1);
//Lancia il trigger
palSetPad(T1_PORT,T1_PIN);
chThdSleepMicroseconds(10);
palClearPad(T1_PORT,T1_PIN);
//registra la maschera dell'evento
eventmask_t evento = myel.el_events;
//legge il tempo
if(chEvtWaitAnyTimeout(evento, MS2ST(1000000000))){
//rileva il tempo di esecuzione
delta_t = chVTGetSystemTimeX() - start;
}
else{
delta_t = 0;
}
//delta_t = chVTGetSystemTime() - start;
//disabilita l'ext
//extChannelDisableI(&EXTD1, &extcfg1);
extStop(&EXTD1);
//resituisce il tempo
return (float)delta_t;
}
/*-------------------------------
* VARIABILI GLOBALI
--------------------------------*/
float tempo;
int main(void) {
/*
* System initializations.
* - HAL initialization, this also initializes the configured device drivers
* and performs the board-specific initializations.
* - Kernel initialization, the main() function becomes a thread and the
* RTOS is active.
*/
halInit();
chSysInit();
/*
* Activates the serial driver 2 using the driver default configuration.
*/
sdStart(&SD2, NULL);
/*
* Creates the blinker thread.
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
/*
* Normal main() thread activity, in this demo it does nothing except
* sleeping in a loop and check the button state.
*/
/*--------------------------------------
* FUNZIONI DA ESEGUIRE LA PRIMA VOLTA
---------------------------------------*/
init_pin();
while (true) {
/*-------------------------------------
* LOOP
--------------------------------------*/
//legge la distanza
tempo = read_time_distance();
//fa il break
chprintf(chp, "%f\n\r", tempo);
chThdSleepMilliseconds(250);
}
}we have problem: the system return on serial output always the same value (about 480).
How we can resolve?
Thanks!