-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCF8591.cpp
More file actions
68 lines (48 loc) · 922 Bytes
/
Copy pathPCF8591.cpp
File metadata and controls
68 lines (48 loc) · 922 Bytes
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
//
// PCF8591
//
#include <Wire.h>
#ifdef __AVR__
#include <avr/pgmspace.h>
#endif
#include "PCF8591.h"
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// ctor
PCF8591::PCF8591 ()
{
}
// begin
void PCF8591::begin (uint8_t addr)
{
// i2cAddr cannot be higher than 7
if (addr > 7)
addr = 7;
_i2cAddr = PCF8591_ADDRESS | addr;
// initialize i2c library
Wire.begin ();
}
uint8_t PCF8591::analogRead (const uint8_t pin)
{
// there are only 4 pins. so value cannot be more than 3
if (pin > 3)
return 0;
Wire.beginTransmission(_i2cAddr);
Wire.write (pin);
Wire.endTransmission ();
Wire.requestFrom (_i2cAddr, 2);
// read and discard first byte
Wire.read ();
// return 2nd byte
return Wire.read ();
}
void PCF8591::analogWrite (const uint8_t val)
{
Wire.beginTransmission(_i2cAddr);
Wire.write (0x40);
Wire.write (val);
Wire.endTransmission ();
}