c - 使用指针访问 typedef 结构

标签 c pointers struct typedef eeprom

目标:将结构成员的值写入内部缓冲区。

我有一个包含 Uint16(无符号整型)类型成员的结构;这是其中的一小部分:

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;

我的尝试:

这是一个旨在写入 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();
}

当我尝试使用我的参数调用此函数时..

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

我收到类型不兼容的错误:

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

我的下一个想法是我需要一个中间人将它们聚集在一起并使其成为兼容的匹配。关于我可以尝试什么,有什么建议吗?谢谢

最佳答案

问题在于数据的声明和使用。如果您将其声明为

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

并将其命名为

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

它会倒下

*data++

因为它将按 EEPROM_PARAMTERS 的大小递增。如果原型(prototype)声明为

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

它需要被称为

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

这假设 EEPROM_PARAMETERS 中的所有内容都是 Uint16。另一种方法是使用枚举。

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);

关于c - 使用指针访问 typedef 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31814074/

相关文章:

c - fork 调用工作

c++ - vector 无法初始化

Swift:创建指针数组以调用 C 函数

c - 尝试将 CSV 数据解析为 C 中的结构时出错

c - 在C中格式化字符,理由

c - C中 double 组的优化总和

c - 为什么 ffmpeg 开发人员在宏化条件下使用 "runtime if"?

c++ - Boost 智能指针和非 POD 类型 (C++)

interface - 如何获取go struct的字段

c - 从指针赋值给变量失败