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
PCF8574.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.3
5 Created on Date : 19/03/2011
6 Last update : 26/11/2015
7 
8 CopyRight 2006-2014 all rights are reserved
9 
10 ********************************************************
11 SOFTWARE LICENSE AGREEMENT
12 ********************************************************
13 
14 The usage of the supplied software imply the acceptance of the following license.
15 
16 The software supplied herewith by Mauro Laurenti (the Author) is intended for
17 use solely and exclusively on Microchip PIC Microcontroller (registered mark).
18 The software is owned by the Author, and is protected under applicable
19 copyright laws. All rights are reserved.
20 Any use in violation of the foregoing restrictions may subject the
21 user to criminal sanctions under applicable laws, as well as to civil liability
22 for the breach of the terms and conditions of this license.
23 Commercial use is forbidden without a written acknowledgement with the Author.
24 Personal or educational use is allowed if the application containing the
25 following software doesn't aim to commercial use or monetary earning of any kind.
26 
27 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
28 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
29 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
30 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
31 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
32 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
33 
34 *******************************************************************************/
35 
36 #ifdef __XC8
37  #include <xc.h>
38 #endif
39 
40 #include "PCF8574.h"
41 
42 
43 //************************************************************
44 // PCF8574_initialize function implementation
45 //************************************************************
46 void PCF8574_initialize (unsigned int baud_rate_KHz){
47 
49  PCF8574_I2C_baud_rate(baud_rate_KHz);
50 }
51 
52 
53 //************************************************************
54 // PCF8574_write_data function implementation
55 //************************************************************
56 
57 signed char PCF8574_write_data (unsigned char device_address, unsigned char data ){
58 
59 //*****************************
60 // Start Condition and control
61 // Byte are sent
62 //*****************************
63 
65 
67 
69  return (-1);
70  }
71 
72  if (PCF8574_I2C_write_byte(PCF8574_INTERNAL_ADDRESS | (device_address << 1))){
73  return (-1);
74  }
75 
76 
77 //*****************************
78 // Data Byte is sent
79 //*****************************
80 
82 
83  if (!PCF8574_I2C_check_ACK ()){
84 
85  if (PCF8574_I2C_write_byte(data)) {
86  return (-1);
87  }
88  } else {
89  return (-1);
90  }
91 
92 //*****************************
93 // Stop command is sent
94 //*****************************
95 
97 
98  if (!PCF8574_I2C_check_ACK ()) {
99 
101 
102  } else {
103  return (-1);
104  }
105 
107  return (-1);
108  }
109 
110  return (0);
111 }
112 
113 
114 
115 //************************************************************
116 // PCF8574_read_data function implementation
117 //************************************************************
118 
119 signed char PCF8574_read_data (unsigned char device_address, unsigned char *data){
120 
121  unsigned char control_byte;
122 
123  control_byte = PCF8574_INTERNAL_ADDRESS | (device_address << 1);
124 
125 //*****************************
126 // Start Condition and control
127 // Byte are sent
128 //*****************************
129 
131 
133 
135  return (-1);
136  }
137 
138  if (PCF8574_I2C_write_byte(control_byte + 1)){
139  return (-1);
140  }
141 
142 
143 //*****************************
144 // Data is Read
145 //*****************************
146 
148 
149  if (!PCF8574_I2C_check_ACK ()){
150 
151  // 1 byte reception
153 
154  // Send not ACK condition
156 
158 
160  return (-1);
161  }
162 
163  } else {
164  return (-1);
165  }
166 
167  *data = PCF8574_I2C_read_byte ();
168 
169  return (0);
170 }
171