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