|
STM32F103设计电路,在GPIO C口,分别接有一个开关K1和两个指示灯LED1和LED2。两个灯一亮一灭,每按一下开关,两个灯的亮灭状态翻转 - #include"stm32f10x.h"
- /*pc5green,6red,10button*/
- void LED_Init(void)
- GPIO_InitTypeDef GPIO_InitStructure;
- {RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;//输出模式
- GPIO_Init(GPIOC,&GPIO_InitStructure);
- }
- void KEY_Init(void)
- {
- //定义一个GPIO_InitTypeDef类型的结构体
- GPIO_InitTypeDefGPIO_InitStructure;
- //开启GPIO相关的外设时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
- //选择要控制的GPIO引脚
- GPIO_InitStructure.GPIO_Pin= GPIO_Pin_10;
- //设置GPIO引脚的输入模式
- GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPU;//高电平
- //调用库函数初始化GPIO
- GPIO_Init(GPIOC,&GPIO_InitStructure);
- }
- int main(void)
- {int i;
- LED_Init();
- KEY_Init();
- GPIO_ResetBits(GPIOC,GPIO_Pin_5);//green亮
- GPIO_WriteBit(GPIOC,GPIO_Pin_6,Bit_SET);//red灭
- while(1){
- if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10))
- {
- GPIO_WriteBit(GPIOC,GPIO_Pin_5,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_5)));
- for(i=0;i<500000;i++) { } ;
- GPIO_WriteBit(GPIOC,GPIO_Pin_6,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_6)));
- for(i=0;i<500000;i++) { };
- }
- }
- }
复制代码
|
|