关于结构体初始化

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

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


阅读更多
文章标签: stm32
个人分类: stm32
所属专栏: STM32开发
上一篇【STM库应用】stm32 之 中断按键初始化(注意事项)
下一篇【STM库应用】stm32 之 TIM (详解一 通用定时器)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭