stm32F4の例7:stm32F407zgt6LED照明とキーを押す手順

ここに画像の説明を挿入

main.c

/***
	***************************************************************************
	*  1.按键每按下一次就改变LED的亮灭状态
	*	2.串口初始化时打印信息到串口助手
	* 	
	***************************************************************************
***/

#include "stm32f4xx.h"
#include "led.h"   
#include "delay.h"
#include "key.h"
#include "usart.h"  

int main(void)
{
    
    
	u8 key_flag = 0;	//按键标志
	

	Delay_Init();		//延时函数初始化
	LED_Init();			//LED初始化
	KEY_Init();			//按键IO口初始化
	Usart_Config();	// USART初始化函数
	
	printf("FK407测试\r\n");
	
	while (1)
	{
    
    	
		//每次按键按下对标志进行取反
		if( KEY_Scan() == KEY_ON )
		{
    
    			
			key_flag = ~key_flag;	
		}	
		
		//根据按键标志进行LED的亮灭操作
		if(key_flag == 0)
		{
    
    
			LED1_ON;
		}
		else
		{
    
    
			LED1_OFF;
		}
	}
	
	
}






led.h

#ifndef __LED_H
#define __LED_H

#include "stm32f4xx.h"

/*---------------------- LED配置宏 ------------------------*/

#define LED1_PIN             GPIO_Pin_13       		 // LED1 引脚      
#define LED1_PORT            GPIOC                  // LED1 GPIO端口     
#define LED1_CLK             RCC_AHB1Periph_GPIOC	 // LED1 GPIO端口时钟

/*---------------------- LED控制宏 ------------------------*/
					
#define LED1_ON 	  GPIO_ResetBits(LED1_PORT,LED1_PIN);	// 输出低电平,点亮LED1	
#define LED1_OFF 	  GPIO_SetBits(LED1_PORT,LED1_PIN);		// 输出高电平,关闭LED1	

/*---------------------- 函数声明 ----------------------------*/

void LED_Init(void);	//LED初始化函数


#endif //__LED_H


led.c

/***
	***************************************************************************
	*	@file  	led.c
	*	@version V1.0.0
	*	@brief   LED接口相关函数
   ***************************************************************************
   *  @description
	*
	*  初始化LED的IO口,配置为推挽输出、上拉、速度等级2M。
	* 	
	***************************************************************************
***/

#include "led.h"  

// 函数:LED IO口初始化
//
void LED_Init(void)
{
    
    		
	GPIO_InitTypeDef GPIO_InitStructure; //定义结构体
	RCC_AHB1PeriphClockCmd ( LED1_CLK, ENABLE); 	//初始化GPIOG时钟	
	
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;   //输出模式
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  //推挽输出
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;	//上拉
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //速度选择
	
	//初始化 LED1 引脚
	GPIO_InitStructure.GPIO_Pin = LED1_PIN;	 
	GPIO_Init(LED1_PORT, &GPIO_InitStructure);	
	
	GPIO_ResetBits(LED1_PORT,LED1_PIN);  //PG7输出低电平
}


key.h

#ifndef __KEY_H
#define __KEY_H

#include "stm32f4xx.h"
#include "delay.h"

#define	KEY_ON	 1		//按键按下
#define	KEY_OFF	 0		//按键放开

/*---------------------- 按键配置宏 ------------------------*/

#define KEY_PIN           GPIO_Pin_15        		 // KEY 引脚      
#define KEY_PORT          GPIOA                     // KEY GPIO端口     
#define KEY_CLK           RCC_AHB1Periph_GPIOA	    // KEY GPIO端口时钟

/*---------------------- 函数声明 ----------------------------*/

void 	KEY_Init(void);	//按键IO口初始化函数
u8		KEY_Scan(void);   //按键扫描

#endif //__KEY_H



キー、c

/***
	***************************************************************************
	*	@file  	key.c
	*	@version V1.0.0
	*	@brief   按键接口相关函数
   ***************************************************************************
   *  @description
	*
	*  初始化按键引脚,配置为上拉输入、速度等级2M。
	* 	
	***************************************************************************
***/

#include "key.h"  

// 函数:按键IO口初始化
//
void KEY_Init(void)
{
    
    		
	GPIO_InitTypeDef GPIO_InitStructure; //定义结构体
	RCC_AHB1PeriphClockCmd ( KEY_CLK, ENABLE); 	//初始化KEY时钟	
	
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;   //输出模式
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;	//上拉
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //速度选择
	GPIO_InitStructure.GPIO_Pin   = KEY_PIN;	 
	
	GPIO_Init(KEY_PORT, &GPIO_InitStructure);	

}

// 函数:按键扫描
//	返回:KEY_ON - 按键按下,KEY_OFF - 按键放开 
//			
u8	KEY_Scan(void)
{
    
    
	if( GPIO_ReadInputDataBit ( KEY_PORT,KEY_PIN) == 0 )	//检测按键是否被按下
	{
    
    	
		Delay_ms(10);	//延时消抖
		if(GPIO_ReadInputDataBit ( KEY_PORT,KEY_PIN) == 0)	//再次检测是否为低电平
		{
    
    
			while(GPIO_ReadInputDataBit ( KEY_PORT,KEY_PIN) == 0);	//等待按键放开
			return KEY_ON;	//返回按键按下标志
		}
	}
	return KEY_OFF;	
}



delay.c

/***
	***************************************************************************
	*	@file  	delay.c
	*	@brief   delay接口相关函数
   ***************************************************************************
   *  @description
	*
	*  SysTick定时器配置为1ms中断,实现毫秒延时
	* 	
	***************************************************************************
***/

#include "delay.h"

static u32 TimingDelay;  //计数变量


//	函数:延时初始化
//	说明:配置 SysTick 为1ms中断,并启动定时器
//
void Delay_Init(void)
{
    
    
	SysTick_Config(SystemCoreClock / 1000);  //配置SysTick时钟为1ms中断
}

//	函数:计时函数
//	说明:在 SysTick 中断服务函数里被调用
//
void TimingDelay_Decrement(void)
{
    
    
	if (TimingDelay != 0)
	{
    
     
		TimingDelay--;
	}
}

//	函数:毫秒延时
// 参数:nTime - 延时时间,单位ms
//	说明:每次调用都会重新给TimingDelay赋值,实现 n 毫秒的延时,最大延时 4294967295 ms。	
//
void Delay_ms(u32 nTime)
{
    
     
	TimingDelay = nTime;

	while(TimingDelay != 0);
}





delay.h

#ifndef __DELAY_H
#define __DELAY_H

#include "stm32f4xx.h"

void Delay_Init(void);				//延时函数初始化
void Delay_ms(u32 nTime);	//毫秒延时函数

#endif //__DELAY_H


usart.h

#ifndef __USART_H
#define __USART_H

#include "stdio.h"
#include "stm32f4xx.h"

/*----------------------USART配置宏 ------------------------*/

#define  USART1_BaudRate  115200

#define  USART1_TX_PIN				GPIO_Pin_9					// TX 引脚
#define	USART1_TX_PORT				GPIOA							// TX 引脚端口
#define	USART1_TX_CLK				RCC_AHB1Periph_GPIOA		// TX 引脚时钟
#define  USART1_TX_PinSource     GPIO_PinSource9			// 引脚源

#define  USART1_RX_PIN				GPIO_Pin_10             // RX 引脚
#define	USART1_RX_PORT				GPIOA                   // RX 引脚端口
#define	USART1_RX_CLK				RCC_AHB1Periph_GPIOA    // RX 引脚时钟
#define  USART1_RX_PinSource     GPIO_PinSource10        // 引脚源


/*---------------------- 函数声明 ----------------------------*/

void  Usart_Config (void);	// USART初始化函数

#endif //__USART_H


usart.c

/***
	************************************************************************************
	*	@file  	usart.c
	*	@version V1.0.0	
	*	@brief   usart 接口相关函数
   ************************************************************************************
   *  @description
	*
	*  1.初始化USART1的引脚 PA9/PA10,
	*  2.配置USART1工作在收发模式、数位8位、停止位1位、无校验、不使用硬件控制流控制,
	*    串口的波特率设置为115200,若需要更改波特率直接修改usart.h里的宏定义USART1_BaudRate。
	*  3.重定义fputc函数,用以支持使用printf函数打印数据
	*
	************************************************************************************
***/

#include "usart.h"  

// 函数:usart IO口初始化
//
void  USART_GPIO_Config	(void)
{
    
    
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_AHB1PeriphClockCmd ( USART1_TX_CLK|USART1_RX_CLK, ENABLE); 	//IO口时钟配置

	//IO配置
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;   	 //复用模式
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;   //推挽
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;		 //上拉
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //速度等级

	//初始化 TX	引脚
	GPIO_InitStructure.GPIO_Pin = USART1_TX_PIN;	 
	GPIO_Init(USART1_TX_PORT, &GPIO_InitStructure);	
	//初始化 RX 引脚													   
	GPIO_InitStructure.GPIO_Pin = USART1_RX_PIN;	
	GPIO_Init(USART1_RX_PORT, &GPIO_InitStructure);		
	
	//IO复用,复用到USART1
	GPIO_PinAFConfig(USART1_TX_PORT,USART1_TX_PinSource,GPIO_AF_USART1); 
	GPIO_PinAFConfig(USART1_RX_PORT,USART1_RX_PinSource,GPIO_AF_USART1);	
}

// 函数:USART 口初始化
//
void Usart_Config(void)
{
    
    		
	USART_InitTypeDef USART_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	
	// IO口初始化
	USART_GPIO_Config();
		 
	// 配置串口各项参数
	USART_InitStructure.USART_BaudRate 	 = USART1_BaudRate; //波特率
	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_Mode 	    = USART_Mode_Rx | USART_Mode_Tx; //发送和接收模式
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 不使用硬件流控制
	
	USART_Init(USART1,&USART_InitStructure); //初始化串口1
	USART_Cmd(USART1,ENABLE);	//使能串口1
}

// 函数:重定义fputc函数
//
int fputc(int c, FILE *fp)
{
    
    

	USART_SendData( USART1,(u8)c );	// 发送单字节数据
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);	//等待发送完毕 

	return (c); //返回字符
}



おすすめ

転載: blog.csdn.net/lmf666/article/details/110741461