|
STM32F103設(shè)計電路,在GPIO C口,分別接有一個開關(guān)K1和兩個指示燈LED1和LED2。兩個燈一亮一滅,每按一下開關(guān),兩個燈的亮滅狀態(tài)翻轉(zhuǎn)
#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類型的結(jié)構(gòu)體
GPIO_InitTypeDefGPIO_InitStructure;
//開啟GPIO相關(guān)的外設(shè)時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
//選擇要控制的GPIO引腳
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_10;
//設(shè)置GPIO引腳的輸入模式
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPU;//高電平
//調(diào)用庫函數(shù)初始化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++) { };
}
}
}
|
|