【2k行代码】基于stm32f4xx粤嵌GEC-M4的按键密码锁、呼吸灯、蜂鸣器音乐、超声波测距及倒车雷达、温湿度检测、光敏电阻自动灯光调节、USART串口控制系统

【2k行代码】基于stm32f4xx粤嵌GEC-M4的按键密码锁、呼吸灯、蜂鸣器音乐、超声波测距及倒车雷达、温湿度检测、光敏电阻自动灯光调节、USART串口控制系统

前文:
https://blog.csdn.net/weixin_53403301/article/details/121684671?spm=1001.2014.3001.5501
https://blog.csdn.net/weixin_53403301/article/details/121671026?spm=1001.2014.3001.5501
在此基础上增加了光敏电阻自动灯光调节 并增加了音频精度和国际标准音名

音乐播放部分参考:
https://blog.csdn.net/weixin_53403301/article/details/121735779?spm=1001.2014.3001.5501
【极限精度】基于stm32f4xx的蜂鸣器音乐播放及国际绝对音名标准频率定义(32位无符号整型精度、十二等律体系、A4=440.01000Hz)

资源:
https://download.csdn.net/download/weixin_53403301/59622180

第一个循环是按键密码锁 每次按下按键就会有对应LED灯点亮 同时蜂鸣器响一声 输入正确的密码才能进入第二模式 密码是四位 默认4321 输入错误则LED及蜂鸣器报警两次
第二模式即呼吸灯、蜂鸣器播放音乐系统 按键1播放生日快乐 按住按键2实现呼吸灯明暗循环变化 按键3播放Linkin Park的In The End
音乐播放截止前 长按按键1或按键3即能实现循环播放 按键4终止循环
第三模式是温湿度检测及超声波测距系统 通过USART串口显示数据 这里超声波测距模块的TX接到PB8 RX接到PB9
LED的PF10一直随距离变换而闪烁 按键1开启蜂鸣器报警 按键4关闭蜂鸣器 蜂鸣器报警和PF10的闪烁频率相同 距离越近 频率越快 同样此功能可以通过串口输入“S”“Q”来控制
在串口输入L即开启随湿度变化的呼吸灯 湿度越高 呼吸灯越亮 输入E开启流水灯循环 输入D关闭呼吸灯
长按0.5秒按键2则可以实现打断循环 按键3用于终止程序(return 0)
第四模式是光敏电阻的自动灯光调节系统 LED的PF9、PE13和14可以随着光照度变化而自动调节亮度 光照度越高 LED灯光越暗 反之越亮

在这里插入图片描述
代码如下:

# include "stm32f4xx.h"  //stm32库
# include  "GPIO.h"  //引脚宏定义库
# include  "DELAY.h"  //定时器精准延时函数库
# include  "GPIO_BUS.h"  //引脚总线输出库
# include  "MUSIC_NOTE.h"
# include  "PITCH.h"
# include <stdio.h>  //用于串口打印printf

unsigned int  times=0;  //测距计数值
unsigned int	S=0;  //测距结果
int32_t jugg_temp=0;  //判断温湿度获取的参数
uint8_t temp_buf[5]={0};  //温湿度赋值后的数组
static uint16_t tim13_period=0;  //音乐播放模式下 定时器13及PF8复用PWM的定时器分频值
/* PB8=TX PB9=RX */  //超声波测距模块TX接PB8 RX接PB9

//串口打印预设
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
//串口打印配置函数
int fputc(int ch, FILE *f)  
{
	USART_SendData(USART1,ch);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
	USART_ClearFlag(USART1,USART_FLAG_TXE);
	
	return ch;
}
//光敏电阻模式下的PWM输出定时器初始化
void init_TIM14_ADC()
{
	GPIO_InitTypeDef  GPIO_InitStructure;            //GPIO结构体设置
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;  //定时器14结构体设置
	TIM_OCInitTypeDef TIM_OCInitStruct;              //定时器PWM结构体设置

	//使能时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;          //LED9 在PF9引脚
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;       //初始化的复用功能(因为本引脚还要用到PWM输出功能)
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOF, &GPIO_InitStructure);

		//复用映射到定时器14
	GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);

		//定时器14的配置
	TIM_TimeBaseInitStruct.TIM_Prescaler        = 0; //不进行预分频               
	TIM_TimeBaseInitStruct.TIM_CounterMode      = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period           = 100-1;//设置周期为100
	TIM_TimeBaseInitStruct.TIM_ClockDivision    = 0; 
	TIM_TimeBaseInit(TIM14,&TIM_TimeBaseInitStruct);

		//PWM初始化配置
	TIM_OCInitStruct.TIM_OCMode =  TIM_OCMode_PWM1; //模式选择
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;  //选择低电平有效
	TIM_OCInitStruct.TIM_Pulse = 0;//配置占空比,满占空比是99
	TIM_OC1Init(TIM14,&TIM_OCInitStruct);

		//使能预装载寄存器
	TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); 

		//使能自动重装载寄存器
	TIM_ARRPreloadConfig(TIM14,ENABLE);
		//使能TIM14
	TIM_Cmd(TIM14,ENABLE); 
}
//光敏电阻模式下的PWM输出定时器初始化
void init_TIM1_ADC()
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIOE_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
	//初始化结构体
	GPIOE_InitStruct.GPIO_Pin  = GPIO_Pin_13; //定义引脚
	GPIOE_InitStruct.GPIO_Mode = GPIO_Mode_AF;  //定义输出模式
	GPIOE_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIOE_InitStruct.GPIO_OType = GPIO_OType_PP;		//定义推免输出
	GPIOE_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;		//定义是否有上下拉电阻 普通
	//复用功能 定时器14
	GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_TIM1);
	//将结构体给GPIOF组
	GPIO_Init(GPIOE,&GPIOE_InitStruct);
	
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;  //定时器复用功能结构体
	//NVIC_InitTypeDef NVIC_InitStructure;
	/* TIM3 clock enable */ //使能定时器3时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
	//重转载寄存器 定义计数值 决定了计数多少次就进行一次中断 不得大于分频以后的计数值 若改为5000为0.5秒
	TIM_TimeBaseStructure.TIM_Prescaler        = 0; //不进行预分频               
	TIM_TimeBaseStructure.TIM_CounterMode      = TIM_CounterMode_Up;
	TIM_TimeBaseStructure.TIM_Period           = 100-1;//设置周期为100
	TIM_TimeBaseStructure.TIM_ClockDivision    = 0; 
	TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
	
	TIM_OCInitStructure.TIM_OCMode =  TIM_OCMode_PWM1; //模式选择
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;  //选择低电平有效
	TIM_OCInitStructure.TIM_Pulse = 0;//配置占空比,满占空比是99
	TIM_OC3Init(TIM1,&TIM_OCInitStructure);
	
	//使能OC
	TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
	

	TIM_ARRPreloadConfig(TIM1,ENABLE);
	TIM_Cmd(TIM1, ENABLE);
	
	//定义GPIO结构体
	//GPIO_InitTypeDef GPIOE_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
	//初始化结构体
	GPIOE_InitStruct.GPIO_Pin  = GPIO_Pin_14; //定义引脚
	GPIOE_InitStruct.GPIO_Mode = GPIO_Mode_AF;  //定义输出模式
	GPIOE_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIOE_InitStruct.GPIO_OType = GPIO_OType_PP;		//定义推免输出
	GPIOE_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;		//定义是否有上下拉电阻 普通
	//复用功能 定时器14
	GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_TIM1);
	//将结构体给GPIOF组
	GPIO_Init(GPIOE,&GPIOE_InitStruct);
	
	//TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	//TIM_OCInitTypeDef  TIM_OCInitStructure;  //定时器复用功能结构体
	//NVIC_InitTypeDef NVIC_InitStructure;
	/* TIM3 clock enable */ //使能定时器3时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
	//重转载寄存器 定义计数值 决定了计数多少次就进行一次中断 不得大于分频以后的计数值 若改为5000为0.5秒
	TIM_TimeBaseStructure.TIM_Prescaler        = 0; //不进行预分频               
	TIM_TimeBaseStructure.TIM_CounterMode      = TIM_CounterMode_Up;
	TIM_TimeBaseStructure.TIM_Period           = 100-1;//设置周期为100
	TIM_TimeBaseStructure.TIM_ClockDivision    = 0; 
	TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
	//设置中断触发方式 计数更新
	TIM_ITConfig(TIM1,TIM_IT_Update, ENABLE);
	
	TIM_OCInitStructure.TIM_OCMode =  TIM_OCMode_PWM1; //模式选择
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;  //选择低电平有效
	TIM_OCInitStructure.TIM_Pulse = 0;//配置占空比,满占空比是99
	TIM_OC4Init(TIM1, &TIM_OCInitStructure);	//OC4表示通道4 即CH4
	//使能OC
	TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
	TIM_ARRPreloadConfig(TIM1,ENABLE);
	TIM_Cmd(TIM1, ENABLE);
}
//ADC通道初始化
void init_ADC(void) 
{
	//定义结构体
	GPIO_InitTypeDef  GPIO_InitStructure;
	ADC_CommonInitTypeDef ADC_CommonInitStruct;
	ADC_InitTypeDef ADC_InitStruct;
	//打开ADC3和GPIOF时钟使能
	RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOF, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);  
	/* 初始化ADC3对应的GPIOF引脚 */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模拟输入
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//输入输出频率
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//配置为不上拉也不下拉
	GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化

	ADC_DeInit();//对ADC进行复位(进行缺省初始化)

	ADC_CommonInitStruct.ADC_DMAAccessMode=ADC_DMAAccessMode_Disabled;//DMA失能
	ADC_CommonInitStruct.ADC_Mode=ADC_Mode_Independent;//独立模式
	ADC_CommonInitStruct.ADC_Prescaler=ADC_Prescaler_Div4;//APB2四分频 即84/4=21MHz
	ADC_CommonInitStruct.ADC_TwoSamplingDelay=ADC_TwoSamplingDelay_5Cycles;//两个采样阶段的延时5个时钟
	ADC_CommonInit(&ADC_CommonInitStruct);//初始化


	ADC_InitStruct.ADC_ContinuousConvMode=DISABLE;//关闭连续AD转换
	ADC_InitStruct.ADC_DataAlign=ADC_DataAlign_Right;//右对齐
	ADC_InitStruct.ADC_ExternalTrigConvEdge=ADC_ExternalTrigConvEdge_None;//禁止使用软件触发检测
	ADC_InitStruct.ADC_NbrOfConversion=5;//设置为通道5
	ADC_InitStruct.ADC_Resolution=ADC_Resolution_8b;//8位精度
	ADC_InitStruct.ADC_ScanConvMode=DISABLE;//非扫描模式
	ADC_Init(ADC3, &ADC_InitStruct);//初始化

	ADC_Cmd(ADC3, ENABLE);//开启ADC
}
 //获取ADC数模转换值的函数
uint16_t  get_ADC(uint8_t ch) //获得某个通道值 
{
	
	ADC_RegularChannelConfig(ADC3, ch, 1, ADC_SampleTime_480Cycles );//ADC3 通道ch 一个序列 采样时间		    

	ADC_SoftwareStartConv(ADC3);//启动ADC转换	
	 
	while(!ADC_GetFlagStatus(ADC3, ADC_FLAG_EOC ));//等待转换结束

	return ADC_GetConversionValue(ADC3);//读取转换结果
}

//温湿度模块初始化
void init_TEMP(void)
{
	//端口G硬件时钟使能
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);	
	
	GPIO_InitTypeDef GPIO_InitStructure;
	//配置PG9为输出模式
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;		//第9根引脚
	GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT;	//输出模式
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;	//推挽输出,增加输出电流能力。
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//高速响应
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;	//没有使能上下拉电阻

	GPIO_Init(GPIOG,&GPIO_InitStructure);		
	
	//PG9初始状态为高电平,看时序图
	PG_O(9)=1;
}
//读取温湿度模块
int32_t read_TEMP(uint8_t *pbuf)
{
	uint32_t t=0;
	int32_t i=0,j=0;
	uint8_t d=0;
	uint8_t *p=pbuf;
	uint32_t check_sum=0;
	
	GPIO_InitTypeDef GPIO_InitStructure;
	//---------------------通信开始的第一部分,主机信号
	//PG9设置为输出模式
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;		//第9根引脚
	GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT;	//输出模式
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;	//推挽输出,增加输出电流能力。
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//高速响应
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;	//没有使能上下拉电阻

	GPIO_Init(GPIOG,&GPIO_InitStructure);	
	
	PG_O(9)=0;
	delay_ms(20);
	
	PG_O(9)=1;
	delay_us(30);	
	
	//-----通信开始的第二部分,DHT信号
	//PG9设置为输入模式
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;		//第9根引脚
	GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN;		//输入模式
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//高速响应
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;	//没有使能上下拉电阻

	GPIO_Init(GPIOG,&GPIO_InitStructure);	

	//等待低电平出现
	t=0;
	while(PG_I(9))
	{
		t++;
		
		delay_us(1);
		
		if(t>=4000)
			return -1;
	}
	
	//测量低电平合法性
	t=0;
	while(PG_I(9)==0)
	{
		t++;
		
		delay_us(1);
		
		if(t>=1000)
			return -2;
	}

	//测量高电平合法性
	t=0;
	while(PG_I(9))
	{
		t++;
		delay_us(1);
		if(t>=1000)
			return -3;
	}
	
	//数据0和1的读取
	for(j=0; j<5; j++)
	{
		d=0;
		//一个字节的接收,从最高有效位开始接收
		for(i=7; i>=0; i--)
		{
			//等待数据0/数据1的前置低电平持续完毕
			t=0;
			while(PG_I(9)==0)
			{
				t++;
				delay_us(1);				
				if(t>=1000)
					return -4;
			}

			//延时40us
			delay_us(40);
		
			//判断当前PG9是否为高电平还是低电平
			//若是高电平,则为数据1;
			//若是低电平,则为数据0;
			if(PG_I(9))
			{
				d|=1<<i;//将变量d对应的比特位置1,如i=7,d|=1<<7就是将变量d的bit7置1
			
				//等待高电平持续完毕
				t=0;
				while(PG_I(9))
				{
					t++;
					delay_us(1);
					if(t>=1000)
						return -5;
				}		
			}
		}
		p[j]=d;	
	}
	
	//校验和
	check_sum = (p[0]+p[1]+p[2]+p[3])&0xFF;
	if(p[4] != check_sum)
		return -6;
	
	return 0;
}
//按键初始化
void init_KEEY(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIO_KEEY_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	//初始化结构体
	GPIO_KEEY_InitStruct.GPIO_Pin  = GPIO_Pin_0; //定义引脚
	GPIO_KEEY_InitStruct.GPIO_Mode = GPIO_Mode_IN;  //定义输出模式
	GPIO_KEEY_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIO_KEEY_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;		//定义是否有上下拉电阻 普通
	//将结构体给GPIOF组
	GPIO_Init(GPIOA,&GPIO_KEEY_InitStruct);
	
	GPIO_KEEY_InitStruct.GPIO_Pin  = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
	GPIO_Init(GPIOE,&GPIO_KEEY_InitStruct);
}
//蜂鸣器初始化
void init_BEEP(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIOF_BEEP_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	//初始化结构体
	GPIOF_BEEP_InitStruct.GPIO_Pin  = GPIO_Pin_8; //定义引脚
	GPIOF_BEEP_InitStruct.GPIO_Mode = GPIO_Mode_OUT;  //定义输出模式
	GPIOF_BEEP_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIOF_BEEP_InitStruct.GPIO_OType = GPIO_OType_PP;		//定义推挽输出
	GPIOF_BEEP_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;		//定义是否有上下拉电阻 普通
	//将结构体给GPIOF组
	GPIO_Init(GPIOF,&GPIOF_BEEP_InitStruct);
}
//关闭BEEP
void close_BEEP(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIOF_BEEP_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	//初始化结构体
	GPIOF_BEEP_InitStruct.GPIO_Pin  = GPIO_Pin_8; //定义引脚
	GPIOF_BEEP_InitStruct.GPIO_Mode = GPIO_Mode_IN;  //定义输出模式
	GPIOF_BEEP_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIOF_BEEP_InitStruct.GPIO_OType = GPIO_OType_PP;		//定义推挽输出
	GPIOF_BEEP_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;		//定义是否有上下拉电阻 普通
	//将结构体给GPIOF组
	GPIO_Init(GPIOF,&GPIOF_BEEP_InitStruct);
}
//两个引脚整体初始化函数
void init_GPIO_EF_OUT(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIO_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	//初始化结构体
	GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_9 | GPIO_Pin_10; //定义引脚
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;  //定义输出模式
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;		//定义推挽输出
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;		//定义是否有上下拉电阻 普通
	//将结构体给GPIOF组
	GPIO_Init(GPIOF,&GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_13 | GPIO_Pin_14; //定义引脚
	//将结构体给GPIOE组
	GPIO_Init(GPIOE,&GPIO_InitStruct);
}
//串口初始化函数
void init_USART1(unsigned int Baud)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIO_KEEY_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	//初始化结构体
	GPIO_KEEY_InitStruct.GPIO_Pin  = GPIO_Pin_9 | GPIO_Pin_10; //定义引脚
	GPIO_KEEY_InitStruct.GPIO_Mode = GPIO_Mode_AF;  //定义输出模式
	GPIO_KEEY_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIO_KEEY_InitStruct.GPIO_OType = GPIO_OType_PP;		//定义推挽输出
	GPIO_KEEY_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;		//定义是否有上下拉电阻 普通
	//将结构体给GPIOF组
	GPIO_Init(GPIOA,&GPIO_KEEY_InitStruct);
	
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);  //复用功能
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);  //复用功能
	
	USART_InitTypeDef USART_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	//配置串口
	USART_InitStructure.USART_BaudRate = Baud;  //波特率
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //8位数据位
	USART_InitStructure.USART_StopBits = USART_StopBits_1;  //1个停止位
  USART_InitStructure.USART_Parity = USART_Parity_No;  //无校验位
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  //无硬件流控制
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  //支持收发数据
	USART_Init(USART1, &USART_InitStructure);
	//中断方式
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  //使能RX

	//中断优先级
	NVIC_InitTypeDef NVIC_InitStructure;
 
   /* Enable the USARTx Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	//使能串口
	USART_Cmd(USART1, ENABLE);
}
//GPIOB_8初始化使能
void init_TX(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIO_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	//初始化结构体
	GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_8; //定义引脚
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;  //定义输出模式
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;		//定义推免输出
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;		//定义是否有上下拉电阻 普通
	//将结构体给GPIOB组
	GPIO_Init(GPIOB,&GPIO_InitStruct);
}
//GPIOB_9初始化使能
void init_RX(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIO_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	//初始化结构体
	GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_9; //定义引脚
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;  //定义输出模式
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;		//定义是否有上下拉电阻 普通
	//将结构体给GPIOB组
	GPIO_Init(GPIOB,&GPIO_InitStruct);
}
//定时器14及PF9复用PWM初始化
void init_TIM14_PF9(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIOF_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	//初始化结构体
	GPIOF_InitStruct.GPIO_Pin  = GPIO_Pin_9; //定义引脚
	GPIOF_InitStruct.GPIO_Mode = GPIO_Mode_AF;  //定义输出模式
	GPIOF_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIOF_InitStruct.GPIO_OType = GPIO_OType_PP;		//定义推免输出
	GPIOF_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;		//定义是否有上下拉电阻 普通
	//复用功能 定时器14
	GPIO_PinAFConfig(GPIOF, GPIO_PinSource9, GPIO_AF_TIM14);
	//将结构体给GPIOF组
	GPIO_Init(GPIOF,&GPIOF_InitStruct);
	
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;  //定时器复用功能结构体
	//NVIC_InitTypeDef NVIC_InitStructure;
	/* TIM3 clock enable */ //使能定时器3时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
	//重转载寄存器 定义计数值 决定了计数多少次就进行一次中断 不得大于分频以后的计数值 若改为5000为0.5秒
	TIM_TimeBaseStructure.TIM_Period = 100-1;  //10ms周期
	TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;  //定时器预分频 168000000/2/8400=10000 其值系统默认+1 每10000次计数为1秒
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;  //选择计数模式 向上计数
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数
	TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);
	//设置中断触发方式 计数更新
	TIM_ITConfig(TIM14,TIM_IT_Update, ENABLE);
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  //PWM模式1  有效状态为小于计数的比较值时
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  //输出使能
	TIM_OCInitStructure.TIM_Pulse = 0;  //设置比较值 小于x时为有效 总计数值为100 周期为10ms
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //有效状态为高电平
	//输出化OC
	TIM_OC1Init(TIM14, &TIM_OCInitStructure);
	//使能OC
	TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);
	
//	NVIC_InitTypeDef NVIC_InitStruct;
//	NVIC_InitStruct.NVIC_IRQChannel = TIM8_TRG_COM_TIM14_IRQn;
//	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; //中断抢占优先级
//	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; //响应优先级
//	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;  //这一项关闭的话 定时器也会关闭
//	NVIC_Init(&NVIC_InitStruct);
	
	TIM_Cmd(TIM14, ENABLE);
	TIM_CtrlPWMOutputs(TIM14,ENABLE);		//使能PWM输出控制
}
//定时器1及PE13复用PWM初始化
void init_TIM1_PE13(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIOE_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
	//初始化结构体
	GPIOE_InitStruct.GPIO_Pin  = GPIO_Pin_13; //定义引脚
	GPIOE_InitStruct.GPIO_Mode = GPIO_Mode_AF;  //定义输出模式
	GPIOE_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIOE_InitStruct.GPIO_OType = GPIO_OType_PP;		//定义推免输出
	GPIOE_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;		//定义是否有上下拉电阻 普通
	//复用功能 定时器14
	GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_TIM1);
	//将结构体给GPIOF组
	GPIO_Init(GPIOE,&GPIOE_InitStruct);
	
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;  //定时器复用功能结构体
	//NVIC_InitTypeDef NVIC_InitStructure;
	/* TIM3 clock enable */ //使能定时器3时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
	//重转载寄存器 定义计数值 决定了计数多少次就进行一次中断 不得大于分频以后的计数值 若改为5000为0.5秒
	TIM_TimeBaseStructure.TIM_Period = 100-1;  //10ms周期
	TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;  //定时器预分频 168000000/2/8400=10000 其值系统默认+1 每10000次计数为1秒
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;  //选择计数模式 向上计数
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
	//设置中断触发方式 计数更新
	TIM_ITConfig(TIM1,TIM_IT_Update, ENABLE);
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  //PWM模式1  有效状态为小于计数的比较值时
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  //输出使能
	TIM_OCInitStructure.TIM_Pulse = 0;  //设置比较值 小于x时为有效 总计数值为100 周期为10ms
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //有效状态为高电平
	//输出化OC
	TIM_OC3Init(TIM1, &TIM_OCInitStructure);	//OC3表示通道3 即CH3
	//使能OC
	TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
	
//	NVIC_InitTypeDef NVIC_InitStruct;
//	NVIC_InitStruct.NVIC_IRQChannel = TIM8_TRG_COM_TIM14_IRQn;
//	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; //中断抢占优先级
//	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; //响应优先级
//	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;  //这一项关闭的话 定时器也会关闭
//	NVIC_Init(&NVIC_InitStruct);
	
	TIM_Cmd(TIM1, ENABLE);
	TIM_CtrlPWMOutputs(TIM1,ENABLE);		//使能PWM输出控制
}
//定时器1及PE14复用PWM初始化
void init_TIM1_PE14(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIOE_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
	//初始化结构体
	GPIOE_InitStruct.GPIO_Pin  = GPIO_Pin_14; //定义引脚
	GPIOE_InitStruct.GPIO_Mode = GPIO_Mode_AF;  //定义输出模式
	GPIOE_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIOE_InitStruct.GPIO_OType = GPIO_OType_PP;		//定义推免输出
	GPIOE_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;		//定义是否有上下拉电阻 普通
	//复用功能 定时器14
	GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_TIM1);
	//将结构体给GPIOF组
	GPIO_Init(GPIOE,&GPIOE_InitStruct);
	
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;  //定时器复用功能结构体
	//NVIC_InitTypeDef NVIC_InitStructure;
	/* TIM3 clock enable */ //使能定时器3时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
	//重转载寄存器 定义计数值 决定了计数多少次就进行一次中断 不得大于分频以后的计数值 若改为5000为0.5秒
	TIM_TimeBaseStructure.TIM_Period = 100-1;  //10ms周期
	TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;  //定时器预分频 168000000/2/8400=10000 其值系统默认+1 每10000次计数为1秒
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;  //选择计数模式 向上计数
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
	//设置中断触发方式 计数更新
	TIM_ITConfig(TIM1,TIM_IT_Update, ENABLE);
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  //PWM模式1  有效状态为小于计数的比较值时
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  //输出使能
	TIM_OCInitStructure.TIM_Pulse = 0;  //设置比较值 小于x时为有效 总计数值为100 周期为10ms
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //有效状态为高电平
	//输出化OC
	TIM_OC4Init(TIM1, &TIM_OCInitStructure);	//OC4表示通道4 即CH4
	//使能OC
	TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
	
//	NVIC_InitTypeDef NVIC_InitStruct;
//	NVIC_InitStruct.NVIC_IRQChannel = TIM8_TRG_COM_TIM14_IRQn;
//	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; //中断抢占优先级
//	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; //响应优先级
//	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;  //这一项关闭的话 定时器也会关闭
//	NVIC_Init(&NVIC_InitStruct);
	
	TIM_Cmd(TIM1, ENABLE);
	TIM_CtrlPWMOutputs(TIM1,ENABLE);		//使能PWM输出控制
}
//定时器13及PF8复用PWM初始化
void init_TIM13_PF8_F(uint32_t freq)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIOF_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	//初始化结构体
	GPIOF_InitStruct.GPIO_Pin  = GPIO_Pin_8; //定义引脚
	GPIOF_InitStruct.GPIO_Mode = GPIO_Mode_AF;  //定义输出模式
	GPIOF_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIOF_InitStruct.GPIO_OType = GPIO_OType_PP;		//定义推免输出
	GPIOF_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;		//定义是否有上下拉电阻 普通
	//复用功能 定时器14
	GPIO_PinAFConfig(GPIOF, GPIO_PinSource8, GPIO_AF_TIM13);
	//将结构体给GPIOF组
	GPIO_Init(GPIOF,&GPIOF_InitStruct);
	
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;  //定时器复用功能结构体
	//NVIC_InitTypeDef NVIC_InitStructure;
	/* TIM3 clock enable */ //使能定时器3时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13, ENABLE);
	//重转载寄存器 定义计数值 决定了计数多少次就进行一次中断 不得大于分频以后的计数值 若改为5000为0.5秒
	TIM_TimeBaseStructure.TIM_Period = (8400000000000/freq)-1;  //10ms周期
	tim13_period = TIM_TimeBaseStructure.TIM_Period;
	TIM_TimeBaseStructure.TIM_Prescaler = 1-1;  //定时器预分频 168000000/2/8400=10000 其值系统默认+1 每10000次计数为1秒
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;  //选择计数模式 向上计数
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数
	TIM_TimeBaseInit(TIM13, &TIM_TimeBaseStructure);
	//设置中断触发方式 计数更新
	TIM_ITConfig(TIM13,TIM_IT_Update, ENABLE);
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  //PWM模式1  有效状态为小于计数的比较值时
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  //输出使能
	TIM_OCInitStructure.TIM_Pulse = 0;  //设置比较值 小于x时为有效 总计数值为100 周期为10ms
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //有效状态为高电平
	//输出化OC
	TIM_OC1Init(TIM13, &TIM_OCInitStructure);
	//使能OC
	TIM_OC1PreloadConfig(TIM13, TIM_OCPreload_Enable);

//	NVIC_InitTypeDef NVIC_InitStruct;
//	NVIC_InitStruct.NVIC_IRQChannel = TIM8_UP_TIM13_IRQn;
//	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; //中断抢占优先级
//	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; //响应优先级
//	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;  //这一项关闭的话 定时器也会关闭
//	NVIC_Init(&NVIC_InitStruct);
	
	TIM_Cmd(TIM13, ENABLE);
}
//设置定时器13的PWM占空比
//0%~100%  0.0 ~ 1
void TIM13_Set_duty(uint16_t duty)
{
	uint32_t cmp=0;
	
	cmp = (tim13_period+1)*duty/100;//比较值
	
	TIM_SetCompare1(TIM13,cmp);
}
//定时器3初始化
void init_TIM3_RX(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	//TIM_OCInitTypeDef  TIM_OCInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	/* TIM3 clock enable */ //使能定时器3时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	//重转载寄存器 定义计数值 决定了计数多少次就进行一次中断 不得大于分频以后的计数值 若改为5000为0.5秒
	TIM_TimeBaseStructure.TIM_Period = 2-1;  
	TIM_TimeBaseStructure.TIM_Prescaler = 84-1;  //定时器预分频 168000000/2/84=1000000 其值系统默认+1 每1000000次计数为1秒
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;  //选择计数模式 向上计数
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
	//设置中断触发方式 计数更新
	TIM_ITConfig(TIM3,TIM_IT_Update, ENABLE);
	
	/* Enable the TIM3 gloabal Interrupt *///使能定时器3的中断通道
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	//使能定时器3
	TIM_Cmd(TIM3, DISABLE);
}
//定时器2初始化
void init_TIM2_TX(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	//TIM_OCInitTypeDef  TIM_OCInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	/* TIM2 clock enable */ //使能定时器2时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	//重转载寄存器 定义计数值 决定了计数多少次就进行一次中断 不得大于分频以后的计数值 若改为5000为0.5秒
	TIM_TimeBaseStructure.TIM_Period = 5000-1;  
	TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;  //定时器预分频 168000000/2/8400=10000 其值系统默认+1 每10000次计数为1秒
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;  //选择计数模式 向上计数
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
	//设置中断触发方式 计数更新
	TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE);
	
	/* Enable the TIM2 gloabal Interrupt *///使能定时器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);
	//使能定时器2
	TIM_Cmd(TIM2, ENABLE);
}
//定时器4初始化
void init_TIM4_Warning(unsigned int tim)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	//TIM_OCInitTypeDef  TIM_OCInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	/* TIM3 clock enable */ //使能定时器3时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
	//重转载寄存器 定义计数值 决定了计数多少次就进行一次中断 不得大于分频以后的计数值 若改为5000为0.5秒
	TIM_TimeBaseStructure.TIM_Period = tim-1;  
	TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;  //定时器预分频 168000000/2/84=10000 其值系统默认+1 每10000次计数为1秒
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;  //选择计数模式 向上计数
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数
	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
	//设置中断触发方式 计数更新
	TIM_ITConfig(TIM4,TIM_IT_Update, ENABLE);
	
	/* Enable the TIM3 gloabal Interrupt *///使能定时器3的中断通道
	NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	//使能定时器4
	TIM_Cmd(TIM4, ENABLE);
}
//外部中断0初始化 PA0
void init_EXTI0(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIO_KEEY_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	//初始化结构体
	GPIO_KEEY_InitStruct.GPIO_Pin  = GPIO_Pin_0; //定义引脚
	GPIO_KEEY_InitStruct.GPIO_Mode = GPIO_Mode_IN;  //定义输出模式
	GPIO_KEEY_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIO_KEEY_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;		//定义是否有上下拉电阻 普通
	//将结构体给GPIOF组
	GPIO_Init(GPIOA,&GPIO_KEEY_InitStruct);
	//选择中断线 中断0 PA组
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0); 
	//使能中断时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); 
	//配置中断
	EXTI_InitTypeDef EXTI_InitStruct;
	EXTI_InitStruct.EXTI_Line = EXTI_Line0;
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发
	EXTI_InitStruct.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStruct);
	//嵌套向量中断控制器 
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x0; //中断抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x0; //响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
}
//外部中断4初始化 PE4
void init_EXTI4(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIO_KEEY_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
	//初始化结构体
	GPIO_KEEY_InitStruct.GPIO_Pin  = GPIO_Pin_4; //定义引脚
	GPIO_KEEY_InitStruct.GPIO_Mode = GPIO_Mode_IN;  //定义输出模式
	GPIO_KEEY_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIO_KEEY_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;		//定义是否有上下拉电阻 普通
	//将结构体给GPIOF组
	GPIO_Init(GPIOE,&GPIO_KEEY_InitStruct);
	//选择中断线 中断0 PA组
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE,EXTI_PinSource4); 
	//使能中断时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); 
	//配置中断
	EXTI_InitTypeDef EXTI_InitStruct;
	EXTI_InitStruct.EXTI_Line = EXTI_Line4;
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发
	EXTI_InitStruct.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStruct);
	
	//嵌套向量中断控制器 
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_InitStruct.NVIC_IRQChannel = EXTI4_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x0; //中断抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x0; //响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
}
//关闭外部中断0初始化 PA0
void close_EXTI0(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIO_KEEY_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	//初始化结构体
	GPIO_KEEY_InitStruct.GPIO_Pin  = GPIO_Pin_0; //定义引脚
	GPIO_KEEY_InitStruct.GPIO_Mode = GPIO_Mode_IN;  //定义输出模式
	GPIO_KEEY_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIO_KEEY_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;		//定义是否有上下拉电阻 普通
	//将结构体给GPIOF组
	GPIO_Init(GPIOA,&GPIO_KEEY_InitStruct);
	//选择中断线 中断0 PA组
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0); 
	//使能中断时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); 
	//配置中断
	EXTI_InitTypeDef EXTI_InitStruct;
	EXTI_InitStruct.EXTI_Line = EXTI_Line0;
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发
	EXTI_InitStruct.EXTI_LineCmd = DISABLE;  //关闭外部中断
	EXTI_Init(&EXTI_InitStruct);
}
//关闭中断4初始化 PE4
void close_EXTI4(void)
{
	//定义GPIO结构体
	GPIO_InitTypeDef GPIO_KEEY_InitStruct;
	//使能GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
	//初始化结构体
	GPIO_KEEY_InitStruct.GPIO_Pin  = GPIO_Pin_4; //定义引脚
	GPIO_KEEY_InitStruct.GPIO_Mode = GPIO_Mode_IN;  //定义输出模式
	GPIO_KEEY_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;	//定义速度
	GPIO_KEEY_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;		//定义是否有上下拉电阻 普通
	//将结构体给GPIOF组
	GPIO_Init(GPIOE,&GPIO_KEEY_InitStruct);
	//选择中断线 中断0 PA组
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE,EXTI_PinSource4); 
	//使能中断时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); 
	//配置中断
	EXTI_InitTypeDef EXTI_InitStruct;
	EXTI_InitStruct.EXTI_Line = EXTI_Line4;
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发
	EXTI_InitStruct.EXTI_LineCmd = DISABLE;  //关闭外部中断
	EXTI_Init(&EXTI_InitStruct);
}
//主循环函数
void mainloop(void)
{
	init_GPIO_EF_OUT();
	init_BEEP();
	init_KEEY();
	MUSIC_BPM=120;
	int y;
	int flag=0;
	delay_ms(500);
	
	while(1)//按键锁循环
	{
		int buf[4];
		uint8_t key0 = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);
		uint8_t key1 = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2);
		uint8_t key2 = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3);
		uint8_t key3 = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4);
		GPIO_SetBits(GPIOF,GPIO_Pin_9);
		GPIO_SetBits(GPIOF,GPIO_Pin_10);
		GPIO_SetBits(GPIOE,GPIO_Pin_13);
		GPIO_SetBits(GPIOE,GPIO_Pin_14);
		PF_O(8)=0;
		
		if( key0 == Bit_RESET)
		{
			y=0;
			flag++;
			while(1){GPIO_ResetBits(GPIOF,GPIO_Pin_9);PF_O(8)=1;
				uint8_t key0 = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);
				if(key0 == Bit_SET)GPIO_SetBits(GPIOF,GPIO_Pin_9);break;}
			delay_ms(200);PF_O(8)=0;PF_O(9)=1;
		}
		if( key1 == Bit_RESET)
		{
			y=1;
			flag++;
			while(1){GPIO_ResetBits(GPIOF,GPIO_Pin_10);PF_O(8)=1;
				uint8_t key1 = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2);
				if(key1 == Bit_SET)GPIO_SetBits(GPIOF,GPIO_Pin_10);break;}
			delay_ms(200);PF_O(8)=0;PF_O(10)=1;
		}
		if( key2 == Bit_RESET)
		{
			y=2;
			flag++;
			while(1){GPIO_ResetBits(GPIOE,GPIO_Pin_13);PF_O(8)=1;
				uint8_t key2 = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3);
				if(key2 == Bit_SET)GPIO_SetBits(GPIOE,GPIO_Pin_13);break;}
			delay_ms(200);PF_O(8)=0;PE_O(13)=1;
		}
		if( key3 == Bit_RESET)
		{
			y=3;
			flag++;
			while(1){GPIO_ResetBits(GPIOE,GPIO_Pin_14);PF_O(8)=1;
				uint8_t key3 = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4);
				if(key3 == Bit_SET)GPIO_SetBits(GPIOE,GPIO_Pin_14);break;}
			delay_ms(200);PF_O(8)=0;PE_O(14)=1;
		}
		if (flag >= 1){buf[flag-1]=y;}
		if (flag == 4){
			buf[flag-1]=y;flag=0;
			if (buf[0]==3 && buf[1]==2 && buf[2]==1 && buf[3]==0)
			{
				PF_O(9)=1;PF_O(10)=1;PE_O(13)=1;PE_O(14)=1;PF_O(8)=0;
				delay_ms(200);	
				PF_O(9)=0;PF_O(10)=0;PE_O(13)=0;PE_O(14)=0;PF_O(8)=1;
				delay_ms(200);
				break;
			}
			else if (buf[0]!=3 | buf[1]!=2 | buf[2]!=1 | buf[3]==0){
				PF_O(9)=1;PF_O(10)=1;PE_O(13)=1;PE_O(14)=1;PF_O(8)=0;
				delay_ms(200);
				PF_O(9)=0;PF_O(10)=0;PE_O(13)=0;PE_O(14)=0;PF_O(8)=1;
				delay_ms(200);
				PF_O(9)=1;PF_O(10)=1;PE_O(13)=1;PE_O(14)=1;PF_O(8)=0;
				delay_ms(200);
				PF_O(9)=0;PF_O(10)=0;PE_O(13)=0;PE_O(14)=0;PF_O(8)=1;
				delay_ms(200);
				PF_O(9)=1;PF_O(10)=1;PE_O(13)=1;PE_O(14)=1;PF_O(8)=0;
				delay_ms(200);
			}
		}	
	}
	PF_O(8)=0;
	init_TIM14_PF9();
	init_TIM1_PE13();
	init_TIM1_PE14();
	PF_O(9)=1;PF_O(10)=1;PE_O(13)=1;PE_O(14)=1;
	TIM_SetCompare1(TIM14,100);  //设置比较值
	TIM_SetCompare3(TIM1,100);
	TIM_SetCompare4(TIM1,100);
	int PWM;
	TIM_Cmd(TIM13, DISABLE);
	while(1)  //呼吸灯及播放音乐
	{	
		if(PE_I(4)==0)
		{
			TIM_Cmd(TIM13, DISABLE);
			TIM_Cmd(TIM14, DISABLE);
			TIM_Cmd(TIM1, DISABLE);
			close_BEEP();
			break;
		}
		while(PE_I(3)==0)
		{
			init_TIM13_PF8_F(A5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(E6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(E6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(C6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(B5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(B5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(B5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(B5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_8*1000);
			init_TIM13_PF8_F(C6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_8*1000);
			if(PE_I(3)==1)TIM13_Set_duty(0);break;
		}
		
		while(PA_I(0)==0)
		{
			init_TIM13_PF8_F(G5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_8*1000);
			init_TIM13_PF8_F(G5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_8*1000);
			init_TIM13_PF8_F(A5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(G5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(C6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(B5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_2*1000);
			
			init_TIM13_PF8_F(G5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_8*1000);
			init_TIM13_PF8_F(G5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_8*1000);
			init_TIM13_PF8_F(A5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(G5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(D6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(C6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_2*1000);
			
			init_TIM13_PF8_F(G5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_8*1000);
			init_TIM13_PF8_F(G5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_8*1000);
			init_TIM13_PF8_F(G6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(E6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(C6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(B5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(A5);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			delay_ms(NOTE_4*1000);
			
			init_TIM13_PF8_F(F6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_8*1000);
			init_TIM13_PF8_F(F6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_8*1000);
			init_TIM13_PF8_F(E6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(C6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(D6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_4*1000);
			init_TIM13_PF8_F(C6);
			TIM13_Set_duty(25);
			delay_ms(NOTE_1*1000);
			if(PA_I(0)==1)TIM13_Set_duty(0);break;
		}

		while(PE_I(2)==0)
		{
			for(PWM=100;PWM>0;PWM--)
			{
				TIM_SetCompare1(TIM14,100-PWM);  //设置比较值
				TIM_SetCompare3(TIM1,100-PWM);
				TIM_SetCompare4(TIM1,100-PWM);
				delay_ms(10);
				if(PE_I(2)==1)
				{
					TIM_SetCompare1(TIM14,100);  //设置比较值
					TIM_SetCompare3(TIM1,100);
					TIM_SetCompare4(TIM1,100);
					break;
				}
			}
			for(PWM=0;PWM<100;PWM++)
			{
				TIM_SetCompare1(TIM14,100-PWM);  //设置比较值
				TIM_SetCompare3(TIM1,100-PWM);
				TIM_SetCompare4(TIM1,100-PWM);
				delay_ms(10);
				if(PE_I(2)==1)
				{
					TIM_SetCompare1(TIM14,100);  //设置比较值
					TIM_SetCompare3(TIM1,100);
					TIM_SetCompare4(TIM1,100);
					break;
				}
			}
			if(PE_I(2)==1)
			{
				TIM_SetCompare1(TIM14,100);  //设置比较值
				TIM_SetCompare3(TIM1,100);
				TIM_SetCompare4(TIM1,100);
				break;
			}
		}
	}
}
//温湿度及距离测量
void Count(void)
{
	S=times*2*0.17;  //单位 mm
	if(jugg_temp == 0)
	{
		TIM_SetCompare1(TIM14,100-temp_buf[0]);  //设置比较值
		TIM_SetCompare3(TIM1,100-temp_buf[0]);
		TIM_SetCompare4(TIM1,100-temp_buf[0]);
		if(S==0)
		{
			printf(" 距离测量失败	|		| 温度:%d.%d℃	| 湿度:%d.%d%%\r\n",temp_buf[2],temp_buf[3],temp_buf[0],temp_buf[1]);  //测量失败
			printf("		|		|		|\r\n");
			TIM_Cmd(TIM4, DISABLE);
			PF_O(8)=0;
		}
		if(S>=1&S<30)
		{
			printf(" 距离过近	|		| 温度:%d.%d℃	| 湿度:%d.%d%%\r\n",temp_buf[2],temp_buf[3],temp_buf[0],temp_buf[1]);  //测量失败
			printf("		|		|		|\r\n");
			TIM_Cmd(TIM4, DISABLE);
			PF_O(8)=0;
		}
		if(S>=30&S<=4000)
		{
			printf("		| 距离:%dmm	| 温度:%d.%d℃	| 湿度:%d.%d%%\r\n",S,temp_buf[2],temp_buf[3],temp_buf[0],temp_buf[1]);
			printf("		|		|		|\r\n");
			init_TIM4_Warning(S);
		}
		if(S>4000)
		{
			printf(" 超出测量范围	|		| 温度:%d.%d℃	| 湿度:%d.%d%%\r\n",temp_buf[2],temp_buf[3],temp_buf[0],temp_buf[1]);
			printf("		|		|		|\r\n");
			TIM_Cmd(TIM4, DISABLE);
			PF_O(8)=0;
		}
	}
	else
	{
		if(S==0)
		{
			printf(" 距离测量失败	|		|		| 温湿度获取错误代码:%d	\r\n",jugg_temp);  //测量失败
			printf("		|		|		|\r\n");
			TIM_Cmd(TIM4, DISABLE);
			PF_O(8)=0;
		}
		if(S>=1&S<30)
		{
			printf(" 距离过近	|		|		| 温湿度获取错误代码:%d	\r\n",jugg_temp);  //测量失败
			printf("		|		|		|\r\n");
			TIM_Cmd(TIM4, DISABLE);
			PF_O(8)=0;
		}
		if(S>=30&S<=4000)
		{
			printf("		| 距离:%dmm	|		| 温湿度获取错误代码:%d	\r\n",S,jugg_temp);
			printf("		|		|		|\r\n");
			init_TIM4_Warning(S);
		}
		if(S>4000)
		{
			printf(" 超出测量范围	|		|		| 温湿度获取错误代码:%d	\r\n",jugg_temp);
			TIM_Cmd(TIM4, DISABLE);
			printf("		|		|		|\r\n");
			PF_O(8)=0;
		}
	}
	times=0;
	S=0;
}
//光敏电阻ADC转换及计算部分函数
void lux_ADC(void)
{
	uint8_t ADC_min=0,ADC_max=0;//记录光敏电阻产生的数据中的最大最小值
	uint8_t ADC_sj=0;			//读取到的光敏电阻ADC数据
	uint8_t ADC_PWM=0;			//PWM占空比
	init_ADC();					//初始化ADC
	init_KEEY();
	init_TIM14_ADC(); 			//初始化PWM
	init_TIM1_ADC();
	ADC_min = ADC_max = get_ADC(5);//最大最小值读取初始数据
	while(1)
	{
		ADC_sj = get_ADC(5);//采集数据
		/****** 进行归一化 ******/
		if(ADC_sj < ADC_min)	ADC_min = ADC_sj;
		if(ADC_sj > ADC_max)	ADC_max = ADC_sj;
		/****** 归一化结束 ******/
		ADC_PWM = (ADC_sj - ADC_min)*99/(ADC_max - ADC_min);//计算相对数据,范围:0~99
		TIM_SetCompare1(TIM14,ADC_PWM);//输入得到的数据,改变PWM占空比
		TIM_SetCompare3(TIM1,ADC_PWM);//输入得到的数据,改变PWM占空比
		TIM_SetCompare4(TIM1,ADC_PWM);//输入得到的数据,改变PWM占空比
		if(PA_I(0)==0)
		{
			TIM_Cmd(TIM2, DISABLE);
			TIM_Cmd(TIM3, DISABLE);
			TIM_Cmd(TIM4, DISABLE);
			TIM_Cmd(TIM1, DISABLE);
			TIM_ARRPreloadConfig(TIM14,DISABLE);
			TIM_ARRPreloadConfig(TIM1,DISABLE);
			TIM_Cmd(TIM14, DISABLE);
			TIM_Cmd(TIM13, DISABLE);
			USART_Cmd(USART1, DISABLE);
			close_EXTI4();
			close_EXTI0();
			init_GPIO_EF_OUT();
			init_BEEP();
			PF_BUS_O(0xFFFF);
			PE_BUS_O(0xFFFF);
			PF_O(8)=0;
			init_KEEY();
			break;
		}
	}
}
//主函数
int main(void)
{	
	mainloop();
	init_USART1(115200);
	init_TEMP();
//	init_KEEY();
//	init_BEEP();
	PF_O(8)=0;
	init_TX();
	PB_O(8)=0;
	init_RX();
	init_GPIO_EF_OUT();
	PF_BUS_O(0x0000);
	PE_BUS_O(0x0000);
	delay_ms(500);
	PF_BUS_O(0xFFFF);
	PE_BUS_O(0xFFFF);
	PF_O(8)=0;
//	init_TIM14_PF9();
//	init_TIM1_PE13();
//	init_TIM1_PE14();
	TIM_SetCompare1(TIM14,100);  //设置比较值
	TIM_SetCompare3(TIM1,100);
	TIM_SetCompare4(TIM1,100);
	printf(" by:Mike Zhou	| 网易独家音乐人	| 嵌入式软件工程师	| https://music.163.com/#/artist?id=12115205\n");
	printf("		|		|		| https://blog.csdn.net/weixin_53403301\r\n");
	init_EXTI0();
	init_EXTI4();
	init_TIM3_RX();  //默认关闭
	init_TIM2_TX();
	while(1)
	{
		while(!PB_I(9));
		TIM_Cmd(TIM3, ENABLE);
		while(PB_I(9));
		TIM_Cmd(TIM3, DISABLE);
		Count();
		jugg_temp = read_TEMP(temp_buf);
		if (PE_I(2)==0)
		{
			TIM_Cmd(TIM2, DISABLE);
			TIM_Cmd(TIM3, DISABLE);
			TIM_Cmd(TIM4, DISABLE);
			TIM_Cmd(TIM1, DISABLE);
			TIM_CtrlPWMOutputs(TIM14,DISABLE);		//使能PWM输出控制
			TIM_Cmd(TIM14, DISABLE);
			TIM_Cmd(TIM13, DISABLE);
			USART_Cmd(USART1, DISABLE);
			close_EXTI4();
			close_EXTI0();
			init_GPIO_EF_OUT();
			init_BEEP();
			PF_BUS_O(0xFFFF);
			PE_BUS_O(0xFFFF);
			PF_O(8)=0;
			init_KEEY();
			break;
		}
		if (PE_I(3)==0)
		{
			TIM_Cmd(TIM2, DISABLE);
			TIM_Cmd(TIM3, DISABLE);
			TIM_Cmd(TIM4, DISABLE);
			TIM_Cmd(TIM1, DISABLE);
			TIM_CtrlPWMOutputs(TIM14,DISABLE);		//使能PWM输出控制
			TIM_Cmd(TIM14, DISABLE);
			TIM_Cmd(TIM13, DISABLE);
			USART_Cmd(USART1, DISABLE);
			close_EXTI4();
			close_EXTI0();
			init_GPIO_EF_OUT();
			init_BEEP();
			PF_BUS_O(0xFFFF);
			PE_BUS_O(0xFFFF);
			PF_O(8)=0;
			init_KEEY();
			return 0;
		}
	}
	lux_ADC();
}
//中断0
void EXTI0_IRQHandler(void)
{
	if(EXTI_GetFlagStatus(EXTI_Line0) == SET) //中断产生
	{
		init_BEEP();
	}
	//结束后清空标志位
	EXTI_ClearITPendingBit(EXTI_Line0);
}
//中断4
void EXTI4_IRQHandler(void)
{
	if(EXTI_GetFlagStatus(EXTI_Line4) == SET) //中断产生
	{
		close_BEEP();
	}
	//结束后清空标志位
	EXTI_ClearITPendingBit(EXTI_Line4);
}
//测距报警的定时器2服务函数
void TIM4_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM4,TIM_IT_Update) == SET)
	{
		PF_O(8)=!PF_O(8);
		PF_O(10)=!PF_O(10);
	}
	TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
}
//测距技术定时器3服务函数
void TIM3_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM3,TIM_IT_Update) == SET)
	{
		times++;
	}
	TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
//定时器2服务函数 提供TX输入
void TIM2_IRQHandler(void)   //每500ms启动一次模块
{
	if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
	{
		PB_O(8)=1;
		delay_us(20);
		PB_O(8)=0;
	}
	TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
//串口1中断服务函数
void USART1_IRQHandler(void) 
{
	if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
	{	
		char data = USART_ReceiveData(USART1);
		USART_SendData(USART1,data);
		while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);//清空标志位
		if(data=='Q')
		{
			close_BEEP();
		}
		if(data=='S')
		{
			init_BEEP();
		}
		if(data=='L')
		{
			init_TIM14_PF9();
			init_TIM1_PE13();
			init_TIM1_PE14();
		}
		if(data=='D')
		{
			TIM_Cmd(TIM1, DISABLE);
			TIM_Cmd(TIM14, DISABLE);
			init_GPIO_EF_OUT();
			PF_O(9)=1;
			PE_O(13)=1;
			PE_O(14)=1;
		}
		if(data=='E')
		{
			TIM_Cmd(TIM1, DISABLE);
			TIM_Cmd(TIM14, DISABLE);
			init_GPIO_EF_OUT();
			PF_O(9)=1;
			PE_O(13)=1;
			PE_O(14)=1;
			for(int i=0;i<2;i++)
			{
				PF_O(9)=0;delay_ms(100);
				PF_O(9)=1;delay_ms(100);
				PE_O(13)=0;delay_ms(100);
				PE_O(13)=1;delay_ms(100);
				PE_O(14)=0;delay_ms(100);
				PE_O(14)=1;delay_ms(100);
				PF_O(9)=0;delay_ms(100);
				PE_O(13)=0;delay_ms(100);
				PE_O(14)=0;delay_ms(100);
				PE_O(14)=0;delay_ms(100);
				PE_O(13)=0;delay_ms(100);
				PF_O(9)=0;delay_ms(100);
				PF_O(9)=1;delay_ms(100);
				PE_O(13)=1;delay_ms(100);
				PE_O(14)=1;delay_ms(100);
			}
		}
	}
}


以下是导入的函数及库

# include "stm32f4xx.h"

void delay_ms(unsigned int ms)
{
	while(ms--)
	{
		SysTick->CTRL = 0; // Disable SysTick  关闭系统定时器
		SysTick->LOAD = 168000000/1000-1; // Count from 255 to 0 (256 cycles)  载入计数值 定时器从这个值开始计数
		SysTick->VAL = 0; // Clear current value as well as count flag  清空计数值到达0后的标记
		SysTick->CTRL = 5; // Enable SysTick timer with processor clock  使能168MHz的系统定时器
		while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set  等待
	}
	SysTick->CTRL = 0; // Disable SysTick  关闭系统定时器
}
void delay_us(unsigned int us)
{
	while(us--)
	{
		SysTick->CTRL = 0; // Disable SysTick  关闭系统定时器
		SysTick->LOAD = 168000000/1000/1000-1; // Count from 255 to 0 (256 cycles)  载入计数值 定时器从这个值开始计数
		SysTick->VAL = 0; // Clear current value as well as count flag  清空计数值到达0后的标记
		SysTick->CTRL = 5; // Enable SysTick timer with processor clock  使能168MHz的系统定时器
		while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set  等待
	}
	SysTick->CTRL = 0; // Disable SysTick  关闭系统定时器
}

#ifndef __DELAY_H__
#define __DELAY_H__

void delay_ms(unsigned int ms);
void delay_us(unsigned int us);

#endif

#ifndef __GPIO_H__
#define __GPIO_H__

#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2)) 
#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr)) 
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum)) 

#define GPIOA_ODR_Addr    (GPIOA_BASE+20) //0x40020014
#define GPIOB_ODR_Addr    (GPIOB_BASE+20) //0x40020414 
#define GPIOC_ODR_Addr    (GPIOC_BASE+20) //0x40020814 
#define GPIOD_ODR_Addr    (GPIOD_BASE+20) //0x40020C14 
#define GPIOE_ODR_Addr    (GPIOE_BASE+20) //0x40021014 
#define GPIOF_ODR_Addr    (GPIOF_BASE+20) //0x40021414    
#define GPIOG_ODR_Addr    (GPIOG_BASE+20) //0x40021814   
#define GPIOH_ODR_Addr    (GPIOH_BASE+20) //0x40021C14    
#define GPIOI_ODR_Addr    (GPIOI_BASE+20) //0x40022014     

#define GPIOA_IDR_Addr    (GPIOA_BASE+16) //0x40020010 
#define GPIOB_IDR_Addr    (GPIOB_BASE+16) //0x40020410 
#define GPIOC_IDR_Addr    (GPIOC_BASE+16) //0x40020810 
#define GPIOD_IDR_Addr    (GPIOD_BASE+16) //0x40020C10 
#define GPIOE_IDR_Addr    (GPIOE_BASE+16) //0x40021010 
#define GPIOF_IDR_Addr    (GPIOF_BASE+16) //0x40021410 
#define GPIOG_IDR_Addr    (GPIOG_BASE+16) //0x40021810 
#define GPIOH_IDR_Addr    (GPIOH_BASE+16) //0x40021C10 
#define GPIOI_IDR_Addr    (GPIOI_BASE+16) //0x40022010 
 
#define PA_O(n)   	BIT_ADDR(GPIOA_ODR_Addr,n)  //输出 
#define PA_I(n)    	BIT_ADDR(GPIOA_IDR_Addr,n)  //输入 

#define PB_O(n)   	BIT_ADDR(GPIOB_ODR_Addr,n)  //输出 
#define PB_I(n)    	BIT_ADDR(GPIOB_IDR_Addr,n)  //输入 

#define PC_O(n)   	BIT_ADDR(GPIOC_ODR_Addr,n)  //输出 
#define PC_I(n)    	BIT_ADDR(GPIOC_IDR_Addr,n)  //输入 

#define PD_O(n)   	BIT_ADDR(GPIOD_ODR_Addr,n)  //输出 
#define PD_I(n)    	BIT_ADDR(GPIOD_IDR_Addr,n)  //输入 

#define PE_O(n)   	BIT_ADDR(GPIOE_ODR_Addr,n)  //输出 
#define PE_I(n)    	BIT_ADDR(GPIOE_IDR_Addr,n)  //输入

#define PF_O(n)   	BIT_ADDR(GPIOF_ODR_Addr,n)  //输出 
#define PF_I(n)    	BIT_ADDR(GPIOF_IDR_Addr,n)  //输入

#define PG_O(n)   	BIT_ADDR(GPIOG_ODR_Addr,n)  //输出 
#define PG_I(n)    	BIT_ADDR(GPIOG_IDR_Addr,n)  //输入

#define PH_O(n)   	BIT_ADDR(GPIOH_ODR_Addr,n)  //输出 
#define PH_I(n)    	BIT_ADDR(GPIOH_IDR_Addr,n)  //输入

#define PI_O(n)  	 	BIT_ADDR(GPIOI_ODR_Addr,n)  //输出 
#define PI_I(n)   	BIT_ADDR(GPIOI_IDR_Addr,n)  //输入

#endif

# include "stm32f4xx.h"
# include "GPIO.h"

void PA_BUS_O(unsigned int num)  //输入值num最大为0xFFFF
{
	int i;
	for(i=0;i<16;i++)
	{
		PA_O(i)=(num>>i)&0x0001;
	}
}
unsigned int PA_BUS_I(void)  //输出值num最大为0xFFFF
{
	unsigned int num;
	int i;
	for(i=0;i<16;i++)
	{
		num=num+(PA_I(i)<<i)&0xFFFF;
	}
	return num;
}

void PB_BUS_O(unsigned int num)  //输入值num最大为0xFFFF
{
	int i;
	for(i=0;i<16;i++)
	{
		PB_O(i)=(num>>i)&0x0001;
	}
}
unsigned int PB_BUS_I(void)  //输出值num最大为0xFFFF
{
	unsigned int num;
	int i;
	for(i=0;i<16;i++)
	{
		num=num+(PB_I(i)<<i)&0xFFFF;
	}
	return num;
}

void PC_BUS_O(unsigned int num)  //输入值num最大为0xFFFF
{
	int i;
	for(i=0;i<16;i++)
	{
		PC_O(i)=(num>>i)&0x0001;
	}
}
unsigned int PC_BUS_I(void)  //输出值num最大为0xFFFF
{
	unsigned int num;
	int i;
	for(i=0;i<16;i++)
	{
		num=num+(PC_I(i)<<i)&0xFFFF;
	}
	return num;
}

void PD_BUS_O(unsigned int num)  //输入值num最大为0xFFFF
{
	int i;
	for(i=0;i<16;i++)
	{
		PD_O(i)=(num>>i)&0x0001;
	}
}
unsigned int PD_BUS_I(void)  //输出值num最大为0xFFFF
{
	unsigned int num;
	int i;
	for(i=0;i<16;i++)
	{
		num=num+(PD_I(i)<<i)&0xFFFF;
	}
	return num;
}

void PE_BUS_O(unsigned int num)  //输入值num最大为0xFFFF
{
	int i;
	for(i=0;i<16;i++)
	{
		PE_O(i)=(num>>i)&0x0001;
	}
}
unsigned int PE_BUS_I(void)  //输出值num最大为0xFFFF
{
	unsigned int num;
	int i;
	for(i=0;i<16;i++)
	{
		num=num+(PE_I(i)<<i)&0xFFFF;
	}
	return num;
}

void PF_BUS_O(unsigned int num)  //输入值num最大为0xFFFF
{
	int i;
	for(i=0;i<16;i++)
	{
		PF_O(i)=(num>>i)&0x0001;
	}
}
unsigned int PF_BUS_I(void)  //输出值num最大为0xFFFF
{
	unsigned int num;
	int i;
	for(i=0;i<16;i++)
	{
		num=num+(PF_I(i)<<i)&0xFFFF;
	}
	return num;
}

void PG_BUS_O(unsigned int num)  //输入值num最大为0xFFFF
{
	int i;
	for(i=0;i<16;i++)
	{
		PG_O(i)=(num>>i)&0x0001;
	}
}
unsigned int PG_BUS_I(void)  //输出值num最大为0xFFFF
{
	unsigned int num;
	int i;
	for(i=0;i<16;i++)
	{
		num=num+(PG_I(i)<<i)&0xFFFF;
	}
	return num;
}

void PH_BUS_O(unsigned int num)  //输入值num最大为0xFFFF
{
	int i;
	for(i=0;i<16;i++)
	{
		PH_O(i)=(num>>i)&0x0001;
	}
}
unsigned int PH_BUS_I(void)  //输出值num最大为0xFFFF
{
	unsigned int num;
	int i;
	for(i=0;i<16;i++)
	{
		num=num+(PH_I(i)<<i)&0xFFFF;
	}
	return num;
}

void PI_BUS_O(unsigned int num)  //输入值num最大为0xFFFF
{
	int i;
	for(i=0;i<16;i++)
	{
		PI_O(i)=(num>>i)&0x0001;
	}
}
unsigned int PI_BUS_I(void)  //输出值num最大为0xFFFF
{
	unsigned int num;
	int i;
	for(i=0;i<16;i++)
	{
		num=num+(PI_I(i)<<i)&0xFFFF;
	}
	return num;
}

#ifndef __GPIO_BUS_H__
#define __GPIO_BUS_H__

void PA_BUS_O(unsigned int num);
unsigned int PA_BUS_I(void);

void PB_BUS_O(unsigned int num);
unsigned int PB_BUS_I(void);

void PC_BUS_O(unsigned int num);
unsigned int PC_BUS_I(void);

void PD_BUS_O(unsigned int num);
unsigned int PD_BUS_I(void);

void PE_BUS_O(unsigned int num);
unsigned int PE_BUS_I(void);

void PF_BUS_O(unsigned int num);
unsigned int PF_BUS_I(void);

void PG_BUS_O(unsigned int num);
unsigned int PG_BUS_I(void);

void PH_BUS_O(unsigned int num);
unsigned int PH_BUS_I(void);

void PI_BUS_O(unsigned int num);
unsigned int PI_BUS_I(void);

#endif

#ifndef __PITCH_H__
#define __PITCH_H__

/*利用Python来编写定义
代码:
f = open('./PITCH.h', 'w')
C=44001000/((2**(1/12))**9)
Db=44001000/((2**(1/12))**8)
D=44001000/((2**(1/12))**7)
Eb=44001000/((2**(1/12))**6)
E=44001000/((2**(1/12))**5)
F=44001000/((2**(1/12))**4)
Gb=44001000/((2**(1/12))**3)
G=44001000/((2**(1/12))**2)
Ab=44001000/(2**(1/12))
A=44001000
Bb=44001000*(2**(1/12))
B=44001000*((2**(1/12))**2)
f.write('#ifndef __PITCH_H__\n')
f.write('#define __PITCH_H__\n')
f.write('\n')
for i in range(0,10):
    f.write('#define    C'+str(i)+'    '+str(int(C/(2**(4-i))))+'\n')
    f.write('#define    Db'+str(i)+'    '+str(int(Db/2**(4-i)))+'\n')
    f.write('#define    D'+str(i)+'    '+str(int(D/2**(4-i)))+'\n')
    f.write('#define    Eb'+str(i)+'    '+str(int(Eb/2**(4-i)))+'\n')
    f.write('#define    E'+str(i)+'    '+str(int(E/2**(4-i)))+'\n')
    f.write('#define    F'+str(i)+'    '+str(int(F/2**(4-i)))+'\n')
    f.write('#define    Gb'+str(i)+'    '+str(int(Gb/2**(4-i)))+'\n')
    f.write('#define    G'+str(i)+'    '+str(int(G/2**(4-i)))+'\n')
    f.write('#define    Ab'+str(i)+'    '+str(int(Ab/2**(4-i)))+'\n')
    f.write('#define    A'+str(i)+'    '+str(int(A/2**(4-i)))+'\n')
    f.write('#define    Bb'+str(i)+'    '+str(int(Bb/2**(4-i)))+'\n')
    f.write('#define    B'+str(i)+'    '+str(int(B/2**(4-i)))+'\n')
    f.write('\n')
f.write('#endif\n')
f.close()
*/

#define	C0	1635196
#define	Db0	1732430
#define	D0	1835446
#define	Eb0	1944587
#define	E0	2060219
#define	F0	2182726
#define	Gb0	2312517
#define	G0	2450027
#define	Ab0	2595713
#define	A0	2750062
#define	Bb0	2913589
#define	B0	3086840

#define	C1	3270393
#define	Db1	3464861
#define	D1	3670893
#define	Eb1	3889175
#define	E1	4120438
#define	F1	4365452
#define	Gb1	4625035
#define	G1	4900054
#define	Ab1	5191426
#define	A1	5500125
#define	Bb1	5827179
#define	B1	6173681

#define	C2	6540787
#define	Db2	6929723
#define	D2	7341786
#define	Eb2	7778351
#define	E2	8240876
#define	F2	8730904
#define	Gb2	9250070
#define	G2	9800108
#define	Ab2	10382853
#define	A2	11000250
#define	Bb2	11654358
#define	B2	12347363

#define	C3	13081575
#define	Db3	13859446
#define	D3	14683572
#define	Eb3	15556702
#define	E3	16481752
#define	F3	17461808
#define	Gb3	18500141
#define	G3	19600217
#define	Ab3	20765706
#define	A3	22000500
#define	Bb3	23308717
#define	B3	24694726

#define	C4	26163151
#define	Db4	27718893
#define	D4	29367144
#define	Eb4	31113405
#define	E4	32963504
#define	F4	34923616
#define	Gb4	37000283
#define	G4	39200434
#define	Ab4	41531413
#define	A4	44001000
#define	Bb4	46617435
#define	B4	49389452

#define	C5	52326302
#define	Db5	55437786
#define	D5	58734288
#define	Eb5	62226810
#define	E5	65927009
#define	F5	69847233
#define	Gb5	74000566
#define	G5	78400868
#define	Ab5	83062827
#define	A5	88002000
#define	Bb5	93234871
#define	B5	98778905

#define	C6	104652604
#define	Db6	110875572
#define	D6	117468576
#define	Eb6	124453621
#define	E6	131854019
#define	F6	139694467
#define	Gb6	148001132
#define	G6	156801737
#define	Ab6	166125654
#define	A6	176004000
#define	Bb6	186469742
#define	B6	197557810

#define	C7	209305209
#define	Db7	221751144
#define	D7	234937153
#define	Eb7	248907243
#define	E7	263708038
#define	F7	279388934
#define	Gb7	296002265
#define	G7	313603475
#define	Ab7	332251309
#define	A7	352008000
#define	Bb7	372939484
#define	B7	395115620

#define	C8	418610418
#define	Db8	443502288
#define	D8	469874307
#define	Eb8	497814487
#define	E8	527416077
#define	F8	558777869
#define	Gb8	592004530
#define	G8	627206951
#define	Ab8	664502618
#define	A8	704016000
#define	Bb8	745878969
#define	B8	790231241

#define	C9	837220836
#define	Db9	887004577
#define	D9	939748614
#define	Eb9	995628975
#define	E9	1054832155
#define	F9	1117555739
#define	Gb9	1184009061
#define	G9	1254413903
#define	Ab9	1329005236
#define	A9	1408032000
#define	Bb9	1491757939
#define	B9	1580462482

#endif

#ifndef __MUSIC_NOTE_H__
#define __MUSIC_NOTE_H__

unsigned int MUSIC_BPM=120;
#define	NOTE_4	(60/MUSIC_BPM)
#define	NOTE_8	(30/MUSIC_BPM)
#define	NOTE_16	(15/MUSIC_BPM)
#define	NOTE_32	(7.5/MUSIC_BPM)
#define	NOTE_64	(3.75/MUSIC_BPM)
#define	NOTE_2	(120/MUSIC_BPM)
#define	NOTE_1	(240/MUSIC_BPM)

#endif

版权声明:本文为CSDN博主「网易独家音乐人Mike Zhou」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_53403301/article/details/121843525

生成海报
点赞 0

网易独家音乐人Mike Zhou

我还没有学会写个人说明!

暂无评论

相关推荐

MCU串行通讯和并行通讯的区别以及UART的理解

假如我们需要从一个MCU发送一段数据到另一个MCU,我们可以选择两种通信方式,串行通信或者并行通信。 假如我们要发送的数据是数字198转化为二进制,就是11000110,如果使用串行通信

KEIL5安装C51依赖,解决KEIL5无法创建C51工程

KEIL5安装C51依赖,解决KEIL5无法创建C51工程 相信很多STM32初学者在把自己的keil4升级成keil5之后,再想创建89C51的工程却发现找不到89C51的芯片包了。今天给大家分享一下解决办法。

串口-RS232/RS485-TTL/CMOS

目录 串口通信基本原理 串口、RS-232、RS-485 (1)物理接口形式 (2)电平标准 TTL与CMOS: RS-232、RS-485、RS-422 串口形式