STRANGE behaviour of the Nucleo STM32F401RE ans STM32F411RE based boards

12 Jun 2015

/media/uploads/tondium/ultrasound.c.txt

/* The program is compiled using the online mbed compiler */

#include "mbed.h"
// This program uses the ultrasound sensor HC-SR04 to get the distance between the sensor and an object
// For somme reason I would like to discover, some pins of the nucleo STM32F401/411RE may not be used as input or output of the sensor!
// The schematics of the nucleo boards do not mention any specific usage of these two pins.
// There may be others pins that would not work too aprt from those which are dedicated for special purpose like:
// PC-14, PC_15, PH_0, PH_1, PA_13 and PA_14

Ticker led_Tmr;
DigitalOut myled(LED1); 
Timer timer_for_debug;
 

Serial pc(USBTX, USBRX);
Ticker ticker_80ms;
Ticker ticker_100ms; 
Timer uSon_Tmr;

/*
The ISRs "ISR_trig_rise()" and "ISR_trig_fall()" are never executed if the pins used are PB_8 and PC_9,
they are executed and the program behaves as expected if the pins used are PC_11 and PD_2 respectivelly.
This problem occurs on nucleo STM32F401RE and STM32F411RE !!!
*/
DigitalOut    trig_pin(PB_8)  ; // works fine if "PB_8" is replaced by  PC_11 
InterruptIn   echo_pin(PC_9)  ; // works fine if "PC_9" is replaced by   PD_2   


volatile unsigned char start_trig = 0;
volatile unsigned int nb_of_trig = 0, nb_ISR_80ms = 0;
volatile unsigned int nbRise=0, nbFall = 0 ;
volatile unsigned int uSon_duration = 0, distance = 0; 


/***************************************************************************/
void ISR_toggleLED()  // 100ms
{
    static unsigned int j = 0;

    j++;
    if(j >= 20)// 2secondes
        j = 0;
    if(j < 3) 
        myled = 1 ;
    
    else 
        myled = 0;
        
    return ; 
}

void ISR_80ms()
{
    float temp;
       
    temp = (uSon_duration * 17)/1000;
    distance = (unsigned int)(temp);

    start_trig =1;
    nb_ISR_80ms++ ;
    
    return ; 
}

void ISR_trig_rise() {
    uSon_Tmr.start() ;
    nbRise++;   
    return ; 
}

void ISR_trig_fall() {
    uSon_duration = uSon_Tmr.read_us() ;
    nbFall++;
    return ; 
}

void init_trig()
{  
    uSon_Tmr.stop() ;
    uSon_Tmr.reset() ;

    trig_pin  = 1;  
    wait_us(12);
    trig_pin  = 0;
  
    nb_of_trig++;
    return;
    
}
/************************************************************************************************/

int main() {
    
    led_Tmr.attach_us(&ISR_toggleLED, 100000); //100ms

    echo_pin.mode(PullUp) ;
    echo_pin.rise(&ISR_trig_rise);
    echo_pin.fall(&ISR_trig_fall);
    
    wait_ms(1);
    
    ticker_80ms.attach_us(&ISR_80ms, 80000); //80ms  
    ticker_100ms.attach_us(&ISR_toggleLED, 100000); 
  

    timer_for_debug.start(); /* timer used for display refresh intervals */
    
    pc.printf("Hello World !\n");
    while(1) {          
        
       if(start_trig == 1){
            start_trig = 0;
            init_trig();
       }
        
       // display the mesured distance, and how many times each ISR is executed    
       if(timer_for_debug.read_ms() > 3000){    //3sec refresh time  
            pc.printf("Distance = %d\n", distance);
            pc.printf("number of rise  = %d\n", nbRise);
            pc.printf("number of fall = %d\n", nbFall);
            
            pc.printf("nb_of_trig = %d\n", nb_of_trig);
            pc.printf("nb_ISR_80ms = %d\n\n", nb_ISR_80ms);  
            
            timer_for_debug.reset() ;
        }    
   } 
   
    
}

Thank you in advance for any information. Regards, TONDEN

29 Jan 2015

please use the <<code>> and <</code>> tags on separate lines around your posted code to keep it readable.

30 Jan 2015

HI Wim, thank you for your comment and advise. By clicking the link above "/media/uploads/tondium/ultrasound.c.txt", you get the source code. Regards, TONDE.N