STM32F103 ultrasonic serial port output data

Publisher:huanhuiLatest update time:2019-04-08 Source: eefocusKeywords:STM32F103 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include "cj.h"

#include "stm32f10x.h"

#include "delay.h"

#include "usart.h"


unsigned int overcount=0;



void NVIC_Config(void)//priority setting

{

NVIC_InitTypeDef NVIC_InitStructer;


NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);


NVIC_InitStructer.NVIC_IRQChannelPreemptionPriority=0;

NVIC_InitStructer.NVIC_IRQChannelSubPriority=0;

NVIC_InitStructer.NVIC_IRQChannel=TIM2_IRQn;//

NVIC_InitStructer.NVIC_IRQChannelCmd=ENABLE;


NVIC_Init(&NVIC_InitStructer);

}


void CH_SR04_Init(void) //Ultrasonic module initialization

{

GPIO_InitTypeDef GPIO_InitStructer;

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructer;


RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);


GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;

GPIO_InitStructer.GPIO_Pin=GPIO_Pin_8;

GPIO_Init(GPIOB, &GPIO_InitStructer); //Set PB8 as the trigger pin



GPIO_InitStructer.GPIO_Mode=GPIO_Mode_IN_FLOATING;

GPIO_InitStructer.GPIO_Pin=GPIO_Pin_9;

GPIO_Init(GPIOB,&GPIO_InitStructer); //Set PB9 as the receiving pin


TIM_DeInit(TIM2);

TIM_TimeBaseInitStructer.TIM_Period=65534;

TIM_TimeBaseInitStructer.TIM_Prescaler=71; //After pre-scaling, the value of the counter increases by one every 0.5us.

TIM_TimeBaseInitStructer.TIM_ClockDivision=TIM_CKD_DIV1;//

TIM_TimeBaseInitStructer.TIM_CounterMode=TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructer);


 


    //TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC1,ENABLE);//


TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//

NVIC_Config();

TIM_Cmd(TIM2,DISABLE);//


}



 


float Senor_Using(void) //Distance measurement function

{

float length=0,sum=0;

u16 tim;

unsigned int i=0;


 


while(i!=5)

{


PBout(8)=1; //

delay_us(20); //

PBout(8)=0;


while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)==RESET);

TIM_Cmd(TIM2,ENABLE);//

i+=1;

while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)==SET);//

TIM_Cmd(TIM2,DISABLE);//


tim = TIM_GetCounter (TIM2); //


length=(tim+overcount*1000)/58.0;//


sum=length+sum;

TIM2->CNT=0; //

overcount=0; //

delay_ms(100);

}

length=sum/5;

return length;//

}


 


void TIM2_IRQHandler(void) //

{

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

  {

TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//

overcount++;


  }

}

 


Since it is copied and pasted, only some parts are commented. In fact, it is very simple. It is to output a high level of more than 10us as a pulse signal through a pin, and set a pin as a receiving pin. When a high level is received, the timer counts, and when a low level is detected, the count stops. Take out the value and calculate the distance through the formula. Finally, the serial port output, directly #include "stdio.h" and then use pirntf. But don't forget to configure the compiler before that. Please Baidu it yourself. It's very simple.

Keywords:STM32F103 Reference address:STM32F103 ultrasonic serial port output data

Previous article:About the programming of matrix buttons of STM32F103 model
Next article:PWM frequency and duty cycle setting in stm32

Latest Microcontroller Articles
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号