English 中文(简体)
C. 固定数据的人口结构方法
原标题:C - Methods of populating structures with static data
  • 时间:2012-01-15 18:33:13
  •  标签:
  • c
  • arrays

下面的法典显示我正在做些什么。 这对我来说是可怕的。 是否有更好的/更合理的方式供任何人建议?

增 编

#define DS1337_SECONDS_REG              0x00
// Default values used for initialisation
#define DS1337_DEFAULT_SECONDS          0x00
#define DS1337_DEFAULT_MINUTES          0x00
#define DS1337_DEFAULT_HOURS_HR_MODE    0x40
#define DS1337_DEFAULT_DAY              0x00 /* Sun */
#define DS1337_DEFAULT_DATE             0x01
#define DS1337_DEFAULT_MONTH            0x01
#define DS1337_DEFAULT_YEAR             0x0C /* 2012 */
#define DS1337_DEFAULT_ALM1_SECS        0x00
#define DS1337_DEFAULT_ALM1_MINS        0x00
#define DS1337_DEFAULT_ALM1_HRS         0x00
#define DS1337_DEFAULT_ALM1_DAY_DATE    0x00
#define DS1337_DEFAULT_ALM2_MINS        0x00
#define DS1337_DEFAULT_ALM2_HRS         0x00
#define DS1337_DEFAULT_ALM2_DAY_DATE    0x00

extern i2c_err_t i2c_send(const i2c_ch_t channel, const uint8_t data[], const uint32_t length, const i2c_stop_t stop);

rtc_err_t ds1337_init(void)
{
    uint8_t data_to_send[17] = { DS1337_SECONDS_REG,       /* Address of first register */
                             DS1337_DEFAULT_SECONDS, DS1337_DEFAULT_MINUTES, DS1337_DEFAULT_HOURS_HR_MODE,
                             DS1337_DEFAULT_DAY, DS1337_DEFAULT_DATE, DS1337_DEFAULT_MONTH, DS1337_DEFAULT_YEAR,
                             DS1337_DEFAULT_ALM1_SECS, DS1337_DEFAULT_ALM1_MINS, DS1337_DEFAULT_ALM1_HRS, DS1337_DEFAULT_ALM1_DAY_DATE,
                             DS1337_DEFAULT_ALM2_MINS, DS1337_DEFAULT_ALM2_HRS, DS1337_DEFAULT_ALM2_DAY_DATE,
                             DS1337_CTRL_REG_INIT_VAL, /* Turn off the squarewave output pin */
                             DS1337_CLEAR_STATUS_VAL   /* Clear the status registers */
                           };

if (i2c_set_address(DS1337_CHANNEL, DS1337_SPI_ADDRESS) != I2C_NO_ERROR)
     return RTC_I2C_ADDRESS_ERROR;

if (i2c_send(DS1337_CHANNEL, data_to_send, sizeof(data_to_send), STOP_CONDITION) != I2C_NO_ERROR)
    return RTC_I2C_SEND_ERROR;

//enable_interrupts(GLOBAL);

return RTC_NO_ERROR;
}
最佳回答

这是一种相当的标准做法。 Enums(而不是定义)获得的,使你的源代码大大缩短。 硬化法典中的常数将使其无法读用。 对i2c_send(端)有多条电话,没有阵列,虽然可能稍有增加,而且可读性较少。 界定一些职能以混淆一点,是使源代码更加庞大的可靠途径。 我可能只是保留法典。

问题回答

Use enums rather than #defines, and I would say "12" rather than "0x0C" for 2012.

考虑仅以硬编码“0”代替显然未使用的<代码>*ALM*数值。

#include a header file to get the declaration of i2c_send(), rather than explicitly declaring it.

http://www.un.org/Depts/DGACM/index_french.htm





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签