完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
这是主函数:
#include "pbdata.h" #include "STM32f10x.h" void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void USART_Configuration(void); int fputc(int ch,FILE *f) { USART_SendData(USART1,(u8)ch); while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET); return ch; } //TRIG接PD3 发射 //ECHO接PD6 接收 int main(void) { u16 count; float length; RCC_Configuration(); //系统时钟初始化 GPIO_Configuration();//端口初始化 USART_Configuration(); NVIC_Configuration(); Tim2_Init(); delay_ms(5); GPIO_ResetBits(GPIOA,GPIO_Pin_2); while (1) { GPIO_SetBits(GPIOA,GPIO_Pin_2); delay_us(20);//拉高超过10us,发射超声波 GPIO_ResetBits(GPIOA,GPIO_Pin_2); TIM2->CNT=0;//计数器清0 while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3)==0);//等待ECHO脚高电平 TIM_Cmd(TIM2, ENABLE);// TIM2 enable counter [允许tim2计数] while((GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3)==1)&&(TIM2->CNT TIM_Cmd(TIM2, DISABLE); count=TIM2->CNT;//ECHO脚低电平后读取计数器的值,从而算出往返时间 length=count/58.0; // length=100; printf("距离=%f CM",length); delay_ms(1000); delay_ms(1000); delay_ms(1000); } } void RCC_Configuration(void) { SystemInit();//72m RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; //LED GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA,&GPIO_InitStructure); //超声波 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//TX GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;//RX GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA,&GPIO_InitStructure); } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void USART_Configuration(void) { USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate=9600; USART_InitStructure.USART_WordLength=USART_WordLength_8b; USART_InitStructure.USART_StopBits=USART_StopBits_1; USART_InitStructure.USART_Parity=USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; USART_Init(USART1,&USART_InitStructure); USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); USART_Cmd(USART1,ENABLE); USART_ClearFlag(USART1,USART_FLAG_TC); } 然后,这是TIM2的函数: void Tim2_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_DeInit(TIM2);//复位TIM2定时器 TIM_TimeBaseStructure.TIM_Period = 49999; TIM_TimeBaseStructure.TIM_Prescaler = 71; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure); TIM_ClearFlag(TIM2, TIM_FLAG_Update); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); } ,以上是我的主要的程序,我遇到的问题是, 1.定时器的初值返回不正常,我不能收到定时器的初值,由此导致我的测量结果没有。 2.怎么样根据定时器的初值,计算定时器的时间,已知Period和Prescaler,系统工作在72M晶振下。 3.求大神帮我看一下程序,指导一下,我是新手入门,修改别人程序来做超声波测距。 跪求大神指导,万分感谢。 |
|
相关推荐
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1373 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1303 浏览 3 评论
2616 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1450 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1882 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-2 04:50 , Processed in 0.666154 second(s), Total 76, Slave 55 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号