说明:目的是为了实现通过两个按键控制,一个实现控制流水灯的方向,另一个是控制流水灯的快慢。
2:芯片基于STM32F103RB 型号为——CT117E
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x.h"
#include "core_cm3.h"
#include "misc.h"
#define LED0 GPIO_Pin_8
#define LED1 GPIO_Pin_9
#define LED2 GPIO_Pin_10
#define LED3 GPIO_Pin_11
#define LED4 GPIO_Pin_12
#define LED5 GPIO_Pin_13
#define LED6 GPIO_Pin_14
#define LED7 GPIO_Pin_15
#define LED_ALL (LED0 | LED1 | LED2 | LED3 | LED4 | LED5 | LED6 | LED7)
#define LED_PORT GPIOC
#define N_LE GPIO_Pin_2
#define N_LE_PORT GPIOD
#define SWN 4 /*????*/
#define N_K1 GPIO_Pin_0
#define N_K2 GPIO_Pin_8
#define N_K3 GPIO_Pin_1
#define N_K4 GPIO_Pin_2
#define N_K1C GPIOA
#define N_K2C GPIOA
#define N_K3C GPIOB
#define N_K4C GPIOB
#define LED_OUT(x) GPIO_SetBits(LED_PORT,LED_ALL), \
GPIO_ResetBits(LED_PORT, x<<8), \
GPIO_SetBits(N_LE_PORT,N_LE), \
GPIO_ResetBits(N_LE_PORT,N_LE)
标记的定义:
typedef enum
{
FALSE = 0,
TRUE
}y_bool;
static y_bool led_t = TRUE; //发光标记
static y_bool rl = TRUE; //左移右移
static y_bool km = TRUE; //快慢
static y_bool key_t = FALSE; //按键检测标记
void init()
{
GPIO_InitTypeDef GPIO_InitStructure;
//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
//LED
GPIO_InitStructure.GPIO_Pin = LED_ALL ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LED_PORT , &GPIO_InitStructure);
/*N_LE*/
GPIO_InitStructure.GPIO_Pin=N_LE;
GPIO_Init(N_LE_PORT , &GPIO_InitStructure);
/* */
GPIO_InitStructure.GPIO_Pin = N_K1 | N_K2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(N_K1C , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = N_K3 | N_K4;
GPIO_Init(N_K3C , &GPIO_InitStructure);
/* 时钟*/
//SysTick_Config(8000);
}
//SysTick定义快慢
void SysTick_Handler()
{
static uint16_t i = 0;
if(++i >= 1000)
i = 0;
if(i % 20 == 0)
key_t = TRUE;
if(i%500 == 0)
led_t = TRUE;
}
/*时钟的初始化*/
void SysT()
{
if(km)
SysTick_Config(8000);
else
SysTick_Config(2000);
}
/*Led显示*/
void Led()
{
static uint8_t led_b = 0x01;
LED_OUT(led_b);//数据输出
SysT();//调用时钟
if(rl)//判断左移还是右移
{
if((led_b <<= 1) == 0x00)
led_b = 0x01;
}
else
{
if((led_b >>= 1) == 0x00)
led_b = 0x80;
}
}
/*按键的检测*/
uint8_t key_c()
{
static uint8_t key_o = 0x0f;
uint8_t i, key_n, x;
const uint16_t key_pin[SWN] = {N_K1, N_K2, N_K3, N_K4}; //引脚
const GPIO_TypeDef* key_port[SWN] = {N_K1C, N_K2C, N_K3C, N_K4C};//各个端口
for(key_n = 0, i = 0; i < SWN; i++)
{
key_n <<= 1;
if(GPIO_ReadInputDataBit((GPIO_TypeDef*)key_port[i], key_pin[i]))
key_n |= 0x01;
}
x = (key_n ^ key_o) & key_o;//检测有无按键按下
key_o = key_n;//当前按键值作为下一次比较的值
return x;
}
/*按键的处理*/
void key_d(uint8_t x)
{
if(x & 0x08)
rl = !rl;
if(x & 0x04)
km = !km;
}
int main()
{
uint8_t key_dn;
init();
while(1)
{
if(led_t)
{
led_t = FALSE;
Led();
}
/*°´¼üÒ»¼ì²â£¨×óÓÒÒƶ¯£©*/
if(key_t)
{
key_t = FALSE;
key_dn = key_c();
}
if(key_dn)
{
key_d(key_dn);
key_dn = 0x00;
}
}
}