stm32控制io
led.h
#include "led.h"
//初始化PF9和PF10为输出口.并使能这两个口的时钟
//LED IO初始化
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOF时钟
//GPIOF9,F10初始化设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
// GPIO_SetBits(GPIOA,GPIO_Pin_4 );//GPIOF9,F10设置高,灯灭
// GPIO_ResetBits(GPIOA,GPIO_Pin_5);
}
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOA,GPIOE时钟
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4; //KEY0 KEY1 KEY2对应引脚
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
// GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE2,3,4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;//WK_UP对应引脚PA0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;//下拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOA0
}
//u8 KEY_Scan(u8 mode)
//{
//
// if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0)
// return 1;
// else if(KEY1==0)return 2;
// else if(KEY2==0)return 3;
// else if(WK_UP==1)return 4;
// }else if(KEY0==1&&KEY1==1&&KEY2==1&&WK_UP==0)key_up=1;
// return 0;// 无按键按下
//}
led.c
#ifndef __LED_H
#define __LED_H
#include "sys.h"
//LED端口定义
#define LED0 PFout(9) // DS0
#define LED1 PFout(10) // DS1
void LED_Init(void);//初始化
void KEY_Init(void);
#endif