久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 8173|回復(fù): 0
收起左側(cè)

STM32_GPIO配置及庫(kù)函數(shù)講解—LED跑馬燈

[復(fù)制鏈接]
ID:89763 發(fā)表于 2015-9-10 00:59 | 顯示全部樓層 |閱讀模式
本帖最后由 liuzhu 于 2015-9-10 01:18 編輯

gpio general-purpose input/output   通用輸入/輸出端口

GPIO寄存器縮寫(xiě)列表
GPIO 端口的每個(gè)位可以由軟件分別配置成多種模式。




復(fù)位期間和剛復(fù)位后,復(fù)用功能未開(kāi)啟,I/O 端口被配置成浮空輸入模式。

LED硬件連接如下圖所示:高電平點(diǎn)亮LED



要想成功點(diǎn)亮一個(gè)LED,程序所需如下步驟:(必須的

第一步:配置系統(tǒng)時(shí)鐘。見(jiàn)STM32F103x RCC寄存器配置


除此之外,還需將GPIO外設(shè)時(shí)鐘打開(kāi)。


    /* Enable GPIOC clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    第二步:配置中斷向量表。決定將程序下載到RAM中還是FLASH中。以后講。

void NVIC_Configuration(void)
{
#ifdef  VECT_TAB_RAM                 //VECT_TAB_RAM沒(méi)在程序中定義,所以將程序下載到Flash中
  /* Set the Vector Table base location at 0x20000000 */
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else  /* VECT_TAB_FLASH  */
  /* Set the Vector Table base location at 0x08000000 */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
#endif
}
第三步:配置GPIO的模式。輸入模式還是輸出模式。本章重點(diǎn)。void GPIO_Configuration(void)
    {
      GPIO_InitTypeDef GPIO_InitStructure;

      /* Configure PC.06, PC.07, PC.08 and PC.09 as Output push-pull */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
      GPIO_Init(GPIOC, &GPIO_InitStructure);
    }
其實(shí),使用GPIO十分簡(jiǎn)單,只需填寫(xiě)如下結(jié)構(gòu)體的成員變量typedef struct
    {
      u16 GPIO_Pin;                   //哪個(gè)管腳
      GPIOSpeed_TypeDef GPIO_Speed;   //如果是輸出模式的話(huà),還需要設(shè)置速度
      GPIOMode_TypeDef GPIO_Mode;     //管腳的類(lèi)型
    }GPIO_InitTypeDef;
然后,調(diào)用GPIO_Init函數(shù),GPIO的模式就配置好了。當(dāng)然,對(duì)于使用者來(lái)說(shuō),GPIO_Init函數(shù)相當(dāng)于“黑匣子”,我們不知道其內(nèi)部是怎樣實(shí)現(xiàn)的,執(zhí)行完步驟三。我們就可以向該管腳寫(xiě)1還是寫(xiě)0了。提示GPIO_Init設(shè)計(jì)的比較巧妙,大家有興趣的話(huà)可以跟蹤調(diào)試,將該函數(shù)中的變量添加到watch窗口,看看GPIO相關(guān)寄存器是怎樣變化的。第四步:向指定Port指定Pin,寫(xiě)1還是寫(xiě)0。上述原理圖中LED都是高電平點(diǎn)亮。需要介紹兩個(gè)庫(kù)函數(shù)。v GPIO_SetBits        向指定Port指定Pin寫(xiě)1:void GPIO_SetBits(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)
    {
      /* Check the parameters */
      assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
      assert_param(IS_GPIO_PIN(GPIO_Pin));
  
      GPIOx->BSRR = GPIO_Pin;
    }
涉及到GPIO_BSRR寄存器,如下所示



又牽扯到GPIOx_ODR,如下所示
v GPIO_ResetBits        向指定Port指定Pin寫(xiě)0:
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)
{
  /* Check the parameters */
  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  assert_param(IS_GPIO_PIN(GPIO_Pin));
  
  GPIOx->BRR = GPIO_Pin;
}
涉及到GPIO_BRR寄存器,如下所示




經(jīng)過(guò)上面4步,就可以成功驅(qū)動(dòng)LED。


下面給出LED跑馬燈程序:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"

/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void Delay(vu32 nCount);

/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Return         : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
  debug();
#endif
  
  /* Configure the system clocks */
  RCC_Configuration();
   
  /* NVIC Configuration */
  NVIC_Configuration();
  
  /* Configure the GPIO ports */
  GPIO_Configuration();
  
  /* Infinite loop */
  while (1)
  {
    GPIO_SetBits(GPIOC,GPIO_Pin_6);//點(diǎn)亮LED1
    Delay(1000000);
    Delay(1000000);//多點(diǎn)亮一會(huì),使人能看到LED的確切變化
    GPIO_ResetBits(GPIOC,GPIO_Pin_6);//熄滅LED1
   
    GPIO_SetBits(GPIOC,GPIO_Pin_7);//點(diǎn)亮LED2
    Delay(1000000);
    Delay(1000000);
    GPIO_ResetBits(GPIOC,GPIO_Pin_7);//熄滅LED2
   
    GPIO_SetBits(GPIOC,GPIO_Pin_8);//點(diǎn)亮LED3
    Delay(1000000);
    Delay(1000000);
    GPIO_ResetBits(GPIOC,GPIO_Pin_8);//熄滅LED3
   
    GPIO_SetBits(GPIOC,GPIO_Pin_9);//點(diǎn)亮LED4
    Delay(1000000);
    Delay(1000000);
    GPIO_ResetBits(GPIOC,GPIO_Pin_9);//熄滅LED4
  }
}

/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
  ErrorStatus HSEStartUpStatus;
  
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();

  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);

  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  if (HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);

    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
  
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);

    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);

    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

    /* Enable PLL */
    RCC_PLLCmd(ENABLE);

    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {}

    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08) {}
  }
  
  /* Enable GPIOC clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
}

/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures Vector Table base location.
* Input          : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef  VECT_TAB_RAM  
  /* Set the Vector Table base location at 0x20000000 */
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else  /* VECT_TAB_FLASH  */
  /* Set the Vector Table base location at 0x08000000 */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
#endif
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure PC.06, PC.07, PC.08 and PC.09 as Output push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}

/*******************************************************************************
* Function Name  : Delay
* Description    : Inserts a delay time.
* Input          : nCount: specifies the delay time length.
* Return         : None
*******************************************************************************/
void Delay(vu32 nCount)
{
  for(; nCount != 0; nCount--);
}

#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
  /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif


如何調(diào)試:在while (1)處設(shè)個(gè)斷點(diǎn)。

執(zhí)行完GPIO_Configuration函數(shù)后,觀察GPIO_CRL和GPIO_CRH寄存器,可以看到:


每個(gè)管腳模式配置由GPIO_CRL或GPIO_CRH中的4位決定,例如:PC6管腳由GPIO_CRL中的MODE6[1:0]和CNF6[1:0]這4位決定,其他的以此類(lèi)推。

涉及到GPIO_CRL寄存器,如下所示




   
因?yàn)镸ODE6[1:0]=11,查看上述表格,可以得出PC6是輸出模式,且最大速度是50MHZ。由于CNF6[1:0]=00且為輸出模式,所以以通用推挽輸出模式使用輸出驅(qū)動(dòng)器。





執(zhí)行完GPIO_SetBits(GPIOC,GPIO_Pin_6);                //點(diǎn)亮LED1,可以看到:GPIO_ODR的ODR6=1

執(zhí)行完GPIO_ResetBits(GPIOC,GPIO_Pin_6);                //熄滅LED1,可以看到:GPIO_ODR的ODR6=0

其他管腳如此類(lèi)推。




回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 色综合色综合色综合 | 国产免费一区二区 | 夜夜精品浪潮av一区二区三区 | 羞羞视频在线观看网站 | 国产免费观看久久黄av片涩av | 精品视频一区二区三区四区 | 久久久久久久久久久久91 | 天天操欧美 | 亚洲一区二区三区在线播放 | 成年免费大片黄在线观看岛国 | 狠狠入ady亚洲精品经典电影 | 亚洲综合色视频在线观看 | 欧美日韩18| 欧美日韩精品一区二区三区四区 | 日韩一级黄色毛片 | 伊人久久大香线 | 日韩图区 | 欧美精品一区三区 | 亚洲毛片在线观看 | 色综久久| 韩日一区二区 | 国产精品久久久亚洲 | 日韩在线免费播放 | 青青草原综合久久大伊人精品 | 国产在线视频一区二区 | 天天久久 | 无码日韩精品一区二区免费 | 狠狠综合久久av一区二区小说 | 欧美激情国产日韩精品一区18 | 日韩中文不卡 | 亚洲一区电影 | 精品久久久999 | 最新av中文字幕 | 久久精品二区亚洲w码 | 久久久久久久久久久久久9999 | 国产精品视频久久久 | 日韩成人免费视频 | 国产91观看 | 午夜视频一区二区 | 中国一级特黄真人毛片免费观看 | 中国一级特黄真人毛片 |