3 * I2C communications module library header
5 * written by Vangelis Rokas, 2005 <vrokas AT otenet.gr>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 /* link the I/O library */
37 #include <pic18fregs.h>
40 #define _I2CPARAM_SPEC __data
43 /* I2C modes of operation */
44 #define I2C_SLAVE10B_INT 0x0f
45 #define I2C_SLAVE7B_INT 0x0e
46 #define I2C_SLAVE_IDLE 0x0b
47 #define I2C_MASTER 0x08
48 #define I2C_SLAVE10B 0x07
49 #define I2C_SLAVE7B 0x06
52 /* slew rate control */
53 #define I2C_SLEW_OFF 0xc0
54 #define I2C_SLEW_ON 0x00
56 /* macros to generate hardware conditions on I2C module */
58 /* generate stop condition */
59 #define I2C_STOP() SSPCON2bits.PEN=1
61 /* generate start condition */
62 #define I2C_START() SSPCON2bits.SEN=1
64 /* generate restart condition */
65 #define I2C_RESTART() SSPCON2bits.RSEN=1
67 /* generate not acknoledge condition */
68 #define I2C_NACK() SSPCON2bits.ACKDT=1; SSPCON2bits.ACKEN=1
70 /* generate acknoledge condition */
71 #define I2C_ACK() SSPCON2bits.ACKDT=0; SSPCON2bits.ACKEN=1
73 /* wait until I2C is idle */
74 #define I2C_IDLE() while((SSPCON2 & 0x1f) | (SSPSTATbits.R_W));
76 /* is data ready from I2C module ?? */
77 #define I2C_DRDY() (SSPSTATbits.BF)
80 /* function equivalent to macros for generating hardware conditions */
89 void i2c_restart(void);
97 /* wait until I2C goes idle */
100 /* is character ready in I2C buffer ?? */
101 unsigned char i2c_drdy(void);
103 /* read a character from I2C module */
104 unsigned char i2c_readchar(void);
106 /* read a string from I2C module */
107 char i2c_readstr(_I2CPARAM_SPEC unsigned char *ptr, unsigned char len);
109 /* write a character to I2C module */
110 char i2c_writechar(unsigned char dat);
112 /* write a string to I2C module */
113 char i2c_writestr(unsigned char *ptr);
115 /* configure I2C port for operation */
116 void i2c_open(unsigned char mode, unsigned char slew, unsigned char addr_brd);
118 void i2c_close(void);
121 #endif /* __I2C_H__ */