STM32学习笔记——使用函数库编程控制GPIO口输出
2.1.7GPIO_Write函数
函数名 | GPIO_Write |
函数原型 | voidGPIO_Write(GPIO_TypeDef* GPIOx,u16 PortVal) |
功能描述 | 写数据到指定的GPIO端口数据寄存器 |
输入参数1 | GPIOx:x=A…E |
输入参数2 | PortVal:写入到数据端口寄存器的值 |
输出参数 | 无 |
返回参数 | 无 |
前提条件 | 无 |
调用函数 | 无 |
实例:
- GPIO_Write(GPIOA,0x1101);
2.2 完整程序:
- #include"stm32f10x.h"
- voiddelay(void);
- voidGPIO_Configuration(void);
- intmain(void)
- {
- //使能GPIOC时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
- //此条语句一定要在时钟使能后,否则无效(费了好多时间才找到原因)
- GPIO_Configuration();
- while(1)
- {
- //利用GPIO_SetBits函数与GPIO_ResetBits函数点亮与熄灭led
- GPIO_ResetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_9);
- GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8);
- delay();
- GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8);
- GPIO_SetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_9);
- delay();
- //利用GPIO_Write函数点亮与熄灭led
- GPIO_Write(GPIOC,0x0140);
- delay();
- GPIO_Write(GPIOC,0x0280);
- delay();
- }
- }
- //GPIO口设置
- voidGPIO_Configuration(void)
- {
- //声明结构体
- GPIO_InitTypeDefGPIO_InitStructure;
- //设置选择引脚
- 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;
- 根据设置的InitStructure初始化GPIO口
- GPIO_Init(GPIOC,&GPIO_InitStructure);
- }
- voiddelay(void)
- {
- unsignedlongj,n=100000;
- while(n--)
- {
- j=12;
- while(j--);
- }
- }
编译通过烧写到开发板上后,最终结果是:led1和led3与led2和led4两两交替亮灭。
参考文献
[1]jhliuzj.IAR FOR ARM6.20工程创建建议(固件库为3.5)[EB/OL].
http://hi.baidu.com/jhliuzj/item/459830ae7e19e136020a4d3f
[2]kiropower.IARSTM32项目工程创建[EB/OL].http://hi.baidu.com/kiropower/item/e20faad0007502352b35c785
[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,
- #ifdefUSE_STDPERIPH_DRIVER
- #include"stm32f10x_conf.h"
- #endif
评论