i2c.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*-------------------------------------------------------------------------
  2. i2c.h - I2C communications module library header
  3. Copyright (C) 2005, Vangelis Rokas <vrokas AT otenet.gr>
  4. This library is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this library; see the file COPYING. If not, write to the
  14. Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  15. MA 02110-1301, USA.
  16. As a special exception, if you link this library with other files,
  17. some of which are compiled with SDCC, to produce an executable,
  18. this library does not by itself cause the resulting executable to
  19. be covered by the GNU General Public License. This exception does
  20. not however invalidate any other reasons why the executable file
  21. might be covered by the GNU General Public License.
  22. -------------------------------------------------------------------------*/
  23. /*
  24. * Devices implemented:
  25. * PIC18F[24][45][28]
  26. */
  27. #ifndef __I2C_H__
  28. #define __I2C_H__
  29. /* link the I/O library */
  30. #pragma library io
  31. #include <pic18fregs.h>
  32. #define _I2CPARAM_SPEC __data
  33. /* I2C modes of operation */
  34. #define I2C_SLAVE10B_INT 0x0f
  35. #define I2C_SLAVE7B_INT 0x0e
  36. #define I2C_SLAVE_IDLE 0x0b
  37. #define I2C_MASTER 0x08
  38. #define I2C_SLAVE10B 0x07
  39. #define I2C_SLAVE7B 0x06
  40. /* slew rate control */
  41. #define I2C_SLEW_OFF 0x80
  42. #define I2C_SLEW_ON 0x00
  43. /* macros to generate hardware conditions on I2C module */
  44. /* generate stop condition */
  45. #define I2C_STOP() do { SSPCON2bits.PEN = 1; } while (0)
  46. /* generate start condition */
  47. #define I2C_START() do { SSPCON2bits.SEN = 1; } while (0)
  48. /* generate restart condition */
  49. #define I2C_RESTART() do { SSPCON2bits.RSEN = 1; } while (0)
  50. /* generate not acknowledge condition */
  51. #define I2C_NACK() do { SSPCON2bits.ACKDT = 1; SSPCON2bits.ACKEN = 1; } while (0)
  52. /* generate acknowledge condition */
  53. #define I2C_ACK() do { SSPCON2bits.ACKDT = 0; SSPCON2bits.ACKEN = 1; } while (0)
  54. /* wait until I2C is idle */
  55. #define I2C_IDLE() do { /* busy waiting */ } while ((SSPCON2 & 0x1f) | (SSPSTATbits.R_W))
  56. /* is data ready from I2C module ?? */
  57. #define I2C_DRDY() (SSPSTATbits.BF)
  58. /* function equivalent to macros for generating hardware conditions */
  59. /* stop */
  60. void i2c_stop(void);
  61. /* start */
  62. void i2c_start(void);
  63. /* restart */
  64. void i2c_restart(void);
  65. /* not acknowledge */
  66. void i2c_nack(void);
  67. /* acknowledge */
  68. void i2c_ack(void);
  69. /* wait until I2C goes idle */
  70. void i2c_idle(void);
  71. /* is character ready in I2C buffer ?? */
  72. unsigned char i2c_drdy(void);
  73. /* read a character from I2C module */
  74. unsigned char i2c_readchar(void);
  75. /* read a string from I2C module */
  76. char i2c_readstr(_I2CPARAM_SPEC unsigned char *ptr, unsigned char len);
  77. /* write a character to I2C module */
  78. char i2c_writechar(unsigned char dat);
  79. /* write a string to I2C module */
  80. char i2c_writestr(unsigned char *ptr);
  81. /* configure I2C port for operation */
  82. void i2c_open(unsigned char mode, unsigned char slew, unsigned char addr_brd);
  83. void i2c_close(void);
  84. #endif /* __I2C_H__ */