新闻中心

EEPW首页>嵌入式系统>设计应用> 【STM32 Cotex-M3处理器系列编程】按键灯亮

【STM32 Cotex-M3处理器系列编程】按键灯亮

作者: 时间:2016-11-27 来源:网络 收藏
//分别按下S1~S4,D1~D4分别点亮
#include "stm32f10x.h"
void Delay(unsigned int x);
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE,ENABLE);//IO口使能设置
GPIO_InitTypeDef GPIO_InitStructure; //定义结构体
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;// LED管脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_13; //LED管脚
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;//按键管脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置为上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
while (1)
{
if(!(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_5)))
GPIO_SetBits(GPIOC, GPIO_Pin_6);
else
GPIO_ResetBits(GPIOC, GPIO_Pin_6);
if(!(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4)))
GPIO_SetBits(GPIOC, GPIO_Pin_7);
else
GPIO_ResetBits(GPIOC, GPIO_Pin_7);
if(!(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3)))
GPIO_SetBits(GPIOD, GPIO_Pin_13);
else
GPIO_ResetBits(GPIOD, GPIO_Pin_13);
if(!(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2)))
GPIO_SetBits(GPIOD, GPIO_Pin_6);
else
GPIO_ResetBits(GPIOD, GPIO_Pin_6);
}
}
void Delay(unsigned int x)
{
unsigned int t;
t=x;
while(t--);
}


评论


技术专区

关闭