本项目主要用面包板搭建,主要由 stm32f103c8t6最小系统板,hc-sr04超声波模块,ds18b20温度传感器,syn6288语音合成模块,四针脚oled显示屏四个按键一个led,若干杜邦线组成。
hc-05超声波测距模块
超声波需要5v供电,3.3v会导致数据异常。代码中的delay函数沿用江科大的。
#ifndef __HCSR04_H
#define __HCSR04_H
#define Trig_Port GPIOA
#define Trig_Pin GPIO_Pin_0
#define Trig_RCC RCC_APB2Periph_GPIOA
#define Echo_Port GPIOA
#define Echo_Pin GPIO_Pin_1
#define Echo_RCC RCC_APB2Periph_GPIOA
void HCSR04_Init();
uint16_t HCSR04_GetValue();
#endif
HCSR04.c
#include "stm32f10x.h" // Device header
#include "HCSR04.h"
#include "Timer.h"
#include "Delay.h"
uint16_t Time;
void HCSR04_Init()
{
RCC_APB2PeriphClockCmd(Trig_RCC, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = Trig_Pin;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Trig_Port, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStruct.GPIO_Pin = Echo_Pin;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Echo_Port, &GPIO_InitStruct);
GPIO_ResetBits(Trig_Port, Trig_Pin);
}
void HCSR04_Start()
{
GPIO_SetBits(Trig_Port, Trig_Pin);
Delay_us(45);
GPIO_ResetBits(Trig_Port, Trig_Pin);
Timer_Init();
}
uint16_t HCSR04_GetValue()
{
HCSR04_Start();
Delay_ms(100);
return ((Time * 0.0001) * 34000) / 2;
// return Time;
}
HCSR04.h
#include "stm32f10x.h" // Device header
#include "Timer.h"
#include "usart.h"
extern uint16_t Time;
void Timer_Init()
{
Time = 0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //选择APB1总线下的定时器Timer2
TIM_InternalClockConfig(TIM2); //TIM2使用内部时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数模式,此处为向上计数
TIM_TimeBaseInitStructure.TIM_Period = 7199; //ARR 1 = 0.0001S
TIM_TimeBaseInitStructure.TIM_Prescaler = 0; //PSC
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //高级计时器特有,重复计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
TIM_ClearFlag(TIM2, TI