新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > STM32学习笔记——使用函数库编程控制GPIO口输出

STM32学习笔记——使用函数库编程控制GPIO口输出

作者: 时间:2016-11-28 来源:网络 收藏

2.1.7GPIO_Write函数

函数名

GPIO_Write

函数原型

voidGPIO_Write(GPIO_TypeDef* GPIOx,u16 PortVal)

功能描述

写数据到指定的GPIO端口数据寄存器

输入参数1

GPIOx:x=A…E

输入参数2

PortVal:写入到数据端口寄存器的值

输出参数

返回参数

前提条件

调用函数

实例:

[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函数与GPIO_ResetBits函数点亮与熄灭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函数点亮与熄灭led
  20. GPIO_Write(GPIOC,0x0140);
  21. delay();
  22. GPIO_Write(GPIOC,0x0280);
  23. delay();
  24. }
  25. }
  26. //GPIO口设置
  27. voidGPIO_Configuration(void)
  28. {
  29. //声明结构体
  30. GPIO_InitTypeDefGPIO_InitStructure;
  31. //设置选择引脚
  32. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
  33. //设置引脚最大输出频率
  34. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  35. //设置引脚输出模式
  36. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  37. 根据设置的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. }


编译通过烧写到开发板上后,最终结果是:led1和led3与led2和led4两两交替亮灭。

参考文献

[1]jhliuzj.IAR FOR ARM6.20工程创建建议(固件库为3.5)[EB/OL].

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

[2]kiropower.IARSTM32项目工程创建[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点个灯[操作寄存器+库函数][EB/OL].

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

20120827附:
关于标签定义问题,由于在main函数开头即声明#include "stm32f10x.h",而该头文件里又有如下代码:
[cpp]view plaincopy
  1. #ifdefUSE_STDPERIPH_DRIVER
  2. #include"stm32f10x_conf.h"
  3. #endif
即当定义USE_STDPERIPH_DRIVER时,就会包含stm32f10x_conf.h,而我们在设置项目的时候,在"c/c++ compiler>define" 项中已经对其进行了预定义,所以也会自动加入stm32f10x_conf.h文件,该文件中包含了所有外设的头文件,因此可以直接调用函数。对不需要使用的外设,可以直接在该文件中将其注释掉,这样在编译的时候就不会编译被注释的文件。
参考文献:jack.stm32f10x_conf.h与stm32f10x.h(转载)[EB/OL].http://blog.sina.com.cn/s/blog_7b93041501013o5b.html,2012-05-01/2012-08-27

上一页 1 2 3 下一页

关键词: STM32函数库编程控制GPIO

评论


相关推荐

技术专区

关闭