新手上路
- 积分
- 24
- 金钱
- 24
- 注册时间
- 2018-9-12
- 在线时间
- 11 小时
|
15金钱
这是主程序
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "LCD1602.h"
//³¬Éù²¨Ó²¼þ½Ó¿Ú¶¨Òå
#define HCSR04_PORT GPIOB
#define HCSR04_CLK RCC_APB2Periph_GPIOB
#define HCSR04_TRIG GPIO_Pin_11
#define HCSR04_ECHO GPIO_Pin_10
#define TRIG_Send PBout(11)
#define ECHO_Reci PBin(10)
//³¬Éù²¨¼ÆÊý
u16 msHcCount = 0;
//¶¨Ê±Æ÷2ÉèÖÃ
void hcsr04_NVIC()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void NVIC_Configuration(void) //²»Ð´³öwarning
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
}
//IO¿Ú³õʼ»¯ ¼°ÆäËû³õʼ»¯
void Hcsr04Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(HCSR04_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin =HCSR04_TRIG;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(HCSR04_PORT, &GPIO_InitStructure);
GPIO_ResetBits(HCSR04_PORT,HCSR04_TRIG);
GPIO_InitStructure.GPIO_Pin = HCSR04_ECHO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(HCSR04_PORT, &GPIO_InitStructure);
GPIO_ResetBits(HCSR04_PORT,HCSR04_ECHO);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = (1000-1);
TIM_TimeBaseStructure.TIM_Prescaler =(72-1);
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
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);
hcsr04_NVIC();
TIM_Cmd(TIM2,DISABLE);
}
//´ò¿ª¶¨Ê±Æ÷2
static void OpenTimerForHc()
{
TIM_SetCounter(TIM2,0);
msHcCount = 0;
TIM_Cmd(TIM2, ENABLE);
}
//¹Ø±Õ¶¨Ê±Æ÷2
static void CloseTimerForHc()
{
TIM_Cmd(TIM2, DISABLE);
}
//¶¨Ê±Æ÷2ÖÕÖжÏ
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update );
msHcCount++;
}
}
//»ñÈ¡¶¨Ê±Æ÷2¼ÆÊýÆ÷Öµ
u32 GetEchoTimer(void)
{
u32 t = 0;
t = msHcCount*1000;
t += TIM_GetCounter(TIM2);
TIM4->CNT = 0;
delay_ms(50);
return t;
}
//ͨ¹ý¶¨Ê±Æ÷2¼ÆÊýÆ÷ÖµÍÆËã¾àÀë
float Hcsr04GetLength(void )
{
u32 t = 0;
int i = 0;
float lengthTemp = 0;
float sum = 0;
while(i!=5)
{
TRIG_Send = 1;
delay_us(20);
TRIG_Send = 0;
while(ECHO_Reci == 0);
OpenTimerForHc();
i = i + 1;
while(ECHO_Reci == 1);
CloseTimerForHc();
t = GetEchoTimer();
lengthTemp = ((float)t/58.0);//cm
sum = lengthTemp + sum ;
}
lengthTemp = sum/5.0;
return lengthTemp;
}
//²âÊÔÖ÷º¯Êý
int main(void)
{
u8 a,b,c,d;
u8 str[6]={0};
u8 length;
delay_init(); //ÑÓʱº¯Êý³õʼ»¯
NVIC_Configuration();
SystemInit();
uart_init(9600); //´®¿Ú³õʼ»¯9600
Hcsr04Init();
LCD1602_Init();
LCD1602_ClearScreen();
LCD1602_Show_Str(4, 1, "598");
while(1)
{
length = Hcsr04GetLength();
a=(length/1000);
b=((length%1000)/100);
c=((length%100)/10);
d=(length%10);
str[0]=a+0x30;
str[1]=b+0x30;
str[2]=c+0x30;
str[3]=d+0x30;
str[4]='c';
str[5]='m';
LCD1602_Show_Str(4, 1, "598");
delay_ms(1000);
}
}
|
|