新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > STM32學(xué)習(xí)筆記——使用函數(shù)庫編程控制GPIO口輸出

STM32學(xué)習(xí)筆記——使用函數(shù)庫編程控制GPIO口輸出

作者: 時間:2016-11-28 來源:網(wǎng)絡(luò) 收藏

2.1.7GPIO_Write函數(shù)

函數(shù)名

GPIO_Write

函數(shù)原型

voidGPIO_Write(GPIO_TypeDef* GPIOx,u16 PortVal)

功能描述

寫數(shù)據(jù)到指定的GPIO端口數(shù)據(jù)寄存器

輸入?yún)?shù)1

GPIOx:x=A…E

輸入?yún)?shù)2

PortVal:寫入到數(shù)據(jù)端口寄存器的值

輸出參數(shù)

返回參數(shù)

前提條件

調(diào)用函數(shù)

實例:

[cpp]view plaincopy
  1. GPIO_Write(GPIOA,0x1101);

2.2 完整程序:

[cpp]view plaincopy
  1. #include"stm32f10x.h"
  2. voiddelay(void);
  3. voidGPIO_Configuration(void);
  4. intmain(void)
  5. {
  6. //使能GPIOC時鐘
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  8. //此條語句一定要在時鐘使能后,否則無效(費了好多時間才找到原因)
  9. GPIO_Configuration();
  10. while(1)
  11. {
  12. //利用GPIO_SetBits函數(shù)與GPIO_ResetBits函數(shù)點亮與熄滅led
  13. GPIO_ResetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_9);
  14. GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8);
  15. delay();
  16. GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8);
  17. GPIO_SetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_9);
  18. delay();
  19. //利用GPIO_Write函數(shù)點亮與熄滅led
  20. GPIO_Write(GPIOC,0x0140);
  21. delay();
  22. GPIO_Write(GPIOC,0x0280);
  23. delay();
  24. }
  25. }
  26. //GPIO口設(shè)置
  27. voidGPIO_Configuration(void)
  28. {
  29. //聲明結(jié)構(gòu)體
  30. GPIO_InitTypeDefGPIO_InitStructure;
  31. //設(shè)置選擇引腳
  32. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
  33. //設(shè)置引腳最大輸出頻率
  34. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  35. //設(shè)置引腳輸出模式
  36. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  37. 根據(jù)設(shè)置的InitStructure初始化GPIO口
  38. GPIO_Init(GPIOC,&GPIO_InitStructure);
  39. }
  40. voiddelay(void)
  41. {
  42. unsignedlongj,n=100000;
  43. while(n--)
  44. {
  45. j=12;
  46. while(j--);
  47. }
  48. }


編譯通過燒寫到開發(fā)板上后,最終結(jié)果是:led1和led3與led2和led4兩兩交替亮滅。

參考文獻(xiàn)

[1]jhliuzj.IAR FOR ARM6.20工程創(chuàng)建建議(固件庫為3.5)[EB/OL].

http://hi.baidu.com/jhliuzj/item/459830ae7e19e136020a4d3f,2011-10-03/2012-08-25.

[2]kiropower.IARSTM32項目工程創(chuàng)建[EB/OL].http://hi.baidu.com/kiropower/item/e20faad0007502352b35c785,2011-11-10/2012-08-25.

[3]gasbi.startup_stm32f10x_xx.s啟動代碼文件選擇[EB/OL].

http://blog.csdn.net/gasbi/article/details/7545568,2012-05-08/2012-08-25.

[4]IAR Systems AB.Releasenotes for the IAR C/C++ Compiler for ARM 6.20.1[EB/OL].http://supp.iar.com/FilesPublic/UPDINFO/005832/arm/doc/infocenter/iccarm.ENU.html,2012-08-25

[5]Changing.用stm32點個燈[操作寄存器+庫函數(shù)][EB/OL].

http://www.ichanging.org/stm32_gpio_led.html,2012-06-29/2012-08-25.

20120827附:
關(guān)于標(biāo)簽定義問題,由于在main函數(shù)開頭即聲明#include "stm32f10x.h",而該頭文件里又有如下代碼:
[cpp]view plaincopy
  1. #ifdefUSE_STDPERIPH_DRIVER
  2. #include"stm32f10x_conf.h"
  3. #endif
即當(dāng)定義USE_STDPERIPH_DRIVER時,就會包含stm32f10x_conf.h,而我們在設(shè)置項目的時候,在"c/c++ compiler>define" 項中已經(jīng)對其進(jìn)行了預(yù)定義,所以也會自動加入stm32f10x_conf.h文件,該文件中包含了所有外設(shè)的頭文件,因此可以直接調(diào)用函數(shù)。對不需要使用的外設(shè),可以直接在該文件中將其注釋掉,這樣在編譯的時候就不會編譯被注釋的文件。
參考文獻(xiàn):jack.stm32f10x_conf.h與stm32f10x.h(轉(zhuǎn)載)[EB/OL].http://blog.sina.com.cn/s/blog_7b93041501013o5b.html,2012-05-01/2012-08-27

上一頁 1 2 3 下一頁

關(guān)鍵詞: STM32函數(shù)庫編程控制GPIO

評論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉