STM32 controls 3 ultrasonic sensors

Publisher:平章大人Latest update time:2018-05-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Use the STM32 timer input capture module to control 3-way ultrasonic sensors. The 
ultrasonic sensor used this time is the common HC-SR04, which is often used in the obstacle avoidance system of small robots and smart cars. 
Write the picture description here 
In the above figure, 5v and GND provide power to the module, Trig is used to trigger the module to measure distance, and Echo is used to receive the return level signal. 
Its operation timing diagram is as follows: 
Write the picture description here 
As shown in the above figure, STM32 gives the Trig pin a high level of more than 10us to enable the ranging circuit inside the module. The module will cyclically send out 8 40kHz pulses and emit ultrasonic waves. Then, by detecting the high level time of the Echo pin, the distance between the module and the obstacle can be measured. Its calculation formula can be expressed as follows: 

distance=340∗Echo high level time 2distance=340∗Echo high level time 2


The module driver is as follows:

void HC_SR04_Init(void)

{

    /*Initialize GPIO*/

   GPIO_InitTypeDef  GPIO_InitStructure;

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

    NVIC_InitTypeDef         NVIC_InitStructure;

    TIM_ICInitTypeDef        TIM_ICInitStructure;


  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //Enable GPIOA clock

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //Enable timer 2


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6; //Trig:PA4,PA5,PA6

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

    GPIO_Init(GPIOA, &GPIO_InitStructure);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3; //Echo: PA2,PA3 correspond to channels 3,4 of TIM2

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //Turn on pin multiplexing

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 

    GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_TIM2);

    GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_TIM2);

  GPIO_Init(GPIOA, &GPIO_InitStructure); //Initialize GPIOA


    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//Enable GPIOB's Clock

     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PB3 refers to TIM2's channel 2

     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 

    GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_TIM2);

  GPIO_Init(GPIOB, &GPIO_InitStructure);//initialize GPIOB


      /Initialize TIM2*/

    TIM_TimeBaseStructure.TIM_Period = 0xffffffff; //The maximum detection distance of the sensor is 4000mm, which generally does not overflow

    TIM_TimeBaseStructure.TIM_Prescaler =84-1; //Set TIM2 clock frequency to 1MHz

    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); 


    //Initialize TIM2 input capture


    TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; 

  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; //Both rising and falling trigger input capture

  TIM_ICInitStructure.TIM_ICSelection =  TIM_ICSelection_DirectTI;

  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; 

  TIM_ICInitStructure.TIM_ICFilter = 0x00;

  TIM_ICInit(TIM2, &TIM_ICInitStructure);


    TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; 

  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; 

  TIM_ICInitStructure.TIM_ICSelection =  TIM_ICSelection_DirectTI;

  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;  

  TIM_ICInitStructure.TIM_ICFilter = 0x00;

  TIM_ICInit(TIM2, &TIM_ICInitStructure);


    TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; 

  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; 

  TIM_ICInitStructure.TIM_ICSelection =  TIM_ICSelection_DirectTI;

  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;  

  TIM_ICInitStructure.TIM_ICFilter = 0x00;

  TIM_ICInit(TIM2, &TIM_ICInitStructure);


    //Set up input capture interrupt

    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; 

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 

    NVIC_Init(&NVIC_InitStructure);  


    TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE);//使能中断

  TIM_Cmd(TIM2,ENABLE);     

    dist.overflow=0;

}

It is worth noting that the edge polarity of the input capture here is set to capture both the rising and falling edges, which is more efficient. Some experts also start to use rising edge capture, and then set the capture memory to falling edge capture in the interrupt. I have tried this method, and the program is prone to jamming, so I did not use it. 
The interrupt program can be written like this:

void TIM2_IRQHandler(void)

{

    static uint16_t cnt_f,cnt_l,cnt_r;


   if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)

     {

         dist.overflow++;

         TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

//       printf("overflow\n");

     }


     if(TIM_GetITStatus(TIM2, TIM_IT_CC2)!=RESET)

     {

       cnt_f++;

         if(cnt_f%2==1)//A rising level is detected

         {

              TIM2->CNT=0; //Counter clear

         }

         else //A falling edge level is detected, and the distance value is read

         {

             dist.cnt=TIM2->CNT;

             TIM2->CNT=0;

             dist.f_distance=(dist.overflow*0xffffffff+dist.cnt)*0.17f;

             printf("F_dis=%.2fmm\n",dist.f_distance);

             dist.overflow=0;

         }

         if(cnt_f>65535)cnt_f=0;

         TIM_ClearITPendingBit(TIM2,  TIM_IT_CC2);

     }


     if(TIM_GetITStatus(TIM2, TIM_IT_CC3)!=RESET)

     {

       cnt_l++;

         if(cnt_l%2==1)//A rising level is detected

         {

              TIM2->CNT=0;

         }

         else

         {

             dist.cnt=TIM2->CNT;

             TIM2->CNT=0;

             dist.l_distance=(dist.overflow*0xffffffff+dist.cnt)*0.17f;

             printf("L_dis=%.2fmm\n",dist.l_distance);

             dist.overflow=0;

         }

         if(cnt_l>65535)cnt_l=0;

         TIM_ClearITPendingBit(TIM2,  TIM_IT_CC3);

     }


     if(TIM_GetITStatus(TIM2, TIM_IT_CC4)!=RESET)

     {

       cnt_r++;

         if(cnt_r%2==1)

         {

              TIM2->CNT=0;

         }

         else

         {

             dist.cnt=TIM2->CNT;

             TIM2->CNT=0;

             dist.r_distance=(dist.overflow*0xffffffff+dist.cnt)*0.17f;

             printf("R_dis=%.2fmm\n",dist.r_distance);

             dist.overflow=0;

         }

         if(cnt_r>65535)cnt_r=0;

         TIM_ClearITPendingBit(TIM2,  TIM_IT_CC4);

     }

}

The advantage of this design is that the interrupt program design is simple and can ensure real-time performance. 
The module is triggered to measure by giving the module's Trig pin a high-level signal of more than 10us to trigger the module to measure the distance. The program code is as follows:


/*

function: Start distance measurement

Choice: 0: Left ultrasonic module

        1: Middle ultrasonic module

        2: Right ultrasonic module

*/

void Get_Distance(uint8_t choice)

{

    switch(choice)

    {

      case 0:

       GPIO_ResetBits(GPIOA,GPIO_Pin_4);

         GPIO_SetBits(GPIOA,GPIO_Pin_4);

         Delay_us(15);

         GPIO_ResetBits(GPIOA,GPIO_Pin_4);  

       break;

    case 1:

             GPIO_ResetBits(GPIOA,GPIO_Pin_5);

         GPIO_SetBits(GPIOA,GPIO_Pin_5);

         Delay_us(15);

         GPIO_ResetBits(GPIOA,GPIO_Pin_5);  

       break;

        case 2:

             GPIO_ResetBits(GPIOA,GPIO_Pin_6);

         GPIO_SetBits(GPIOA,GPIO_Pin_6);

         Delay_us(15);

         GPIO_ResetBits(GPIOA,GPIO_Pin_6);  

       break;

    }

}


Reference address:STM32 controls 3 ultrasonic sensors

Previous article:stm32 startup mode + pull-up (pull-down) resistor two-in-one
Next article:The principle and function of crystal oscillator in STM32

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号