I am creating a library that talks to a DS1307 RTC that uses functions from the Wire library. I am having troubles getting my .cpp file to include Wire.h correctly.
my .h file
#ifndef DS1307_h
#define DS1307_h
#include "WProgram.h"
#define DS1307_ADDR 0x68
//masks
#define CH_BIT 0x7F
#define TIME_STYLE_24H 0x3F
#define IF_SQ_OFF_MASK 0x80
#define FREQ_CLEAR 0xFC
#define SQWE_MASK 0x10
//square wave values
#define SQ_1HZ 0x10
#define SQ_4KHZ 0x11
#define SQ_8KHZ 0x12
#define SQ_32KHZ 0x13
#define SQ_OFF_LOW 0x00
#define SQ_OFF_HIGH 0x80
class DS1307 {
public:
DS1307 ();
void begin ();
void begin (boolean);
void begin (boolean, int);
void setDate (byte, byte, byte, byte, byte, byte, byte);
void getDate (byte*, byte*, byte*, byte*, byte*, byte*, byte*);
void setSecond (byte);
void setMinute (byte);
void setHour (byte);
void setDayOfWeek (byte);
void setDayOfMonth (byte);
void setMonth (byte);
void setYear (byte);
byte getSecond ();
byte getMinute ();
byte getHour ();
byte getDayOfWeek ();
byte getDayOfMonth ();
byte getMonth ();
byte getYear ();
boolean getAMPM ();
void outputIfSquareOff (boolean);
void squareChangeFreq (int);
void squareOnOff (boolean);
void setSquare (boolean, boolean, int);
private:
void _init (boolean, boolean, int);
byte _decToBcd (byte);
byte _bcdToDec (byte);
byte _originalHour;
byte _squareVal;
boolean _hourStyle12;
};
extern DS1307 RTC;
#endif
my .cpp file (since its long, I'm only showing functions that include Wire functions, and the functions included in those functions)
#include "WProgram.h"
#include "DS1307.h"
#include "../Wire/Wire.h"
DS1307::DS1307 () {}
void DS1307::_init (boolean hourStyle12, boolean SQWE, int freq) {
Wire.begin ();
_squareVal = SQ_OFF_LOW;
_hourStyle12 = hourStyle12;
if (SQWE) {
switch (freq) {
case 1:
_squareVal = SQ_1HZ;
break;
case 4:
_squareVal = SQ_4KHZ;
break;
case 8:
_squareVal = SQ_8KHZ;
break;
case 32:
_squareVal = SQ_32KHZ;
break;
default:
_squareVal = SQ_1HZ;
break;
}
}
}
byte DS1307::_decToBcd (byte val) {
return ((val / 10 * 16) + (val % 10));
}
byte DS1307::_bcdToDec (byte val) {
return ((val / 16 * 10) + (val % 16));
}
void DS1307::setDate (byte second, byte minute, byte hour,
byte dayOfWeek, byte dayOfMonth, byte month, byte year) {
Wire.beginTransmission (DS1307_ADDR);
Wire.send (0);
Wire.send (_decToBcd(second) & CH_BIT); //0 to bit 7 starts the clock
Wire.send (_decToBcd(minute));
Wire.send (_decToBcd(hour));
Wire.send (_decToBcd(dayOfWeek));
Wire.send (_decToBcd(dayOfMonth));
Wire.send (_decToBcd(month));
Wire.send (_decToBcd(year));
Wire.send (_squareVal);
Wire.endTransmission();
}
void DS1307::getDate (byte *second, byte *minute, byte *hour,
byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year) {
Wire.beginTransmission (DS1307_ADDR);
Wire.send (0);
Wire.endTransmission ();
Wire.requestFrom (DS1307_ADDR, 7);
*second = _bcdToDec (Wire.receive() & CH_BIT);
*minute = _bcdToDec (Wire.receive());
*hour = _bcdToDec (Wire.receive() & TIME_STYLE_24H);
_originalHour = *hour;
if (_hourStyle12) {
if (*hour == 0) {
*hour = 12;
}
else if (*hour >= 13) {
*hour -= 12;
}
}
*dayOfWeek = _bcdToDec (Wire.receive());
*dayOfMonth = _bcdToDec (Wire.receive());
*month = _bcdToDec (Wire.receive());
*year = _bcdToDec (Wire.receive());
}
DS1307 RTC = DS1307();
my error (I'm "------"ing out file paths that include personal info in the path name)
C:\Users\------\AppData\Local\Temp\build2087115825781654835.tmp\DS1307\DS1307.cpp.o: In function `DS1307::getDate(unsigned char*, unsigned char*, unsigned char*, unsigned char*, unsigned char*, unsigned char*, unsigned char*)':
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:136: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:136: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:136: undefined reference to `TwoWire::beginTransmission(int)'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:137: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:137: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:137: undefined reference to `TwoWire::send(int)'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:138: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:138: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:138: undefined reference to `TwoWire::endTransmission()'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:140: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:140: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:140: undefined reference to `TwoWire::requestFrom(int, int)'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:141: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:141: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:141: undefined reference to `TwoWire::receive()'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:142: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:142: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:142: undefined reference to `TwoWire::receive()'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:143: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:143: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:143: undefined reference to `TwoWire::receive()'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:153: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:153: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:153: undefined reference to `TwoWire::receive()'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:154: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:154: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:154: undefined reference to `TwoWire::receive()'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:155: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:155: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:155: undefined reference to `TwoWire::receive()'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:156: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:156: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:156: undefined reference to `TwoWire::receive()'
C:\Users\------\AppData\Local\Temp\build2087115825781654835.tmp\DS1307\DS1307.cpp.o: In function `DS1307::_init(unsigned char, unsigned char, int)':
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:27: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:27: undefined reference to `Wire'
C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:27: undefined reference to `TwoWire::begin()'
Any and all help would be appreciated.
By the way, my sketch that impliments this works right if I have a #include <Wire.h> at the top of the sketch. It's weird, because it should work exactly the same whether you include Wire.h once or twice.