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

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

1.7.10のファイルに更新

File size: 3.0 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#ifdef __SAM3X8E__
32#define Wire Wire1
33#endif
34
35// CONSTRUCTUR
36CAT9555::CAT9555(uint8_t addr)
37{
38 address = addr; // Store address into private variable
39}
40
41void CAT9555::begin(){
42
43 writeRegister(CONFIG_PORT0, 0x0E); // setup direction register port0
44 writeRegister(CONFIG_PORT1, 0x7F); // setup direction register port1
45 writeRegister(OUTPUT_PORT0, 0x3c); // set all output pin to LOW level
46
47}
48
49void CAT9555::digitalWrite(int PIN, int data){
50
51 uint8_t data_reg = read_8_Register(INPUT_PORT0);
52 if (data == HIGH )
53 if (PIN == LED1 || PIN == LED2)
54 data_reg = ~PIN >> 8 & data_reg;
55 else
56 data_reg = PIN >> 8 | data_reg;
57 else if (data == LOW)
58 if (PIN == LED1 || PIN == LED2)
59 data_reg = PIN >> 8 | data_reg;
60 else
61 data_reg = (0xFF ^ PIN >> 8) & data_reg;
62 writeRegister(OUTPUT_PORT0, data_reg);
63 data_reg = read_8_Register(INPUT_PORT0);
64
65}
66
67int CAT9555::digitalRead(int PIN){
68
69 uint16_t data = read_16_Register(INPUT_PORT0);
70 //Serial.println(data,BIN);
71 int result = (data ^ 0x30FF) & PIN; //0xFFFF
72 if (result)
73 return HIGH;
74 else
75 return result;
76
77}
78// WRITE REGISTER
79void CAT9555::writeRegister(int reg, int data)
80{
81
82 Wire.beginTransmission(address);
83 Wire.write(reg);
84 Wire.write(data);
85 Wire.endTransmission();
86}
87
88
89uint8_t CAT9555::read_8_Register(int reg)
90{
91 uint8_t _data0;
92 Wire.beginTransmission(address);
93 Wire.write(reg);
94 Wire.endTransmission();
95 Wire.requestFrom(address, (byte)1);
96 if(Wire.available()) {
97 _data0 = Wire.read();
98 }
99 return _data0;
100}
101// READ REGISTER
102uint16_t CAT9555::read_16_Register(int reg)
103{
104 uint16_t _data0;
105 Wire.beginTransmission(address);
106 Wire.write(reg);
107 Wire.endTransmission();
108 Wire.requestFrom(address, (byte)2);
109 if(Wire.available()) {
110 _data0 = Wire.read() << 8 ;
111 }
112 if(Wire.available()) {
113 _data0 |= Wire.read();
114 }
115
116 return _data0;
117}
118
119CAT9555 cat9555;
Note: See TracBrowser for help on using the repository browser.