易備份在Blogger上, 以方便之後有需要的時候容易能馬上使用.
2. 使用I2C Master Driver之前, 必須要先include以下的c & h file, 才能正常使用, 檔名及路徑如
下:
- 測試的版本皆是在nRF51_SDK_9.0.0_2e23562.zip這個sdk版本下進行測試的.
- twi_hw_master.c:
\components\drivers_nrf\twi_master\incubated\
- twi_master.h:
\components\drivers_nrf\twi_master\incubated\
- twi_master_config.h:
\components\drivers_nrf\twi_master\incubated\config\
3. 接著就是使用底下的i2c.c & i2c.h 這兩個檔案就可以使用了.
i2c.c:
--------------------------------------------------------------------------------------------------------------------------
i2c.h:
--------------------------------------------------------------------------------------------------------------------------
i2c.c:
--------------------------------------------------------------------------------------------------------------------------
#include "i2c.h" void I2C_Master_Init(void) { twi_master_init(); } int I2C_Write(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToWrite) { #define i2c_write_data_len 6 uint8_t w2_data[i2c_write_data_len+1], i; w2_data[0] = RegAddr; for ( i = 0 ; i < NumByteToWrite ; i++ ) { w2_data[i +1] = pBuffer[i]; } if(twi_master_transfer(DeviceAddr,w2_data,NumByteToWrite+1,TWI_ISSUE_STOP) == false) return -1; return 0; } int I2C_Read(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToRead) { if(twi_master_transfer(DeviceAddr, &RegAddr, 1, TWI_DONT_ISSUE_STOP) == false) return -1; if(twi_master_transfer(DeviceAddr|TWI_READ_BIT, pBuffer, NumByteToRead, TWI_ISSUE_STOP) == false) return -1; return 0; }
i2c.h:
--------------------------------------------------------------------------------------------------------------------------
#include "twi_master.h" void I2C_Master_Init(void); int I2C_Write(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToWrite); int I2C_Read(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToRead);
4. 調整i2c speed的地方在twi_master_init()裡面:
沒有留言:
張貼留言