#include "drv_iic_01.h" #include "nrf_delay.h" #define delayTime 1 static void IIC_01_Start(void) { IIC_01_SDA_SET; IIC_01_SCL_SET; nrf_delay_us(5); IIC_01_SDA_CLR; nrf_delay_us(5); IIC_01_SCL_CLR; } static void IIC_01_Stop(void) { IIC_01_SCL_CLR; IIC_01_SDA_CLR; nrf_delay_us(5); IIC_01_SCL_SET; IIC_01_SDA_SET; nrf_delay_us(5); } static void IIC_01_ACK(void) { IIC_01_SCL_CLR; nrf_delay_us(1); IIC_01_SDA_CLR; nrf_delay_us(1); IIC_01_SCL_SET; nrf_delay_us(1); IIC_01_SCL_CLR; } static void IIC_01_NoACK(void) { IIC_01_SCL_CLR; IIC_01_SDA_SET; nrf_delay_us(1); IIC_01_SCL_SET; nrf_delay_us(1); IIC_01_SCL_CLR; } static bool IIC_01_WaitACK(void) { uint8_t tim = 0; // IIC_01_SCL_CLR; IIC_01_SDA_SET; nrf_delay_us(1); IIC_01_SCL_SET; // nrf_delay_us(1); while(IIC_01_SDA_READ){ if(++tim>=50){ IIC_01_Stop(); return false; } nrf_delay_us(1); } IIC_01_SCL_CLR; return true; } static void IIC_01_SendByte(uint8_t _byte) { uint8_t i = 0; for (i=0; i<8; i++){ IIC_01_SCL_CLR; // nrf_delay_us(1); if (_byte&0x80) IIC_01_SDA_SET; else IIC_01_SDA_CLR; _byte <<= 1; IIC_01_SCL_SET; // nrf_delay_us(1); } IIC_01_SCL_CLR; } static uint8_t IIC_01_RecByte(void) { uint8_t i = 0; uint8_t rec_byte; IIC_01_SDA_SET; for (i=0; i<8; i++){ rec_byte <<= 1; IIC_01_SCL_CLR; IIC_01_SCL_CLR; IIC_01_SCL_CLR; IIC_01_SCL_CLR; // nrf_delay_us(1); IIC_01_SCL_SET; // nrf_delay_us(1); if (IIC_01_SDA_READ) rec_byte |= 0x01; } IIC_01_SCL_CLR; return rec_byte; } /********************************************/ bool IIC_01_WriteBytes(uint8_t add,uint8_t reg,uint8_t* p,uint8_t len) { uint8_t i = 0; IIC_01_Start(); IIC_01_SendByte(add); if(!IIC_01_WaitACK()) return false; IIC_01_SendByte(reg); if(!IIC_01_WaitACK()) return false; for(i=0;i