main.c
#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();
KEY_Init();
Usart_Config();
printf("FK407测试\r\n");
while (1)
{
if( KEY_Scan() == KEY_ON )
{
key_flag = ~key_flag;
}
if(key_flag == 0)
{
LED1_ON;
}
else
{
LED1_OFF;
}
}
}
led.h
#ifndef __LED_H
#define __LED_H
#include "stm32f4xx.h"
#define LED1_PIN GPIO_Pin_13
#define LED1_PORT GPIOC
#define LED1_CLK RCC_AHB1Periph_GPIOC
#define LED1_ON GPIO_ResetBits(LED1_PORT,LED1_PIN);
#define LED1_OFF GPIO_SetBits(LED1_PORT,LED1_PIN);
void LED_Init(void);
#endif
led.c
#include "led.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd ( LED1_CLK, ENABLE);
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;
GPIO_InitStructure.GPIO_Pin = LED1_PIN;
GPIO_Init(LED1_PORT, &GPIO_InitStructure);
GPIO_ResetBits(LED1_PORT,LED1_PIN);
}
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
#define KEY_PORT GPIOA
#define KEY_CLK RCC_AHB1Periph_GPIOA
void KEY_Init(void);
u8 KEY_Scan(void);
#endif
key,c
#include "key.h"
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd ( KEY_CLK, ENABLE);
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);
}
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
#include "delay.h"
static u32 TimingDelay;
void Delay_Init(void)
{
SysTick_Config(SystemCoreClock / 1000);
}
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0)
{
TimingDelay--;
}
}
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
usart.h
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "stm32f4xx.h"
#define USART1_BaudRate 115200
#define USART1_TX_PIN GPIO_Pin_9
#define USART1_TX_PORT GPIOA
#define USART1_TX_CLK RCC_AHB1Periph_GPIOA
#define USART1_TX_PinSource GPIO_PinSource9
#define USART1_RX_PIN GPIO_Pin_10
#define USART1_RX_PORT GPIOA
#define USART1_RX_CLK RCC_AHB1Periph_GPIOA
#define USART1_RX_PinSource GPIO_PinSource10
void Usart_Config (void);
#endif
usart.c
#include "usart.h"
void USART_GPIO_Config (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd ( USART1_TX_CLK|USART1_RX_CLK, ENABLE);
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;
GPIO_InitStructure.GPIO_Pin = USART1_TX_PIN;
GPIO_Init(USART1_TX_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART1_RX_PIN;
GPIO_Init(USART1_RX_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(USART1_TX_PORT,USART1_TX_PinSource,GPIO_AF_USART1);
GPIO_PinAFConfig(USART1_RX_PORT,USART1_RX_PinSource,GPIO_AF_USART1);
}
void Usart_Config(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_GPIO_Config();
USART_InitStructure.USART_BaudRate = USART1_BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_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);
USART_Cmd(USART1,ENABLE);
}
int fputc(int c, FILE *fp)
{
USART_SendData( USART1,(u8)c );
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return (c);
}