1. #define I2C_SCL_0()
    2. #define I2C_SCL_1()
    3. #define I2C_SDA_0()
    4. #define I2C_SDA_1()
    5. #define I2C_DELAY()
    6. #define I2C_ReadSDA();
    7. void I2cStart(void)
    8. {
    9. I2C_SCL_1();
    10. I2C_SDA_1();
    11. I2C_DELAY();
    12. I2C_SDA_0();
    13. I2C_DELAY();
    14. }
    15. void I2cStop(void)
    16. {
    17. I2C_SCL_1();
    18. I2C_SDA_0();
    19. I2C_DELAY();
    20. I2C_SDA_1();
    21. I2C_DELAY();
    22. }
    23. uint8_t I2cReadAck(void)
    24. {
    25. uint8_t val = 0;
    26. uint8_t timeout = 0;
    27. I2C_SCL_0();
    28. I2C_DELAY();
    29. do
    30. {
    31. val = I2C_ReadSDA();
    32. if (++timeout > 50)
    33. {
    34. break;
    35. }
    36. }while(val == 0);
    37. I2C_SCL_1();
    38. I2C_DELAY();
    39. return val;
    40. }
    41. void I2cSendByte(uint8_t data);
    42. {
    43. uint8_t i;
    44. for (i = 0; i < 8; i++)
    45. {
    46. I2C_SCL_0();
    47. I2C_DELAY();
    48. if (data & 0x80)
    49. {
    50. I2C_SDA_1();
    51. }
    52. else
    53. {
    54. I2C_SDA_0();
    55. }
    56. data = data << 1;
    57. I2C_SCL_1();
    58. I2C_DELAY();
    59. }
    60. }
    61. uint8_t I2cReadByte(void)
    62. {
    63. uint8_t val = 0;
    64. for (i = 0; i < 8; i++)
    65. {
    66. data = data << 1;
    67. I2C_SCL_0();
    68. I2C_DELAY();
    69. if (I2C_ReadSDA())
    70. {
    71. data |= 0x01;
    72. }
    73. I2C_SCL_1();
    74. I2C_DELAY();
    75. }
    76. }
    77. void I2cWrite(uint8_t addr, uint8_t *buff, uint32_t len)
    78. {
    79. uint32_t i;
    80. uint8_t t;
    81. I2cStart();
    82. t = (addr << 1) | 0;
    83. I2cSendByte(t);
    84. for (i = 0; i < len; i++)
    85. {
    86. I2cSendByte(buff[i]);
    87. }
    88. I2cStop();
    89. }
    90. void I2cRead(uint8_t addr, uint8_t *buff, uint32_t len)
    91. {
    92. uint32_t i;
    93. uint8_t t;
    94. I2cStart();
    95. t = (addr << 1) | 1;
    96. I2cSendByte(t);
    97. for (i = 0; i < len; i++)
    98. {
    99. I2cReadByte(buff[i]);
    100. }
    101. I2cStop();
    102. }