GPIO
1、创建完工程之后,会出现编译警告出错问题,解决方法可以参考以下注释,因此新建工程后,必须在工程选项卡当中,【C/C++】中的全局宏定义添加"USE_STDPERIPH_DRIVER",即这个定义控制了是否在应用中启用外设驱动。我们使用标准外设库本来就为了方便控制外设,所以要添加这个定义,以启用外设驱动。
在stm32f4xx.h文件中,说明如下:
1)USE_STDPERIPH_DRIVER宏定义
/** ****************************************************************************** * @file stm32f4xx.h * @author MCD Application Team * @version V1.3.0 modified by Keil (added USB definitions) * @date 08-November-2013 * @brief CMSIS Cortex-M4 Device Peripheral Access Layer Header File. * This file contains all the peripheral register's definitions, bits * definitions and memory mapping for STM32F4xx devices. * * The file is the unique include file that the application programmer * is using in the C source code, usually in main.c. This file contains: * - Configuration section that allows to select: * - The device used in the target application * - To use or not the peripheral’s drivers in application code(i.e. //对于是否启用外设驱动,决定于USE_STDPERIPH_DRIVER * code will be based on direct access to peripheral’s registers * rather than drivers API), this option is controlled by * "#define USE_STDPERIPH_DRIVER" * - To change few application-specific parameters such as the HSE * crystal frequency * - Data structures and the address mapping for all peripherals * - Peripheral's registers declarations and bits definition * - Macros to access peripheral’s registers hardware * ******************************************************************************
2) .\Objects\project.axf: Error: L6218E: Undefined symbol RCC_AHB1PeriphResetCmd (referred from stm32f4xx_gpio.o).
解决方法:在Keil5 添加RCC的库函数。
2、添加头文件路径,在工程选项卡当中,【C/C++】中的“Include Paths”添加相应的头文件路径,否则这些文件会出现感叹号,编译器说找不到。
.\RTE\Device\STM32F407ZE;.\RTE
上述包含两个头文件路径,不同的头文件路径以分号进行隔离。
3、编译的时候,总是出现以下警告,
main.c(8): warning: #1-D: last line of file ends without a newline //文件末尾没有新行
解决方法:每个公司都有自己的编写代码规范,都需要看到回车换行,在文件末尾按一下回车键。
4、设置Keil编译完之后,生成Hex文件。
在工程选项【output】的标签,勾选“Create HEX File”。
重新编译之后,就生成一下信息。
Build target 'led'
Build target 'led'
FromELF: creating hex file...
"
.\Objects\project.axf" - 0 Error(s), 0 Warning(s).
"
.\Objects\project.axf" - 0 Error(s), 0 Warning(s).
三、GPIO的库函数
/** * @brief Sets the selected data port bits. //设置选择好的端口的引脚电平,说白将对应的引脚变为高电平 * @note This functions uses GPIOx_BSRR register to allow atomic read/modify * accesses. In this way, there is no risk of an IRQ occurring between * the read and the modify access. * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F405xx/407xx and STM32F415xx/417xx devices * x can be (A..I) to select the GPIO peripheral for STM32F42xxx/43xxx devices. * x can be (A, B, C, D and H) to select the GPIO peripheral for STM32F401xx devices. * @param GPIO_Pin: specifies the port bits to be written. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15). * @retval None */ void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) /** * @brief Clears the selected data port bits. //清空选择好的端口的引脚电平,说白将对应的引脚变为低电平 * @note This functions uses GPIOx_BSRR register to allow atomic read/modify * accesses. In this way, there is no risk of an IRQ occurring between * the read and the modify access. * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F405xx/407xx and STM32F415xx/417xx devices * x can be (A..I) to select the GPIO peripheral for STM32F42xxx/43xxx devices. * x can be (A, B, C, D and H) to select the GPIO peripheral for STM32F401xx devices. * @param GPIO_Pin: specifies the port bits to be written. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15). * @retval None */ void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
四、通过J-Link仿真下载器进行下载代码
1)J-Link正确连接到开发板
2)安装驱动Setup_JLinkARM_V415e.exe
3)在工程选项的【Debug】标签,选中“J-LINK/J-TRACE Cortex”,接着点击“settings”,设置port为“sw”,频率为“5MHz”;然后点击【Flash Download】,勾选“Reset and Run”。最后点击“确定”按钮退出。
4)在Keil5 界面当中,点击“Download”图标,就可以进行程序下载。
五、使用库函数编程技巧
1、阅读硬件原理图《GEC-M4原理图2016-07-29.pdf》,了解当前需要使用STM32芯片哪个硬件,就可以知道使用哪些库函数接口。
2、使用库函数的时候,只需要了解该函数的使用方法,如传入参数、返回值、功能描述就足矣。库函数里面的编写内容不需要了解,这些代码都是由ST公司去实现的。
3、如何使用库函数实现一个具体的功能,ST公司都会提供例子文档,告诉我们库函数如何使用,无论是新手还是老手都要看的。
.STM32F4xx中文参考手册.pdf
.stm32f4xx_dsp_stdperiph_lib_um.chm
.STM32F4xx中文参考手册.pdf
.stm32f4xx_dsp_stdperiph_lib_um.chm
#include <stdio.h> #include "stm32f4xx.h" GPIO_InitTypeDef GPIO_InitStructure; void delay(void) { int i=0x500000; while(i--); } int main(void) { /* GPIOF Peripheral clock enable,使能GPIO第F组的端口硬件时钟 */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); /* Configure PF10 in output pushpull mode,配置PF10引脚为输出模式 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //第9根引脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //设置输出模式 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽模式,增加驱动电流 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //设置IO的速度为100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //不需要上拉电阻 GPIO_Init(GPIOF, &GPIO_InitStructure); while(1) { //设置PF9引脚为高电平 GPIO_SetBits(GPIOF,GPIO_Pin_9); delay(); //设置PF9引脚为低电平 GPIO_ResetBits(GPIOF,GPIO_Pin_9); delay(); } }
1.配置引脚为输入模式
/* 配置PA0引脚为输入模式 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //第0根引脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //设置输入模式[重点修改] GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //设置IO的速度为100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //不需要上拉电阻 GPIO_Init(GPIOF, &GPIO_InitStructure);
2.读取电平
a.读取整个端口的电平(0~15号的引脚的电平都能够同时进行读取)
/** * @brief Reads the specified GPIO input data port. * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F405xx/407xx and STM32F415xx/417xx devices * x can be (A..I) to select the GPIO peripheral for STM32F42xxx/43xxx devices. * x can be (A, B, C, D and H) to select the GPIO peripheral for STM32F401xx devices. * @retval GPIO input data port value. */ uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
b.读取端口某个引脚的电平值
/** * @brief Reads the specified input port pin. * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F405xx/407xx and STM32F415xx/417xx devices * x can be (A..I) to select the GPIO peripheral for STM32F42xxx/43xxx devices. * x can be (A, B, C, D and H) to select the GPIO peripheral for STM32F401xx devices. * @param GPIO_Pin: specifies the port bit to read. * This parameter can be GPIO_Pin_x where x can be (0..15). * @retval The input port pin value. */ uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)