main.c函数
#include "math.h"
#include "led.h"
#include "key.h"
int main(void)
{
//初始化LED1234.
led_config(GPIOF,LED1_pin);
led_config(GPIOF,LED2_pin);
led_config(GPIOF,LED3_pin);
led_config4();
//初始化按键
key1_config(GPIOE,key_pin2);
key1_config(GPIOE,key_pin3);
key1_config(GPIOE,key_pin4);
key_config();
//将LED灯点亮
led_T(LED_port,LED1_pin,1);
led_T(LED_port,LED2_pin,1);
led_T(LED_port,LED3_pin,1);
led_T(LED4_port,LED4_pin,1);
while (1)
{
switch(key_on())
{
case 1:led_F(LED_port,LED1_pin);break;
case 2:led_F(LED_port,LED2_pin);break;
case 3:led_F(LED_port,LED3_pin);break;
case 4:led_F(LED4_port,LED4_pin);break;
}
}
}
led.c函数
#include "led.h"
//对LED1 2 3进行初始化
void led_config(GPIO_TypeDef *GPIOx,u32 pin)
{
//开时钟 LED1 2 3.
RCC->AHB1ENR |= (1<<5);
//输出类型
GPIOx->MODER &= ~(0X3<<(2*pin));
GPIOx->MODER |= (0X1<<(2*pin));
//推挽
GPIOx->OTYPER &= ~(0X1<<pin);
//输出速度:快速
GPIOx->OSPEEDR &= ~(0X3<<(2*pin));
GPIOx->OSPEEDR |= (0X2<<(2*pin));
//无上下拉
GPIOx->PUPDR &= ~(0X3<<(2*pin));
//
GPIOx->ODR |= (0X1<<pin);
}
//对LED4进行初始化
void led_config4(void)
{
//开时钟 LED1 2 3.
RCC->AHB1ENR |= (1<<2);
//输出类型
GPIOC->MODER &= ~(0X3<<0);
GPIOC->MODER |= (0X1<<0);
//推挽
GPIOC->OTYPER &= ~(0X1<<0);
//输出速度:快速
GPIOC->OSPEEDR &= ~(0X3<<0);
GPIOC->OSPEEDR |= (0X2<<0);
//无上下拉
GPIOC->PUPDR &= ~(0X3<<0);
//高电平关闭灯
GPIOC->ODR |= (0X1<<0);
}
void led_F(GPIO_TypeDef *GPIOx,u32 pin)
{
GPIOx->ODR ^= (0x1<<pin);
}
void led_T(GPIO_TypeDef *GPIOx,u32 pin,u32 OnOrOff)
{
if(OnOrOff == 1)
{
GPIOx->ODR &= ~(0x1<<pin);
}
if(OnOrOff == 0)
{
GPIOx->ODR |= (0x1<<pin);
}
}
key.c函数
#include "key.h"
#include "led.h"
#include "delay.h"
//key1初始化
void key_config(void)
{
RCC->AHB1ENR |= (1<<0);
GPIOA->MODER &= ~(0x1<<0);
GPIOA->PUPDR &= ~(0x1<<0);
}
//key2,3,4初始化
void key1_config(GPIO_TypeDef *GPIOx,u32 pin)
{
//开时钟
RCC->AHB1ENR |= (0x1<<4);
//输出类型
GPIOx->MODER &= ~(0x3<<(2*pin));
//无上下拉
GPIOx->PUPDR &= ~(0X3<<(2*pin));
}
u8 key_on(void)
{
if(GPIOA->IDR & (1<<0))//IDR是对按键的
{
Delay_nms(10);
//Delay_nms(10);
if(GPIOA->IDR & (1<<0))
{
while(GPIOA->IDR & (1<<0));
return 1;
}
}
if(!(GPIOE->IDR & (1<<2)))
{
Delay_nms(10);
//Delay_nms(10);
if(!(GPIOE->IDR & (1<<2)))
{
while(!(GPIOE->IDR & (1<<2)));
return 2;
}
}
if(!(GPIOE->IDR & (1<<3)))
{
Delay_nms(10);
//Delay_nms(10);
if(!(GPIOE->IDR & (1<<3)))
{
while(!(GPIOE->IDR & (1<<3)));
return 3;
}
}
if(!(GPIOE->IDR & (1<<4)))
{
Delay_nms(10);
//Delay_nms(10);
if(!(GPIOE->IDR & (1<<4)))
{
while(!(GPIOE->IDR & (1<<4)));
return 4;
}
}
return 0;
}
delay.c函数
#include "delay.h"
void Delay_nus(u32 n)
{
while(n--)
{
delay_1us();
}
}
void Delay_nms(u32 n)
{
n *= 1000;
while(n--)
{
delay_1us();
}
}
led.h函数
#ifndef __LED_H
#define __LED_H
#include "stm32f4xx.h"
#define LED_port GPIOF
#define LED1_pin 6
#define LED2_pin 9
#define LED3_pin 10
#define LED4_port GPIOC
#define LED4_pin 0
//对LED1 2 3,进行初始化的函数
void led_config(GPIO_TypeDef *GPIOx,u32 pin);
//对LED4进行初始化的函数
void led_config4(void);
void led_T(GPIO_TypeDef *GPIOx,u32 pin,u32 OnOrOff);
void led_F(GPIO_TypeDef *GPIOx,u32 pin);
#endif
key.h函数
#ifndef __KEY_H
#define __KEY_H
#include "stm32f4xx.h"
#define key_port GPIOE
#define key_pin2 2
#define key_pin3 3
#define key_pin4 4
void key_config(void);
void key1_config(GPIO_TypeDef *GPIOx,u32 pin);
u8 key_on(void);
#endif```
delay.h函数
#ifndef __DELAY_H
#define __DELAY_H
#include “stm32f4xx.h”
#define delay_1us() {
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
}
void Delay_nus(u32 n);
void Delay_nms(u32 n);
#endif
main.h函数为库文件。