i2c.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef F_CPU
  2. #define F_CPU 16000000UL
  3. #endif
  4. #include <avr/io.h>
  5. #include <util/twi.h>
  6. #include "i2c.h"
  7. #define F_SCL 100000UL // SCL frequency
  8. #define Prescaler 1
  9. #define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16 ) / 2)
  10. void i2c_init(void)
  11. {
  12. TWBR = (uint8_t) TWBR_val;
  13. }
  14. uint8_t i2c_start(uint8_t address)
  15. {
  16. // reset TWI control register
  17. TWCR = 0;
  18. // transmit START condition
  19. TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
  20. // wait for end of transmission
  21. while (!(TWCR & (1 << TWINT))) ;
  22. // check if the start condition was successfully transmitted
  23. if ((TWSR & 0xF8) != TW_START) {
  24. return 1;
  25. }
  26. // load slave address into data register
  27. TWDR = address;
  28. // start transmission of address
  29. TWCR = (1 << TWINT) | (1 << TWEN);
  30. // wait for end of transmission
  31. while (!(TWCR & (1 << TWINT))) ;
  32. // check if the device has acknowledged the READ / WRITE mode
  33. uint8_t twst = TW_STATUS & 0xF8;
  34. if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK))
  35. return 1;
  36. return 0;
  37. }
  38. uint8_t i2c_write(uint8_t data)
  39. {
  40. // load data into data register
  41. TWDR = data;
  42. // start transmission of data
  43. TWCR = (1 << TWINT) | (1 << TWEN);
  44. // wait for end of transmission
  45. while (!(TWCR & (1 << TWINT))) ;
  46. if ((TWSR & 0xF8) != TW_MT_DATA_ACK) {
  47. return 1;
  48. }
  49. return 0;
  50. }
  51. uint8_t i2c_read_ack(void)
  52. {
  53. // start TWI module and acknowledge data after reception
  54. TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
  55. // wait for end of transmission
  56. while (!(TWCR & (1 << TWINT))) ;
  57. // return received data from TWDR
  58. return TWDR;
  59. }
  60. uint8_t i2c_read_nack(void)
  61. {
  62. // start receiving without acknowledging reception
  63. TWCR = (1 << TWINT) | (1 << TWEN);
  64. // wait for end of transmission
  65. while (!(TWCR & (1 << TWINT))) ;
  66. // return received data from TWDR
  67. return TWDR;
  68. }
  69. uint8_t i2c_transmit(uint8_t address, uint8_t * data, uint16_t length)
  70. {
  71. if (i2c_start(address | I2C_WRITE))
  72. return 1;
  73. for (uint16_t i = 0; i < length; i++) {
  74. if (i2c_write(data[i]))
  75. return 1;
  76. }
  77. i2c_stop();
  78. return 0;
  79. }
  80. uint8_t i2c_receive(uint8_t address, uint8_t * data, uint16_t length)
  81. {
  82. if (i2c_start(address | I2C_READ))
  83. return 1;
  84. for (uint16_t i = 0; i < (length - 1); i++) {
  85. data[i] = i2c_read_ack();
  86. }
  87. data[(length - 1)] = i2c_read_nack();
  88. i2c_stop();
  89. return 0;
  90. }
  91. uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t * data,
  92. uint16_t length)
  93. {
  94. if (i2c_start(devaddr | 0x00))
  95. return 1;
  96. i2c_write(regaddr);
  97. for (uint16_t i = 0; i < length; i++) {
  98. if (i2c_write(data[i]))
  99. return 1;
  100. }
  101. i2c_stop();
  102. return 0;
  103. }
  104. uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t * data,
  105. uint16_t length)
  106. {
  107. if (i2c_start(devaddr))
  108. return 1;
  109. i2c_write(regaddr);
  110. if (i2c_start(devaddr | 0x01))
  111. return 1;
  112. for (uint16_t i = 0; i < (length - 1); i++) {
  113. data[i] = i2c_read_ack();
  114. }
  115. data[(length - 1)] = i2c_read_nack();
  116. i2c_stop();
  117. return 0;
  118. }
  119. void i2c_stop(void)
  120. {
  121. // transmit STOP condition
  122. TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
  123. }