CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

DS1307.h ???

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Doug



Joined: 30 Mar 2004
Posts: 2

View user's profile Send private message

DS1307.h ???
PostPosted: Tue Mar 30, 2004 6:01 pm     Reply with quote

Where can I locate the library for the ds1307 chip. I need to use the chip for a project but I do not have the ds1307.h file. Thanks.
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

DS1307
PostPosted: Tue Mar 30, 2004 6:12 pm     Reply with quote

I have the latest PCWH files and there is no DS1307.* that I see. There is a DS1302.c file (which is similar) but it does not have/require an .H file.

There is code for it here:
http://users.skynet.be/bk317494/routines/ds1307_void_a.htm


and here:
http://www.lecad.uni-lj.si/~leon/electronics/thermo/src/ds1307.c.html

BTW, I found this by simply doing a Google on DS1307.c, try it sometime!
Doug



Joined: 30 Mar 2004
Posts: 2

View user's profile Send private message

DS1307.h
PostPosted: Tue Mar 30, 2004 6:59 pm     Reply with quote

Those two links did not provide the library needed. I need the actually library to use those functions listed in the .c files. I did do a search for ds1307.h on google but got know results. Any other suggestions?
Mark Weir



Joined: 11 Sep 2003
Posts: 51
Location: New Zealand

View user's profile Send private message

DS 1307
PostPosted: Wed Mar 31, 2004 3:46 pm     Reply with quote

Hi there, I have this file which i think came from the forum. Maybe this will help.
Code:

///////////////////////////////////////////////////////////////////////////////
//
//
//       DS1307 Driver file from PCM Programmer       18-11-03
//       Version 1
//
//
////////////////////////////////////////////////////////////////////////////////

#include <16F877.h>

// If your demo board is running at 4 MHz, then change
// the HS to be XT, in the line below.

#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP

// If you are using a 4 MHz crystal (or 20 MHz)  then change
// the value in the next line, to match your crystal frequency.

#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

// My test board uses pin B7 and B6, instead of C4 and C3, so
// I've changed the #use i2c statement to reflect that, below.

#use i2c(Master, SDA=PIN_B7, SCL=PIN_B6)
//#use i2c(Master, SDA=PIN_C4, SCL=PIN_C3)

//========================================

// #include <ds1307.h>

// ds1307.h -- This is the include file that goes with ds1307.c

// i2c addresses
#define DS1307_I2C_WRITE_ADDR 0xd0
#define DS1307_I2C_READ_ADDR  0xd1

// DS1307 register offsets
#define DS1307_SECONDS_REG  0
#define DS1307_MINUTES_REG  1
#define DS1307_HOURS_REG    2
#define DS1307_DAY_OF_WEEK_REG 3
#define DS1307_DATE_REG     4
#define DS1307_MONTH_REG    5
#define DS1307_YEAR_REG     6
#define DS1307_CONTROL_REG  7


#define DS1307_DATE_TIME_BYTE_COUNT 7 // Includes bytes 0-6

#define DS1307_NVRAM_START_ADDR 8

// We disable the SQWV output, because it uses
// a lot of battery current when it's enabled.
// Disable it by setting Out = Open Collector.

#define DS1307_CONTROL_REG_INIT_VALUE 0x80

// 32.768 KHz output
//#define DS1307_CONTROL_REG_INIT_VALUE 0x13

//--------------------------------------------------------------------------
// GLOBAL VARIABLES

// This is a global array to hold data which is passed to/from the
// functions that set the ds1307 date and time.
char gca_ds1307_regs[DS1307_DATE_TIME_BYTE_COUNT];

//-----------------------------------------------------------------------
// FUNCTION PROTOTYPES

void ds1307_set_date_time(void);
void ds1307_read_date_time(void);

void ds1307_write_byte(char addr, char value);
char ds1307_read_byte(char addr);

char bin2bcd(char binary_value);
char bcd2bin(char bcd_value);

//=============================================
main()
{
char sec;
char min;
char hrs;
char day;
char date;
char month;
char yr;

printf("Start\n\r");

// Put some date and time values into the global date & time array.
gca_ds1307_regs[DS1307_SECONDS_REG] = 0;       // 0 seconds
gca_ds1307_regs[DS1307_MINUTES_REG] = 10;      // 10 minutes
gca_ds1307_regs[DS1307_HOURS_REG] = 8;         // 8 AM
gca_ds1307_regs[DS1307_DAY_OF_WEEK_REG]= 0;    // Skip this.
gca_ds1307_regs[DS1307_DATE_REG] = 19;         // 19th
gca_ds1307_regs[DS1307_MONTH_REG] = 4;         // April
gca_ds1307_regs[DS1307_YEAR_REG] = 02;         // 2002

// Write these values to the DS1307, for testing.
ds1307_set_date_time();

// Now read the date and time once every second, and display it.
while(1)
{
delay_ms(1000);

ds1307_read_date_time();

// Get these into variables with shorter names, so I can
// put them into printf more easily.
sec = gca_ds1307_regs[DS1307_SECONDS_REG];
min = gca_ds1307_regs[DS1307_MINUTES_REG];
hrs = gca_ds1307_regs[DS1307_HOURS_REG];
day = gca_ds1307_regs[DS1307_DAY_OF_WEEK_REG];
date = gca_ds1307_regs[DS1307_DATE_REG];
month = gca_ds1307_regs[DS1307_MONTH_REG];
yr = gca_ds1307_regs[DS1307_YEAR_REG];


printf("\%d/\%d/\%02d \%d:\%d:\%02d\n\r",month,date,yr, hrs,min,sec);
}

}


// #include <ds1307.c>

//=========================================
// ds1307.c -- Functions for the Dallas Semiconductor DS1307
// real time clock and NVRAM chip.
//
// The DS1307 uses BCD as its internal format, but we use it
// in binary format, outside of this module. So we have code
// to convert to/from BCD to binary in the functions below.

//----------------------------------------------------------------------
// Set the date and time.

/*
The registers inside the ds1307 are in this format. The values are in BCD.

DS1307_SECONDS_REG 0
DS1307_MINUTES_REG 1
DS1307_HOURS_REG 2
DS1307_DAY_OF_WEEK_REG 3 // We don't use this register. Set it to 0.
DS1307_DATE_REG 4
DS1307_MONTH_REG 5
DS1307_YEAR_REG 6
*/

void ds1307_set_date_time(void)
{
char i;
// Convert the binary ds1307 data, which is passed in a global array,
// into bcd data. Store it in the same array.

for(i = 0; i < 7; i++)
   {
    gca_ds1307_regs[i] = bin2bcd(gca_ds1307_regs[i]);
   }

// There are two control bits embedded in the following data.
// The Clock Halt bit is in bit 7 of the DS1307 Seconds register.
// We need to make sure that it's = 0, to make the clock run.
// The other bit is the 24/12 hour clock format bit, in bit 6 of
// the DS1307 Hours register. We need 24 hour mode, so set it = 0.

gca_ds1307_regs[DS1307_SECONDS_REG] &= 0x7f;
gca_ds1307_regs[DS1307_HOURS_REG]   &= 0x3f;

// Now write the 7 bytes of BCD data to the ds1307,
// using inline code for speed.
disable_interrupts(GLOBAL);

i2c_start();
i2c_write(DS1307_I2C_WRITE_ADDR);
// Start reading at the Seconds register.
i2c_write(DS1307_SECONDS_REG);

// Write 7 bytes, to registers 0 to 6.

for(i = 0; i < 7; i++)
   {
    i2c_write(gca_ds1307_regs[i]);
   }

// After setting the time in registers 0-6, also set the
// Control register. (index = 7)

// This just turns off the squarewave output pin.
// Doing it here, every time we set the clock registers,
// seems less risky than setting it near the
// start of the program, every time the unit powers-up.
i2c_write(DS1307_CONTROL_REG_INIT_VALUE);

i2c_stop();

enable_interrupts(GLOBAL);
}

//----------------------------------------------------------------------
// Read the date and time.

// The registers inside the ds1307 are in this order.
// The values inside the registers are in BCD.

// DS1307_SECONDS_REG_ADDR 0
// DS1307_MINUTES_REG_ADDR 1
// DS1307_HOURS_REG_ADDR 2
// DS1307_DAY_OF_WEEK_REG_ADDR 3
// DS1307_DATE_REG_ADDR 4
// DS1307_MONTH_REG_ADDR 5
// DS1307_YEAR_REG_ADDR 6

// We return the data in a global array. The data in is binary.

// seconds          // 0-59 seconds
// minutes          // 0-59 minutes
// hours             // 0-23 hours
// day_of_week  // 1-7
// date               // 1-31 date
// month            // 1-12 month
// year              // 00-99 year (based on year 2000)

void ds1307_read_date_time(void)
{
char i;
disable_interrupts(GLOBAL);

i2c_start();
i2c_write(DS1307_I2C_WRITE_ADDR);
// Start reading at the Seconds register.
i2c_write(DS1307_SECONDS_REG);

i2c_start();
i2c_write(DS1307_I2C_READ_ADDR);

// Read the 7 bytes from the ds1307. Mask off the unused bits.
gca_ds1307_regs[DS1307_SECONDS_REG] = i2c_read() & 0x7f;
gca_ds1307_regs[DS1307_MINUTES_REG] = i2c_read() & 0x7f;
gca_ds1307_regs[DS1307_HOURS_REG]   = i2c_read() & 0x3f;
gca_ds1307_regs[DS1307_DAY_OF_WEEK_REG] = i2c_read() & 0x07;
gca_ds1307_regs[DS1307_DATE_REG]    = i2c_read() & 0x3f;
gca_ds1307_regs[DS1307_MONTH_REG]   = i2c_read() & 0x1f;
gca_ds1307_regs[DS1307_YEAR_REG]    = i2c_read(0);

i2c_stop();

enable_interrupts(GLOBAL);

// Now convert the data from BCD to binary.
// Do it after reading the bytes, so that
// the i2c reads can be done quickly.

for(i = 0; i < 7; i++)
   {
    gca_ds1307_regs[i] = bcd2bin(gca_ds1307_regs[i]);
   }

}

//------------------------------------------------------------------------
// Read one byte at the specified address.
// This function is used to access the control byte
// or the NVRAM bytes.

char ds1307_read_byte(char addr)
{
char retval;

disable_interrupts(GLOBAL);
i2c_start();
i2c_write(DS1307_I2C_WRITE_ADDR);
i2c_write(addr);

i2c_start();
i2c_write(DS1307_I2C_READ_ADDR);
retval = i2c_read(0);   // Don't ACK the last byte read
i2c_stop();
enable_interrupts(GLOBAL);

return(retval);
}

//----------------------------------------------------------------------
// Write one byte to the DS1307.
// This function is used to access the control byte
// or the NVRAM bytes.

void ds1307_write_byte(char addr, char value)
{
disable_interrupts(GLOBAL);
i2c_start();
i2c_write(DS1307_I2C_WRITE_ADDR);
i2c_write(addr);
i2c_write(value);
i2c_stop();
enable_interrupts(GLOBAL);
}
//-------------------------------------------------------------
// This function converts an 8 bit binary value
// to an 8 bit BCD value.
// The input range must be from 0 to 99.

char bin2bcd(char binary_value)
{
char temp;
char retval;

temp = binary_value;
retval = 0;

while(1)
{
// Get the tens digit by doing multiple subtraction
// of 10 from the binary value.
if(temp >= 10)
  {
   temp -= 10;
   retval += 0x10;
  }
else // Get the ones digit by adding the remainder.
  {
   retval += temp;
   break;
  }
}

return(retval);
}

//--------------------------------------------------------------
// This function converts an 8 bit BCD value to
// an 8 bit binary value.
// The input range must be from 00 to 99.

char bcd2bin(char bcd_value)
{
char temp;

temp = bcd_value;
// Shifting upper digit right by 1 is same as multiplying by 8.
temp >>= 1;
// Isolate the bits for the upper digit.
temp &= 0x78;

// Now return: (Tens * 8) + (Tens * 2) + Ones

return(temp + (temp >> 2) + (bcd_value & 0x0f));

}

Cheers
Mark
Guest








Have you used this driver ?
PostPosted: Thu Nov 18, 2004 3:24 pm     Reply with quote

Mark,

Have you used this driver ?


Does it work ?
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

Re: DS1307.h
PostPosted: Fri Nov 19, 2004 9:09 am     Reply with quote

Doug wrote:
Those two links did not provide the library needed. I need the actually library to use those functions listed in the .c files. I did do a search for ds1307.h on google but got know results. Any other suggestions?


1) CCS does not have a linker so it cannot use LIBRARIES as such.

2) Are you saying that you cannot figure out how to make your own header file when presented with a source file?

3) The Google search results from above are for code that should support a DS1307 IC however they do not seem to be written with CCS "C" syntax. Translating them is a pretty straight forward task.

4) The code Mark Weir posted does look to be CCS "C" and should work with the appropriate changes to fuses, xtal and I2C pin assignments.

The keys to using any I2C interfaced chip with the CCS compiler is understanding the following functions(refer to the help file that came with your compiler, printed documentation or PDF file which you can download from the CCS web site).

#use i2c(...)
i2c_start()
i2c_write(...)
i2c_read(...)
i2c_stop()

Read the data sheet for the particular PIC you are using, especially if you are going to be using its hardware implemented I2C interface. The CCS compiler can generate a bit-banging I2C interface or use built-in hardware.

Next, you need to understand how the PIC interrupt system works and why it is a good idea to disable then re-enable when performing some I2C commands.

Next, you will need to understand how BCD (that is Binary Coded Decimal) works. Google can find lots of stuff about BCD.

And last of all, the DS1307 data sheet. There may even be some application notes from Maxim-IC/Dallas Semi for that chip that focus on interfacing. Not sure but again, Google is your friend and the manufacturer's web site is also your friend.

A lot of stuff to read but if you can get some uninterrupted time, it should only be a long afternoon or perhaps a full day to get through it all and have a VERY good understanding of what is happening and why it happens with your DS1307 code.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Guest








PostPosted: Sat Nov 20, 2004 6:21 pm     Reply with quote

I have used a slightly modified version of the code Mark posted, and it works fine. I did a talking time & temperature clock with DS1307, DS1620, EMIC text-to-speech module, and 4-digit serial LED display.

You can find the project & CCS C code here: http://www.rentron.com/CCS_C/SERLED_C.htm
mahdouchmehdi
Guest







Re: DS 1307
PostPosted: Thu Nov 13, 2008 5:38 pm     Reply with quote

Hi,
Can someone help me to use switches to modify a clock ?
I use a PIC 16F877 with a LCD 2x16 and I have two switches, one for selection and another for modifying the date, hour.

thanks
hayate_kun



Joined: 22 Sep 2010
Posts: 8
Location: Malaysia

View user's profile Send private message

Re: DS 1307
PostPosted: Sun Nov 14, 2010 11:55 pm     Reply with quote

Thank you very much, it was very helpful! Razz
and it runs ok! ^^
ali1_f



Joined: 19 Aug 2010
Posts: 4

View user's profile Send private message

NICE CODE
PostPosted: Sat Jul 02, 2011 2:16 pm     Reply with quote

THANKS VERY MUCH
ITS WORKING
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group