-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpcf8574.c
159 lines (139 loc) · 3.72 KB
/
pcf8574.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
pcf8574 lib 0x01
copyright (c) Davide Gironi, 2012
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "pcf8574.h"
//path to i2c fleury lib
#include PCF8574_I2CFLEURYPATH
/*
* initialize
*/
void pcf8574_init(void) {
//init i2c
i2c_init();
_delay_us(10);
//reset the pin status
uint8_t i = 0;
for(i=0; i<PCF8574_MAXDEVICES; i++)
pcf8574_pinstatus[i] = 0;
}
/*
* get output status
*/
int8_t pcf8574_getoutput(uint8_t deviceid) {
int8_t data = -1;
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES)) {
data = pcf8574_pinstatus[deviceid];
}
return data;
}
/*
* get output pin status
*/
int8_t pcf8574_getoutputpin(uint8_t deviceid, uint8_t pin) {
int8_t data = -1;
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES) && (pin >= 0 && pin < PCF8574_MAXPINS)) {
data = pcf8574_pinstatus[deviceid];
data = (data >> pin) & 0b00000001;
}
return data;
}
/*
* set output pins
*/
int8_t pcf8574_setoutput(uint8_t deviceid, uint8_t data) {
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES)) {
pcf8574_pinstatus[deviceid] = data;
i2c_start(((PCF8574_ADDRBASE+deviceid)<<1) | I2C_WRITE);
i2c_write(data);
i2c_stop();
return 0;
}
return -1;
}
/*
* set output pins, replace actual status of a device from pinstart for pinlength with data
*/
int8_t pcf8574_setoutputpins(uint8_t deviceid, uint8_t pinstart, uint8_t pinlength, int8_t data) {
//example:
//actual data is 0b01101110
//want to change ---
//pinstart 4
//data 101 (pinlength 3)
//result 0b01110110
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES) && (pinstart - pinlength + 1 >= 0 && pinstart - pinlength + 1 >= 0 && pinstart < PCF8574_MAXPINS && pinstart > 0 && pinlength > 0)) {
uint8_t b = 0;
b = pcf8574_pinstatus[deviceid];
uint8_t mask = ((1 << pinlength) - 1) << (pinstart - pinlength + 1);
data <<= (pinstart - pinlength + 1);
data &= mask;
b &= ~(mask);
b |= data;
pcf8574_pinstatus[deviceid] = b;
//update device
i2c_start(((PCF8574_ADDRBASE+deviceid)<<1) | I2C_WRITE);
i2c_write(b);
i2c_stop();
return 0;
}
return -1;
}
/*
* set output pin
*/
int8_t pcf8574_setoutputpin(uint8_t deviceid, uint8_t pin, uint8_t data) {
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES) && (pin >= 0 && pin < PCF8574_MAXPINS)) {
uint8_t b = 0;
b = pcf8574_pinstatus[deviceid];
b = (data != 0) ? (b | (1 << pin)) : (b & ~(1 << pin));
pcf8574_pinstatus[deviceid] = b;
//update device
i2c_start(((PCF8574_ADDRBASE+deviceid)<<1) | I2C_WRITE);
i2c_write(b);
i2c_stop();
return 0;
}
return -1;
}
/*
* set output pin high
*/
int8_t pcf8574_setoutputpinhigh(uint8_t deviceid, uint8_t pin) {
return pcf8574_setoutputpin(deviceid, pin, 1);
}
/*
* set output pin low
*/
int8_t pcf8574_setoutputpinlow(uint8_t deviceid, uint8_t pin) {
return pcf8574_setoutputpin(deviceid, pin, 0);
}
/*
* get input data
*/
int8_t pcf8574_getinput(uint8_t deviceid) {
int8_t data = -1;
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES)) {
i2c_start(((PCF8574_ADDRBASE+deviceid)<<1) | I2C_READ);
data = ~i2c_readNak();
i2c_stop();
}
return data;
}
/*
* get input pin (up or low)
*/
int8_t pcf8574_getinputpin(uint8_t deviceid, uint8_t pin) {
int8_t data = -1;
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES) && (pin >= 0 && pin < PCF8574_MAXPINS)) {
data = pcf8574_getinput(deviceid);
if(data != -1) {
data = (data >> pin) & 0b00000001;
}
}
return data;
}