请教stm32f2xx系列的GPIO的配置问题
答案:3 悬赏:30 手机版
解决时间 2021-04-24 03:18
- 提问者网友:谁的错
- 2021-04-23 04:09
请教stm32f2xx系列的GPIO的配置问题
最佳答案
- 五星知识达人网友:胯下狙击手
- 2021-04-23 04:15
给你个配置举例:
void GPIO_I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(sEE_I2C_CLK, ENABLE);
RCC_AHB1PeriphClockCmd(sEE_I2C_SCL_GPIO_CLK | sEE_I2C_SDA_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
RCC_APB1PeriphResetCmd(sEE_I2C_CLK, ENABLE);
RCC_APB1PeriphResetCmd(sEE_I2C_CLK, DISABLE);
GPIO_InitStructure.GPIO_Pin = sEE_I2C_SCL_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(sEE_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = sEE_I2C_SDA_PIN;
GPIO_Init(sEE_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(sEE_I2C_SCL_GPIO_PORT, sEE_I2C_SCL_SOURCE, sEE_I2C_SCL_AF);
GPIO_PinAFConfig(sEE_I2C_SDA_GPIO_PORT, sEE_I2C_SDA_SOURCE, sEE_I2C_SDA_AF);
}
GPIO_PinAFConfig(sEE_I2C_SCL_GPIO_PORT, sEE_I2C_SCL_SOURCE, sEE_I2C_SCL_AF); 这句语句分配为它的复用功能,可查看库 的函数定义和说明。
void GPIO_I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(sEE_I2C_CLK, ENABLE);
RCC_AHB1PeriphClockCmd(sEE_I2C_SCL_GPIO_CLK | sEE_I2C_SDA_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
RCC_APB1PeriphResetCmd(sEE_I2C_CLK, ENABLE);
RCC_APB1PeriphResetCmd(sEE_I2C_CLK, DISABLE);
GPIO_InitStructure.GPIO_Pin = sEE_I2C_SCL_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(sEE_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = sEE_I2C_SDA_PIN;
GPIO_Init(sEE_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(sEE_I2C_SCL_GPIO_PORT, sEE_I2C_SCL_SOURCE, sEE_I2C_SCL_AF);
GPIO_PinAFConfig(sEE_I2C_SDA_GPIO_PORT, sEE_I2C_SDA_SOURCE, sEE_I2C_SDA_AF);
}
GPIO_PinAFConfig(sEE_I2C_SCL_GPIO_PORT, sEE_I2C_SCL_SOURCE, sEE_I2C_SCL_AF); 这句语句分配为它的复用功能,可查看库 的函数定义和说明。
全部回答
- 1楼网友:冷風如刀
- 2021-04-23 05:16
同学,你好!应该是错在IO口配置那位置,因为USART2用的IO口不再是PA9和PA10。如果和我芯片一样STM32F103ZET6,那就应该是换成PA2和PA3了!自己仔细看下自己芯片是哪个IO口吧!
望采纳!
完蛋,没仔细看,你已经改了IO口的,那问题可能就是:如果用的是开发板的话,可能是没有把这个串口2引出来了。程序是没错的!
- 2楼网友:酒者煙囻
- 2021-04-23 04:46
**
******************************************************************************
* @file stm32f4xx_gpio.h
* @author mcd application team
* @version v1.0.0
* @date 30-september-2011
* @brief this file contains all the functions prototypes for the gpio firmware
* library.
******************************************************************************
* @attention
*
* the present firmware which is for guidance only aims at providing customers
* with coding information regarding their products in order for them to save
* time. as a result, stmicroelectronics shall not be held liable for any
* direct, indirect or consequential damages with respect to any claims arising
* from the content of such firmware and/or the use made by customers of the
* coding information contained herein in connection with their products.
*
* © copyright 2011 stmicroelectronics
******************************************************************************
*/
#ifndef __stm32f4xx_gpio_h
#define __stm32f4xx_gpio_h
#ifdef __cplusplus
extern "c" {
#endif
#include "stm32f4xx.h"
#define is_gpio_all_periph(periph) (((periph) == gpioa) || \
((periph) == gpiob) || \
((periph) == gpioc) || \
((periph) == gpiod) || \
((periph) == gpioe) || \
((periph) == gpiof) || \
((periph) == gpiog) || \
((periph) == gpioh) || \
((periph) == gpioi))
typedef enum
{
gpio_mode_in = 0x00,
gpio_mode_out = 0x01,
gpio_mode_af = 0x02,
gpio_mode_an = 0x03
}gpiomode_typedef;
#define is_gpio_mode(mode) (((mode) == gpio_mode_in) || ((mode) == gpio_mode_out) || \
((mode) == gpio_mode_af)|| ((mode) == gpio_mode_an))
typedef enum
{
gpio_otype_pp = 0x00,
gpio_otype_od = 0x01
}gpiootype_typedef;
#define is_gpio_otype(otype) (((otype) == gpio_otype_pp) || ((otype) == gpio_otype_od))
typedef enum
{
gpio_speed_2mhz = 0x00,
gpio_speed_25mhz = 0x01,
gpio_speed_50mhz = 0x02,
gpio_speed_100mhz = 0x03
}gpiospeed_typedef;
#define is_gpio_speed(speed) (((speed) == gpio_speed_2mhz) || ((speed) == gpio_speed_25mhz) || \
((speed) == gpio_speed_50mhz)|| ((speed) == gpio_speed_100mhz))
typedef enum
{
gpio_pupd_nopull = 0x00,
gpio_pupd_up = 0x01,
gpio_pupd_down = 0x02
}gpiopupd_typedef;
#define is_gpio_pupd(pupd) (((pupd) == gpio_pupd_nopull) || ((pupd) == gpio_pupd_up) || \
((pupd) == gpio_pupd_down))
typedef enum
{
bit_reset = 0,
bit_set
}bitaction;
#define is_gpio_bit_action(action) (((action) == bit_reset) || ((action) == bit_set))
typedef struct
{
uint32_t gpio_pin;
gpiomode_typedef gpio_mode;
gpiospeed_typedef gpio_speed;
gpiootype_typedef gpio_otype;
gpiopupd_typedef gpio_pupd;
}gpio_inittypedef;
#define gpio_pin_0 ((uint16_t)0x0001)
#define gpio_pin_1 ((uint16_t)0x0002)
#define gpio_pin_2 ((uint16_t)0x0004)
#define gpio_pin_3 ((uint16_t)0x0008)
#define gpio_pin_4 ((uint16_t)0x0010)
#define gpio_pin_5 ((uint16_t)0x0020)
#define gpio_pin_6 ((uint16_t)0x0040)
#define gpio_pin_7 ((uint16_t)0x0080)
#define gpio_pin_8 ((uint16_t)0x0100)
#define gpio_pin_9 ((uint16_t)0x0200)
#define gpio_pin_10 ((uint16_t)0x0400)
#define gpio_pin_11 ((uint16_t)0x0800)
#define gpio_pin_12 ((uint16_t)0x1000)
#define gpio_pin_13 ((uint16_t)0x2000)
#define gpio_pin_14 ((uint16_t)0x4000)
#define gpio_pin_15 ((uint16_t)0x8000)
#define gpio_pin_all ((uint16_t)0xffff)
#define is_gpio_pin(pin) ((((pin) & (uint16_t)0x00) == 0x00) && ((pin) != (uint16_t)0x00))
#define is_get_gpio_pin(pin) (((pin) == gpio_pin_0) || \
((pin) == gpio_pin_1) || \
((pin) == gpio_pin_2) || \
((pin) == gpio_pin_3) || \
((pin) == gpio_pin_4) || \
((pin) == gpio_pin_5) || \
((pin) == gpio_pin_6) || \
((pin) == gpio_pin_7) || \ ne gpio_af_spi1
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯