点灯
宏定义,让代码易于改变
#bsp_led.h
#ifndef __BSP_LED_H
#define __BSP_LED_H
#include "stm32f10x.h"
void LED_Init();
#define RCC_CLK RCC_APB2Periph_GPIOB
#define LED_G_PIN GPIO_Pin_0
#define LED_B_PIN GPIO_Pin_1
#define LED_R_PIN GPIO_Pin_5
#define GPI0_x GPIOB
#define REVERSE1 {GPIOB->ODR^=LED_B_PIN;}
#define REVERSE2 {GPIOB->ODR^=LED_R_PIN;}
#endif
#bsp_led.c
#include "bsp_led.h"
void LED_Init()
{
RCC_APB2PeriphClockCmd(RCC_CLK,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin=LED_G_PIN|LED_B_PIN|LED_R_PIN;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPI0_x, &GPIO_InitStruct);
/* 关闭所有led灯 */
GPIO_SetBits(GPI0_x, LED_G_PIN);
/* 关闭所有led灯 */
GPIO_SetBits(GPI0_x, LED_B_PIN);
/* 关闭所有led灯 */
GPIO_SetBits(GPI0_x, LED_R_PIN);
}
要点: 1.时钟 2.初始化 3.推挽输出
按键
#KEY.h
#ifndef __key_H
#define __key_H
#include "stm32f10x.h"
void KEY_Init();
uint16_t KeyScan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
#define RCC_CLK1 RCC_APB2Periph_GPIOA
#define RCC_CLK2 RCC_APB2Periph_GPIOC
#define KEY_A_PIN GPIO_Pin_0
#define KEY_C_PIN GPIO_Pin_13
#define GPI0_x1 GPIOA
#define GPI0_x2 GPIOC
#endif
#KEY.c
#include "KEY.h"
void KEY_Init()
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_CLK1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_CLK2,ENABLE);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin=KEY_A_PIN;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPI0_x1,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin=KEY_C_PIN;
GPIO_Init(GPI0_x2,&GPIO_InitStruct);
}
uint16_t KeyScan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin))
{
while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin));
return SET;
}
else return RESET;
}
要点: 1.时钟 2.初始化 3.浮空输入
蜂鸣器
#include "beep.h"
#include "sysclock.h"
void BEEP_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
}
void delay(uint32_t ms)
{
while(ms--);
}
void work_beep()
{
BEEP_Init();
wait_ms(100);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
wait_ms(100);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
}
要点: 1.时钟 2.初始化 3.推挽输出
系统时钟
systick就是一个基于M3、M4内核的一个简单的24bit,倒计时,自动重装载定时器,倒计时结束会产生一个中断。
typedef struct
{
__IO uint32_t CTRL; /*!< Offset: 0x00 SysTick Control and Status Register */
__IO uint32_t LOAD; /*!< Offset: 0x04 SysTick Reload Value Register */
__IO uint32_t VAL; /*!< Offset: 0x08 SysTick Current Value Register */
__I uint32_t CALIB; /*!< Offset: 0x0C SysTick Calibration Register */
} SysTick_Type;
#include "sysclock.h"
# if 0
static __INLINE uint32_t SysTick_Config(uint32_t ticks)
{
//判断 tick的值是否大于2^24,如果大于,则不符合规则
if (ticks > SysTick_LOAD_RELOAD_Msk) return (1);
//初始化reload寄存器的值
SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;
//配置中断优先级,配置为15,默认最低
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
//初始化count的值
SysTick->VAL = 0;
//配置systick的时钟为72M
//使能中断
//使能systick
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk;
return (0);
}
#endif
void wait_us(uint32_t us)
{
uint32_t i;
SysTick_Config(72);
for (i=0;i<us;i++)
{
while(((SysTick->CTRL)&(1<<16))==RESET);
}
SysTick->CTRL=~SysTick_CTRL_ENABLE_Msk;
}
void wait_ms(uint32_t ms)
{
uint32_t i;
SysTick_Config(72000);
for (i=0;i<ms;i++)
{
while(((SysTick->CTRL)&(1<<16))==RESET);
}
SysTick->CTRL=~SysTick_CTRL_ENABLE_Msk;
}
外部中断
输入线具体有20根
通过AFIO外部中断寄存器控制
要点:1.GPIO初始化 2.GPIO输入线 !!!!!!!!!别忘记时钟配置
3.中断初始化 4.NVIC分组,初始化 5.中断函数
!!!!!!!!!!!!!!!!!!!!!!注意 GPIO口pin 要对象相应的中断
#include "exit.h"
#include "bsp_led.h"
static void gpio_config()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//GPIO初始化
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin=EXIT_PIN;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOx,&GPIO_InitStruct);
//GPIO线
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
}
static void exit_config()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
EXTI_InitTypeDef EXTI_InitStruct;
EXTI_InitStruct.EXTI_Line=EXTI_Line0;
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStruct);
}
static void NVIC_config()
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStruct.NVIC_IRQChannel=EXTI0_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct);
}
void exit()
{
gpio_config();
exit_config();
NVIC_config();
}
void EXTI0_IRQHandler()
{
if(EXTI_GetITStatus( EXTI_Line0))
{
REVERSE1;
EXTI_ClearITPendingBit( EXTI_Line0);
}
}
红外扫描计数
#include "CountSensor.h"
void NVIC_config2(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel=EXTI4_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(& NVIC_InitStruct);
}
void GPIO_config2(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_ON;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOx, &GPIO_InitStruct);
GPIO_EXTILineConfig( GPIO_PortSourceGPIOA,GPIO_PinSource4);
}
void Sensor(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
EXTI_InitTypeDef EXTI_InitStruct;
EXTI_InitStruct.EXTI_Line=EXTI_Line4;
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStruct);
}
void CountSensor_all(void)
{
NVIC_config2();
GPIO_config2();
Sensor();
}
extern volatile uint32_t time;
void EXTI4_IRQHandler()
{
if(EXTI_GetITStatus(EXTI_Line4))
{
time++;
EXTI_ClearITPendingBit(EXTI_Line4);
}
}
串口发送一个d
要点:1.初始化GPIO !!!!!!!!!!!!!!!!!!!!发送是复用输出 接收是浮空输入,上拉输入
2.初始化串口 注意 时钟 初始化 cmd使能
3.中断
!!!!!!!!!!!注意:要先初始化串口,再初始化GPIO,不然串口上电就会蹦出一个FF
#include "uart.h"
void uart_gpio(void)
{
RCC_APB2PeriphClockCmd(RCC_CLOCK,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin=uart_T_pin;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(uart_GPIO,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin=uart_R_pin;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(uart_GPIO,&GPIO_InitStruct);
}
void uart()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate=BaudRate;
USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStruct.USART_Parity=USART_Parity_Odd;
USART_InitStruct.USART_StopBits=USART_StopBits_1;
USART_InitStruct.USART_WordLength=USART_WordLength_9b;
USART_Init(uart_USARTx,& USART_InitStruct);
USART_ITConfig(uart_USARTx,USART_IT_RXNE, ENABLE);
USART_Cmd(uart_USARTx, ENABLE);
}
static void NVIC_config()
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn ;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct);
}
void uart_config()
{
uart_gpio();
uart();
NVIC_config();
}
void Send_data(USART_TypeDef* USART_x, uint8_t Data)
{
USART_SendData(USART_x, Data);
while(USART_GetFlagStatus( USART_x, USART_FLAG_TXE)==RESET);
}
void USART1_IRQHandler(void)
{
}
#ifndef __uart_H
#define __uart_H
#include "stm32f10x.h"
#define uart_GPIO GPIOA
#define uart_T_pin GPIO_Pin_9
#define uart_R_pin GPIO_Pin_10
#define RCC_CLOCK RCC_APB2Periph_GPIOA
#define uart_USARTx USART1
#define BaudRate 115200
void uart_config(void);
void Send_data(USART_TypeDef* USART_x, uint8_t Data);
#endif
用c语言printf控制串口要重写fputc,fgetc
#include "uart.h"
#include "stdio.h"
void uart_gpio(void)
{
RCC_APB2PeriphClockCmd(RCC_CLOCK,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin=uart_T_pin;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(uart_GPIO,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin=uart_R_pin;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(uart_GPIO,&GPIO_InitStruct);
}
void uart()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate=BaudRate;
USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStruct.USART_Parity=USART_Parity_No;
USART_InitStruct.USART_StopBits=USART_StopBits_1;
USART_InitStruct.USART_WordLength=USART_WordLength_8b;
USART_Init(uart_USARTx,& USART_InitStruct);
USART_ITConfig(uart_USARTx,USART_IT_RXNE, ENABLE);
USART_Cmd(uart_USARTx, ENABLE);
}
static void NVIC_config()
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn ;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct);
}
void uart_config()
{
uart();
uart_gpio();
//NVIC_config();
}
void Send_Byte(USART_TypeDef* USART_x, uint16_t Data)
{
USART_SendData(USART_x, Data);
while(USART_GetFlagStatus( USART_x, USART_FLAG_TXE)==RESET);
}
void Send_Str(char *str)
{
while(*str!='\0')
{
Send_Byte(uart_USARTx,*str++);
}
while(USART_GetFlagStatus( uart_USARTx, USART_FLAG_TC)==RESET);
}
int fputc(int ch,FILE *f)
{
USART_SendData(uart_USARTx,(uint8_t) ch);
while(USART_GetFlagStatus( uart_USARTx, USART_FLAG_TXE)==RESET);
return (ch);
}
int fgetc(FILE *f)
{
while(USART_GetFlagStatus( uart_USARTx, USART_FLAG_RXNE)==RESET);
return (int)USART_ReceiveData(uart_USARTx);
}
//void USART1_IRQHandler(void)
//{
// uint16_t temp;
// while(USART_GetFlagStatus( uart_USARTx, USART_FLAG_RXNE)==RESET);
// temp=USART_ReceiveData(uart_USARTx);
// Send_Byte( uart_USARTx, temp);
//}
串口控制灯光
#include "stm32f10x.h" // 相当于51单片机中的 #include <reg51.h>
#include "bsp_led.h"
#include "KEY.h"
#include "OLED.h"
#include "beep.h"
#include "sysclock.h"
#include "exit.h"
#include "CountSensor.h"
#include "uart.h"
#include "stdio.h"
// 来到这里的时候,系统的时钟已经被配置成72M。
volatile uint32_t time = 0;
int main(void)
{
uart_config();
uint8_t ch;
printf("请选择灯光:\n");
printf("1:亮红灯\n");
printf("2:亮绿灯\n");
printf("3:亮蓝灯\n");
printf("4:亮白灯\n");
while(1)
{
ch=getchar();
LED_Init();
switch(ch)
{
case '1':
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
break;
case '2':
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
break;
case '3':
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
break;
case '4':
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
break;
}
}
}
时钟
定时器1s计时:
要点:1.时钟 2.初始化 3.中断使能 4.时钟使能
#include "timer.h"
static void timer()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period=10000-1;
TIM_TimeBaseInitStruct.TIM_Prescaler=7200-1;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM_x,&TIM_TimeBaseInitStruct);
TIM_ITConfig(TIM_x,TIM_IT_Update, ENABLE );
TIM_Cmd(TIM_x,ENABLE );
}
static void NVIC_config()
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStruct.NVIC_IRQChannel=TIM_IRQn ;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct);
}
void timer_config()
{
timer();
NVIC_config();
}
uint16_t getCount()
{
return TIM_GetCounter(TIM_x);
}
extern uint32_t num;
void TIM2_IRQHandler()
{
if(TIM_GetITStatus(TIM_x, TIM_IT_Update)==SET)
{
num++;
TIM_ClearITPendingBit(TIM_x, TIM_IT_Update);
}
}
#ifndef __timer_H
#define __timer_H
#include "stm32f10x.h"
#define TIM_x TIM2
#define TIM_IRQn TIM2_IRQn
void timer_config(void);
uint16_t getCount(void);
#endif
PWM呼吸灯
仿真:
软件仿真-8M
DARMSTM.DLL -pSTM32F103VE
!!!!!!!!!!可以是PORTB.1
呼吸灯:
要点: 1.GPIO配置 2.timer配置(时钟,初始化,使能)
3.pwm配置
#include "pwm.h"
static void gpio()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
}
static void timer()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period=100-1;
TIM_TimeBaseInitStruct.TIM_Prescaler=720-1;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(pwm_TIM_x,&TIM_TimeBaseInitStruct);
TIM_Cmd(pwm_TIM_x,ENABLE );
}
void pwm()
{
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse=0;
TIM_OC3Init( pwm_TIM_x, &TIM_OCInitStruct);
}
void pwm_config()
{
gpio();
timer();
pwm();
}
void set_Compare(uint16_t Compare)
{
TIM_SetCompare3(pwm_TIM_x, Compare);
}
#ifndef __pwm_H
#define __pwm_H
#include "stm32f10x.h"
#define pwm_TIM_x TIM3
void pwm_config(void);
#endif
#include "stm32f10x.h" // 相当于51单片机中的 #include <reg51.h>
#include "bsp_led.h"
#include "KEY.h"
#include "OLED.h"
#include "beep.h"
#include "sysclock.h"
#include "exit.h"
#include "CountSensor.h"
#include "uart.h"
#include "timer.h"
#include "stdio.h"
#include "pwm.h"
// 来到这里的时候,系统的时钟已经被配置成72M。
volatile uint32_t time = 0;
uint32_t num;
int main(void)
{
uint32_t i;
uart_config();
OLED_Init();
pwm_config();
while(1)
{
for(i=0;i<=100;i++)
{
TIM_SetCompare3(pwm_TIM_x, i);
wait_ms(10);
}
for(i=0;i<=100;i++)
{
TIM_SetCompare3(pwm_TIM_x,100-i);
wait_ms(10);
}
}
}
超声波模块
基本工作原理:
(1)采用IO口TRIG触发测距,给最少10us的高电平信呈。
(2)模块自动发送8个40khz的方波,自动检测是否有信号返回;
(3)有信号返回,通过IO口ECHO输出一个高电平,高电平持续的时间就是超声波从发射到返回的时间。测试距离=(高电平时间*声速(340M/S))/2;
代码:
#include "ultrasonic.h"
#include "sysclock.h"
static void gpio()
{
//PE6触发信号 PC4接收信号
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_ResetBits(GPIOE,GPIO_Pin_6);
GPIO_ResetBits(GPIOC,GPIO_Pin_4);
}
uint32_t ultrasonic(void)
{
uint32_t t=0;
uint32_t d=0;
gpio();
GPIO_SetBits(GPIOE,GPIO_Pin_6);
wait_us(20);
GPIO_ResetBits(GPIOE,GPIO_Pin_6);
//接收
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_4)==RESET)
{
t++;
wait_us(1);
if(t>=10000)
return 1;
}
t=0;
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_4))
{
t++;
wait_us(9); //9us==3mm
if(t>=10000)
return 2;
}
d=t/2*3/10;//换成cm
return d;
}