资源简介

本工程是基于stm32f103rct6的驱动超声波测距模块,HC-SR04,里面用到定时器、外部中断等知识。需要的器件:stm32f103rct6、HC-SR04 、蓝牙串口(usb串口一样)。keil写的c程序。

资源截图

代码片段和文件信息

/*
****************************说明**********************
1.stm32f103rct6
2.hc-sr04 :PA0、PA1
3.串口:USART1
4.具体连线及讲解见博客:https://blog.csdn.net/sf9090/article/details/105151911
*******************************************************************************
*******************谢谢支持****************************************************
********************************作者:https://blog.csdn.net/sf9090************
*/

#include 
#include 
int b = 0;
int c = 0;
double a = 0;
/*串口输出*/
int fputc(int ch FILE*fp)
{

if(fp == stdout)
{
if(ch == ‘\n‘)
{
while((USART1->SR & USART_SR_TXE) == 0);
USART1->DR = ‘\r‘;
}
while((USART1->SR & USART_SR_TXE) == 0);
USART1->DR = ch;

}
return 0;

}
/*
************延时函数***********
*/
void delay_us(int x)
{
TIM1->ARR = x-1;
TIM1->PSC = 71;
TIM1->EGR = TIM_EGR_UG;
TIM1->CR1 = TIM_CR1_OPM | TIM_CR1_CEN; 
while(TIM1->CR1 & TIM_CR1_CEN);
}
/*初始化测距模块*/
void hcsr04_init()
{
GPIOA->ODR |= GPIO_ODR_ODR1;
 delay_us(20);
GPIOA->ODR &=~ GPIO_ODR_ODR1;
}

int main(void)
{
RCC->APB2ENR = RCC_APB2ENR_IOPAEN | RCC_APB2ENR_USART1EN | RCC_APB2ENR_TIM1EN | RCC_APB2ENR_AFIOEN;
RCC->APB1ENR = RCC_APB1ENR_TIM2EN;
GPIOA->CRH = (GPIOA->CRH & 0xffff000f) | 0x34b0;
GPIOA->CRL = (GPIOA->CRL & 0xffffff00) | 0x34;
USART1->BRR = SystemCoreClock / 9600;
USART1->CR1 = USART_CR1_RE | USART_CR1_UE | USART_CR1_TE;
printf(“******************stm32f103rct6 HC-SR04**************\n“);
AFIO->EXTICR[2] |= AFIO_EXTICR1_EXTI0_PA; 
EXTI->IMR |= EXTI_IMR_MR0; 
EXTI->RTSR |= EXTI_RTSR_TR0;
  EXTI->FTSR |= EXTI_FTSR_TR0;
TIM2->ARR = 65535;
TIM2->PSC = 71;
TIM2->EGR = TIM_EGR_UG;
TIM2->CR1 =  TIM_CR1_CEN;
NVIC_SetPriority(EXTI0_IRQn 1);
NVIC_EnableIRQ(EXTI0_IRQn);


while(1)
{
hcsr04_init();
}
}
/*
***********************外部中断****************
*/
void EXTI0_IRQHandler(void)
{

if(GPIOA->IDR& GPIO_IDR_IDR0)
{
TIM2->CR1 |=  TIM_CR1_CEN;
}
else
{
TIM2->CR1 &=~  TIM_CR1_CEN; 
TIM2->EGR = TIM_EGR_UG;

b=  TIM2->CNT;
b=b*170/10000;
printf(“%dcm   “b);
for(int i = 0; i < 3000000; i++);
}
EXTI->PR = EXTI_PR_PR0;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6956  2015-12-10 17:06  stm32f103rc_hcsr04\DebugConfig\Target_1_STM32F103RC_1.0.0.dbgconf

     文件        339  2020-03-27 13:25  stm32f103rc_hcsr04\EventRecorderStub.scvd

     文件      53648  2020-03-03 22:35  stm32f103rc_hcsr04\Listings\startup_stm32f10x_hd.lst

     文件      54585  2020-03-30 06:51  stm32f103rc_hcsr04\Listings\test.map

     文件       2254  2020-03-30 06:51  stm32f103rc_hcsr04\mian.c

     文件         19  2020-03-27 13:26  stm32f103rc_hcsr04\objects\ExtDll.iex

     文件     245178  2020-03-30 06:51  stm32f103rc_hcsr04\objects\mian.crf

     文件        748  2020-03-30 06:51  stm32f103rc_hcsr04\objects\mian.d

     文件     264036  2020-03-30 06:51  stm32f103rc_hcsr04\objects\mian.o

     文件         81  2020-03-03 22:35  stm32f103rc_hcsr04\objects\startup_stm32f10x_hd.d

     文件       6704  2020-03-03 22:35  stm32f103rc_hcsr04\objects\startup_stm32f10x_hd.o

     文件     241355  2020-03-03 22:35  stm32f103rc_hcsr04\objects\system_stm32f10x.crf

     文件        828  2020-03-03 22:35  stm32f103rc_hcsr04\objects\system_stm32f10x.d

     文件     260600  2020-03-03 22:35  stm32f103rc_hcsr04\objects\system_stm32f10x.o

     文件     253920  2020-03-30 06:51  stm32f103rc_hcsr04\objects\test.axf

     文件       2378  2020-03-30 06:51  stm32f103rc_hcsr04\objects\test.build_log.htm

     文件      33609  2020-03-30 06:51  stm32f103rc_hcsr04\objects\test.htm

     文件        371  2020-03-30 06:51  stm32f103rc_hcsr04\objects\test.lnp

     文件        494  2020-03-03 21:20  stm32f103rc_hcsr04\objects\test.sct

     文件       2843  2020-03-30 06:51  stm32f103rc_hcsr04\objects\test_Target 1.dep

     文件      64999  2016-09-09 13:58  stm32f103rc_hcsr04\RTE\Device\STM32F103RC\RTE_Device.h

     文件      15503  2016-11-11 11:18  stm32f103rc_hcsr04\RTE\Device\STM32F103RC\startup_stm32f10x_hd.s

     文件      36557  2016-11-11 11:18  stm32f103rc_hcsr04\RTE\Device\STM32F103RC\system_stm32f10x.c

     文件        344  2020-03-03 21:06  stm32f103rc_hcsr04\RTE\_Target_1\RTE_Components.h

    .......    178281  2020-03-30 06:51  stm32f103rc_hcsr04\test.uvguix.pc

    .......      7032  2020-03-27 21:55  stm32f103rc_hcsr04\test.uvoptx

    .......     16453  2020-03-03 21:23  stm32f103rc_hcsr04\test.uvprojx

     目录          0  2020-03-03 21:06  stm32f103rc_hcsr04\RTE\Device\STM32F103RC

     目录          0  2020-03-03 21:06  stm32f103rc_hcsr04\RTE\Device

     目录          0  2020-03-03 21:06  stm32f103rc_hcsr04\RTE\_Target_1

............此处省略8个文件信息

评论

共有 条评论