Hallo Nico,
ik heb jou voorzetje even geprobeerd.
bij het compilen heb ik ik ieder geval geen fouten, ook het uploaden naar de arduino uno geeft geen problemen.
als het programma start in de arduino gekoppeld met de pca9555 ( en een zooi leds op de uitgangen ) dan gaan na een aantal seconden alle uitgangen aan op de pca9555.
om wat te kunnen debuggen heb ik een zooi Serial.println opdrachten erin gezet om te kijken waar hij p dat moment in het programma is.
het lijkt erop dat er iets mis gaat in de configuratie bits.
hieronder de verschillende delen van de class en het programma met de debug lines voor serial output.
Ik heb vanavond meer tijd om nog meer te debuggen.
Dan heb je in ieder geval even een status update. ( gaat de goede kant op )
/*
* clsPCA9555.h
*
* Created on: 27 jul. 2015
* Author: Nico
*/
#ifndef CLSPCA9555_H_
#define CLSPCA9555_H_
#include "Arduino.h"
/** enum with names of ports ED0 - ED15 */
enum {
ED0, ED1, ED2 , ED3 , ED4 , ED5 , ED6 , ED7 ,
ED8, ED9, ED10, ED11, ED12, ED13, ED14, ED15
};
class PCA9555 {
public:
PCA9555(uint8_t port); // constructor
void pinMode(uint8_t pin, uint8_t IOMode ); // pinMode
uint8_t digitalRead(uint8_t pin); // digitalRead
void digitalWrite(uint8_t pin, uint8_t value ); // digitalWrite
private:
union {
struct {
uint8_t _configurationRegister_low; // low order byte
uint8_t _configurationRegister_high; // high order byte
};
uint16_t _configurationRegister; // 16 bits presentation
};
union {
struct {
uint8_t _valueRegister_low; // low order byte
uint8_t _valueRegister_high; // high order byte
};
uint16_t _valueRegister;
};
uint8_t _port; // address of port this class is supporting
};
#endif /* CLSPCA9555_H_ */
/**
* @file clsPCA9555.cpp
* @author Nico Verduin
* @date dd-mm-yyyy
*
* @mainpage clsPCA9555
* Class to enable pinMode(), digitalRead() and digitalWrite() functions on IO expanders
*
*
* @par Version Control
* | Revision | Author | Date |
* | :------- | :----- | :---- |
* | $Revision$ | $Author$ | $Date$ |
*
*
* @par License info
*
* <one line to give the program's name and a brief idea of what it does.>
*
* Copyright (C) 2015 Nico Verduin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Program : clsPCA9555 Copyright (C) 2015 Nico Verduin
* This is free software, and you are welcome to redistribute it.
*
*/
#include "Arduino.h"
#include "clsPCA9555.h"
#include "Wire.h"
/**
* @name PCA9555 constructor
* @param port I2C address of the IO port
* Creates the class interface and sets the I2C Address of the port
*/
PCA9555::PCA9555(uint8_t port) {
_port = port; // save the port id
Serial.println("ccp port adres overzetten in variabele ");
Serial.print("ccp adres is ");
Serial.println(_port);
Wire.begin(); // start I2C communication
Serial.println("ccp Start I2c Communicatie");
}
/**
* @name pinMode
* @param pin pin number
* @param IOMode mode of pin INPUT or OUTPUT
* sets the mode of this IO pin
*/
void PCA9555::pinMode(uint8_t pin, uint8_t IOMode) {
//
// first determine the correct bit to set
//
Serial.print("ccp Pin-number ");
Serial.println(pin);
uint16_t x = 1; // set lowest bit
x = x << pin; // shift to the correct bit.
//
// now set the correct bit in the configuration register
//
if (IOMode == OUTPUT) {
//
// mask correct bit to 0 by inverting x so that only
// the correct bit is LOW. The rest stays HIGH
//
Serial.println("ccp in if statement io-mode = output");
Serial.print("ccp waarde x ");
Serial.println(x,BIN);
x = ~x;
_configurationRegister = _configurationRegister && x;
} else {
//
// or just the required bit to 1
//
_configurationRegister = _configurationRegister || x;
}
//
// write configuration register to chip
//
Serial.println("begin wire transmission");
Serial.println(_port,HEX);
Wire.beginTransmission(_port); // setup direction registers
Wire.write(0x06); // pointer to DDR port 0
// Wire.write(_configurationRegister_low); // DDR Port0
// Wire.write(_configurationRegister_high); // DDR Port1
Wire.write(0x00); // DDR Port0
Wire.write(0x00); // DDR Port1
Wire.endTransmission();
}
/**
* @name digitalRead Reads the high/low value of specified pin
* @param pin
* @return value of pin
*/
uint8_t PCA9555::digitalRead(uint8_t pin) {
//
// first determine the correct bit to read
//
uint16_t x = 1; // set lowest bit
x = x << pin; // shift to the correct bit.
//
// read the port input register
//
Wire.beginTransmission(_port); // setup read registers
Wire.requestFrom(0x00, 2); // pointer to input register port 0
_valueRegister_low = Wire.read(); // Input register 0
_valueRegister_high = Wire.read(); // Input register 1
Wire.endTransmission();
//
// now mask the bit required and see if it is a HIGH
//
if ((_valueRegister & x) > 0){
//
// the bit is HIGH otherwise we would hava a zero value
//
return HIGH;
} else {
return LOW;
}
}
void PCA9555::digitalWrite(uint8_t pin, uint8_t value) {
uint16_t valueToSend;
uint16_t x;
//
// next convert the value we want to set to HIGH or LOW
//
if (value > 0){
valueToSend = HIGH;
} else {
valueToSend = LOW;
}
//
// now read the output register first.
//
Wire.beginTransmission(_port); // setup read registers
Wire.requestFrom(0x02, 2); // pointer to input register port 0
_valueRegister_low = Wire.read(); // Input register 0
_valueRegister_high = Wire.read(); // Input register 1
Wire.endTransmission();
//
// next set the bit we want to set
// if the value is LOW we will and the register value with correct bit set to zero
// if the value is HIGH we will or the register value with correct bit set to HIGH
//
if (valueToSend) {
//
// this is a High value so we will or it with the value register
//
x = valueToSend; // set the bit to set
x = x << pin; // shift to correct position
_valueRegister = _valueRegister | x; // and OR bit in register
} else {
//
// this is a LOW value so we have to AND it with 0 into the _valueRegister
//
x = 1; // set bit on lowest bit position
x = x << pin; // shift to correct position
x = ~x; // invert x. Now bit position is 0 and the rest is 1
_valueRegister = _valueRegister & x; // now AND all bits with corresponding in x
}
//
// write output register to chip
//
Wire.beginTransmission(_port); // setup direction registers
Wire.write(0x06); // pointer to DDR port 0
Wire.write(_configurationRegister_low); // DDR Port0
Wire.write(_configurationRegister_high); // DDR Port1
Wire.endTransmission();
}