打印

stm32超声波模块测距程序

[复制链接]
416|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
小蘭|  楼主 | 2018-6-29 15:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stm32f10x_tim.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"


void LED_Init(void)
{

GPIO_InitTypeDef  GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);         
        
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;        
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;          //*管配置管家
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                 
GPIO_Init(GPIOA, &GPIO_InitStructure);                                         
GPIO_SetBits(GPIOA,GPIO_Pin_All);                                                

        
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);        
        
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;                                 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                // * trlg控制管家配置
GPIO_Init(GPIOB, &GPIO_InitStructure);                                       
GPIO_SetBits(GPIOB,GPIO_Pin_7);                                                

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;                                
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;             / / * echo接收管脚配置        
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;        
GPIO_Init(GPIOB, &GPIO_InitStructure);                                                         
}
void Tim2_Init(void)
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef          TIM_OCInitStructure;
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);        //  时钟配置 求大神看一下这里是否有错误!!
        TIM_DeInit(TIM2);
        TIM_TimeBaseStructure.TIM_Period = 20000;
        TIM_TimeBaseStructure.TIM_Prescaler =36000;
        TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
        TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
        TIM_Cmd(TIM2,ENABLE);
        TIM_ClearFlag(TIM2,TIM_FLAG_Update);
        TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
}
int main(void)
  {               
        const u16 a[10] ={0xd00,0xfddf,0xb04,0x944,0x9c2,0x960,0x920,0xdc4,0x900,0x940 };
         u8 t=0;
        u16 time,juli,i;
        delay_init();         
        NVIC_Configuration();         
        uart_init(9600);
        LED_Init();
        Tim2_Init();
        while(1)
                {
                  GPIO_SetBits(GPIOB,GPIO_Pin_7);
                  TIM2->CNT=0;
                  TIM_Cmd(TIM2,ENABLE);
                  delay_us(1000);
                  GPIO_ResetBits(GPIOB,GPIO_Pin_7);
                        while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_6)==1)
                        {
                                TIM_Cmd(TIM2,DISABLE);
                                time=TIM2->CNT;     //* 这个time为什么一直是0?
                                
                        }
                juli=340*(time/2);
                i=juli%10;
                GPIO_Write(GPIOA,a);
          delay_ms(1000);
}


}
        
经ST-LINK调试 那个time一直为零 这是为什么 程序那里有错误,

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

451

主题

463

帖子

1

粉丝