这是主函数:
#include "pbdata.h"
#include " STM32f10x.h"
void RCC_Configura tion(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 ARR-10));
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.求大神帮我看一下程序,指导一下,我是新手入门,修改别人程序来做超声波测距。
跪求大神指导,万分感谢。
2
|
|
|
|
我的这个程序不能 正常测距,返回到串口的数值一直不发生改变。
求大神给予指导
|
|
|
|
|
while (1)
{
GPIO_SetBits(GPIOA,GPIO_Pin_2);
delay_us(20);//拉高超过10us,发射超声波
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
TIM2->CNT=0;//计数器清0
我用51写过超声波测距,,上面这一段计数器的值还没算就清0了?
|
|
|
|
|
为啥用STM 32 做超声波测距, 不浪费吗 ?????
|
|
|
|
|
希望可以帮助到楼主
|
|
|
|
|
14楼顶 ![](https://staticbbs.elecfans.com/static/image/smiley/coolmonkey/03.gif)
|
|
|
|
|
我用你的程序改了下测得的输出高电平时间是不变的,就把PC7改成了PA7,我的板子PA7对应TIM3_CH2,定时器相关配置也改成了TIM3,我用示波器观察输出,无论探头距障碍物多远输出高电平一直是3.3us,能否给点建议?新手学习中!!
|
|
|
|
|
请问你的超声波问题有解决吗?我也是测距不变的问题
|
|
|
|
|