测试下开发板的GPIO输入与输出测试,开发板上的按键采集输入,LED指示灯显示GPIO的输入变化。
一、硬件电路图
按键和LED灯部分电路图
二、测试程序
2.1、key.c
#include "main.h"
#include "key/key.h"
#include "led/led.h"
#include "usart/usart.h"
void init_key(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//KEY1
GPIO_InitStructure.Pins = KEY1_GPIO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
GPIO_InitStructure.IT = GPIO_IT_FALLING;
GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);
//KEY2
GPIO_InitStructure.Pins = KEY2_GPIO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
GPIO_InitStructure.IT = GPIO_IT_FALLING;
GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStructure);
}
uint8_t key_scan(uint8_t mode)
{
static uint8_t key_up = 1;
uint8_t keyval = 0;
if (mode) key_up = 1;
if (key_up && ((PA01_GETVALUE() == 0)||(PA02_GETVALUE() == 0)))
{
SysTickDelay(10);
key_up = 0;
if (PA01_GETVALUE() == 0) keyval = KEY2_PRES;
if (PA02_GETVALUE() == 0) keyval = KEY1_PRES;
}
else if ((PA01_GETVALUE()!=0) && (PA02_GETVALUE()!=0))
{
key_up = 1;
}
return keyval;
}
void key_test(void)
{
uint8_t key;
key = key_scan(0);
if (key)
{
switch (key)
{
case KEY1_PRES:
led1_tog();
break;
case KEY2_PRES:
led2_tog();
break;
}
}
}
2.2、led.c
#include "main.h"
#include "led/led.h"
#define LED_GPIO_PORT CW_GPIOB
#define LED_GPIO_PINS GPIO_PIN_8 | GPIO_PIN_9
void init_led(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//LED1
GPIO_InitStructure.Pins = LED1_GPIO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.IT = GPIO_IT_NONE;
GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure);
//LED2
GPIO_InitStructure.Pins = LED2_GPIO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.IT = GPIO_IT_NONE;
GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure);
}
2.3、main.c
#include "main.h"
#include "led/led.h"
#include "key/key.h"
#include "usart/usart.h"
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
/******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
/******************************************************************************
* Local variable definitions ('static') *
******************************************************************************/
//KEY
volatile uint8_t gKey1Status,gKey2Status; /* set to 1 after User Button interrupt */
int32_t main(void)
{
RCC_Configuration();
NVIC_Configuration();
init_led();
init_key();
gKey1Status = 0;
gKey2Status = 0;
printf("zhang\r\n");
while(1)
{
key_test();
}
}
三、程序运行
|