PIC18 LaurTec Library
3.1
Open Source C Library for PIC18 Microcontrollers based on C18 - XC8 Compilers
Main Page
Data Structures
Files
File List
Globals
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.1
5
Created on Date : 19/03/2011
6
Last update : 02/02/2013
7
8
CopyRight 2006/2011 all rights are reserved
9
10
11
12
CopyRight 2006-2013 all rights are reserved
13
14
********************************************************
15
SOFTWARE LICENSE AGREEMENT
16
********************************************************
17
18
The usage of the supplied software imply the acceptance of the following license.
19
20
The software supplied herewith by Mauro Laurenti (the Author) is intended for
21
use solely and exclusively on Microchip PIC Microcontroller (registered mark).
22
The software is owned by the Author, and is protected under applicable
23
copyright laws. All rights are reserved.
24
Any use in violation of the foregoing restrictions may subject the
25
user to criminal sanctions under applicable laws, as well as to civil liability
26
for the breach of the terms and conditions of this license.
27
Commercial use is forbidden without a written acknowledgment with the Author.
28
Personal or educational use is allowed if the application containing the
29
following software doesn't aim to commercial use or monetary earning of any kind.
30
31
THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
32
WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
33
TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
34
PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
35
IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
36
CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
37
38
*******************************************************************************/
39
40
#ifdef __XC8
41
#include <xc.h>
42
#ifndef _PIC18
43
#error "The PCF8574 Library supports only PIC18 devices"
44
#endif
45
#endif
46
47
#include "
PCF8574.h
"
48
49
50
//************************************************************
51
// PCF8574_write_data function implementation
52
//************************************************************
53
54
signed
char
PCF8574_write_data
(
unsigned
char
control,
unsigned
char
data ){
55
56
57
//*****************************
58
// Start Condition and control
59
// Byte are sent
60
//*****************************
61
62
// Check if the module is idle
63
IdleI2C();
64
// Initiate START condition
65
StartI2C();
66
67
// Wait until start condition is over
68
while
(SSPCON2bits.SEN);
69
70
// Check if Bus collition happened
71
if
(PIR2bits.BCLIF) {
72
// Return with Bus Collision error
73
return
(-1);
74
}
75
76
// Write control byte - R/W bit should be 0
77
if
(WriteI2C(control)){
78
// Return with write Collision error
79
return
(-3);
80
}
81
82
83
//*****************************
84
// Data Byte is sent
85
//*****************************
86
87
// Check if the module is idle
88
IdleI2C();
89
90
// Check if ACK condition has been received
91
if
(!SSPCON2bits.ACKSTAT){
92
93
// Write data byte to the data port
94
if
(WriteI2C(data)) {
95
// Return with write Collision error
96
return
(-3);
97
}
98
}
else
{
99
// Return with Not Ack error condition
100
return
(-2);
101
}
102
103
//*****************************
104
// Stop command is sent
105
//*****************************
106
107
// Check if the module is idle
108
IdleI2C();
109
110
// Check if ACK condition has been received
111
if
(!SSPCON2bits.ACKSTAT) {
112
113
// Send STOP condition
114
StopI2C();
115
116
// Wait until stop condition is over
117
while
(SSPCON2bits.PEN);
118
119
}
else
{
120
// Return with Not Ack error condition
121
return
(-2);
122
}
123
124
// Test for bus collision
125
if
(PIR2bits.BCLIF){
126
// Return with Bus Collision error
127
return
(-1);
128
}
129
130
// Return with no error
131
return
(1);
132
}
133
134
135
136
//************************************************************
137
// PCF8574_read_data function implementation
138
//************************************************************
139
140
signed
char
PCF8574_read_data
(
unsigned
char
control,
unsigned
char
*data){
141
142
143
//*****************************
144
// Start Condition and control
145
// Byte are sent
146
//*****************************
147
148
// Check if the module is idle
149
IdleI2C();
150
// Initiate START condition
151
StartI2C();
152
153
// Wait until start condition is over
154
while
(SSPCON2bits.SEN);
155
156
// Check if Bus collition happened
157
if
(PIR2bits.BCLIF) {
158
// Return with Bus Collision error
159
return
(-1);
160
}
161
162
// Write Control Byte
163
if
(WriteI2C(control + 1)){
164
// Return with write collision error
165
return
(-3);
166
}
167
168
169
//*****************************
170
// Data is Read
171
//*****************************
172
173
// Check if the module is idle
174
IdleI2C();
175
176
// Check if ACK condition has been received
177
if
(!SSPCON2bits.ACKSTAT){
178
179
// Enable master for 1 byte reception
180
SSPCON2bits.RCEN = 1;
181
182
// Check that receive sequence is over
183
while
(SSPCON2bits.RCEN);
184
185
// Send not ACK condition
186
NotAckI2C();
187
188
// Wait until ACK sequence is over
189
while
(SSPCON2bits.ACKEN );
190
191
// Send STOP condition
192
StopI2C();
193
194
// Wait until stop condition is over
195
while
(SSPCON2bits.PEN);
196
197
// Check if Bus collition happened
198
if
(PIR2bits.BCLIF) {
199
// return with Bus Collision error
200
return
(-1);
201
}
202
203
}
else
{
204
// Return with Not Ack error
205
return
(-2);
206
}
207
208
// Data is read from the buffer
209
*data = SSPBUF;
210
211
// No error occured
212
return
(1);
213
214
}
215
216
LaurTec_PIC_libraries_v_3.1
src
PCF8574.c
Generated on Tue Apr 30 2013 20:06:10 for PIC18 LaurTec Library by
1.8.3.1