新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 基于STM32的LED和KEY

基于STM32的LED和KEY

作者: 时间:2016-12-01 来源:网络 收藏
#include "stm32f10x.h"

/***********************************************************************
************************************************************************/
GPIO_InitTypeDef GPIO_InitStructure;

本文引用地址:https://www.eepw.com.cn/article/201612/324393.htm

/***********************************************************************
************************************************************************/
void delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
//

u8 key(void)
{
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_13) == Bit_RESET ) //读取指定端口引脚的输入
{
return 1;
}
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15) == Bit_RESET ) //读取指定端口引脚的输入
{
return 2;
}
else
return 0;
}
/***********************************************************************
************************************************************************/
main()
{
u8 k=0;

/* GPIOD Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
/* Configure PD.2 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Configure PA.13 and PA15 in input pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);


while(1)
{
k=key();
if(k == 1 )
{
GPIO_WriteBit(GPIOD,GPIO_Pin_2,Bit_RESET); //置位或清除所选定的特定位
}
if(k == 2)
{
GPIO_WriteBit(GPIOD,GPIO_Pin_2,Bit_SET); //置位或清除所选定的特定位
}

}
}

/*PA.13和PA.15端口控制PD.2的LED灯的亮和灭*********************************************
*****************************************************************************/



关键词: STM32LEDKE

评论


技术专区

关闭
站长统计
×

英飞凌|直播中...
想了解下一代汽车电子EEA向中央计算+区域控制架构演进,及其如何推动域融合与软件定义汽车吗?快来和英飞凌一起在线讨论一下吧!参会赢好礼,速入>>