1.目的
IIC通信,
2.分析
3.平台:
协议栈版本:DA1458x_SDK
编译软件:keil 5.20
硬件平台:开发板
例子:DA1458x_SDK5.0.3\DA1458x_SDK5.0.3\DA1458x_SDK\5.0.3\projects\target_apps\ble_examples\prox_reporter\Keil_4
4.步骤
1.初始化 iic管脚
void set_pad_functions(void) // set gpio port function mode
{
#if BLE_PROX_REPORTER
#if USE_PUSH_BUTTON
GPIO_ConfigurePin( GPIO_BUTTON_PORT, GPIO_BUTTON_PIN, INPUT_PULLUP, PID_GPIO, false ); // Push Button
#endif // USE_PUSH_BUTTON
GPIO_ConfigurePin( GPIO_ALERT_LED_PORT, GPIO_ALERT_LED_PIN, OUTPUT, PID_GPIO, false ); //Alert LED
#endif
#if BLE_BAS_SERVER && USE_BAT_LEVEL_ALERT
GPIO_ConfigurePin( GPIO_BAT_LED_PORT, GPIO_BAT_LED_PIN, OUTPUT, PID_GPIO, false ); //Battery alert LED
#endif
#if (BLE_SPOTA_RECEIVER) GPIO_ConfigurePin( SPI_EN_GPIO_PORT, SPI_EN_GPIO_PIN, OUTPUT, PID_SPI_EN, true );
GPIO_ConfigurePin( SPI_CLK_GPIO_PORT, SPI_CLK_GPIO_PIN, OUTPUT, PID_SPI_CLK, false );
GPIO_ConfigurePin( SPI_DO_GPIO_PORT, SPI_DO_GPIO_PIN, OUTPUT, PID_SPI_DO, false );
GPIO_ConfigurePin( SPI_DI_GPIO_PORT, SPI_DI_GPIO_PIN, INPUT, PID_SPI_DI, false );
// Example GPIO configuration for an I2C EEPROM.
//GPIO_ConfigurePin(GPIO_PORT_0, GPIO_PIN_2, INPUT, PID_I2C_SCL, false);
//GPIO_ConfigurePin(GPIO_PORT_0, GPIO_PIN_3, INPUT, PID_I2C_SDA, false);
#endif//IIC³õʼ»¯ IIC串口初始化
GPIO_ConfigurePin( GPIO_PORT_2, GPIO_PIN_2, INPUT_PULLUP, PID_I2C_SCL, true );
GPIO_ConfigurePin( GPIO_PORT_0, GPIO_PIN_7, INPUT_PULLUP, PID_I2C_SDA, true );
//´®¿Ú³õʼ»¯
// Initialize UART component
// SetBits16(CLK_PER_REG, UART2_ENABLE, 1); // enable clock for UART 2
// uart2_init(UART2_BAUDRATE, UART2_DATALENGTH);
}
2.初始化Gsensor
void periph_init(void) // set i2c, spi, uart, uart2 serial clks
{
// Power up peripherals' power domain
SetBits16(PMU_CTRL_REG, PERIPH_SLEEP, 0);
while (!(GetWord16(SYS_STAT_REG) & PER_IS_UP)) ;
SetBits16(CLK_16M_REG, XTAL16_BIAS_SH_ENABLE, 1);
//rom patch
patch_func();
//Init pads
set_pad_functions();
#if BLE_PROX_REPORTER
app_proxr_port_reinit(GPIO_ALERT_LED_PORT, GPIO_ALERT_LED_PIN);
#if USE_PUSH_BUTTON
app_button_enable();
#endif // USE_PUSH_BUTTON
#elif BLE_FINDME_LOCATOR #if USE_PUSH_BUTTON
app_button_enable();
#endif // USE_PUSH_BUTTON
#endif //BLE_PROX_REPORTER
#if BLE_BATTERY_SERVER
app_batt_port_reinit();
#endif //BLE_BATTERY_SERVER
//gsensor ³õʼ»¯
#define IIC_GSENSOR_ADDRESS 0x4c //定义IIC从机地址
Set_IIC_Device_Address(IIC_GSENSOR_ADDRESS)
mcube_device_init(); // Enable the pads
SetBits16(SYS_CTRL_REG, PAD_LATCH_EN, 1);
}
void Set_IIC_Device_Address(uint8_t address)
{
i2c_eeprom_init(address,I2C_FAST,I2C_7BIT_ADDR, I2C_1BYTE_ADDR);
}
uint8_t data = 0;
void Read_Gsensor_ID(void)
{
i2c_eeprom_read_data((uint8_t *)&data,(uint32_t)0x3b,(uint32_t)1) ;
}
5,实验结果如下