mikroelektronika • view topic - ds1307 mikroc library1

18
10/21/2015 mikroElektronika • View topic DS1307 MikroC Library http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 1/18 View unanswered posts | View active topics Board index » Compilers » mikroC Compilers » Older versions of mikroC Compilers » mikroC for PIC » mikroC General All times are UTC + 1 hour Post subject: Posted: 03 Nov 2008 09:09 Login Register FAQ Search It is currently 20 Oct 2015 19:34 DS1307 MikroC Library Page 3 of 3 [ 44 posts ] Go to page Previous 1, 2,3 Print view Previous topic | Next topic Author Message dronology Joined: 29 Dec 2007 23:48 Posts: 106 Location: istanbul Dear majerv, Add these functions to your program. You can display the time value on your screen. But this display function is for T6963C drived screen. If you use KS108 or LM series LCD's (40x2 , 20x2 alphanumeric LCD) or anything else, you need yo modify this code. Code: Select all void Display_Time(char sec, char min, char hr, char day, char mn, char year); void Zero_Fill(char *value); //‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Output values to LCD void Display_Time(char sec, char min, char hr, char day, char mn, char year) { T6963C_waitReady(); ByteToStr(day,tnum); txt0 = rtrim(tnum); Zero_Fill(txt0); T6963C_waitReady(); ByteToStr(day,tnum); txt1 = rtrim(tnum); Zero_Fill(txt1); T6963C_write_text(txt1 , 9, 5, 1) ; //LCD_Out(1,6,txt); T6963C_waitReady(); ByteToStr(mn,tnum); txt2 = rtrim(tnum); Zero_Fill(txt2); T6963C_write_text(txt2 , 12, 5, 1) ; //LCD_Out(1,9,txt); T6963C_waitReady(); ByteToStr(year,tnum); txt3 = rtrim(tnum); Zero_Fill(txt3); T6963C_write_text(txt3 , 15, 5, 1) ; //LCD_Out(1,9,txt); T6963C_waitReady(); ByteToStr(hr,tnum); txt4 = rtrim(tnum); Zero_Fill(txt4); T6963C_write_text(txt4 , 9, 6, 1) ; //LCD_Out(1,9,txt); T6963C_waitReady(); ByteToStr(min,tnum); txt5 = rtrim(tnum); Zero_Fill(txt5); T6963C_write_text(txt5 , 12, 6, 1) ; //LCD_Out(1,9,txt); T6963C_waitReady();

Upload: pankaj-d-patel

Post on 09-Feb-2016

80 views

Category:

Documents


1 download

DESCRIPTION

ds1307 new

TRANSCRIPT

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 1/18

View unanswered posts | View active topics

Board index » Compilers » mikroC Compilers » Older versions of mikroC Compilers » mikroC for PIC » mikroC General All times are UTC + 1 hour

 Post subject: Posted: 03 Nov 2008 09:09 

 Login     Register  FAQ      Search

It is currently 20 Oct 2015 19:34

DS1307 MikroC Library

  Page 3 of 3  [ 44 posts ]  Go to page Previous  1, 2, 3

  Print view Previous topic | Next topic 

Author Message

dronology

Joined: 29 Dec 2007 23:48Posts: 106Location: istanbul

Dear majerv, Add these functions to your program. You can display the time value on your screen. But this display function is for T6963C drived screen.If you use KS108 or LM series LCD's (40x2 , 20x2 alphanumeric LCD) or anything else, you need yo modify this code.

Code: Select all

void Display_Time(char sec, char min, char hr, char day, char mn, char year);void Zero_Fill(char *value);

//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Output values to LCDvoid Display_Time(char sec, char min, char hr, char day, char mn, char year) {    T6963C_waitReady();    ByteToStr(day,tnum);    txt0 = rtrim(tnum);    Zero_Fill(txt0);

    T6963C_waitReady();    ByteToStr(day,tnum);    txt1 = rtrim(tnum);    Zero_Fill(txt1);    T6963C_write_text(txt1 , 9, 5, 1) ;    //LCD_Out(1,6,txt);    T6963C_waitReady();

    ByteToStr(mn,tnum);    txt2 = rtrim(tnum);    Zero_Fill(txt2);    T6963C_write_text(txt2 , 12, 5, 1) ;    //LCD_Out(1,9,txt);    T6963C_waitReady();

    ByteToStr(year,tnum);    txt3 = rtrim(tnum);    Zero_Fill(txt3);    T6963C_write_text(txt3 , 15, 5, 1) ;    //LCD_Out(1,9,txt);    T6963C_waitReady();

    ByteToStr(hr,tnum);    txt4 = rtrim(tnum);    Zero_Fill(txt4);    T6963C_write_text(txt4 , 9, 6, 1) ;    //LCD_Out(1,9,txt);    T6963C_waitReady();

    ByteToStr(min,tnum);    txt5 = rtrim(tnum);    Zero_Fill(txt5);    T6963C_write_text(txt5 , 12, 6, 1) ;    //LCD_Out(1,9,txt);    T6963C_waitReady();

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 2/18

     

 Post subject: Posted: 03 Nov 2008 10:54 

    ByteToStr(sec,tnum);    txt6 = rtrim(tnum);    Zero_Fill(txt6);    T6963C_write_text(txt6 , 15, 6, 1) ;    //LCD_Out(1,9,txt);    T6963C_waitReady();

}//~

void Zero_Fill(char *value) {    // fill text repesentation    if (value[1] == 0) {           //      with leading zero    value[1] = value[0];    value[0] = 48;    value[2] = 0;    }

_________________R. Giskard Reventlov & R. Daneel Olivaw

http://www.circuitechs.com http://www.dronology.com

Top

masterblastersachin

Joined: 07 May 2008 10:01Posts: 34Location: BANGALORE

Mohan wrote:

Hi,

Try this out. I used the code at http://www.mikroe.com/forum/viewtopic.php?t=15813

and adapted for EP5, PIC16F877A, 8Mhz crystal & DS1307LCD at PORTB with EP5 default config, PORTD pull down, Switch Jumper J17 set to VCC

Code: Select all

// Definitions for I2C devices BIOS#define DS1307_HWAddress 0xD0      // Hardware address of the RTC

// Keypad Port and lines#define KEYPAD_PORT      PORTD     // Select keypad port#define KEYPAD_MENU      F0        // Menu/Exit#define KEYPAD_SELECT    F1        // Select#define KEYPAD_INC       F2        // Inc number#define KEYPAD_DEC       F3        // Dec number#define KEYPAD_RESET     F7        // Reset

#ifndef HIGH#define HIGH 1#define LOW  0#endif // HIGH + LOW

unsigned char   bSec, bMin, bStd, bDay, bMon, bYear, oldstate;unsigned int    uSec, uMin, uStd, uDay, uMon, uYear;unsigned char   output[21];

extern void menu( void);extern void StoreToRTC( void);extern void InitDisplay( void);extern void Set_Position( unsigned char Sign);extern void IncDecSign( unsigned Sign, unsigned direct);extern unsigned char DS1307_Read(unsigned char Addr);extern void DS1307_Write(unsigned char Addr, unsigned char Data);

// Reads a byte from the DS1307 (software controlled)// Input: address to write to// Output: the byte value in the memory position// Desc: the function reads a single byte (Data) at a time to the data address indicated in Addr.

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 3/18

unsigned char DS1307_Read(unsigned char Addr){  unsigned char AddrBuff;  AddrBuff=DS1307_HWAddress;  I2C_Start();                       // Issue I2C start signal  I2C_Wr(AddrBuff);               // Send hardware address for writting  I2C_Wr(Addr);                   // Send the data address  I2C_Repeated_Start();  AddrBuff.F0=HIGH;                       // Configure the data direction bit for reading  I2C_Wr(AddrBuff);               // Send device address  AddrBuff=I2C_Rd(0);           // Read the byte from the slave and NACK to end comms  I2C_Stop();                        // Free up the bus  return(AddrBuff);}

// Writes a byte to the DS1307 (software controlled)// Input: address to write to, value to be written// Output: none// Desc: the function writes a single byte (Data) at a time to the data address indicated in DataAddr.void DS1307_Write(unsigned char Addr, unsigned char Data){  I2C_Start();                       // Issue I2C start signal  I2C_Wr(DS1307_HWAddress);       // Send hardware address for writting  I2C_Wr(Addr);                   // Send low byte of the data address  I2C_Wr(Data);                   // Send the data  I2C_Stop();                        // Free the bus   return;}

void InitDisplay( void){  Lcd_Config(&PORTB, 4, 5, 6, 3, 2, 1, 0); // Lcd_Init_EP5, see Autocomplete  Lcd_Cmd(LCD_CLEAR);  Lcd_Cmd(LCD_CURSOR_OFF);   // Turn off cursor  Lcd_Out( 1, 1, "Time: 00:00:00");  Lcd_Out( 2, 1, "Date: 00.00.0000");  Delay_ms( 2000);}

void Reset_Clock( void){  bStd = 0;  bMin = 0;  bSec = 0;  bDay = 18;  bMon = 7;  bYear = 8;  StoreToRTC();  InitDisplay();}

void Set_Position( unsigned char Sign){  switch( Sign)  {    case 0:  uStd = (unsigned int)bStd;             sprinti(&output, "%02u", uStd);             Lcd_Out( 1, 7, output);             Lcd_Out( 1, 8, "");             Lcd_Cmd(LCD_BLINK_CURSOR_ON);             break;    case 1:  uMin = (unsigned int)bMin;             sprinti(&output, "%02u", uMin);             Lcd_Out( 1, 10, output);             Lcd_Out( 1, 11, "");             Lcd_Cmd(LCD_BLINK_CURSOR_ON);             break;    case 2:  uSec = (unsigned int)bSec;             sprinti(&output, "%02u", uSec);             Lcd_Out( 1, 13, output);             Lcd_Out( 1, 14, "");             Lcd_Cmd(LCD_BLINK_CURSOR_ON);             break;    case 3:  uDay = (unsigned int)bDay;             sprinti(&output, "%02u", uDay);             Lcd_Out( 2, 7, output);

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 4/18

             Lcd_Out( 2, 8, "");             Lcd_Cmd(LCD_BLINK_CURSOR_ON);             break;    case 4:  uMon = (unsigned int)bMon;             sprinti(&output, "%02u", uMon);             Lcd_Out( 2, 10, output);             Lcd_Out( 2, 11, "");             Lcd_Cmd(LCD_BLINK_CURSOR_ON);             break;    case 5:  uYear = (unsigned int)bYear + 2000;             sprinti(&output, "%04u", uYear);             Lcd_Out( 2, 13, output);             Lcd_Out( 2, 16, "");             Lcd_Cmd(LCD_BLINK_CURSOR_ON);             break;  };}

void IncDecSign( unsigned Sign, unsigned direct){  switch( Sign)  {    case 0:  if( !direct)             {               bStd++;               if( bStd > 23)                 bStd = 23;             } else {               if( bStd > 0)                 bStd‐‐;             };             uStd = (unsigned int)bStd;             break;    case 1:  if( !direct)             {               bMin++;               if( bMin > 59)                 bMin = 59;             } else {               if( bMin > 0)                 bMin‐‐;             };             uMin = (unsigned int)bMin;             break;    case 2:  if( !direct)             {               bSec++;               if( bSec > 59)                 bSec = 59;             } else {               if( bSec > 0)                 bSec‐‐;             };             uSec = (unsigned int)bSec;             break;    case 3:  if( !direct)             {               bDay++;               if( bDay > 31)                 bDay = 31;             } else {               if( bDay > 0)                 bDay‐‐;             };             uDay = (unsigned int)bDay;             break;    case 4:  if( !direct)             {               bMon++;               if( bMon > 12)                 bMon = 12;             } else {               if( bMon > 0)                 bMon‐‐;             };             uMon = (unsigned int)bMon;

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 5/18

             break;    case 5:  if( !direct)             {               bYear++;               if( bYear > 99)                 bYear = 99;             } else {               if( bYear > 0)                 bYear‐‐;             };             uYear = (unsigned int)bYear + 2000;             break;  };}

// Store all Settings into DS1307/////////////////////////////////void StoreToRTC( void){  Lcd_Cmd(Lcd_CLEAR);  Lcd_Cmd(Lcd_CURSOR_OFF);  Lcd_Out( 1, 1, "  Store to RTC");  bSec = (((bSec / 10) << 4) & 0xF0) + ((bSec‐(bSec / 10) * 10) & 0b00001111);  DS1307_Write( 0x00, bSec);  bMin = (((bMin / 10) << 4) & 0xF0) + ((bMin‐(bMin / 10) * 10) & 0b00001111);  DS1307_Write( 0x01, bMin);  bStd = (((bStd / 10) << 4) & 0xF0) + ((bStd‐(bStd / 10) * 10) & 0b00001111);  DS1307_Write( 0x02, bStd);  // bDay = ((bDay & 0x30) >> 4)*10 + (bDay & 0b00001111);  bDay = (((bDay / 10) << 4) & 0x30) + ((bDay‐(bDay / 10) * 10) & 0b00001111);  DS1307_Write( 0x04, bDay);  bMon = (((bMon / 10) << 4) & 0x10) + ((bMon‐(bMon / 10) * 10) & 0b00001111);  DS1307_Write( 0x05, bMon);  bYear = (((bYear / 10) << 4) & 0xC0) + ((bYear‐(bYear / 10) * 10) & 0b00001111);  DS1307_Write( 0x06, bYear);  delay_ms( 2000);}

// KeyPort, input via button//////////////////////////////// F0 = Menu/Exit// F1 = Select// F2 = INC Number// F3 = DEC Number// F7 = Reset Clock‐Chipvoid menu( void){  unsigned char   Sig;

  Sig = 0;

  Lcd_Out( 1, 1, " Set");  Lcd_Out( 2, 1, "    ");  Lcd_Cmd(LCD_BLINK_CURSOR_ON);

  Set_Position( Sig);  Delay_ms( 2000);

  do  {    // Menu key pressed?    ////////////////////    if (KEYPAD_PORT.KEYPAD_MENU == 1) {      delay_ms( 250);      break;    };

    if( KEYPAD_PORT.KEYPAD_SELECT == 1) {      delay_ms( 250);      Sig++;      if( Sig > 5)        Sig = 0;      Set_Position( Sig);    };

    // Increment

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 6/18

    if( KEYPAD_PORT.KEYPAD_INC == 1) {      delay_ms( 100);      IncDecSign( Sig, 0);      Set_Position( Sig);    };

    // Decrement    if( KEYPAD_PORT.KEYPAD_DEC == 1) {      delay_ms( 100);      IncDecSign( Sig, 1);      Set_Position( Sig);    };

    Delay_ms( 100);  } while( 1);

  // Store all Settings in DS1307  ///////////////////////////////  StoreToRTC();

  // Exit to Clock

  Lcd_Cmd(Lcd_CURSOR_OFF);

  // Refresh Display  InitDisplay();}

void main( void){  TRISD  = 0b10001111;  PORTB  = 0x00;  TRISC  = 0x00;  PORTC  = 0b00;  TRISD  = 0x00;  PORTD  = 0x00;  TRISE  = 0x00;  PORTE  = 0x00;  ADCON1 = 7;        // disable analog inputs

  I2C_Init(100000);                        // initialize I2C  I2C_Start();          // issue start signal  I2C_Wr(0xD0);         // address DS1307  I2C_Wr(0);            // start from word at address 0  I2C_Wr(0);            // write 0 to REG0 (enable counting + 0 sec)  I2C_Stop();           // issue stop signal

  InitDisplay();  oldstate = 0;

  // Clock Routine  ////////////////  do  {    delay_ms( 100);    bSec = DS1307_Read( 0x00);    bSec = ((bSec & 0xF0) >> 4)*10 + (bSec & 0b00001111);    uSec = (unsigned int)bSec;    bMin = DS1307_Read( 0x01);    bMin = ((bMin & 0xF0) >> 4)*10 + (bMin & 0b00001111);    uMin = (unsigned int)bMin;    bStd = DS1307_Read( 0x02);    bStd = ((bStd & 0xF0) >> 4)*10 + (bStd & 0b00001111);    uStd = (unsigned int)bStd;

    bDay = DS1307_Read( 0x04);    bDay = ((bDay & 0x30) >> 4)*10 + (bDay & 0b00001111);    uDay = (unsigned int)bDay;    bMon = DS1307_Read( 0x05);    bMon = ((bMon & 0x10) >> 4)*10 + (bMon & 0b00001111);    uMon = (unsigned int)bMon;    bYear= DS1307_Read( 0x06);    bYear = ((bYear & 0xC0)>> 4)*10 + (bYear & 0b00001111);    uYear = (unsigned int)bYear + 2000;

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 7/18

     

 Post subject: Posted: 03 Nov 2008 11:12 

     

 Post subject: Posted: 04 Nov 2008 04:52 

    sprinti(&output, "%02u:%02u:%02u", uStd, uMin, uSec);    Lcd_out( 1, 7 , output);    sprinti(&output, "%02u.%02u.%04u", uDay, uMon, uYear);    Lcd_out( 2, 7 , output);

    // Menu key pressed?    ////////////////////    if( KEYPAD_PORT.KEYPAD_MENU == 1) {      delay_ms( 250);      menu();    } else if( KEYPAD_PORT.KEYPAD_RESET == 1) {      delay_ms( 250);      Reset_Clock();    };

  } while( 1);}

hey dude ,is this code works without any error.... but in my system its showing the error as demo limit help me yar..... thank u.....

_________________SACHIN http://belgaum.myminicity.com/

Top

petrd

Joined: 14 Feb 2008 11:04Posts: 71Location: Russia, Voronezh

Hi! This may be in case if you use MikroC without activation key and you program use more then 2K program memory (demo limit from mE). Regards!

Top

masterblastersachin

Joined: 07 May 2008 10:01Posts: 34Location: BANGALORE

Mohan wrote:

Hi,

Try this out. I used the code at http://www.mikroe.com/forum/viewtopic.php?t=15813

and adapted for EP5, PIC16F877A, 8Mhz crystal & DS1307LCD at PORTB with EP5 default config, PORTD pull down, Switch Jumper J17 set to VCC

Code: Select all

// Definitions for I2C devices BIOS#define DS1307_HWAddress 0xD0      // Hardware address of the RTC

// Keypad Port and lines#define KEYPAD_PORT      PORTD     // Select keypad port#define KEYPAD_MENU      F0        // Menu/Exit#define KEYPAD_SELECT    F1        // Select#define KEYPAD_INC       F2        // Inc number#define KEYPAD_DEC       F3        // Dec number#define KEYPAD_RESET     F7        // Reset

#ifndef HIGH#define HIGH 1#define LOW  0#endif // HIGH + LOW

unsigned char   bSec, bMin, bStd, bDay, bMon, bYear, oldstate;unsigned int    uSec, uMin, uStd, uDay, uMon, uYear;unsigned char   output[21];

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 8/18

extern void menu( void);extern void StoreToRTC( void);extern void InitDisplay( void);extern void Set_Position( unsigned char Sign);extern void IncDecSign( unsigned Sign, unsigned direct);extern unsigned char DS1307_Read(unsigned char Addr);extern void DS1307_Write(unsigned char Addr, unsigned char Data);

// Reads a byte from the DS1307 (software controlled)// Input: address to write to// Output: the byte value in the memory position// Desc: the function reads a single byte (Data) at a time to the data address indicated in Addr.unsigned char DS1307_Read(unsigned char Addr){  unsigned char AddrBuff;  AddrBuff=DS1307_HWAddress;  I2C_Start();                       // Issue I2C start signal  I2C_Wr(AddrBuff);               // Send hardware address for writting  I2C_Wr(Addr);                   // Send the data address  I2C_Repeated_Start();  AddrBuff.F0=HIGH;                       // Configure the data direction bit for reading  I2C_Wr(AddrBuff);               // Send device address  AddrBuff=I2C_Rd(0);           // Read the byte from the slave and NACK to end comms  I2C_Stop();                        // Free up the bus  return(AddrBuff);}

// Writes a byte to the DS1307 (software controlled)// Input: address to write to, value to be written// Output: none// Desc: the function writes a single byte (Data) at a time to the data address indicated in DataAddr.void DS1307_Write(unsigned char Addr, unsigned char Data){  I2C_Start();                       // Issue I2C start signal  I2C_Wr(DS1307_HWAddress);       // Send hardware address for writting  I2C_Wr(Addr);                   // Send low byte of the data address  I2C_Wr(Data);                   // Send the data  I2C_Stop();                        // Free the bus   return;}

void InitDisplay( void){  Lcd_Config(&PORTB, 4, 5, 6, 3, 2, 1, 0); // Lcd_Init_EP5, see Autocomplete  Lcd_Cmd(LCD_CLEAR);  Lcd_Cmd(LCD_CURSOR_OFF);   // Turn off cursor  Lcd_Out( 1, 1, "Time: 00:00:00");  Lcd_Out( 2, 1, "Date: 00.00.0000");  Delay_ms( 2000);}

void Reset_Clock( void){  bStd = 0;  bMin = 0;  bSec = 0;  bDay = 18;  bMon = 7;  bYear = 8;  StoreToRTC();  InitDisplay();}

void Set_Position( unsigned char Sign){  switch( Sign)  {    case 0:  uStd = (unsigned int)bStd;             sprinti(&output, "%02u", uStd);             Lcd_Out( 1, 7, output);             Lcd_Out( 1, 8, "");             Lcd_Cmd(LCD_BLINK_CURSOR_ON);             break;    case 1:  uMin = (unsigned int)bMin;             sprinti(&output, "%02u", uMin);             Lcd_Out( 1, 10, output);

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 9/18

             Lcd_Out( 1, 11, "");             Lcd_Cmd(LCD_BLINK_CURSOR_ON);             break;    case 2:  uSec = (unsigned int)bSec;             sprinti(&output, "%02u", uSec);             Lcd_Out( 1, 13, output);             Lcd_Out( 1, 14, "");             Lcd_Cmd(LCD_BLINK_CURSOR_ON);             break;    case 3:  uDay = (unsigned int)bDay;             sprinti(&output, "%02u", uDay);             Lcd_Out( 2, 7, output);             Lcd_Out( 2, 8, "");             Lcd_Cmd(LCD_BLINK_CURSOR_ON);             break;    case 4:  uMon = (unsigned int)bMon;             sprinti(&output, "%02u", uMon);             Lcd_Out( 2, 10, output);             Lcd_Out( 2, 11, "");             Lcd_Cmd(LCD_BLINK_CURSOR_ON);             break;    case 5:  uYear = (unsigned int)bYear + 2000;             sprinti(&output, "%04u", uYear);             Lcd_Out( 2, 13, output);             Lcd_Out( 2, 16, "");             Lcd_Cmd(LCD_BLINK_CURSOR_ON);             break;  };}

void IncDecSign( unsigned Sign, unsigned direct){  switch( Sign)  {    case 0:  if( !direct)             {               bStd++;               if( bStd > 23)                 bStd = 23;             } else {               if( bStd > 0)                 bStd‐‐;             };             uStd = (unsigned int)bStd;             break;    case 1:  if( !direct)             {               bMin++;               if( bMin > 59)                 bMin = 59;             } else {               if( bMin > 0)                 bMin‐‐;             };             uMin = (unsigned int)bMin;             break;    case 2:  if( !direct)             {               bSec++;               if( bSec > 59)                 bSec = 59;             } else {               if( bSec > 0)                 bSec‐‐;             };             uSec = (unsigned int)bSec;             break;    case 3:  if( !direct)             {               bDay++;               if( bDay > 31)                 bDay = 31;             } else {               if( bDay > 0)                 bDay‐‐;             };

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 10/18

             uDay = (unsigned int)bDay;             break;    case 4:  if( !direct)             {               bMon++;               if( bMon > 12)                 bMon = 12;             } else {               if( bMon > 0)                 bMon‐‐;             };             uMon = (unsigned int)bMon;             break;    case 5:  if( !direct)             {               bYear++;               if( bYear > 99)                 bYear = 99;             } else {               if( bYear > 0)                 bYear‐‐;             };             uYear = (unsigned int)bYear + 2000;             break;  };}

// Store all Settings into DS1307/////////////////////////////////void StoreToRTC( void){  Lcd_Cmd(Lcd_CLEAR);  Lcd_Cmd(Lcd_CURSOR_OFF);  Lcd_Out( 1, 1, "  Store to RTC");  bSec = (((bSec / 10) << 4) & 0xF0) + ((bSec‐(bSec / 10) * 10) & 0b00001111);  DS1307_Write( 0x00, bSec);  bMin = (((bMin / 10) << 4) & 0xF0) + ((bMin‐(bMin / 10) * 10) & 0b00001111);  DS1307_Write( 0x01, bMin);  bStd = (((bStd / 10) << 4) & 0xF0) + ((bStd‐(bStd / 10) * 10) & 0b00001111);  DS1307_Write( 0x02, bStd);  // bDay = ((bDay & 0x30) >> 4)*10 + (bDay & 0b00001111);  bDay = (((bDay / 10) << 4) & 0x30) + ((bDay‐(bDay / 10) * 10) & 0b00001111);  DS1307_Write( 0x04, bDay);  bMon = (((bMon / 10) << 4) & 0x10) + ((bMon‐(bMon / 10) * 10) & 0b00001111);  DS1307_Write( 0x05, bMon);  bYear = (((bYear / 10) << 4) & 0xC0) + ((bYear‐(bYear / 10) * 10) & 0b00001111);  DS1307_Write( 0x06, bYear);  delay_ms( 2000);}

// KeyPort, input via button//////////////////////////////// F0 = Menu/Exit// F1 = Select// F2 = INC Number// F3 = DEC Number// F7 = Reset Clock‐Chipvoid menu( void){  unsigned char   Sig;

  Sig = 0;

  Lcd_Out( 1, 1, " Set");  Lcd_Out( 2, 1, "    ");  Lcd_Cmd(LCD_BLINK_CURSOR_ON);

  Set_Position( Sig);  Delay_ms( 2000);

  do  {    // Menu key pressed?    ////////////////////    if (KEYPAD_PORT.KEYPAD_MENU == 1) {      delay_ms( 250);

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 11/18

      break;    };

    if( KEYPAD_PORT.KEYPAD_SELECT == 1) {      delay_ms( 250);      Sig++;      if( Sig > 5)        Sig = 0;      Set_Position( Sig);    };

    // Increment    if( KEYPAD_PORT.KEYPAD_INC == 1) {      delay_ms( 100);      IncDecSign( Sig, 0);      Set_Position( Sig);    };

    // Decrement    if( KEYPAD_PORT.KEYPAD_DEC == 1) {      delay_ms( 100);      IncDecSign( Sig, 1);      Set_Position( Sig);    };

    Delay_ms( 100);  } while( 1);

  // Store all Settings in DS1307  ///////////////////////////////  StoreToRTC();

  // Exit to Clock

  Lcd_Cmd(Lcd_CURSOR_OFF);

  // Refresh Display  InitDisplay();}

void main( void){  TRISD  = 0b10001111;  PORTB  = 0x00;  TRISC  = 0x00;  PORTC  = 0b00;  TRISD  = 0x00;  PORTD  = 0x00;  TRISE  = 0x00;  PORTE  = 0x00;  ADCON1 = 7;        // disable analog inputs

  I2C_Init(100000);                        // initialize I2C  I2C_Start();          // issue start signal  I2C_Wr(0xD0);         // address DS1307  I2C_Wr(0);            // start from word at address 0  I2C_Wr(0);            // write 0 to REG0 (enable counting + 0 sec)  I2C_Stop();           // issue stop signal

  InitDisplay();  oldstate = 0;

  // Clock Routine  ////////////////  do  {    delay_ms( 100);    bSec = DS1307_Read( 0x00);    bSec = ((bSec & 0xF0) >> 4)*10 + (bSec & 0b00001111);    uSec = (unsigned int)bSec;    bMin = DS1307_Read( 0x01);    bMin = ((bMin & 0xF0) >> 4)*10 + (bMin & 0b00001111);    uMin = (unsigned int)bMin;    bStd = DS1307_Read( 0x02);    bStd = ((bStd & 0xF0) >> 4)*10 + (bStd & 0b00001111);

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 12/18

     

 Post subject: Posted: 04 Nov 2008 10:01 

     

 Post subject: Posted: 06 Nov 2008 17:52 

     

 Post subject: Re: Code for DS1307 Posted: 25 Apr 2010 15:58 

    uStd = (unsigned int)bStd;

    bDay = DS1307_Read( 0x04);    bDay = ((bDay & 0x30) >> 4)*10 + (bDay & 0b00001111);    uDay = (unsigned int)bDay;    bMon = DS1307_Read( 0x05);    bMon = ((bMon & 0x10) >> 4)*10 + (bMon & 0b00001111);    uMon = (unsigned int)bMon;    bYear= DS1307_Read( 0x06);    bYear = ((bYear & 0xC0)>> 4)*10 + (bYear & 0b00001111);    uYear = (unsigned int)bYear + 2000;

    sprinti(&output, "%02u:%02u:%02u", uStd, uMin, uSec);    Lcd_out( 1, 7 , output);    sprinti(&output, "%02u.%02u.%04u", uDay, uMon, uYear);    Lcd_out( 2, 7 , output);

    // Menu key pressed?    ////////////////////    if( KEYPAD_PORT.KEYPAD_MENU == 1) {      delay_ms( 250);      menu();    } else if( KEYPAD_PORT.KEYPAD_RESET == 1) {      delay_ms( 250);      Reset_Clock();    };

  } while( 1);}

hey dude ,is this code works without any error.... but in my system its showing the error as demo limit help me yar..... thank u.....

_________________SACHIN http://belgaum.myminicity.com/

Top

masterblastersachin

Joined: 07 May 2008 10:01Posts: 34Location: BANGALORE

can any body eaxplain me what is this Demo limit? its not exceeding the 2k program memory.....

_________________SACHIN http://belgaum.myminicity.com/

Top

SPONTEX

Joined: 13 Jun 2007 07:31Posts: 22

Hello,

your code is ok, i compilated it with PIC16F877A and results:

Used ROM: 4734 octets Used RAM: 148 octets

So you are beyond 2Ko... (2734 octets in overflow)

Top

malikmuhammadali

WHEN I BUILD THIS CODE I GET THE ERROR.BCZ TWO MAIN FUNCTIONS ARE DEFINED IN A SINGLE SINGLE CODE FILE.REPLY WT I

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 13/18

     

 Post subject: Re: DS1307 MikroC Library Posted: 28 Apr 2010 12:32 

     

 Post subject: 18f4550 with DS1307 Posted: 10 Nov 2011 21:40 

Joined: 21 Apr 2010 15:30Posts: 1

DO........?

Top

balazs

Joined: 20 Mar 2010 14:53Posts: 37

In the main program you have to declare PORTB as output.That means you have to write TRISB =0,and it will work.

Regards.

Top

doup

Joined: 10 Nov 2011 21:33Posts: 2

Hi! I have a problem. I want to build a digital clock with 18f4550 and RTC. My source

Code: Select all

// LCD setupsbit LCD_RS at LATD0_bit;sbit LCD_EN at LATD1_bit;sbit LCD_D4 at LATA2_bit;sbit LCD_D5 at LATA3_bit;sbit LCD_D6 at LATA4_bit;sbit LCD_D7 at LATA5_bit;

sbit LCD_RS_Direction at TRISD0_bit;sbit LCD_EN_Direction at TRISD1_bit;sbit LCD_D4_Direction at TRISA2_bit;sbit LCD_D5_Direction at TRISA3_bit;sbit LCD_D6_Direction at TRISA4_bit;sbit LCD_D7_Direction at TRISA5_bit;

char seconds, minutes, hours, day, month, year;    // Global date/time variables

// Software I2C connectionssbit Soft_I2C_Scl           at RB1_bit;sbit Soft_I2C_Sda           at RB0_bit;sbit Soft_I2C_Scl_Direction at TRISB1_bit;sbit Soft_I2C_Sda_Direction at TRISB0_bit;// End Software I2C connections

//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Reads time and date information from RTC (PCF8583)void Read_Time() {

  Soft_I2C_Start();               // Issue start signal  Soft_I2C_Write(0xD0);           // Address PCF8583, see PCF8583 datasheet  Soft_I2C_Write(2);              // Start from address 2  Soft_I2C_Start();               // Issue repeated start signal  Soft_I2C_Write(0xD1);           // Address PCF8583 for reading R/W=1

  seconds = Soft_I2C_Read(1);     // Read seconds byte  minutes = Soft_I2C_Read(1);     // Read minutes byte  hours = Soft_I2C_Read(1);       // Read hours byte  day = Soft_I2C_Read(1);         // Read year/day byte  month = Soft_I2C_Read(0);       // Read weekday/month byte  Soft_I2C_Stop();                // Issue stop signal

}

//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Formats date and timevoid Transform_Time() {  seconds  =  ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F);  // Transform seconds  minutes  =  ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F);  // Transform months  hours    =  ((hours & 0xF0)  >> 4)*10  + (hours & 0x0F);    // Transform hours  year     =   (day & 0xC0) >> 6;                             // Transform year  day      =  ((day & 0x30) >> 4)*10    + (day & 0x0F);       // Transform day  month    =  ((month & 0x10)  >> 4)*10 + (month & 0x0F);     // Transform month}

//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Output values to LCDvoid Display_Time() {

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 14/18

     

 Post subject: Re: ds1307 Posted: 04 Nov 2012 15:41 

   Lcd_Chr(1, 6, (day / 10)   + 48);    // Print tens digit of day variable   Lcd_Chr(1, 7, (day % 10)   + 48);    // Print oness digit of day variable   Lcd_Chr(1, 9, (month / 10) + 48);   Lcd_Chr(1,10, (month % 10) + 48);   Lcd_Chr(1,15,  year        + 48);    // Print year variable  (start from year 2010)

   Lcd_Chr(2, 6, (hours / 10)   + 48);   Lcd_Chr(2, 7, (hours % 10)   + 48);   Lcd_Chr(2, 9, (minutes / 10) + 48);   Lcd_Chr(2,10, (minutes % 10) + 48);   Lcd_Chr(2,12, (seconds / 10) + 48);   Lcd_Chr(2,13, (seconds % 10) + 48);}

//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Performs project‐wide initvoid Init_Main() {  OSCCON = 0x71; //‐‐‐ órajel beállítása 8Mhz‐re  while ((OSCCON & 0x4)!=0x4); //‐‐‐ várakozás az órajel stabilitásáig  delay_ms (500);  ADCON1 = 0x0F;                         // Configure all ports with analog function as digital  CMCON  = 7;  TRISD = 0;  PORTD = 0xFF;  TRISD = 0xff;  TRISA = 0;  PORTA = 0xFF;  TRISA = 0xff;

  Soft_I2C_Init();           // Initialize Soft I2C communication  Lcd_Init();                // Initialize LCD  Lcd_Cmd(_LCD_CLEAR);       // Clear LCD display  Lcd_Cmd(_LCD_CURSOR_OFF);  // Turn cursor off

  Lcd_Out(1,1,"Date:");      // Prepare and output static text on LCD  Lcd_Chr(1,8,'.');  Lcd_Chr(1,11,'.');  Lcd_Chr(1,16,'.');  Lcd_Out(2,1,"Time:");  Lcd_Chr(2,8,':');  Lcd_Chr(2,11,':');  Lcd_Out(1,12,"201");       // start from year 2010}

//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Main procedurevoid main() {  Delay_ms(500);

  Init_Main();               // Perform initialization

  while (1) {                // Endless loop    Read_Time();             // Read time from RTC(PCF8583)    Transform_Time();        // Format date and time    Display_Time();          // Prepare and display on LCD  }}

The Lcd display always shows Date:01.00.2010Time:01:01:00 and the RTC is not running.

Somebody help me?

Top

internetuser2k11

Joined: 12 Apr 2012 02:39Posts: 258

Hello! dronology

A part of my code is like this

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 15/18

     

 Post subject: Re: DS1307 MikroC Library Posted: 08 Aug 2013 05:52 

Code: Select all

void Write_Time() {        I2C2_Start();          // issue start signal        I2C2_Wr(DS1307);       // address DS1307        I2C2_Wr(0);            // start from word at address (REG0)        I2C2_Wr(0x80);         // write $80 to REG0. (pause counter + 0 sec)        I2C2_Wr(0);            // write 0 to minutes word to (REG1)        I2C2_Wr(0x17);         // write 17 to hours word (24‐hours mode)(REG2)        I2C2_Wr(0x02);         // write 2 ‐ Monday (REG3)        I2C2_Wr(0x04);         // write 4 to date word (REG4)        I2C2_Wr(0x05);         // write 5 (May) to month word (REG5)        I2C2_Wr(0x01);         // write 01 to year word (REG6)        I2C2_Stop();           // issue stop signal

        I2C2_Start();          // issue start signal        I2C2_Wr(0xD0);         // address DS1307        I2C2_Wr(0);            // start from word at address 0        I2C2_Wr(0);            // write 0 to REG0 (enable counting + 0 sec)        I2C2_Stop();           // issue stop signal}

In that I want to know what value I have to write in this code

Code: Select all

 I2C2_Wr(0x17);         // write 17 to hours word (24‐hours mode)(REG2)

to make the clock work in 12 hr mode? I also want to display AM and PM.

ThanksJayanth D

Top

thronborg

Joined: 06 Aug 2013 11:10Posts: 14

Hi from totally newbee

I use MikroC 6.0.0 and tried to run this code but i only got a lot of ERRORS.

What is the problem, have they changed so the old I2C_Start must be changed to I2C1????

And the same with the Lcd Config?

If the problem is MikroC 6.0.0 i must downgrade to version 4 to get it to work??

Thanks in advance

Attachments:

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 16/18

     

 Post subject: Re: DS1307 MikroC Library Posted: 24 Jan 2014 10:58 

     

 Post subject: Re: DS1307 MikroC Library Posted: 27 Jan 2014 15:12 

     

 Post subject: Re: DS1307 MikroC Library Posted: 28 Nov 2014 10:27 

Skärmavbild 2013­08­08 kl. 06.48.23.png [ 19.21 KiB | Viewed 1394 times ]  

Top

jay bhut

Joined: 24 Jan 2014 10:55Posts: 4

hi i know this is too old post and its not good to replay in this.but i am in hurry and again new be and wanting for code of DS1307 with PIC 16F887 controller.so, please will you give me that code or link?

Top

marina.petrovic

Joined: 18 Apr 2013 08:11Posts: 2986

Hi,

Our RTC2 click features the DS1307 serial real‐time clock (RTC),so please, take a look at RTC2 click examples:http://www.libstock.com/projects/view/2 ... ck‐example

The example is written for EasyPIC v7 ‐ PIC18F45K22, but with the help of PIC16F887 datasheetyou can try to adjust the example to work with your microcontroller/hardware.

Best regards,Marina

Top

[email protected]

Joined: 26 Jun 2014 07:03Posts: 15

Hi am using mikroC compiler for 8051.In this compiler rtc interfacing with 8051 library is given.it works but it do not displays todaysdate & time value plz help me

this is the code

char seconds, minutes, hours, date, month; // Global date/time variablesunsigned int year;unsigned int RTCModuleAddress, YearOffset; // RTC chip description variables

#define DS1307 // Uncomment this line if you use DS1307 RTC chip (mE RTC2 extra board)

// Software I2C connectionssbit Soft_I2C_Scl at P2_3_bit;sbit Soft_I2C_Sda at P2_4_bit;// End Software I2C connections

// LCD module connectionssbit LCD_RS at P2_0_bit;sbit LCD_EN at P2_1_bit;

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 17/18

sbit LCD_D4 at P0_4_bit;sbit LCD_D5 at P0_5_bit;sbit LCD_D6 at P0_6_bit;sbit LCD_D7 at P0_7_bit;// End LCD module connections

//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Performs project‐wide initvoid Init_Main() {

#ifdef DS1307RTCModuleAddress = 0xD0;YearOffset = 2000;#endif

Soft_I2C_Init(); // Initialize Soft I2C communicationLcd_Init(); // Initialize LCDLcd_Cmd(_LCD_CLEAR); // Clear LCD displayLcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off

LCD_Out(1,1,"Date:"); // Prepare and output static text on LCDLcd_Chr(1,8,':');Lcd_Chr(1,11,':');LCD_Out(2,1,"Time:");Lcd_Chr(2,8,':');Lcd_Chr(2,11,':');}

//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Reads time and date information from DS1307 RTCvoid Read_Time_DS1307() {char byte_read;

Soft_I2C_Start(); // Issue start signalSoft_I2C_Write(RTCModuleAddress); // RTC module address + write (R#/W = 0)Soft_I2C_Write(0); // Start from seconds byteSoft_I2C_Start(); // Issue repeated start signalSoft_I2C_Write(RTCModuleAddress+1); // RTC module address + read (R#/W = 1)

byte_read = Soft_I2C_Read(1); // Read seconds byteseconds = ((byte_read & 0x70) >> 4)*10 + (byte_read & 0x0F); // Transform seconds

byte_read = Soft_I2C_Read(1); // Read minutes byteminutes = ((byte_read & 0x70) >> 4)*10 + (byte_read & 0x0F); // Transform minutes

byte_read = Soft_I2C_Read(1); // Read hours byte/*if (byte_read.B6) { // 12h formathours = ((byte_read & 0x10) >> 4)*10 + (byte_read & 0x0F); // Transform hoursif (byte_read.B5) // PM flaghours = hours + 12;}elsehours = ((byte_read & 0x30) >> 4)*10 + (byte_read & 0x0F); // Transform hours

byte_read = Soft_I2C_Read(1); // Read weekday byte

byte_read = Soft_I2C_Read(1); // Read date bytedate = ((byte_read & 0x30) >> 4)*10 + (byte_read & 0x0F); // Transform date

byte_read = Soft_I2C_Read(1); // Read month bytemonth = ((byte_read & 0x10) >> 4)*10 + (byte_read & 0x0F); // Transform month

byte_read = Soft_I2C_Read(1); // Read year byteyear = YearOffset + ((byte_read & 0xF0) >> 4)*10 + (byte_read & 0x0F); // Transform year */

Soft_I2C_Stop(); // Issue stop signal}

//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Reads time and date information from RTCvoid Read_Time() {

#ifdef DS1307Read_Time_DS1307();#endif

10/21/2015 mikroElektronika • View topic ­ DS1307 MikroC Library

http://www.mikroe.com/forum/viewtopic.php?f=13&t=16343&start=30 18/18

     

Board index » Compilers » mikroC Compilers » Older versions of mikroC Compilers » mikroC for PIC » mikroC General All times are UTC + 1 hour

}

//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Output values to LCDvoid Display_Time() {

Lcd_Chr(1, 6, (date / 10) + 48); // Print tens digit of date variableLcd_Chr(1, 7, (date % 10) + 48); // Print oness digit of date variableLcd_Chr(1, 9, (month / 10) + 48);Lcd_Chr(1,10, (month % 10) + 48);Lcd_Chr(1,12, ((year / 1000) % 10) + 48); // Print yearLcd_Chr(1,13, ((year / 100) % 10) + 48);Lcd_Chr(1,14, ((year / 10) % 10) + 48);Lcd_Chr(1,15, (year % 10) + 48);

Lcd_Chr(2, 6, (hours / 10) + 48);Lcd_Chr(2, 7, (hours % 10) + 48);Lcd_Chr(2, 9, (minutes / 10) + 48);Lcd_Chr(2,10, (minutes % 10) + 48);Lcd_Chr(2,12, (seconds / 10) + 48);Lcd_Chr(2,13, (seconds % 10) + 48);}

//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Main procedurevoid main() {

Delay_ms(2000);

Init_Main(); // Perform initialization

while (1) { // Endless loopRead_Time(); // Read time from RTCDisplay_Time(); // Prepare and display on LCD

Delay_ms(1000); // Wait 1 second}}

Top

Display posts from previous:   All posts  Sort by  Post time   Ascending   Go

  Page 3 of 3  [ 44 posts ]  Go to page Previous  1, 2, 3

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forum

You cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:     Go Jump to:               mikroC General   Go

Powered by phpBB® Forum Software © phpBB Group