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

help me about DS1307

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



Joined: 03 Mar 2010
Posts: 6

View user's profile Send private message AIM Address ICQ Number

help me about DS1307
PostPosted: Wed Jun 02, 2010 1:36 am     Reply with quote

Hi everybody. I'm from Vietnam. I'm working on a project about DS1307 (real time) connect LCD (16x2). I used CCS software. I have a problem in Void main. I don't know to call function of DS1307. Please help me.

Code:

/////////////////////////my function/////////////
//#use fast_io(b)
#define DS1307_SCL  PIN_c3
#define DS1307_SDA  PIN_c4
#use i2c(master, fast, sda=DS1307_SDA, scl=DS1307_SCL)
//===================================
int rm_bcd(BYTE data) // CHUYEN BCD SANG SO NGUYEN
{
   int i;
   i=data;
   data=(i>>4)*10;
   data=data+(i<<4>>4);
   return data;
}
// Convert normal decimal numbers to binary coded decimal
int8 decToBcd(int8 val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers

int8 bcdToDec(int8 val)
{
  return ( (val/16*10) + (val%16) );
}
//==========================
// initial DS1307
//==========================
void init_DS1307()
{
   output_float(DS1307_SCL);
   output_float(DS1307_SDA);
}
//==========================
// write data one byte to
// DS1307
//==========================
void write_DS1307(byte address, BYTE data)
{
   short int status;
   i2c_start();
   i2c_write(0xd0);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
   i2c_start();
   status=i2c_write(0xd0);
   while(status==1)
   {
      i2c_start();
      status=i2c_write(0xd0);
   }
}
//==========================
// read data one byte from DS1307
//==========================
BYTE read_DS1307(byte address)
{
   BYTE data;
   i2c_start();
   i2c_write(0xd0);
   i2c_write(address);
   i2c_start();
   i2c_write(0xd1);
   data=i2c_read(0);
   i2c_stop();
   return(data);
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(int8 second,        // 0-59
                   int8 minute,        // 0-59
                   int8 hour,          // 1-23
                   int8 dayOfWeek,     // 1-7
                   int8 dayOfMonth,    // 1-28/29/30/31
                   int8 month,         // 1-12
                   int8 year)          // 0-99
{
   write_DS1307(0x0, decToBcd(second));    // 0 to bit 7 starts the clock
   write_DS1307(0x1, decToBcd(minute));
   write_DS1307(0x2, decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   write_DS1307(0x3, decToBcd(dayOfWeek));
   write_DS1307(0x4, decToBcd(dayOfMonth));
   write_DS1307(0x5, decToBcd(month));
   write_DS1307(0x6, decToBcd(year));
}








/*


//viet ra gia tri thoi gian
void write_RTC(int add, BYTE data)
{
   short int status;
   i2c_start(); // bao hieu bat dau giao tiep i2c
   i2c_write(0xd0);  // dia chi thiet bi nhan
   i2c_write(add);  // dia chi thanh ghi
   i2c_write(data);  //gui data vao dia chi thanh ghi cua thiet bi nhan
   i2c_write(0x90);
   i2c_stop();    // bao hieu ngung ghi
   i2c_start();   // bao hieu bat dau giao tiep
   status=i2c_write(0xd0); //kiem tra trang thai cua thiet bi nhan
   while(status==1)  //lap lai cho den khi thiet bi da nhan xong (No Ack)
   {
      i2c_start();
      status=i2c_write(0xd0);
   }
   delay_us(10);
}
//doc gia tri thoi gian
BYTE read_RTC(int add)
{
   BYTE data;
   i2c_start();
   i2c_write(0xd0);
   i2c_write(add);
   i2c_start();
   i2c_write(0xd1);
   data=i2c_read(0); // No Ack
   i2c_stop();
   delay_ms(10);
   return(data);
}
*/
//================================================
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 02, 2010 11:44 am     Reply with quote

Try to use this ds1307 driver and the example code:
http://www.ccsinfo.com/forum/viewtopic.php?t=23255

Make certain that:
1. You have a 4.7K pull-up resistor on each i2c line: SDA and SCL.
2. You have a 3 volt lithium battery on the Vbat pin of the ds1307.
3. You must have a 32.768 KHz watch crystal on the ds1307.

Notice how this little board has the pull-up resistors, the battery, and
the watch crystal. All of these are needed. If you forget one of them,
your project will not work.
http://www.futurlec.com/Pictures/ET-MINI_DS1307.jpg
tieungu13



Joined: 03 Mar 2010
Posts: 6

View user's profile Send private message AIM Address ICQ Number

PostPosted: Wed Jun 02, 2010 11:57 am     Reply with quote

PCM programmer wrote:
Try to use this ds1307 driver and the example code:
http://www.ccsinfo.com/forum/viewtopic.php?t=23255

Make certain that:
1. You have a 4.7K pull-up resistor on each i2c line: SDA and SCL.
2. You have a 3 volt lithium battery on the Vbat pin of the ds1307.
3. You must have a 32.768 KHz watch crystal on the ds1307.

Notice how this little board has the pull-up resistors, the battery, and
the watch crystal. All of these are needed. If you forget one of them,
your project will not work.
http://www.futurlec.com/Pictures/ET-MINI_DS1307.jpg

Thanks for helping. I will try now !
tieungu13



Joined: 03 Mar 2010
Posts: 6

View user's profile Send private message AIM Address ICQ Number

PostPosted: Wed Jun 02, 2010 1:13 pm     Reply with quote

I try but it did not work. I want to show values (min , hour, sec, month , date....) on the LCD.
This is my code and I demo on proteus. I used 18f4550 pic.
Code:

///////////////////////////my function ////////////
#include "C:\Users\Thanh Hai\Desktop\final code\18F4550.h"
#use delay(clock=20000000)
#fuses  HS
#define RTC_SDA  PIN_B0
#define RTC_SCL  PIN_B1
#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)
#include <lcd_lib_8bit.c>
#include <ds1307_thuong.c>

void main()

   
  BYTE sec;
  BYTE min;
  BYTE hrs;
  BYTE day;
  BYTE month;
  BYTE yr;
  BYTE dow;
 
 ds1307_init();
 ds1307_set_date_time(15,6,5,2,15,20,55);
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_oscillator(False);
Lcd_init();
//set_tris_b(0x00);
///set_tris_d(0x00);
   // TODO: USER CODE!!
   while(1)
   {
     delay_ms(1000);
   
    ds1307_get_date(day,month,yr,dow);
    ds1307_get_time(hrs,min,sec);

    printf(LCD_putchar,"\f\%02d/\%02d/\%02d\r\n",day,month,yr);
   printf(LCD_putchar,"\%02d:\%02d:\%02d", hrs,min,sec);
 
   }
}
////////////////////////my lib LCD ////////////////
#define LCD_RS PIN_B2
//#define LCD_RW
#define LCD_EN PIN_B3

#define LCD_data Port_D

// misc display defines-
#define Line_1 0x80
#define Line_2 0xC0
#define Clear_Scr 0x01

// prototype statements
#separate void LCD_Init ( void );// ham khoi tao LCD
#separate void LCD_SetPosition ( unsigned int cX );//Thiet lap vi tri con tro
#separate void LCD_PutChar ( char cX );// Ham viet1kitu/1chuoi len LCD
#separate void LCD_PutCmd (int cX) ;// Ham gui lenh len LCD
#separate void LCD_PulseEnable ( void );
#separate int1 LCD_ready();

// D/n Cong

#use standard_io (D)
#use standard_io (B)


//khoi tao LCD**********************************************
#separate void LCD_Init ( void )
{
   output_D(~0x00); delay_ms(200);
   LCD_Putcmd(0x38);
   LCD_Putcmd(0x0C);
   LCD_Putcmd(0x01);
   LCD_Putcmd(0x06);

}

#separate void LCD_PutChar ( char cX )
{
   output_high(LCD_RS);
  // output_low(LCD_RW);
   output_D(cX);
   LCD_PulseEnable();
   delay_ms(5);
}

#separate void LCD_PutCmd (int cX )//Gui lenh den LCD
{
   output_low(LCD_RS);//RS = 0
  // output_low(LCD_RW);//RW = 0
   output_D(cX);        // Dua du lieu ra
   LCD_PulseEnable();
   delay_ms(5);
}

//#separate int1 LCD_ready ()
//{  int1 busy_flag;

  // trisc7 = 1;//set input
 //  output_low(LCD_RS);
  // output_high(LCD_RW);
  // LCD_pulseEnable();
  // busy_flag = RC7;
//   return(busy_flag);
//}
#separate void LCD_PulseEnable ( void )
{
output_high ( LCD_EN );// E = 1
delay_us ( 3 ); // was 10
output_low ( LCD_EN );
delay_ms ( 3 ); // was 5
}
////////////////////////////my lib DS1307//////////
////////////////////////////////////////////////////////////////////////////////
///                               DS1307.C                                   ///
///                     Driver for Real Time Clock                           ///
///                                                                          ///
/// ds1307_init() - Enable oscillator without clearing the seconds register -///
///                 used when PIC loses power and DS1307 run from 3V BAT     ///
///               - Disable squarewave output                                ///
///                                                                          ///
/// ds1307_set_date_time(day,mth,year,dow,hour,min,sec)  Set the date/time   ///
///                                                                          ///
/// ds1307_get_date(day,mth,year,dow)               Get the date             ///
///                                                                          ///
/// ds1307_get_time(hr,min,sec)                     Get the time             ///
///                                                                          ///
////////////////////////////////////////////////////////////////////////////////

#define RTC_SDA  PIN_B0
#define RTC_SCL  PIN_B1

#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)

BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);

void ds1307_init(void)
{
   BYTE seconds = 0;

   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_start();
   i2c_write(0xD1);      // RD from RTC
   seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307
   i2c_stop();
   seconds &= 0x7F;

   delay_us(3);

   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_write(bin2bcd(seconds));     // Start oscillator with current "seconds value
   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x07);      // Control Register
   i2c_write(0x80);     // Disable squarewave output pin
   i2c_stop();

}

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
  sec &= 0x7F;
  hr &= 0x3F;

  i2c_start();
  i2c_write(0xD0);            // I2C write address
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_write(bin2bcd(sec));      // REG 0
  i2c_write(bin2bcd(min));      // REG 1
  i2c_write(bin2bcd(hr));      // REG 2
  i2c_write(bin2bcd(dow));      // REG 3
  i2c_write(bin2bcd(day));      // REG 4
  i2c_write(bin2bcd(mth));      // REG 5
  i2c_write(bin2bcd(year));      // REG 6
  i2c_write(0x80);            // REG 7 - Disable squarewave output pin
  i2c_stop();
}

void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x03);            // Start at REG 3 - Day of week
  i2c_start();
  i2c_write(0xD1);
  dow  = bcd2bin(i2c_read() & 0x7f);   // REG 3
  day  = bcd2bin(i2c_read() & 0x3f);   // REG 4
  mth  = bcd2bin(i2c_read() & 0x1f);   // REG 5
  year = bcd2bin(i2c_read(0));            // REG 6
  i2c_stop();
}

void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_start();
  i2c_write(0xD1);
  sec = bcd2bin(i2c_read() & 0x7f);
  min = bcd2bin(i2c_read() & 0x7f);
  hr  = bcd2bin(i2c_read(0) & 0x3f);
  i2c_stop();

}

BYTE bin2bcd(BYTE binary_value)
{
  BYTE temp;
  BYTE 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);
}


// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
  BYTE 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));
}

I hope every one will help me ! Thanks To All.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 02, 2010 1:17 pm     Reply with quote

Quote:
I try but it did not work.

Tell us what you see. Give the details on how "it did not work".
tieungu13



Joined: 03 Mar 2010
Posts: 6

View user's profile Send private message AIM Address ICQ Number

PostPosted: Wed Jun 02, 2010 11:42 pm     Reply with quote

PCM programmer wrote:
Quote:
I try but it did not work.

Tell us what you see. Give the details on how "it did not work".

This is my project: time, date, sec, min ....the values did not show on the LCD.
I have a Proteus file in the folder. Please check it for me. Thanks.
http://megashare.vn/dl.php/1232346
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