<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > 為什么要學(xué)STM32?答案就在這里

為什么要學(xué)STM32?答案就在這里

作者: 時(shí)間:2018-10-22 來(lái)源:網(wǎng)絡(luò ) 收藏

  為什么要學(xué)?

本文引用地址:http://dyxdggzs.com/article/201810/393174.htm

  是32位的卻只要八位的價(jià)格,速度也是八位的好幾倍。

  更重要的是它作為ARM入門(mén)級的芯片比較容易掌握,網(wǎng)上資料也很多,很多人都在用。

  的IO端口有7個(gè)寄存器來(lái)控制,但是我們常用的就4個(gè)CRL CRH IDR ODR 。

  端口配置低寄存器(GPIOx_CRL)

  端口配置高寄存器(GPIOx_CRH)

  端口輸入數據寄存器(GPIOx_IDR)

  端口輸出數據寄存器(GPIOx_ODR)

  其中CRL 控制高8位的 IO CRH 低8這兩個(gè)實(shí)質(zhì)是一樣的。

  對照我們AVR來(lái)看GPIOx_CRL就相當于DDRx ,GPIOx_ODR就相當于PORTx,GPIOx_ODR就相當于PINxSTM32的 IO 口可以由 口可以由 軟件配置成 軟件配置成 軟件配置成 8種模式: 種模式:



  1、輸入浮空

  2、輸入上拉

  3、輸入下拉

  4、模擬輸入

  5、開(kāi)漏輸出

  6、推挽輸出

  7、推挽式復用功能

  8、開(kāi)漏復用功能

  簡(jiǎn)單的來(lái)說(shuō)STM32的CRL寄存器可以設置輸入、輸出模式,還可以設置輸出的最大速率。

  輸入浮空 :既不是上拉也不是下拉輸入。

  開(kāi)漏模式和推挽模式:弱上拉和下拉電阻被禁止,開(kāi)漏模式時(shí),讀輸入數據寄存器時(shí)可得到I/O口狀態(tài),推挽模式時(shí),讀輸出數據寄存器時(shí)可得到最后一次寫(xiě)的值,模擬輸入配置。弱上拉和下拉電阻被禁止讀取輸入數據寄存器時(shí)數值為’0’。

  好接下來(lái)要說(shuō)下一個(gè)常常聽(tīng)到的控制方法了,直接操作寄存器和庫函數方法直接操作寄存器就是直接對CRL等寄存器寫(xiě)值。庫函數是ST官方吧所有的控制都寫(xiě)好了,我們只需要調用。直接操作寄存器

  GPIOA-CRH=0XFFFFFFF0;

  GPIOA-CRH|=0X00000003;/PA8 推挽輸出

  GPIOA-ODR|=18; /PA8 輸出高

  庫函數

  GPIO_InitTypeDef GPIO_InitStruct;

  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12 ;

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStruct.GPIO_Speed =GPIO_Speed_Level_3;

  GPIO_Init(GPIOA, GPIO_InitStruct);

  GPIO_SetBits(GPIOA, GPIO_Pin_11 | GPIO_Pin_12 );

  現在寫(xiě)一個(gè)LED的初始化

  直接操作寄存器

  /LED IO初始化

  void LED_Init(void)

  {

  RCC-APB2ENR|=12; /使能PORTA時(shí)鐘

  GPIOA-CRH=0XFFFFFFF0; /PA8 推挽輸出

  GPIOA-CRH|=0X00000003;/速率

  GPIOA-ODR|=18; /PA8 輸出高 一開(kāi)始高滅燈

  }

  注:先要使能PORTA時(shí)鐘

  庫函數

  void LED_Init(void)

  {

  GPIO_InitTypeDefGPIO_InitStructure; /聲明結構體ST庫

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);/使能PA端口時(shí)鐘

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;/LED0--PA.8 端口配置

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /推挽輸出

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /速率

  GPIO_Init(GPIOA, GPIO_InitStructure); /結構體初始化

  GPIO_SetBits(GPIOA,GPIO_Pin_8);/PA.8 輸出高

  }

  OK到這兩種方法的初始化都OK了

  main 函數

  int main(void)

  {

  SystemInit(); /系統時(shí)鐘初始化為72M SYSCLK_FREQ_72MHz

  delay_init(72); /延時(shí)函數初始化

  NVIC_Configuration(); /設置NVIC中斷分組2:2位搶占優(yōu)先級,2位響應優(yōu)先級

  LED_Init(); /LED端口初始化

  while(1)

  {

  LED0=0;/也可以使用 (庫)GPIO_ResetBits(GPIOA,GPIO_Pin_8);

  /寄存器GPIOA-ODR=~(18);

  delay_ms(300);

  LED0=1; /也可以使用 GPIO_SetBits(GPIOA,GPIO_Pin_8);

  /寄存器 GPIOA-ODR|=18;

  delay_ms(300);

  }

  }

  使能PORTA時(shí)鐘,這里說(shuō)明下STM32的時(shí)鐘樹(shù)和AVR51等有很大的區別,它每個(gè)外設都會(huì )有自己的時(shí)鐘,要使用一個(gè)外設必須要先開(kāi)外設的時(shí)鐘。這樣也一定程度上減少了功耗。



關(guān)鍵詞: STM32 單片機

評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>