关于结构体初始化


我们一般见的结构体初始化有两种:

typedef struct
{
uint32_t I2C_ClockSpeed; /*!< Specifies the clock frequency.
This parameter must be set to a value lower than 400kHz */

uint16_t I2C_Mode; /*!< Specifies the I2C mode.
This parameter can be a value of @ref I2C_mode */

uint16_t I2C_DutyCycle; /*!< Specifies the I2C fast mode duty cycle.
This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */

uint16_t I2C_OwnAddress1; /*!< Specifies the first device own address.
This parameter can be a 7-bit or 10-bit address. */

uint16_t I2C_Ack; /*!< Enables or disables the acknowledgement.
This parameter can be a value of @ref I2C_acknowledgement */

uint16_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.
This parameter can be a value of @ref I2C_acknowledged_address */
}I2C_InitTypeDef;

比如这个结构体,我们一般这样初始化:

I2C_InitTypeDef i2c;
i2c.I2C_Ack = I2C_Ack_Enable;
i2c.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
i2c.I2C_ClockSpeed = clock*1000;
i2c.I2C_DutyCycle = I2C_DutyCycle_2;
i2c.I2C_Mode = I2C_Mode_I2C;
i2c.I2C_OwnAddress1 = addr;

I2C_InitTypeDef i2c = {
I2C_Ack_Enable,
I2C_AcknowledgedAddress_7bit,
clock*1000,
I2C_DutyCycle_2,
I2C_Mode_I2C,
addr};
希望大家不要纠结我这个结构体变量的顺序,肯定错了,我只是说下有这种方法!

但是在linux内核里面有这样一种初始化方法:

I2C_InitTypeDef i2c = {
.I2C_Ack = I2C_Ack_Enable ,
.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit,
.I2C_ClockSpeed = 40000,
.I2C_DutyCycle = I2C_DutyCycle_2,
.I2C_Mode = I2C_Mode_I2C ,
.I2C_OwnAddress1 = 0x0a
};
但是在keil里面默认是不可以用这种方法初始化的,他属于C99的标准,但是keil默认是C90的标准!

如果需要这样用的话,或者你拿到的工程代码里面有这样定义的,我们可以这样设置就可以了!

蓝桥杯-嵌入式交流群 147520657


本站声明
本文转载自:http://blog.csdn.net/ieczw/article/details/17074573     作者:ieczw     发布日期:2013/12/02     本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。


 
© 2014-2017 ITdaan.com 粤ICP备14056181号