目标:根据结构成员的值写入内部缓冲区。
我的结构包含Uint16类型的成员(unsigned int);这里只是其中的一小部分:
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 *"
我的下一个想法是,我需要一个中间人将它们组合在一起并使其成为兼容的匹配。请问我能尝试什么提示?谢谢
答案 0 :(得分:0)
问题是数据的声明和使用。如果您将其声明为
void eeprom_write(Uint16 address, EEPROM_PARAMETERS* data, Int16 len);
并将其命名为
eeprom_write(0, &eepromParameters, sizeof(eepromParameters));
它会落在
中*data++
因为它将增加EEPROM_PARAMTERS的大小。如果原型声明为
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);