求助各位大神看看我这超声波测距问题出在哪里
时间:10-02
整理:3721RD
点击:
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "exti.h"
#define Trig PAout(1)
#define Echo PAin(0)
u16 duan[]={0x3fff,0x06ff,0x5bff,0x4fff,0x66ff,0x6dff,0x7dff,0x07ff,0x7fff,0x6fff};
u16 wei[]={0xfffe,0xfffd,0xfffb};
void xianshi_init(u16 s) //数码管显示
{
s=s/1000;
GPIOB->ODR=duan[(s%10)]&wei[0];
delay_ms(5);
GPIOB->ODR=duan[((s/10)%10)]&wei[1];
delay_ms(5);
GPIOB->ODR=duan[(s/100)]&wei[2];
delay_ms(5);
}
int main(void)
{
u16 s;
Stm32_Clock_Init(9)
delay_init(72);
xianshi_init(s);
RCC->APB2ENR|=1<<3;
GPIOB->CRH&=0X00000000
GPIOB->CRH|=0X33333333;
GPIOB->CRL&=0X00000000;
GPIOB->CRL|=0X33333333;
GPIOB->ODR=0X00FF;
RCC->APB2ENR|=1<<2;
GPIOA->CRL&=0XFFFFFF00;
GPIOA->CRL|=0X00000038;
GPIOA->ODR=0XFFF0;
RCC->APB1ENR|=1<<1;
while(1)
{
Trig=1;
delay_us(30);
Trig=0;
TIM3->CNT=0;
while(Echo==0);
TIM3->CR1=0x01;
while((Echo==1)&&(TIM3->CNT<TIM3->ARR-10));
TIM3->CR1=0x00;
s=((TIM3->CNT)*272);
xianshi_init(s);
}
}
首先,你那部分显示的函数写的就不对,各位是取余10没错,但十位不应该是直接除以十,而是(s%100)/10