简体   繁体   English

使用指针访问typedef结构

[英]Using a pointer to access typedef struct

Objective: Writing to an internal buffer from the values of members of a structure. 目标:从结构成员的值写入内部缓冲区。

I have a structure that contains members of type Uint16 (unsigned int); 我有一个包含Uint16类型的成员的结构(unsigned int); here is a small portion of it: 这只是其中的一小部分:

typedef unsigned int    Uint16;
typedef struct 
{
    Uint16 ee_Speed_Control_Mode;
    Uint16 ee_Motor_Type;
    Uint16 ee_Max_Electrical_Speed;
    Uint16 ee_Carrier_Frequency;
    Uint16 ee_Rated_Motor_Frequency;
    Uint16 ee_Rated_Motor_Current;
    Uint16 ee_Rs; // extern
    Uint16 ee_Rr; // extern
    Uint16 ee_L_lkg; // extern
    Uint16 ee_Lm;    // extern
    Uint16 ee_No_Load_Current;
    Uint16 ee_Phase_Reversal;
    .....
    .....
} EEPROM_PARAMETERS;

EEPROM_PARAMETERS eepromParameters;

My attempt: 我的尝试:

Here is a function that is intended to write to eeprom: (Most of it is not shown for simplicity; the focus is occurring in the 'for' loop 这是一个打算写给eeprom的函数:(为了简单起见,大多数都没有显示;焦点出现在“ for”循环中

void eeprom_write(Uint16 address, Uint32 *data, Int16 len)
{
    Uint16 i;

    // Multiple bytes will be written
    // Page Write operation will be used
    // Page Write bits to be sent:

    // bit 0: Start condition, a high-to-low transition of SDA with SCL high
    startCondition();

    // bits 1-8: Device address
    I2caRegs.I2CDXR = DEVICE_ADDRESS_WRITE;

    // bit 9: EEPROM outputs 0 as ACK bit
    // Check for ACK bit
    while (I2caRegs.I2CDRR != 0)
    {
        wait();
    }

    // bits 10-17, bit 18 (ACK) and bits 19-26: two 8-bit word addresses
    I2caRegs.I2CDXR = address;

    // After setting the address, page write is capable of writing 64 bytes without stopping
    // The EEPROM will respond with a zero after each data word has been received
    // The data word address lower 6 bits are internally incremented following the receipt of each data word
    // If more than 64 data words are written, data word addresses will "roll over" and previous data will be overwritten

    for (i = 0; i < len; i++)
    {
        // How to increment address in data structure?
        I2caRegs.I2CDXR = *data++; 
    }

    // After page write operation is complete, execute stop condition
    stopCondition();
}

When I try to call this function with my parameters.. 当我尝试使用我的参数调用此函数时。

eeprom_write(0, &eepromParameters, sizeof(eepromParameters) );

I get a incompatible type error: 我收到一个不兼容的类型错误:

error #169: argument of type "EEPROM_PARAMETERS *" is incompatible with parameter of type "Uint16 *"

My next thought would be that I need a middle man to bring them together and make it a compatible match. 我的下一个想法是,我需要一个中间人来将它们组合在一起,并使其成为兼容的比赛。 Any tips please on what I can try? 请问有什么建议可以尝试吗? Thanks 谢谢

The problem is the declaration and usage of data. 问题是数据的声明和使用。 If you declare it as 如果您声明为

void eeprom_write(Uint16 address, EEPROM_PARAMETERS* data, Int16 len);

and call it as 并称其为

eeprom_write(0, &eepromParameters, sizeof(eepromParameters));

It will fall over in 它会掉进去

*data++

since it will increment by the size of EEPROM_PARAMTERS. 因为它将增加EEPROM_PARAMTERS的大小。 If the prototype is declared as 如果原型声明为

void eeprom_write(Uint16 address, UInt16* data, Int16 len);

It needs to be called as 它需要称为

eeprom_write(0, &eepromParameters.ee_Speed_Control_Mode, sizeof(eepromParameters) / sizeof(Uint16));

This assumes that everything in EEPROM_PARAMETERS is Uint16. 这假定EEPROM_PARAMETERS中的所有内容均为Uint16。 Another way of doing this is to use enums. 这样做的另一种方法是使用枚举。

enum EEOffsets
{
    ee_Speed_Control_Mode,
    ee_Motor_Type,
    ee_Max_Electrical_Speed,
    ee_Carrier_Frequency,
    ...
    ee_Max
};

// Initialize the parameters
Uint16 eepromParameters[ee_Max] = { ... };

// If you need to assign
eepromParameters[ee_Carrier_Frequency] = 85;
...
eeprom_write(0, eepromParameters, ee_Max);

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

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