source: rtos_arduino/trunk/arduino_lib/libraries/LuckyShield/src/lib/CAT9555.cpp@ 175

Last change on this file since 175 was 175, checked in by ertl-honda, 8 years ago

ライブラリを Arduino IDE 1.7.9 にupdate

File size: 2.9 KB
Line 
1/*
2****************************************************************************
3* Copyright (c) 2015 Arduino srl. All right reserved.
4*
5* File : CAT9555.cpp
6* Date : 2016/03/21
7* Revision : 0.0.1 $
8* Author: andrea[at]arduino[dot]org
9*
10****************************************************************************
11
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 2.1 of the License, or (at your option) any later version.
16
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
21
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26******************************************************************************/
27
28#include "CAT9555.h"
29#include <Wire.h>
30
31// CONSTRUCTUR
32CAT9555::CAT9555(uint8_t addr)
33{
34 address = addr; // Store address into private variable
35}
36
37void CAT9555::begin(){
38
39 writeRegister(CONFIG_PORT0, 0x0E); // setup direction register port0
40 writeRegister(CONFIG_PORT1, 0x7F); // setup direction register port1
41 writeRegister(OUTPUT_PORT0, 0x3c); // set all output pin to LOW level
42
43}
44
45void CAT9555::digitalWrite(int PIN, int data){
46
47 uint8_t data_reg = read_8_Register(INPUT_PORT0);
48 if (data == HIGH )
49 if (PIN == LED1 || PIN == LED2)
50 data_reg = ~PIN >> 8 & data_reg;
51 else
52 data_reg = PIN >> 8 | data_reg;
53 else if (data == LOW)
54 if (PIN == LED1 || PIN == LED2)
55 data_reg = PIN >> 8 | data_reg;
56 else
57 data_reg = (0xFF ^ PIN >> 8) & data_reg;
58 writeRegister(OUTPUT_PORT0, data_reg);
59 data_reg = read_8_Register(INPUT_PORT0);
60
61}
62
63int CAT9555::digitalRead(int PIN){
64
65 uint16_t data = read_16_Register(INPUT_PORT0);
66 //Serial.println(data,BIN);
67 int result = (data ^ 0x30FF) & PIN; //0xFFFF
68 if (result)
69 return HIGH;
70 else
71 return result;
72
73}
74// WRITE REGISTER
75void CAT9555::writeRegister(int reg, int data)
76{
77
78 Wire.beginTransmission(address);
79 Wire.write(reg);
80 Wire.write(data);
81 Wire.endTransmission();
82}
83
84
85uint8_t CAT9555::read_8_Register(int reg)
86{
87 uint8_t _data0;
88 Wire.beginTransmission(address);
89 Wire.write(reg);
90 Wire.endTransmission();
91 Wire.requestFrom(address, (byte)1);
92 if(Wire.available()) {
93 _data0 = Wire.read();
94 }
95 return _data0;
96}
97// READ REGISTER
98uint16_t CAT9555::read_16_Register(int reg)
99{
100 uint16_t _data0;
101 Wire.beginTransmission(address);
102 Wire.write(reg);
103 Wire.endTransmission();
104 Wire.requestFrom(address, (byte)2);
105 if(Wire.available()) {
106 _data0 = Wire.read() << 8 ;
107 }
108 if(Wire.available()) {
109 _data0 |= Wire.read();
110 }
111
112 return _data0;
113}
114
115CAT9555 cat9555;
Note: See TracBrowser for help on using the repository browser.