我們一般見的結構體初始化有兩種:
~~~
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的第一個程序--LED
- 【菜鳥入門】stm32 之 掃描按鍵
- 【菜鳥入門】stm32 之 中斷按鍵
- 【菜鳥入門】stm32 之 USART
- 【菜鳥入門】stm32 之 iic
- 【菜鳥入門】stm32 之 eeprom
- 【菜鳥入門】stm32 之 pwm
- 【菜鳥入門】stm32 之 ADC 模數轉換
- 【菜鳥入門】stm32 之 實時時鐘
- 【菜鳥入門】stm32 之 DMA
- 【菜鳥入門】stm32 之 DAC
- 【STM庫應用】stm32 之 USART
- 中斷源去抖辦法
- stm32 啟動代碼應用技巧
- 【STM庫應用】stm32 之 IIC應用
- 【STM庫應用】stm32 之 中斷按鍵初始化(注意事項)
- 關于結構體初始化
- 【STM庫應用】stm32 之 TIM (詳解一 通用定時器)
- 【STM庫應用】stm32 之 TIM (詳解二 脈沖寬度、周期測量)
- 【stm32庫應用】SD驅動移植(基于SDIO外設)
- SD卡fat文件系統移植
- stm32 DMA初始化選項研究
- stm32 靈活靜態存儲控制器(FSMC)(NORFLASH\PSRAM)
- 【stm32+uC/OS-II】ucosii移植簡單詳細步驟
- STM32 加入調試信息來調試代碼
- NRF24L01 無線通信模塊使用