1.GPIO,LED的点亮
IDR端口输入寄存器,读取输入状态GPIO的值。(只读并只能以字(16位)的形式读出)
ODR端口输出寄存器,设置输出状态的GPIO的值。(可读可写并只能以字(16位)的形式操作)
BSRR端口位设置/清除寄存器,设置单个位的输出值。设置单个位的值,可以置1和0。(只能写入并只能以字(16位)的形式操作)
BRR端口位清除寄存器,清除单个位的输出值。只能用于置0。(只能写入并只能以字(16位)的形式操作)
注:BSRR和BRR本质是操作ODR寄存器。
贴上代码如下:LED_Display传入的值1为亮0为灭
//led.h
#define LED1 (1)
#define LED2 (1<<1)
#define LED3 (1<<2)
#define LED4 (1<<3)
#define LED5 (1<<4)
#define LED6 (1<<5)
#define LED7 (1<<6)
#define LED8 (1<<7)
extern u8 LED_data;
//led.c
u8 LED_data=0;
void LED_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);
{
GPIO_InitTypeDef GPIO_InitStructure;
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);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All;//PC 8-15
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
LED_Display(0x00);
}
/*******************************************************************************
* Function Name : LED_Display
* Description :
* Input : 8位值,1为亮0为灭
* Output : None
* Return : None
*******************************************************************************/
//LED_Display( LED_data ^= LED1 ); 单个LED翻转
//LED_Display( LED_data |= LED1 ); 点亮单个LED
//LED_Display( LED_data &= ~LED1 ); 关闭单个LED
void LED_Display(u8 data)
{
GPIOC->ODR = (~data)<<8;
// GPIOD->BRR = GPIO_Pin_2;//573置0
GPIOD->BSRR = GPIO_Pin_2;//573置1
GPIOD->BRR = GPIO_Pin_2;//573置0 防止写lCD干扰到LED的状态
}
2.KEY,按键的使用
前言:在学习按键的时候看到网络上有两大类按键的配置
一类是扫描、占用CPU时间、反应不是非常及时、
一类是中断、不好消抖、进中断后打断主程序、
经考虑后还是决定使用扫描模式。
贴上代码如下:主要思路是在Systick_Hander里置位标志位,在main函数主循环里调用按键扫描函数。
void SysTick_Handler(void)
{
static u8 num_20ms=1;
if(++num_20ms >= 20)
{
num_20ms = 0;
KeyScan_flag = SET;
}
}
int main(void)
{
SysTick_Config(SystemCoreClock/1000);//1ms
Key_Init();