LTlib LaurTec Library  4.0.0 Beta
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 void I2C1_wait_bus_IDLE (void);
169 
170 #ifdef I2C_MODULE_TYPE_2
171  void I2C2_wait_bus_IDLE (void);
172 #endif
173 
174 
185 void I2C1_start_bit (void);
186 
187 #ifdef I2C_MODULE_TYPE_2
188  void I2C2_start_bit (void);
189 #endif
190 
191 
202 void I2C1_stop_bit (void);
203 
204 #ifdef I2C_MODULE_TYPE_2
205  void I2C2_stop_bit (void);
206 #endif
207 
208 
218 
219 #ifdef I2C_MODULE_TYPE_2
220  void I2C2_reset_write_collision_flag (void);
221 #endif
222 
223 
237 signed char I2C1_write_byte_to_external_device (unsigned char control_byte, unsigned char register_address, unsigned char data);
238 
239 #ifdef I2C_MODULE_TYPE_2
240  signed char I2C2_write_byte_to_external_device (unsigned char control_byte, unsigned char register_address, unsigned char data);
241 #endif
242 
243 
256 signed char I2C1_read_byte_from_external_device (unsigned char control_byte, unsigned char register_address);
257 
258 #ifdef I2C_MODULE_TYPE_2
259  signed char I2C2_read_byte_from_external_device (unsigned char control_byte, unsigned char register_address);
260 #endif
261 
262 
263 
273 #define I2C1_check_data_ready() (SSPSTATbits.BF)
274 
275 #ifdef I2C_MODULE_TYPE_2
276  #define I2C2_check_data_ready() (SSP2STATbits.BF)
277 #endif
278 
279 
290 #define I2C1_check_read_write_operation() (SSPSTATbits.R_W)
291 
292 #ifdef I2C_MODULE_TYPE_2
293  #define I2C2_check_read_write_operation() (SSP2STATbits.R_W)
294 #endif
295 
306 #define I2C1_check_stop_bit() (SSPSTATbits.P)
307 
308 #ifdef I2C_MODULE_TYPE_2
309  #define I2C2_check_stop_bit() (SSP2STATbits.P)
310 #endif
311 
312 
323 #define I2C1_check_start_bit() (SSPSTATbits.S)
324 
325 #ifdef I2C_MODULE_TYPE_2
326  #define I2C2_check_start_bit() (SSP2STATbits.S)
327 #endif
328 
339 #define I2C1_check_bus_collision() (PIR2bits.BCLIF)
340 
341 #ifdef I2C_MODULE_TYPE_2
342  #define I2C2_check_bus_collision() (PIR3bits.BCL2IF)
343 #endif
344 
355 #define I2C1_check_ACK() (SSPCON2bits.ACKSTAT)
356 
357 #ifdef I2C_MODULE_TYPE_2
358  #define I2C2_check_ACK() (SSP2CON2bits.ACKSTAT)
359 #endif
360 
361 
370 #define I2C1_restart_communication() SSPCON2bits.RSEN=1;while(SSPCON2bits.RSEN)
371 
372 #ifdef I2C_MODULE_TYPE_2
373  #define I2C2_restart_communication() SSP2CON2bits.RSEN=1;while(SSP2CON2bits.RSEN)
374 #endif
375 
384 #define I2C1_negative_ACK() SSPCON2bits.ACKDT=1, SSPCON2bits.ACKEN=1;while(SSPCON2bits.ACKEN)
385 
386 #ifdef I2C_MODULE_TYPE_2
387  #define I2C2_negative_ACK() SSP2CON2bits.ACKDT=1, SSP2CON2bits.ACKEN=1;while(SSP2CON2bits.ACKEN)
388 #endif
389 
398 #define I2C1_set_master_as_receiver() SSPCON2bits.RCEN = 1;while (SSPCON2bits.RCEN)
399 
400 #ifdef I2C_MODULE_TYPE_2
401  #define I2C2_set_master_as_receiver() SSP2CON2bits.RCEN = 1; while (SSP2CON2bits.RCEN)
402 #endif
403 
404 
405 #endif