專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

基于STM32的LED和KEY

作者:黃賓山   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時間:2014年03月03日   【字體:

#include "stm32f10x.h"
/***********************************************************************
************************************************************************/
GPIO_InitTypeDef GPIO_InitStructure;

/***********************************************************************
************************************************************************/
void delay(vu32 nCount)
 {
 for(; nCount != 0; nCount--);
 }
//

u8 key(void)
{
 if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_13) == Bit_RESET )  //讀取指定端口引腳的輸入
  {
   return 1;
  }
 if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15) == Bit_RESET )      //讀取指定端口引腳的輸入
  {
   return 2;
  }
    else
   return 0;
}
/***********************************************************************
************************************************************************/
main()
{
 u8 k=0;

 /* GPIOD Periph clock enable */
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
 /* Configure PD.2 in output pushpull mode */
 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);


 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
 /* Configure PA.13 and PA15 in input pushpull mode */
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
 GPIO_Init(GPIOA, &GPIO_InitStructure);


 while(1)
 {
  k=key();
  if(k == 1 )
   {
    GPIO_WriteBit(GPIOD,GPIO_Pin_2,Bit_RESET); //置位或清除所選定的特定位
   }
  if(k == 2)
   {
    GPIO_WriteBit(GPIOD,GPIO_Pin_2,Bit_SET); //置位或清除所選定的特定位
   }    

 }
}

/*PA.13和PA.15端口控制PD.2的LED燈的亮和滅*********************************************
*****************************************************************************/

關(guān)閉窗口

相關(guān)文章