main函数
#include "stm32f10x.h"
#include "./led/bsp_led.h"
#include "./systick/bsp_systick.h"
#include "./beep/bsp_beep.h"
#include "./key/bsp_key.h"
#include "./usart/bsp_usart.h"
#include "./HC_SR04/bsp_hcsr04.h"
void Soft_Delay(unsigned int time)
{
while(time--);
}
int main(void)
{
int32_t d = 0;
LED_GPIO_Init();
Beep_Init();
KEY_GPIO_Init();
USARTX_Config();
HC_SR04_GPIO_Init();
printf("HC_SR04测试模块\r\n");
while(1)
{
d = HC_SR04_Config();
if(d > 0)
{
if(d >= 20 && d<= 4000)
{
printf("超声波所测距离=%d mm\r\n",d);
if(d > 450)
{
Red_Led_OFF;
Green_Led_OFF;
Blue_Led_OFF;
Beep_OFF;
}
else if(350 <= d && d <= 450)
{
Red_Led_OFF;
Green_Led_ON;
Blue_Led_OFF;
Beep_OFF;
}
else if(250 <= d && d <= 350)
{
Red_Led_ON;
Green_Led_OFF;
Blue_Led_OFF;
Beep_OFF;
}
else if(150 <= d && d <= 250)
{
Red_Led_ON;
Green_Led_ON;
Blue_Led_OFF;
Beep_OFF;
}
else if(d <= 150)
{
Red_Led_ON;
Green_Led_ON;
Blue_Led_ON;
Beep_ON;
}
}
}
Delay_ms(2000);
}
}
LED
#include "./led/bsp_led.h"
void LED_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure={0};
Led_RCC_CLK_ENABLE(Led_RCC_CLK_PERIPH,ENABLE);
GPIO_InitStructure.GPIO_Pin =Red_Led_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(Red_Led_GPIO_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =Green_Led_GPIO_PIN;
GPIO_Init(Green_Led_GPIO_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =Blue_Led_GPIO_PIN;
GPIO_Init(Blue_Led_GPIO_PORT,&GPIO_InitStructure);
Red_Led_OFF;
Green_Led_OFF;
Blue_Led_OFF;
}
#ifndef __BSP_LED_H
#define __BSP_LED_H
#include "stm32f10x.h"
#define Led_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define Led_RCC_CLK_PERIPH RCC_APB2Periph_GPIOB
#define Red_Led_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define Red_Led_RCC_CLK_PERIPH RCC_APB2Periph_GPIOB
#define Red_Led_GPIO_PORT GPIOB
#define Red_Led_GPIO_PIN GPIO_Pin_5
#define Red_Led_ON GPIO_ResetBits(Red_Led_GPIO_PORT,Red_Led_GPIO_PIN)
#define Red_Led_OFF GPIO_SetBits(Red_Led_GPIO_PORT, Red_Led_GPIO_PIN)
#define Red_Led_TOGGLE {Red_Led_GPIO_PORT->ODR ^= Red_Led_GPIO_PIN;}
#define Green_Led_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define Green_Led_RCC_CLK_PERIPH RCC_APB2Periph_GPIOB
#define Green_Led_GPIO_PORT GPIOB
#define Green_Led_GPIO_PIN GPIO_Pin_0
#define Green_Led_ON GPIO_ResetBits(Green_Led_GPIO_PORT,Green_Led_GPIO_PIN)
#define Green_Led_OFF GPIO_SetBits(Green_Led_GPIO_PORT, Green_Led_GPIO_PIN)
#define Green_Led_TOGGLE {Green_Led_GPIO_PORT->ODR ^= Green_Led_GPIO_PIN;}
#define Blue_Led_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define Blue_Led_RCC_CLK_PERIPH RCC_APB2Periph_GPIOB
#define Blue_Led_GPIO_PORT GPIOB
#define Blue_Led_GPIO_PIN GPIO_Pin_1
#define Blue_Led_ON GPIO_ResetBits(Blue_Led_GPIO_PORT,Blue_Led_GPIO_PIN)
#define Blue_Led_OFF GPIO_SetBits(Blue_Led_GPIO_PORT, Blue_Led_GPIO_PIN)
#define Blue_Led_TOGGLE {Blue_Led_GPIO_PORT->ODR ^= Blue_Led_GPIO_PIN;}
extern void LED_GPIO_Init(void);
#endif
SysTick
#include "./systick/bsp_systick.h"
int32_t Delay_us(uint32_t us)
{
uint32_t temp1 = 0;
SysTick->CTRL = 0;
SysTick->VAL = 0;
SysTick->LOAD = ((us*72)-1);
SysTick->CTRL = 0x05;
while(1)
{
temp1 = SysTick->CTRL;
if(temp1 & 0x10000)
{
break;
}
if((temp1 & 0x01) == 0)
{
return -1;
}
}
SysTick->CTRL = 0;
return 0;
}
int32_t Delay_ms(uint32_t ms)
{
uint32_t temp2 = 0;
while(ms--)
{
SysTick->CTRL = 0;
SysTick->VAL = 0;
SysTick->LOAD = (72000-1);
SysTick->CTRL = 0x05;
while(1)
{
temp2 = SysTick->CTRL;
if(temp2 & 0x10000)
{
break;
}
if((temp2 & 0x01) == 0)
{
return -1;
}
}
}
SysTick->CTRL = 0;
return 0;
}
#ifndef __BSP_SYSTICK_H
#define __BSP_SYSTICK_H
#include "stm32f10x.h"
#include "core_cm3.h"
extern int32_t Delay_us(uint32_t us);
extern int32_t Delay_ms(uint32_t ms);
#endif
Beep
#include "./beep/bsp_beep.h"
void Beep_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure={0};
Beep_RCC_CLK_ENABLE(Beep_RCC_CLK_PERIPH,ENABLE);
GPIO_InitStructure.GPIO_Pin =Beep_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(Beep_GPIO_PORT,&GPIO_InitStructure);
Beep_OFF;
}
#ifndef __BSP_BEEP_H
#define __BSP_BEEP_H
#include "stm32f10x.h"
#define Beep_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define Beep_RCC_CLK_PERIPH RCC_APB2Periph_GPIOA
#define Beep_GPIO_PORT GPIOA
#define Beep_GPIO_PIN GPIO_Pin_8
#define Beep_OFF GPIO_ResetBits(Beep_GPIO_PORT,Beep_GPIO_PIN)
#define Beep_ON GPIO_SetBits(Beep_GPIO_PORT, Beep_GPIO_PIN)
extern void Beep_Init(void);
#endif
Key
#include "./key/bsp_key.h"
#include "./systick/bsp_systick.h"
void KEY_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure={0};
Key1_RCC_CLK_ENABLE(Key1_RCC_CLK_PERIPH,ENABLE);
Key2_RCC_CLK_ENABLE(Key2_RCC_CLK_PERIPH,ENABLE);
GPIO_InitStructure.GPIO_Pin = Key1_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(Key1_GPIO_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Key2_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(Key2_GPIO_PORT,&GPIO_InitStructure);
}
uint8_t Key_State_Scan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == Key_Down)
{
Delay_ms(20);
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == Key_Down)
{
while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == Key_Down);
return Key_Down;
}
else
{
return Key_Up;
}
}
else
{
return Key_Up;
}
}
#ifndef __BSP_KEY_H
#define __BSP_KEY_H
#include "stm32f10x.h"
#define Key_Down 1
#define Key_Up 0
#define Key1_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define Key1_RCC_CLK_PERIPH RCC_APB2Periph_GPIOA
#define Key1_GPIO_PORT GPIOA
#define Key1_GPIO_PIN GPIO_Pin_0
#define Key2_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define Key2_RCC_CLK_PERIPH RCC_APB2Periph_GPIOC
#define Key2_GPIO_PORT GPIOC
#define Key2_GPIO_PIN GPIO_Pin_13
extern void KEY_GPIO_Init(void);
extern uint8_t Key_State_Scan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
#endif
USART
#include "./usart/bsp_usart.h"
static void USARTX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
USARTX_TX_RX_RCC_CLK_ENABLE(USARTX_TX_RX_RCC_CLK_PERIPH,ENABLE);
GPIO_InitStructure.GPIO_Pin = USARTX_TX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USARTX_TX_GPIO_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USARTX_RX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USARTX_RX_GPIO_PORT,&GPIO_InitStructure);
}
static void USARSTX_NVIC_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure = {0};
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USARTX_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void USARTX_Mode_Init(void)
{
USART_InitTypeDef USART_InitStructure = {0};
USARTX_RCC_CLK_ENABLE(USARTX_RCC_CLK_PERIPH,ENABLE);
USART_InitStructure.USART_BaudRate = USARTX_Baudrate;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = (USART_Mode_Tx|USART_Mode_Rx);
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USARTX,&USART_InitStructure);
USARSTX_NVIC_Init();
USART_ITConfig(USARTX,USART_IT_RXNE,ENABLE);
USART_Cmd(USARTX,ENABLE);
}
void USARTX_Config(void)
{
USARTX_GPIO_Init();
USARTX_Mode_Init();
}
void Usart_SendByte(USART_TypeDef * pUSARTx,uint8_t ch)
{
USART_SendData(pUSARTx,ch);
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TXE) == RESET);
}
void Usart_SendArray(USART_TypeDef *pUSARTx,uint8_t *array,uint16_t num)
{
uint8_t i = 0;
for(i=0 ; i<8 ;i++)
{
Usart_SendByte(pUSARTx,array[i]);
}
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC) == RESET);
}
void Usart_SendString( USART_TypeDef * pUSARTx, char *str)
{
unsigned int k = 0;
do
{
Usart_SendByte(pUSARTx,*(str+k));
k++;
}while(*(str+k) != '\0');
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC) == RESET);
}
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
{
uint8_t High_Temp = 0,Low_Temp = 0;
High_Temp = ((ch&0xff00)>>8);
Low_Temp = (ch&0xff);
USART_SendData(pUSARTx,High_Temp);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
USART_SendData(pUSARTx,Low_Temp);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}
int fputc(int ch,FILE *f)
{
USART_SendData(USARTX,(uint8_t)ch);
while (USART_GetFlagStatus(USARTX, USART_FLAG_TXE) == RESET);
return (ch);
}
int fgetc(FILE *f)
{
int Receive_Date = 0;
while (USART_GetFlagStatus(USARTX, USART_FLAG_RXNE) == RESET);
Receive_Date = (int)USART_ReceiveData(USARTX);
return (Receive_Date);
}
#ifndef __BSP_USART_H
#define __BSP_USART_H
#include "stm32f10x.h"
#include <stdio.h>
#define USARTX_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define USARTX_RCC_CLK_PERIPH RCC_APB2Periph_USART1
#define USARTX USART1
#define USARTX_Baudrate 115200
#define USARTX_IRQn USART1_IRQn
#define USARTX_IRQHandler USART1_IRQHandler
#define USARTX_TX_RX_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define USARTX_TX_RX_RCC_CLK_PERIPH RCC_APB2Periph_GPIOA
#define USARTX_TX_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define USARTX_TX_RCC_CLK_PERIPH RCC_APB2Periph_GPIOA
#define USARTX_TX_GPIO_PORT GPIOA
#define USARTX_TX_GPIO_PIN GPIO_Pin_9
#define USARTX_RX_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define USARTX_RX_RCC_CLK_PERIPH RCC_APB2Periph_GPIOA
#define USARTX_RX_GPIO_PORT GPIOA
#define USARTX_RX_GPIO_PIN GPIO_Pin_10
extern void USARTX_Config(void);
extern void Usart_SendByte(USART_TypeDef * pUSARTx,uint8_t ch);
extern void Usart_SendArray(USART_TypeDef *pUSARTx,uint8_t *array,uint16_t num);
extern void Usart_SendString( USART_TypeDef * pUSARTx, char *str);
extern void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch);
extern int fputc(int ch,FILE *f);
extern int fgetc(FILE *f);
#endif
HC_SR04
#include "./HC_SR04/bsp_hcsr04.h"
#include "./systick/bsp_systick.h"
void HC_SR04_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure={0};
HC_SR04_RCC_CLK_ENABLE(HC_SR04_CLK_PERIPH,ENABLE);
GPIO_InitStructure.GPIO_Pin = HC_SR04_TRIG_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(HC_SR04_TRIG_GPIO_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = HC_SR04_ECHO_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(HC_SR04_ECHO_GPIO_PORT,&GPIO_InitStructure);
HC_SR04_TRIG_State_Low;
}
int32_t HC_SR04_Config(void)
{
uint32_t time = 0;
int32_t Distance = 0;
HC_SR04_TRIG_State_High;
Delay_us(20);
HC_SR04_TRIG_State_Low;
while(HC_SR04_ECHO_ReadState == HC_SR04_ECHO_LOW)
{
time++;
Delay_us(1);
if(time >= 1000000)
{
return -1;
}
}
time = 0;
while(HC_SR04_ECHO_ReadState == HC_SR04_ECHO_HIGH)
{
time++;
Delay_us(9);
if(time >= 1000000)
{
return -2;
}
}
time = time/2;
Distance = time*3;
return (Distance);
}
#ifndef __BSP_HCSR04_H
#define __BSP_HCSR04_H
#include "stm32f10x.h"
#define HC_SR04_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define HC_SR04_CLK_PERIPH RCC_APB2Periph_GPIOB
#define HC_SR04_TRIG_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define HC_SR04_TRIG_RCC_CLK_PERIPH RCC_APB2Periph_GPIOB
#define HC_SR04_TRIG_GPIO_PORT GPIOB
#define HC_SR04_TRIG_GPIO_PIN GPIO_Pin_6
#define HC_SR04_TRIG_State_Low GPIO_ResetBits(HC_SR04_TRIG_GPIO_PORT,HC_SR04_TRIG_GPIO_PIN)
#define HC_SR04_TRIG_State_High GPIO_SetBits(HC_SR04_TRIG_GPIO_PORT,HC_SR04_TRIG_GPIO_PIN)
#define HC_SR04_ECHO_RCC_CLK_ENABLE RCC_APB2PeriphClockCmd
#define HC_SR04_ECHO_RCC_CLK_PERIPH RCC_APB2Periph_GPIOB
#define HC_SR04_ECHO_GPIO_PORT GPIOB
#define HC_SR04_ECHO_GPIO_PIN GPIO_Pin_7
#define HC_SR04_ECHO_ReadState GPIO_ReadInputDataBit(HC_SR04_ECHO_GPIO_PORT,HC_SR04_ECHO_GPIO_PIN)
#define HC_SR04_ECHO_HIGH 1
#define HC_SR04_ECHO_LOW 0
extern void HC_SR04_GPIO_Init(void);
extern int32_t HC_SR04_Config(void);
#endif