stm32:简单按键输入实现
开发环境keil4,芯片STM32F103C8T6
1、main.c
//串口实验
#include "sys.h"
#include "delay.h"
#include "key.h"
#define DC12VDO_ON() GPIO_SetBits (GPIOC, GPIO_Pin_13)
#define DC12VDO_OFF() GPIO_ResetBits (GPIOC, GPIO_Pin_13) int Index1,Index2,Index3 = ;
int time1;
unsigned char gUart_Rece_Buf1[];
unsigned char gUart_Rece_Buf2[];
unsigned char gUart_Rece_Buf3[];
int i,j=;
u8 key;
int main(void)
{
delay_init(); //延时函数初始化
sys_Init(); //系统初始化(时钟初始化、中断初始化、GPIOx初始化、串口1初始化、串口2初始化、串口3初始化)
while()
{
DC12VDO_OFF();//led常亮
key=KEY_Scan();
if(key==)
{
DC12VDO_ON();//灭led
delay_ms();//等待
}
}
}
2、key.c
#include "key.h"
#include "delay.h"
#include "sys.h"
uint8_t K1_Value;
u8 KEY_Scan(void)
{
if(!GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0))
{
delay_ms(); //防抖
if(!GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0))
{
K1_Value=;
while(!GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0));
}
}
else K1_Value=;
return K1_Value;
}
3、delay.c
#include "delay.h"
#include "sys.h"
////////////////////////////////////////////////////////////////////////////////// static u8 fac_us=;//us延时倍乘数
static u16 fac_ms=;//ms延时倍乘数 void delay_init() { SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //选择外部时钟 HCLK/8
fac_us=SystemCoreClock/; //为系统时钟的1/8
fac_ms=(u16)fac_us*; //非OS下,代表每个ms需要的systick时钟数 } void delay_us(u32 nus)
{
u32 temp;
SysTick->LOAD=nus*fac_us; //时间加载
SysTick->VAL=0x00; //清空计数器
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始倒数
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(<<))); //等待时间到达
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器
SysTick->VAL =0X00; //清空计数器
} void delay_ms(u16 nms)
{
u32 temp;
SysTick->LOAD=(u32)nms*fac_ms; //时间加载(SysTick->LOAD为24bit)
SysTick->VAL =0x00; //清空计数器
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始倒数
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(<<))); //等待时间到达
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器
SysTick->VAL =0X00; //清空计数器
}
4、sys.c
#include "sys.h" /*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;//HSEStartUpStatus是枚举函数的参数,两个参数ERROR = 0, SUCCESS = !ERROR
/* Setup the microcontroller system. Initialize the Embedded Flash Interface,
initialize the PLL and update the SystemFrequency variable. */
//SystemInit(); /* RCC system reset(for debug purpose) */
RCC_DeInit(); //初始化RCC /* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS)
{ /* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2); /* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Enable PLL */
RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
} /* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
} /* Enable GPIOx clock */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC , ENABLE );
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE );
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA , ENABLE );
RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO , ENABLE ); /* Enable USART1&2 clocks */
// Enable GPIOA clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE ); /* Enable USART3 clocks */
// Enable GPIOB clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE );
} /*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //--------------------------->PB0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); //-------------------------USART1_TX-->PA9 , USART1_RX-->PA10
/* Configure USART1_Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1_Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure); //-------------------------USART2_TX-->PA2 , USART2_RX-->PA3
/* Configure USART2_Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART2_Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure); //-------------------------USART3_TX-->PB10 , USART3_RX-->PB11
/* Configure USART3_Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure USART3_Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); } /*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
#define VECT_TAB_FLASH
//#define VECT_TAB_FLASH_IAP
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure; #if defined (VECT_TAB_RAM)
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#elif defined(VECT_TAB_FLASH_IAP)
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x18000);
__set_FAULTMASK();//开放总中断
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
//__set_FAULTMASK(0);//开放总中断
#endif /* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); /* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the USART3 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
} /*******************************************************************************
* Function Name : USART1_Configuration
* Description : Configures USART1.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure; /* USART1 configuration ------------------------------------------------------*/
/*
USART1 configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_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_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//USART_InitStructure.USART_Mode = USART_Mode_Rx; /* Configure the USART1 */
USART_Init(USART1, &USART_InitStructure); /* Enable USART1 Receive interrupt */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); /* Enable the USART1 */
USART_Cmd(USART1, ENABLE); /* 如下语句解决第1个字节无法正确发送出去的问题 */
USART_ClearFlag(USART1, USART_FLAG_TC); // 清标志
} /*******************************************************************************
* Function Name : USART2_Configuration
* Description : Configures USART2.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure; /* USART2 configuration ------------------------------------------------------*/
/*
USART2 configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_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_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//USART_InitStructure.USART_Mode = USART_Mode_Rx; /* Configure the USART2 */
USART_Init(USART2, &USART_InitStructure); /* Enable USART2 Receive interrupt */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); /* Enable the USART2 */
USART_Cmd(USART2, ENABLE); /* 如下语句解决第1个字节无法正确发送出去的问题 */
USART_ClearFlag(USART2, USART_FLAG_TC); // 清标志
} /*******************************************************************************
* Function Name : USART3_Configuration
* Description : Configures USART3.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART3_Configuration(void)
{
USART_InitTypeDef USART_InitStructure; /* USART3 configuration ------------------------------------------------------*/
/*
USART3 configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_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_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//USART_InitStructure.USART_Mode = USART_Mode_Rx; /* Configure the USART3 */
USART_Init(USART3, &USART_InitStructure); /* Enable USART3 Receive interrupt */
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); /* Enable the USART3 */
USART_Cmd(USART3, ENABLE); /* 如下语句解决第1个字节无法正确发送出去的问题 */
USART_ClearFlag(USART3, USART_FLAG_TC); // 清标志
} //BSP初始化函数
void sys_Init(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
USART1_Configuration();
USART2_Configuration();
USART3_Configuration();
} #ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */
while ()
{
}
}
#endif /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
stm32:简单按键输入实现的更多相关文章
- STM32基本GPIO操作:按键输入(扫描+外部中断)
(涉及专有名词较多,难免解释不到位,若有错误还请指出,谢谢!) 硬件连接图如下: 一.扫描 思路是在main函数中通过死循环来扫描端口电平状态检测,以此判断按键是否按下.实现较为简单. 1.初始化(注 ...
- linux下如何模拟按键输入和模拟鼠标【转】
转自:http://www.cnblogs.com/leaven/archive/2010/11/30/1891947.html 查看/dev/input/eventX是什么类型的事件, cat /p ...
- linux输入子系统(input subsystem)之按键输入和LED控制
实验现象:在控制台打印按键值,并且通过按键控制相应的LED亮灭. 1.代码 input_subsys_drv.c #include <linux/module.h> #include &l ...
- Python脚本控制的WebDriver 常用操作 <十二> send_keys模拟按键输入
下面将使用WebDriver中的send_keys来模拟键盘按键输入 测试用例场景 send_keys方法可以模拟一些组合键操作: ctrl+a ctrl+c ctrl+v 等. 另外有时候我们需要在 ...
- 1102: 零起点学算法09——继续练习简单的输入和计算(a-b)
1102: 零起点学算法09--继续练习简单的输入和计算(a-b) Time Limit: 1 Sec Memory Limit: 520 MB 64bit IO Format: %lldSub ...
- 1101: 零起点学算法08——简单的输入和计算(a+b)
1101: 零起点学算法08--简单的输入和计算(a+b) Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lldSubmitt ...
- ADB——模拟手机按键输入
基本命令 adb 模拟按键输入的命令主要通过 input 进行 Usage: input [<source>] <command> [<arg>...] The s ...
- adb命令模拟按键输入keycode
adb命令模拟按键输入keycode 2017年05月18日 14:57:32 阅读数:1883 例子: //这条命令相当于按了设备的Backkey键 adb shell input keyevent ...
- adb shell命令模拟按键/输入input使用keycode 列表详解
在adb shell里有一个非常使用的命令,模拟按键输入,这里首先不要理解为是键盘的模拟按键,下面命令的使用和键值做一个详解. input命令格式 adb shell input keyevent & ...
随机推荐
- 第八章、小节三keep-alive
主要缓存的是ajax中的json 我的路由中的内容被加载过一次,我就把路由中的内容放到内存中,下次再进入这个路由的时候,不需要重新加载页面,直接从内存中获取数据. 切换不同城市,调用不同城市数据 但是 ...
- 基于springcloud搭建项目-公共篇(二)
上一篇已经写过如何搭建注册中心eureka,这一篇主要是搭建一些公共的api接口服务,并把实体类单独拿出来放到一个服务上引用,比较简单的 1.首先.创建一个实体类服务,这样就不用在每个服务里创建实体了 ...
- 外部配置属性值是如何被绑定到XxxProperties类属性上的?--SpringBoot源码(五)
注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 SpringBoot是如何实现自动配置的?--SpringBoot源码(四) 温故而知新,我们来简单回顾一下上 ...
- 扩展el-input 加一个控制精度的命令(directives)
使用el-input时 出现需要精度控制的需求,因为每一个el-input需要精度不用 所以我给这个虚拟dom绑定了一个精度值,根据这个值去控制精度. 下面附上代码: html: <el-inp ...
- 【牛客】乃爱与城市拥挤程度 — 树形dp,up and down
我太难了 这题做得我要死了,来来回回写了大概八九个小时 错误的原因要么是快速幂写错(一生之敌,要么是忘取模爆\(longlong\)变负数\(QAQ\) \(update\) \(2019.11.13 ...
- CF1082B Vova and Trophies 题解
CF1082B Vova and Trophies 题解 瞎搞题,推荐的,一看是道水题,就随手A了-- 题目描述 Vova has won \(n\)trophies in different com ...
- oracle的sql语句优化
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- gRPC (1):入门及服务端创建和调用原理
1. RPC 入门 1.1 RPC 框架原理 RPC 框架的目标就是让远程服务调用更加简单.透明,RPC 框架负责屏蔽底层的传输方式(TCP 或者 UDP).序列化方式(XML/Json/ 二进制)和 ...
- VMware安装Ubutun之SSH为何安装不上之谜
一把心酸泪,鼻涕泪两行.谁人解我苦中苦,原是SSH安装不上去. 多方找寻,想要寻求解答. 首先我想到一个办法,找到进程,并且狠狠的用RM把它移除掉. NO,这个方法最后竟然不行,文件删了,锁还是获取不 ...
- STL vector容器 和deque容器
前言 STL是C++的框架,然后vector容器和deque容器又是STL的一部分... 这块的内容都是理解.概念为主,没什么捷径,希望读者能静下来记. 先来讲vector容器(单端动态数组) 1.v ...