Go Down

Topic: DS1307 RTC Does not name a type (Read 11589 times) previous topic - next topic

acosicdm

I downloaded the library DS1307 created by Henning Karlsen. But I cannot compile it. It Says "DS1307 Does not name a type"


Error:

DS1307_Serial_Easy:14: error: 'DS1307' does not name a type
DS1307_Serial_Easy.pde: In function 'void setup()':
DS1307_Serial_Easy:19: error: 'rtc' was not declared in this scope
DS1307_Serial_Easy:25: error: 'SUNDAY' was not declared in this scope
DS1307_Serial_Easy.pde: In function 'void loop()':
DS1307_Serial_Easy:33: error: 'rtc' was not declared in this scope



here's the code:

// DS1307_Serial_Easy (C)2010 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// A quick demo of how to use my DS1307-library to
// quickly send time and date information over a serial link
//
// I assume you know how to connect the DS1307.
// DS1307:  SDA pin   -> Arduino Digital 4
//          SCL pin   -> Arduino Digital 5

#include <DS1307.h>

// Init the DS1307
DS1307 rtc(4, 5);

void setup()
{
  // Set the clock to run-mode
  rtc.halt(false);
 
  // Setup Serial connection
  Serial.begin(9600);

  // The following lines can be commented out to use the values already stored in the DS1307
  rtc.setDOW(SUNDAY);        // Set Day-of-Week to SUNDAY
  rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(3, 10, 2010);   // Set the date to October 3th, 2010
}

void loop()
{
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
 
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
 
  // Wait one second before repeating :)
  delay (1000);
}

MorganS

Does this help?

It looks like the library didn't get included or compiled properly. Is it showing any other errors? Fix those and it will probably work.
"The problem is in the code you didn't post."

acosicdm

No other errors. What should I do? or do you have any library for RTC DS1307. Thaanks

UKHeliBob

Where is the library installed ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Nick_Pyner

Did you re-start the Arduino IDE. That's the bit that is easy to forget.

acosicdm

here is the header file .h



/*
 DS1307.h - library for DS1307 rtc
*/

// ensure this library description is only included once
#ifndef DS1307_h
#define DS1307_h

// include types & constants of Wiring core API
#include <WConstants.h>

// include types & constants of Wire ic2 lib
#include <../Wire/Wire.h>

#define DS1307_SEC 0
#define DS1307_MIN 1
#define DS1307_HR 2
#define DS1307_DOW 3
#define DS1307_DATE 4
#define DS1307_MTH 5
#define DS1307_YR 6

#define DS1307_BASE_YR 2000

#define DS1307_CTRL_ID B1101000  //DS1307

// Define register bit masks 
#define DS1307_CLOCKHALT B10000000

#define DS1307_LO_BCD  B00001111
#define DS1307_HI_BCD  B11110000

#define DS1307_HI_SEC  B01110000
#define DS1307_HI_MIN  B01110000
#define DS1307_HI_HR   B00110000
#define DS1307_LO_DOW  B00000111
#define DS1307_HI_DATE B00110000
#define DS1307_HI_MTH  B00110000
#define DS1307_HI_YR   B11110000

// library interface description
class DS1307
{
 // user-accessible "public" interface
 public:
   DS1307();
   void get(int *, boolean);
   int get(int, boolean);
     void set(int, int);
   void start(void);
   void stop(void);

 // library-accessible "private" interface
 private:
   byte rtc_bcd[7]; // used prior to read/set ds1307 registers;
     void read(void);
     void save(void);
};

extern DS1307 RTC;

#endif

acosicdm

here is the .cpp

extern "C" {
#include <../Wire/Wire.h>
}
#include "DS1307.h"

DS1307::DS1307()
{
 Wire.begin();
}

DS1307 RTC=DS1307();

// PRIVATE FUNCTIONS

// Aquire data from the RTC chip in BCD format
// refresh the buffer
void DS1307::read(void)
{
 // use the Wire lib to connect to tho rtc
 // reset the resgiter pointer to zero
 Wire.beginTransmission(DS1307_CTRL_ID);
   Wire.send(0x00);
 Wire.endTransmission();

 // request the 7 bytes of data    (secs, min, hr, dow, date. mth, yr)
 Wire.requestFrom(DS1307_CTRL_ID, 7);
 for(int i=0; i<7; i++)
 {
   // store data in raw bcd format
   rtc_bcd=Wire.receive();
 }
}

// update the data on the IC from the bcd formatted data in the buffer
void DS1307::save(void)
{
 Wire.beginTransmission(DS1307_CTRL_ID);
 Wire.send(0x00); // reset register pointer
 for(int i=0; i<7; i++)
 {
   Wire.send(rtc_bcd);
 }
 Wire.endTransmission();
}


// PUBLIC FUNCTIONS
void DS1307::get(int *rtc, boolean refresh)   // Aquire data from buffer and convert to int, refresh buffer if required
{
 if(refresh) read();
 for(int i=0;i<7;i++)  // cycle through each component, create array of data
 {
     rtc=get(i, 0);
 }
}

int DS1307::get(int c, boolean refresh)  // aquire individual RTC item from buffer, return as int, refresh buffer if required
{
 if(refresh) read();
 int v=-1;
 switch(c)
 {
 case DS1307_SEC:
   v=(10*((rtc_bcd[DS1307_SEC] & DS1307_HI_SEC)>>4))+(rtc_bcd[DS1307_SEC] & DS1307_LO_BCD);
     break;
 case DS1307_MIN:
   v=(10*((rtc_bcd[DS1307_MIN] & DS1307_HI_MIN)>>4))+(rtc_bcd[DS1307_MIN] & DS1307_LO_BCD);
     break;
 case DS1307_HR:
   v=(10*((rtc_bcd[DS1307_HR] & DS1307_HI_HR)>>4))+(rtc_bcd[DS1307_HR] & DS1307_LO_BCD);
     break;
 case DS1307_DOW:
   v=rtc_bcd[DS1307_DOW] & DS1307_LO_DOW;
     break;
 case DS1307_DATE:
   v=(10*((rtc_bcd[DS1307_DATE] & DS1307_HI_DATE)>>4))+(rtc_bcd[DS1307_DATE] & DS1307_LO_BCD);
     break;
 case DS1307_MTH:
   v=(10*((rtc_bcd[DS1307_MTH] & DS1307_HI_MTH)>>4))+(rtc_bcd[DS1307_MTH] & DS1307_LO_BCD);
     break;
 case DS1307_YR:
   v=(10*((rtc_bcd[DS1307_YR] & DS1307_HI_YR)>>4))+(rtc_bcd[DS1307_YR] & DS1307_LO_BCD)+DS1307_BASE_YR;
     break;
 } // end switch
 return v;
}

void DS1307::set(int c, int v)  // Update buffer, then update the chip
{
 switch(c)
 {
 case DS1307_SEC:
   if(v<60 && v>-1)
   {
     //preserve existing clock state (running/stopped)
     int state=rtc_bcd[DS1307_SEC] & DS1307_CLOCKHALT;
     rtc_bcd[DS1307_SEC]=state | ((v / 10)<<4) + (v % 10);
   }
   break;
 case DS1307_MIN:
   if(v<60 && v>-1)
   {
     rtc_bcd[DS1307_MIN]=((v / 10)<<4) + (v % 10);
   }
   break;
 case DS1307_HR:
 // TODO : AM/PM  12HR/24HR
   if(v<24 && v>-1)
   {
     rtc_bcd[DS1307_HR]=((v / 10)<<4) + (v % 10);
   }
   break;
 case DS1307_DOW:
   if(v<8 && v>-1)
   {
     rtc_bcd[DS1307_DOW]=v;
   }
   break;
 case DS1307_DATE:
   if(v<31 && v>-1)
   {
     rtc_bcd[DS1307_DATE]=((v / 10)<<4) + (v % 10);
   }
   break;
 case DS1307_MTH:
   if(v<13 && v>-1)
   {
     rtc_bcd[DS1307_MTH]=((v / 10)<<4) + (v % 10);
   }
   break;
 case DS1307_YR:
   if(v<13 && v>-1)
   {
     rtc_bcd[DS1307_YR]=((v / 10)<<4) + (v % 10);
   }
   break;
 } // end switch
 save();
}

void DS1307::stop(void)
{
     // set the ClockHalt bit high to stop the rtc
     // this bit is part of the seconds byte
   rtc_bcd[DS1307_SEC]=rtc_bcd[DS1307_SEC] | DS1307_CLOCKHALT;
   save();
}

void DS1307::start(void)
{
     // unset the ClockHalt bit to start the rtc
     // TODO : preserve existing seconds
   rtc_bcd[DS1307_SEC]=0;
     save();
}



JimboZA

I had trouble getting HK's 1307 library to work, even though it compiled. Try this one rather.


Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

acosicdm

I had trouble getting HK's 1307 library to work, even though it compiled. Try this one rather.



I get error.   tmElements_t tm; was not declare in this scope

acosicdm

Where is the library installed ?
It is installed in arduino/libraries

JimboZA

I get error.   tmElements_t tm; was not declare in this scope
Try run the code in the library examples folder.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

UKHeliBob

It is installed in arduino/libraries
Can you please give the full path as the Arduino installation has two libraries folders ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

acosicdm

Try run the code in the library examples folder.
Yes I run the 2 examples in the library. I get both error.

acosicdm

Can you please give the full path as the Arduino installation has two libraries folders ?
What do you mean sir?  Sorry I don't get it. I'm Just new in using arduino.

JimboZA

Yes I run the 2 examples in the library. I get both error.
Well I just this minute compiled both SetTime and ReadTest in DS1307RTC's Examples folder and they're clean.

I can't run them right now since my Uno is busy logging the temp and humidity as a test but I know they both work, I used them recently.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

Go Up