基于STM32F407ZE粤嵌GEC-M4开发板的呼吸灯、外部中断、定时器延时、PWM及按键密码系统 沿用https://blog.csdn.net/weixin_53403301/article/details/121531067?spm=1001.2014.3001.5502 的改进 直接上代码:
# include "stm32f4xx.h"
# include "GPIO.h"
# include "DELAY.h"
void init_EXTI0(void)
{
GPIO_InitTypeDef GPIO_KEEY_InitStruct;
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;
GPIO_Init(GPIOA,&GPIO_KEEY_InitStruct);
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);
}
void init_EXTI4(void)
{
GPIO_InitTypeDef GPIO_KEEY_InitStruct;
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;
GPIO_Init(GPIOE,&GPIO_KEEY_InitStruct);
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;
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);
EXTI_Init(&EXTI_InitStruct);
}
void close_EXTI0(void)
{
GPIO_InitTypeDef GPIO_KEEY_InitStruct;
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;
GPIO_Init(GPIOA,&GPIO_KEEY_InitStruct);
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);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStruct.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&NVIC_InitStruct);
}
void close_EXTI4(void)
{
GPIO_InitTypeDef GPIO_KEEY_InitStruct;
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;
GPIO_Init(GPIOE,&GPIO_KEEY_InitStruct);
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);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x1;
NVIC_InitStruct.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&NVIC_InitStruct);
}
void init_GPIOF_9_10_OUT()
{
GPIO_InitTypeDef GPIOF_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIOF_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIOF_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIOF_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIOF_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIOF_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOF,&GPIOF_InitStruct);
}
void init_GPIOE_13_14_OUT()
{
GPIO_InitTypeDef GPIOE_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIOE_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
GPIOE_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIOE_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIOE_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIOE_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE,&GPIOE_InitStruct);
}
void init_GPIO_EF_OUT()
{
GPIO_InitTypeDef GPIO_InitStruct;
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;
GPIO_Init(GPIOF,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
GPIO_Init(GPIOE,&GPIO_InitStruct);
}
void init_BEEP(void)
{
GPIO_InitTypeDef GPIOF_BEEP_InitStruct;
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;
GPIO_Init(GPIOF,&GPIOF_BEEP_InitStruct);
}
void init_KEEY(void)
{
GPIO_InitTypeDef GPIO_KEEY_InitStruct;
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;
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_TIM3(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 10000-1;
TIM_TimeBaseStructure.TIM_Prescaler = 8400-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);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3, ENABLE);
}
void init_TIM2(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 5000-1;
TIM_TimeBaseStructure.TIM_Prescaler = 8400-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);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE);
}
void init_TIM14_PF9(void)
{
GPIO_InitTypeDef GPIOF_InitStruct;
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;
GPIO_PinAFConfig(GPIOF, GPIO_PinSource9, GPIO_AF_TIM14);
GPIO_Init(GPIOF,&GPIOF_InitStruct);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 100-1;
TIM_TimeBaseStructure.TIM_Prescaler = 8400-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;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM14, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);
TIM_Cmd(TIM14, ENABLE);
}
void init_TIM13_PF8(void)
{
GPIO_InitTypeDef GPIOF_InitStruct;
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;
GPIO_PinAFConfig(GPIOF, GPIO_PinSource8, GPIO_AF_TIM13);
GPIO_Init(GPIOF,&GPIOF_InitStruct);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 100-1;
TIM_TimeBaseStructure.TIM_Prescaler = 8400-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;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM13, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM13, TIM_OCPreload_Enable);
TIM_Cmd(TIM13, ENABLE);
}
void mainloop(void)
{
init_GPIOF_9_10_OUT();
init_GPIOE_13_14_OUT();
init_BEEP();
init_KEEY();
int x;
int y;
int flag=0;
delay_ms(500);
while(1)
{
if(PA_I(0)==0)
{
PF_O(9)=0;
}
if(PE_I(2)==0)
{
PF_O(10)=0;
}
if(PE_I(3)==0)
{
PE_O(13)=0;
}
if(PE_I(4)==0)
{
PE_O(14)=0;
PE_O(13)=0;
PF_O(9)=0;
PF_O(10)=0;
delay_ms(500);
break;
}
else
{
PF_O(9)=1;
PF_O(10)=1;
PE_O(13)=1;
PE_O(14)=1;
}
}
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);
if( key0 == Bit_RESET)
{
y=0;
flag++;
while(1){GPIO_ResetBits(GPIOF,GPIO_Pin_9);
uint8_t key0 = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);
if(key0 == Bit_SET)GPIO_SetBits(GPIOF,GPIO_Pin_9);break;}
delay_ms(500);
}
if( key1 == Bit_RESET)
{
y=1;
flag++;
while(1){GPIO_ResetBits(GPIOF,GPIO_Pin_10);
uint8_t key1 = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2);
if(key1 == Bit_SET)GPIO_SetBits(GPIOF,GPIO_Pin_10);break;}
delay_ms(500);
}
if( key2 == Bit_RESET)
{
y=2;
flag++;
while(1){GPIO_ResetBits(GPIOE,GPIO_Pin_13);
uint8_t key2 = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3);
if(key2 == Bit_SET)GPIO_SetBits(GPIOE,GPIO_Pin_13);break;}
delay_ms(500);
}
if( key3 == Bit_RESET)
{
y=3;
flag++;
while(1){GPIO_ResetBits(GPIOE,GPIO_Pin_14);
uint8_t key3 = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4);
if(key3 == Bit_SET)GPIO_SetBits(GPIOE,GPIO_Pin_14);break;}
delay_ms(500);
}
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){
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
GPIO_ResetBits(GPIOE,GPIO_Pin_13);
GPIO_ResetBits(GPIOE,GPIO_Pin_14);
delay_ms(500);
GPIO_SetBits(GPIOF,GPIO_Pin_9);
GPIO_SetBits(GPIOF,GPIO_Pin_10);
GPIO_SetBits(GPIOE,GPIO_Pin_13);
GPIO_SetBits(GPIOE,GPIO_Pin_14);
}
if (buf[0]==3 && buf[1]==2 && buf[2]==1 && buf[3]==0)
{
GPIO_SetBits(GPIOF,GPIO_Pin_9);GPIO_SetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_SetBits(GPIOF,GPIO_Pin_10);GPIO_ResetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_SetBits(GPIOE,GPIO_Pin_13);GPIO_SetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_SetBits(GPIOE,GPIO_Pin_14);GPIO_ResetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_ResetBits(GPIOF,GPIO_Pin_9);GPIO_SetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_ResetBits(GPIOF,GPIO_Pin_10);GPIO_ResetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_ResetBits(GPIOE,GPIO_Pin_13);GPIO_SetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_ResetBits(GPIOE,GPIO_Pin_14);GPIO_ResetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_ResetBits(GPIOE,GPIO_Pin_14);GPIO_SetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_ResetBits(GPIOE,GPIO_Pin_13);GPIO_ResetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_ResetBits(GPIOF,GPIO_Pin_10);GPIO_SetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_ResetBits(GPIOF,GPIO_Pin_9);GPIO_ResetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_SetBits(GPIOE,GPIO_Pin_14);GPIO_SetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_SetBits(GPIOE,GPIO_Pin_13);GPIO_ResetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_SetBits(GPIOF,GPIO_Pin_10);GPIO_SetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
GPIO_SetBits(GPIOF,GPIO_Pin_9);GPIO_ResetBits(GPIOF,GPIO_Pin_8);
delay_us(10000);
break;
}
}
}
while(1)
{
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);
if( key0 == Bit_RESET)
{
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
GPIO_ResetBits(GPIOE,GPIO_Pin_13);
GPIO_ResetBits(GPIOE,GPIO_Pin_14);
GPIO_SetBits(GPIOF,GPIO_Pin_8);
}
if( key1 == Bit_RESET)
{
GPIO_SetBits(GPIOF,GPIO_Pin_10);
GPIO_SetBits(GPIOF,GPIO_Pin_9);
GPIO_SetBits(GPIOE,GPIO_Pin_14);
GPIO_SetBits(GPIOE,GPIO_Pin_13);
GPIO_ResetBits(GPIOF,GPIO_Pin_8);
}
if( key2 == Bit_RESET)
{
x=1;
while(x--)
{
GPIO_SetBits(GPIOF,GPIO_Pin_9);
delay_us(10000);
GPIO_SetBits(GPIOF,GPIO_Pin_10);
delay_us(10000);
GPIO_SetBits(GPIOE,GPIO_Pin_13);
delay_us(10000);
GPIO_SetBits(GPIOE,GPIO_Pin_14);
delay_us(10000);
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
delay_us(10000);
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
delay_us(10000);
GPIO_ResetBits(GPIOE,GPIO_Pin_13);
delay_us(10000);
GPIO_ResetBits(GPIOE,GPIO_Pin_14);
delay_us(10000);
}
}
if( key3 == Bit_RESET)
{
x=1;
while(x--)
{
GPIO_ResetBits(GPIOE,GPIO_Pin_14);
delay_us(10000);
GPIO_ResetBits(GPIOE,GPIO_Pin_13);
delay_us(10000);
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
delay_us(10000);
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
delay_us(10000);
GPIO_SetBits(GPIOE,GPIO_Pin_14);
delay_us(10000);
GPIO_SetBits(GPIOE,GPIO_Pin_13);
delay_us(10000);
GPIO_SetBits(GPIOF,GPIO_Pin_10);
delay_us(10000);
GPIO_SetBits(GPIOF,GPIO_Pin_9);
delay_us(10000);
}
}
}
}
void breath_LED(void)
{
TIM_SetCompare1(TIM14,100);
TIM_SetCompare1(TIM13,0);
int PWM;
while(PE_I(2)==0)
{
for(PWM=100;PWM>0;PWM--)
{
TIM_SetCompare1(TIM14,100-PWM);
TIM_SetCompare1(TIM13,100-PWM);
delay_ms(10);
if(PE_I(2)==1)
{
TIM_SetCompare1(TIM14,100);
TIM_SetCompare1(TIM13,0);
break;
}
}
for(PWM=0;PWM<100;PWM++)
{
TIM_SetCompare1(TIM14,100-PWM);
TIM_SetCompare1(TIM13,100-PWM);
delay_ms(10);
if(PE_I(2)==1)
{
TIM_SetCompare1(TIM14,100);
TIM_SetCompare1(TIM13,0);
break;
}
}
if(PE_I(2)==1)
{
TIM_SetCompare1(TIM14,100);
TIM_SetCompare1(TIM13,0);
break;
}
}
}
int main(void)
{
init_GPIO_EF_OUT();
init_BEEP();
init_KEEY();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
init_EXTI0();
init_EXTI4();
init_TIM14_PF9();
init_TIM13_PF8();
delay_ms(500);
PF_O(9)=1;
PF_O(10)=1;
PE_O(13)=1;
PE_O(14)=1;
TIM_SetCompare1(TIM14,100);
TIM_SetCompare1(TIM13,0);
while(1)
{
breath_LED();
}
}
void EXTI0_IRQHandler(void)
{
delay_us(100000);
if(EXTI_GetFlagStatus(EXTI_Line0) == SET)
{
TIM_SetCompare1(TIM14,100);
TIM_SetCompare1(TIM13,0);
init_TIM3();
init_TIM2();
PE_O(14)=0;
delay_us(100000);
PE_O(14)=1;
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
void EXTI4_IRQHandler(void)
{
delay_us(100000);
if(EXTI_GetFlagStatus(EXTI_Line4) == SET)
{
int z = 5;
while(z--)
{
PE_O(14)=0;
delay_us(50000);
PE_O(14)=1;
delay_us(50000);
}
}
EXTI_ClearITPendingBit(EXTI_Line4);
close_EXTI0();
close_EXTI4();
mainloop();
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update) == SET)
{
PE_O(13)^=1;
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
{
PF_O(10)^=1;
}
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
DELAY.c:
# include "stm32f4xx.h"
void delay_ms(unsigned int ms)
{
while(ms--)
{
SysTick->CTRL = 0;
SysTick->LOAD = 168000000/1000-1;
SysTick->VAL = 0;
SysTick->CTRL = 5;
while ((SysTick->CTRL & 0x00010000)==0);
}
SysTick->CTRL = 0;
}
void delay_us(unsigned int us)
{
while(us--)
{
SysTick->CTRL = 0;
SysTick->LOAD = 168000000/1000/1000-1;
SysTick->VAL = 0;
SysTick->CTRL = 5;
while ((SysTick->CTRL & 0x00010000)==0);
}
SysTick->CTRL = 0;
}
GPIO.H
#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)
#define GPIOB_ODR_Addr (GPIOB_BASE+20)
#define GPIOC_ODR_Addr (GPIOC_BASE+20)
#define GPIOD_ODR_Addr (GPIOD_BASE+20)
#define GPIOE_ODR_Addr (GPIOE_BASE+20)
#define GPIOF_ODR_Addr (GPIOF_BASE+20)
#define GPIOG_ODR_Addr (GPIOG_BASE+20)
#define GPIOH_ODR_Addr (GPIOH_BASE+20)
#define GPIOI_ODR_Addr (GPIOI_BASE+20)
#define GPIOA_IDR_Addr (GPIOA_BASE+16)
#define GPIOB_IDR_Addr (GPIOB_BASE+16)
#define GPIOC_IDR_Addr (GPIOC_BASE+16)
#define GPIOD_IDR_Addr (GPIOD_BASE+16)
#define GPIOE_IDR_Addr (GPIOE_BASE+16)
#define GPIOF_IDR_Addr (GPIOF_BASE+16)
#define GPIOG_IDR_Addr (GPIOG_BASE+16)
#define GPIOH_IDR_Addr (GPIOH_BASE+16)
#define GPIOI_IDR_Addr (GPIOI_BASE+16)
#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
|