Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

twi.h 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #ifndef _I2CMASTER_H
  2. #define _I2CMASTER_H 1
  3. /*************************************************************************
  4. * Title: C include file for the I2C master interface
  5. * (i2cmaster.S or twimaster.c)
  6. * Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
  7. * File: $Id: i2cmaster.h,v 1.10 2005/03/06 22:39:57 Peter Exp $
  8. * Software: AVR-GCC 3.4.3 / avr-libc 1.2.3
  9. * Target: any AVR device
  10. * Usage: see Doxygen manual
  11. **************************************************************************/
  12. #ifdef DOXYGEN
  13. /**
  14. @defgroup pfleury_ic2master I2C Master library
  15. @code #include <i2cmaster.h> @endcode
  16. @brief I2C (TWI) Master Software Library
  17. Basic routines for communicating with I2C slave devices. This single master
  18. implementation is limited to one bus master on the I2C bus.
  19. This I2c library is implemented as a compact assembler software implementation of the I2C protocol
  20. which runs on any AVR (i2cmaster.S) and as a TWI hardware interface for all AVR with built-in TWI hardware (twimaster.c).
  21. Since the API for these two implementations is exactly the same, an application can be linked either against the
  22. software I2C implementation or the hardware I2C implementation.
  23. Use 4.7k pull-up resistor on the SDA and SCL pin.
  24. Adapt the SCL and SDA port and pin definitions and eventually the delay routine in the module
  25. i2cmaster.S to your target when using the software I2C implementation !
  26. Adjust the CPU clock frequence F_CPU in twimaster.c or in the Makfile when using the TWI hardware implementaion.
  27. @note
  28. The module i2cmaster.S is based on the Atmel Application Note AVR300, corrected and adapted
  29. to GNU assembler and AVR-GCC C call interface.
  30. Replaced the incorrect quarter period delays found in AVR300 with
  31. half period delays.
  32. @author Peter Fleury pfleury@gmx.ch http://jump.to/fleury
  33. @par API Usage Example
  34. The following code shows typical usage of this library, see example test_i2cmaster.c
  35. @code
  36. #include <i2cmaster.h>
  37. #define Dev24C02 0xA2 // device address of EEPROM 24C02, see datasheet
  38. int main(void)
  39. {
  40. unsigned char ret;
  41. i2c_init(); // initialize I2C library
  42. // write 0x75 to EEPROM address 5 (Byte Write)
  43. i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
  44. i2c_write(0x05); // write address = 5
  45. i2c_write(0x75); // write value 0x75 to EEPROM
  46. i2c_stop(); // set stop conditon = release bus
  47. // read previously written value back from EEPROM address 5
  48. i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
  49. i2c_write(0x05); // write address = 5
  50. i2c_rep_start(Dev24C02+I2C_READ); // set device address and read mode
  51. ret = i2c_readNak(); // read one byte from EEPROM
  52. i2c_stop();
  53. for(;;);
  54. }
  55. @endcode
  56. */
  57. #endif /* DOXYGEN */
  58. /**@{*/
  59. #if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
  60. #error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
  61. #endif
  62. #include <avr/io.h>
  63. /** defines the data direction (reading from I2C device) in i2c_start(),i2c_rep_start() */
  64. #define I2C_READ 1
  65. /** defines the data direction (writing to I2C device) in i2c_start(),i2c_rep_start() */
  66. #define I2C_WRITE 0
  67. /**
  68. @brief initialize the I2C master interace. Need to be called only once
  69. @param void
  70. @return none
  71. */
  72. extern void i2c_init(void);
  73. /**
  74. @brief Terminates the data transfer and releases the I2C bus
  75. @param void
  76. @return none
  77. */
  78. extern void i2c_stop(void);
  79. /**
  80. @brief Issues a start condition and sends address and transfer direction
  81. @param addr address and transfer direction of I2C device
  82. @retval 0 device accessible
  83. @retval 1 failed to access device
  84. */
  85. extern unsigned char i2c_start(unsigned char addr);
  86. /**
  87. @brief Issues a repeated start condition and sends address and transfer direction
  88. @param addr address and transfer direction of I2C device
  89. @retval 0 device accessible
  90. @retval 1 failed to access device
  91. */
  92. extern unsigned char i2c_rep_start(unsigned char addr);
  93. /**
  94. @brief Issues a start condition and sends address and transfer direction
  95. If device is busy, use ack polling to wait until device ready
  96. @param addr address and transfer direction of I2C device
  97. @return none
  98. */
  99. extern void i2c_start_wait(unsigned char addr);
  100. /**
  101. @brief Send one byte to I2C device
  102. @param data byte to be transfered
  103. @retval 0 write successful
  104. @retval 1 write failed
  105. */
  106. extern unsigned char i2c_write(unsigned char data);
  107. /**
  108. @brief read one byte from the I2C device, request more data from device
  109. @return byte read from I2C device
  110. */
  111. extern unsigned char i2c_readAck(void);
  112. /**
  113. @brief read one byte from the I2C device, read is followed by a stop condition
  114. @return byte read from I2C device
  115. */
  116. extern unsigned char i2c_readNak(void);
  117. /**
  118. @brief read one byte from the I2C device
  119. Implemented as a macro, which calls either i2c_readAck or i2c_readNak
  120. @param ack 1 send ack, request more data from device<br>
  121. 0 send nak, read is followed by a stop condition
  122. @return byte read from I2C device
  123. */
  124. extern unsigned char i2c_read(unsigned char ack);
  125. #define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();
  126. /**@}*/
  127. #endif