![]() |
發布時間: 2020-6-21 11:22
正文摘要:#include "stm32f10x.h" uint16_t temp,i; void Delay(unsigned int count) { unsigned int i; for(;count!=0;count--) &n ... |
是IPU(上拉輸入),而IPD是(下拉),很明顯PA0接了個電阻到VDD,而當有按鍵按下時,電路接通到地,所以PA0處讀到的電壓是0,當釋放時,與VDD想接,是高電平為1 |
我把程序改為 按鍵按下燈取反四次 #include "stm32f10x.h" #include "sys.h" //#include "intrins.h" #define LED1 PDout(2) // PD2 uint16_t temp,i; void Delay(unsigned int count) { unsigned int i; for(;count!=0;count--) { i=5000; while(i--); } } int main(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//注意GPIO_Mode_IPD不是GPIO_Mode_IPU GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD,&GPIO_InitStructure); temp=0x0001;//注要放在循環前邊 while(1) { if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1) //沒按下按鍵為0 按下按鍵為1 { Delay(200); if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1) { for(i=0;i<8;i++) { // GPIO_Write(GPIOD,temp); LED1=!LED1; Delay(1000); } } }while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0); //按鍵釋放應該放在按鍵判斷之外 } } |