LTlib LaurTec Library  4.0.3
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.2
5 
6 Created on Date : 02/10/2010
7 Last update : 16/03/2016
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 
261 signed char I2C1_read_byte_from_external_device (unsigned char control_byte, unsigned char register_address, unsigned char *data);
262 
263 #ifdef I2C_MODULE_TYPE_2
264  signed char I2C2_read_byte_from_external_device (unsigned char control_byte, unsigned char register_address, unsigned char *data);
265 #endif
266 
267 
268 
278 #define I2C1_check_data_ready() (SSPSTATbits.BF)
279 
280 #ifdef I2C_MODULE_TYPE_2
281  #define I2C2_check_data_ready() (SSP2STATbits.BF)
282 #endif
283 
284 
295 #define I2C1_check_read_write_operation() (SSPSTATbits.R_W)
296 
297 #ifdef I2C_MODULE_TYPE_2
298  #define I2C2_check_read_write_operation() (SSP2STATbits.R_W)
299 #endif
300 
311 #define I2C1_check_stop_bit() (SSPSTATbits.P)
312 
313 #ifdef I2C_MODULE_TYPE_2
314  #define I2C2_check_stop_bit() (SSP2STATbits.P)
315 #endif
316 
317 
328 #define I2C1_check_start_bit() (SSPSTATbits.S)
329 
330 #ifdef I2C_MODULE_TYPE_2
331  #define I2C2_check_start_bit() (SSP2STATbits.S)
332 #endif
333 
344 #define I2C1_check_bus_collision() (PIR2bits.BCLIF)
345 
346 #ifdef I2C_MODULE_TYPE_2
347  #define I2C2_check_bus_collision() (PIR3bits.BCL2IF)
348 #endif
349 
360 #define I2C1_check_ACK() (SSPCON2bits.ACKSTAT)
361 
362 #ifdef I2C_MODULE_TYPE_2
363  #define I2C2_check_ACK() (SSP2CON2bits.ACKSTAT)
364 #endif
365 
366 
375 #define I2C1_restart_communication() SSPCON2bits.RSEN=1;while(SSPCON2bits.RSEN)
376 
377 #ifdef I2C_MODULE_TYPE_2
378  #define I2C2_restart_communication() SSP2CON2bits.RSEN=1;while(SSP2CON2bits.RSEN)
379 #endif
380 
381 
382 
391 #define I2C1_negative_ACK() SSPCON2bits.ACKDT=1, SSPCON2bits.ACKEN=1;while(SSPCON2bits.ACKEN)
392 
393 #ifdef I2C_MODULE_TYPE_2
394  #define I2C2_negative_ACK() SSP2CON2bits.ACKDT=1, SSP2CON2bits.ACKEN=1;while(SSP2CON2bits.ACKEN)
395 #endif
396 
397 
398 
407 #define I2C1_set_master_as_receiver() SSPCON2bits.RCEN = 1;while (SSPCON2bits.RCEN)
408 
409 #ifdef I2C_MODULE_TYPE_2
410  #define I2C2_set_master_as_receiver() SSP2CON2bits.RCEN = 1; while (SSP2CON2bits.RCEN)
411 #endif
412 
413 
414 #endif