简体   繁体   English

如何使用结构中的引脚

[英]How can I use the Pins in the Struct

Hello I have a problem STM32.你好我有一个问题STM32。 In my library I wrote for eeprom, I want to use the pins and ports in the struct and then use them in the HAL_GPIO_Write_Pin function, but I could not succeed.在我为 eeprom 编写的库中,我想使用结构中的引脚和端口,然后在 HAL_GPIO_Write_Pin function 中使用它们,但我无法成功。 ERROR: incompatible type for argument 2 of 'HAL_GPIO_WritePin' Eeprom.错误:“HAL_GPIO_WritePin”Eeprom 的参数 2 的类型不兼容。

(Sorry if I didn't ask the question properly, it's my first time using it.) (对不起,如果我没有正确地提出问题,这是我第一次使用它。)

EEPROM.h   
 typedef struct
{

    SPI_HandleTypeDef* SPI_EEPROM ;
    uint16_t CS_PIN ;
    GPIO_TypeDef* CS_PORT ;
    uint32_t Write_Protect ;
    GPIO_PinState CS_LOW ;
    GPIO_PinState CS_HIGH ;

}EEPROM;

/*****************************************************************/

EEPROM.c 


SPI_HandleTypeDef* hspi1 ;
EEPROM    EEPROM_1 ;

void Eeprom_Init( )
{

    EEPROM_1.SPI_EEPROM    = hspi1 ;
    EEPROM_1.Write_Protect = EEPROM_WRITE ;
    EEPROM_1.CS_PIN        = SPI2_SS_Pin ;
    EEPROM_1.CS_PORT       = SPI2_SS_GPIO_Port ;
    EEPROM_1.CS_LOW        = GPIO_PIN_RESET ;
    EEPROM_1.CS_HIGH       = GPIO_PIN_SET ;

}


void EEPROM_1_CS_HIGH (EEPROM CS_PORT, EEPROM CS_PIN, EEPROM CS_HIGH)
{
    HAL_GPIO_WritePin( &CS_PORT, CS_PIN, CS_HIGH ) ;
}


void EEPROM_1_CS_LOW (EEPROM CS_PORT, EEPROM CS_PIN, EEPROM CS_LOW)
{
    HAL_GPIO_WritePin( &CS_PORT, CS_PIN, CS_LOW ) ;
}

In the line:在行中:

void EEPROM_1_CS_HIGH (EEPROM CS_PORT, EEPROM CS_PIN, EEPROM CS_HIGH)

You are saying that the function EEPROM_1_CS_HIGH has three arguments, all of which are a whole struct, passed by value.你说的是function EEPROM_1_CS_HIGH有3个arguments,都是一个整体的struct,按值传递。

You probably meant to have just one single argument, which is the struct, and then access its members inside the function.您可能只想有一个参数,即结构,然后访问 function 中的成员。 Also you should pass the struct by reference, using a pointer * .此外,您应该使用指针*通过引用传递结构。

void EEPROM_CS_HIGH (EEPROM *memory)
{
  HAL_GPIO_WritePin(memory->CS_PORT, memory->CS_PIN, memory->CS_HIGH);
}

If you then want to have a function that accesses your global data of the particular instance, you can do:如果你想要一个 function 来访问你的特定实例的全局数据,你可以这样做:

void EEPROM_1_CS_HIGH (void)
{
  EEPROM_CS_HIGH(&EEPROM_1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM