/*****************主要的用戶自己編寫的文件*********************/
/**
***********************************************************************
* @file : main.c
* @author : xr
* @date : 2014年5月14日08:23:25
* @version : V1.0
* @brief : 采用輪詢方式檢測按鍵 STM32F103VE T6 MCU
***********************************************************************
* @attention
* 實驗平臺 : 野火ISO-MINI-STM32開發(fā)板
* 實驗現(xiàn)象 : 按下按鍵K2,實現(xiàn)LED1小燈的亮滅反轉(zhuǎn)
* 按下按鍵K3,實現(xiàn)LED2小燈的亮滅反轉(zhuǎn)
***********************************************************************
*/
#include "stm32f10x.h" /* 上帝之手 STM32庫頭文件 */
#include "led.h"
#include "key.h"
/**
* @brief : 主函數(shù)
* @param : 無
* @retval : 無
*/
int main(void)
{
ConfigLed(); /* 初始化LED控制端口 */
ConfigKey(); /* 初始化按鍵控制端口 */
LED1( ON ); /* 開始時點亮LED1 */
LED2( ON ); /* 開始時點亮LED2 */
while (1)
{
/* add your codes ^_^ */
/* 檢測按鍵K2是否按下 */
if ( KeyScan( GPIOA, GPIO_Pin_0 ) == KEY_ON )
{
/* LED1反轉(zhuǎn) 讀取GPIOB 0端口位的值并用1減去之后再寫入此位即LED1的控制位 */
GPIO_WriteBit( GPIOB, GPIO_Pin_0, (BitAction)(1 - GPIO_ReadOutputDataBit( GPIOB, GPIO_Pin_0 )));
}
/* 檢測按鍵K3是否按下 */
if ( KeyScan( GPIOC, GPIO_Pin_13 ) == KEY_ON )
{
/* LED2反轉(zhuǎn) */
GPIO_WriteBit( GPIOC, GPIO_Pin_4, (BitAction)(1 - GPIO_ReadOutputDataBit( GPIOC, GPIO_Pin_4 )));
}
}
}
/**********************************************END OF FILE******************/
/**
***************************************************************
* @file led.h
* @author xr
* @date 2014年5月17日13:00:58
* @brief LED小燈驅(qū)動頭文件
***************************************************************
* @attention
* 實驗平臺 : 野火ISO-MINI開發(fā)板
* LED小燈硬件連接 : -------------------
* | LED1 ---- PB0 |
* | LED2 ---- PC4 |
* | LED3 ---- PC3 |
* -------------------
***************************************************************
*/
#ifndef _LED_H_
#define _LED_H_
#include "stm32f10x.h" /* 使用stm32庫 */
/* 控制小燈: 1 滅 0 亮 */
#define ON 0
#define OFF 1
/* 帶參宏 */
#define LED1(a) if (a) \
GPIO_SetBits( GPIOB, GPIO_Pin_0 ); \
else \
GPIO_ResetBits( GPIOB, GPIO_Pin_0 )
#define LED2(a) if (a) \
GPIO_SetBits( GPIOC, GPIO_Pin_4 ); \
else \
GPIO_ResetBits( GPIOC, GPIO_Pin_4 )
#define LED3(a) if (a) \
GPIO_SetBits( GPIOC, GPIO_Pin_3 ); \
else \
GPIO_ResetBits( GPIOC, GPIO_Pin_3 )
void ConfigLed(void);
#endif /* _LED_H_ */
/*******************************************************END OF FILE*****/
/**
***************************************************************
* @file led.c
* @author xr
* @date 2014年5月17日13:00:58
* @brief LED小燈驅(qū)動
***************************************************************
* @attention
* 實驗平臺 : 野火ISO-MINI開發(fā)板
* LED小燈硬件連接 : -------------------
* | LED1 ---- PB0 |
* | LED2 ---- PC4 |
* | LED3 ---- PC3 |
* -------------------
***************************************************************
*/
#include "led.h"
/**
* @brief : 配置LED小燈的GPIO端口
* @param : 無
* @retval : 無
*/
void ConfigLed(void)
{
GPIO_InitTypeDef GPIO_InitStruct; /* 定義GPIO_InitTyPeDef結(jié)構(gòu)體變量 */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE ); /* 開啟APB2總線上的GPIOB和GPIOC時鐘 */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; /* 設(shè)置引腳為通用強推挽輸出 */
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; /* 設(shè)置輸出為50MHZ */
/* 調(diào)用庫函數(shù)初始化GPIOB端口 */
GPIO_Init ( GPIOB, &GPIO_InitStruct );
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_3; /* 選擇3,4引腳 */
/* 調(diào)用庫函數(shù)初始化GPIOC的34引腳 */
GPIO_Init (GPIOC, &GPIO_InitStruct );
/* 關(guān)閉LED */
GPIO_SetBits ( GPIOB, GPIO_Pin_0 );
GPIO_SetBits ( GPIOC, GPIO_Pin_4 | GPIO_Pin_3 );
}
/*************************************************END OF FILE***********/
/**
***************************************************************
* @file key.h
* @author xr
* @date 2014年5月17日13:00:58
* @brief 按鍵驅(qū)動頭文件
***************************************************************
* @attention
* 實驗平臺 : 野火ISO-MINI開發(fā)板
* 按鍵硬件連接 : ------------------
* | K2 ---- PA0 |
* | K3 ---- PC13 |
* ------------------
***************************************************************
*/
#ifndef _KEY_H_
#define _KEY_H_
#include "stm32f10x.h"
#define KEY_ON 0
#define KEY_OFF 1
void ConfigKey( void );
uint8_t KeyScan( GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin_x );
#endif /* _KEY_H_ */
/**********************************************END OF FILE*******/
/**
***************************************************************
* @file key.c
* @author xr
* @date 2014年5月17日13:00:58
* @brief 按鍵驅(qū)動
***************************************************************
* @attention
* 實驗平臺 : 野火ISO-MINI開發(fā)板
* 按鍵硬件連接 : ------------------
* | K2 ---- PA0 |
* | K3 ---- PC13 |
* ------------------
***************************************************************
*/
#include "key.h"
/**
* @brief : 不精確延時
* @param : nCounter
* @retval : 無
*/
static void KeyDelay( uint32_t nCounter )
{
while ( nCounter-- );
}
/**
* @brief : 配置按鍵用到的I/O口
* @param : 無
* @retval : 無
*/
void ConfigKey( void )
{
GPIO_InitTypeDef GPIO_InitStructure; /* 定義初始化端口的結(jié)構(gòu)體變量 */
/* 開啟按鍵2的時鐘 */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
//輸入模式不需要配置端口的輸出速率GPIO_Speed
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; /* 配置按鍵的引腳為上拉 */
/* 調(diào)用庫函數(shù)初始化key引腳 */
GPIO_Init( GPIOA, &GPIO_InitStructure );
}
/**
* @brief : 按鍵按下檢測
* @param : 端口 : GPIOx 端口位 : GPIO_Pin_x
* @retval : 按鍵的狀態(tài) : 按下 彈起
*/
uint8_t KeyScan( GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin_x )
{
/* 檢測是否有按鍵按下 */
if ( GPIO_ReadInputDataBit( GPIOx, GPIO_Pin_x ) == KEY_ON )
{
KeyDelay(0x57E40); /* 延時消抖 延時大約5ms */
if ( GPIO_ReadInputDataBit( GPIOx, GPIO_Pin_x ) == KEY_ON )
{
while ( GPIO_ReadInputDataBit( GPIOx, GPIO_Pin_x ) == KEY_ON ); /* 等待按鍵釋放 */
return KEY_ON;
}
else
{
return KEY_OFF;
}
}
return KEY_OFF;
}
/**************************************************END OF FILE*******/