LTlib LaurTec Library  4.0.1
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
module_I2C.h
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Autore : Mauro Laurenti
4 Versione : 1.1
5 
6 Created on Date : 02/10/2010
7 Last update : 22/11/2015
8 
9 CopyRight 2006-2015 all rights are reserved
10 
11 ********************************************************
12 SOFTWARE LICENSE AGREEMENT
13 ********************************************************
14 
15 The usage of the supplied software imply the acceptance of the following license.
16 
17 The software supplied herewith by Mauro Laurenti (the Author) is intended for
18 use solely and exclusively on Microchip PIC Microcontroller (registered mark).
19 The software is owned by the Author, and is protected under applicable
20 copyright laws. All rights are reserved.
21 Any use in violation of the foregoing restrictions may subject the
22 user to criminal sanctions under applicable laws, as well as to civil liability
23 for the breach of the terms and conditions of this license.
24 Commercial use is forbidden without a written acknowledgement with the Author.
25 Personal or educational use is allowed if the application containing the
26 following software doesn't aim to commercial use or monetary earning of any kind.
27 
28 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
29 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
30 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
32 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
33 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
34 
35 *******************************************************************************/
36 
37 #ifndef LTLIB_I2C_MODULE_H
38 #define LTLIB_I2C_MODULE_H
39 
40 #ifdef __XC8
41 #include <xc.h>
42 #endif
43 
44 //*****************************************
45 // Library Test
46 //*****************************************
47 #include "LTlib.h"
48 
49 #ifndef I2C_LIBRARY_SUPPORTED
50  #error (LTlib) The MCU you have selected is not supported by module_I2C library.
51 #endif
52 
53 
54 //****************************************************
55 
56 #define I2C_SLAVE_7_BIT_ADDRESS 0b00000110
57 #define I2C_SLAVE_10_BIT_ADDRESS 0b00000111
58 #define I2C_MASTER 0b00001000
59 
60 //Slewrate should be on for 400Kb/s
61 #define I2C_SLEW_ON 0b00000000
62 
63 //Slewrate should be on for 100Kb/s
64 #define I2C_SLEW_OFF 0b10000000
65 
66 
67 
68 //****************************************************
69 
83 void I2C1_open (unsigned char device_type);
84 
85 #ifdef I2C_MODULE_TYPE_2
86  void I2C2_open (unsigned char device_type);
87 #endif
88 
89 
102 void I2C1_baud_rate (unsigned int bus_baud_rate_KHZ);
103 
104 #ifdef I2C_MODULE_TYPE_2
105  void I2C2_baud_rate (unsigned int bus_baud_rate_KHZ);
106 #endif
107 
108 
118 void I2C1_set_slave_address (unsigned int device_address);
119 
120 #ifdef I2C_MODULE_TYPE_2
121  void I2C2_set_slave_address (unsigned int device_address);
122 #endif
123 
124 
134 #define I2C1_read_byte() (SSPBUF)
135 
136 #ifdef I2C_MODULE_TYPE_2
137  #define I2C2_read_byte() (SSP2BUF)
138 #endif
139 
140 
152 signed char I2C1_write_byte (unsigned char byte_to_send);
153 
154 #ifdef I2C_MODULE_TYPE_2
155  signed char I2C2_write_byte (unsigned char byte_to_send);
156 #endif
157 
158 
159 
168 #define I2C1_wait_bus_IDLE() while((SSPCON2 & 0x1F) | (SSPSTATbits.R_W))
169 
170 #ifdef I2C_MODULE_TYPE_2
171  #define I2C2_wait_bus_IDLE() while((SSP2CON2 & 0x1F) | (SSP2STATbits.R_W))
172 #endif
173 
174 
175 
186 void I2C1_start_bit (void);
187 
188 #ifdef I2C_MODULE_TYPE_2
189  void I2C2_start_bit (void);
190 #endif
191 
192 
203 void I2C1_stop_bit (void);
204 
205 #ifdef I2C_MODULE_TYPE_2
206  void I2C2_stop_bit (void);
207 #endif
208 
209 
219 
220 #ifdef I2C_MODULE_TYPE_2
221  void I2C2_reset_write_collision_flag (void);
222 #endif
223 
224 
238 signed char I2C1_write_byte_to_external_device (unsigned char control_byte, unsigned char register_address, unsigned char data);
239 
240 #ifdef I2C_MODULE_TYPE_2
241  signed char I2C2_write_byte_to_external_device (unsigned char control_byte, unsigned char register_address, unsigned char data);
242 #endif
243 
244 
257 signed char I2C1_read_byte_from_external_device (unsigned char control_byte, unsigned char register_address);
258 
259 #ifdef I2C_MODULE_TYPE_2
260  signed char I2C2_read_byte_from_external_device (unsigned char control_byte, unsigned char register_address);
261 #endif
262 
263 
264 
274 #define I2C1_check_data_ready() (SSPSTATbits.BF)
275 
276 #ifdef I2C_MODULE_TYPE_2
277  #define I2C2_check_data_ready() (SSP2STATbits.BF)
278 #endif
279 
280 
291 #define I2C1_check_read_write_operation() (SSPSTATbits.R_W)
292 
293 #ifdef I2C_MODULE_TYPE_2
294  #define I2C2_check_read_write_operation() (SSP2STATbits.R_W)
295 #endif
296 
307 #define I2C1_check_stop_bit() (SSPSTATbits.P)
308 
309 #ifdef I2C_MODULE_TYPE_2
310  #define I2C2_check_stop_bit() (SSP2STATbits.P)
311 #endif
312 
313 
324 #define I2C1_check_start_bit() (SSPSTATbits.S)
325 
326 #ifdef I2C_MODULE_TYPE_2
327  #define I2C2_check_start_bit() (SSP2STATbits.S)
328 #endif
329 
340 #define I2C1_check_bus_collision() (PIR2bits.BCLIF)
341 
342 #ifdef I2C_MODULE_TYPE_2
343  #define I2C2_check_bus_collision() (PIR3bits.BCL2IF)
344 #endif
345 
356 #define I2C1_check_ACK() (SSPCON2bits.ACKSTAT)
357 
358 #ifdef I2C_MODULE_TYPE_2
359  #define I2C2_check_ACK() (SSP2CON2bits.ACKSTAT)
360 #endif
361 
362 
371 #define I2C1_restart_communication() SSPCON2bits.RSEN=1;while(SSPCON2bits.RSEN)
372 
373 #ifdef I2C_MODULE_TYPE_2
374  #define I2C2_restart_communication() SSP2CON2bits.RSEN=1;while(SSP2CON2bits.RSEN)
375 #endif
376 
377 
378 
387 #define I2C1_negative_ACK() SSPCON2bits.ACKDT=1, SSPCON2bits.ACKEN=1;while(SSPCON2bits.ACKEN)
388 
389 #ifdef I2C_MODULE_TYPE_2
390  #define I2C2_negative_ACK() SSP2CON2bits.ACKDT=1, SSP2CON2bits.ACKEN=1;while(SSP2CON2bits.ACKEN)
391 #endif
392 
393 
394 
403 #define I2C1_set_master_as_receiver() SSPCON2bits.RCEN = 1;while (SSPCON2bits.RCEN)
404 
405 #ifdef I2C_MODULE_TYPE_2
406  #define I2C2_set_master_as_receiver() SSP2CON2bits.RCEN = 1; while (SSP2CON2bits.RCEN)
407 #endif
408 
409 
410 #endif