Digital Clock using PIC Microcontroller and DS1307 RTC

Digital Clock using PIC Microcontroller

Digital Clock using PIC Microcontroller

A Digital Clock can be made easily by using PIC Microcontroller, DS1307 and a 16×2 LCD. I have already posted about Interfacing DS1307 RTC with PIC Microcontroller. The DS1307 RTC can work either in 24-hour mode or 12-hour mode with AM/PM indicator. It automatically adjusts for months fewer than 31 days including leap year compensation up to year 2100.  DS1307 comes with built-in power sensing circuit which senses power failures and automatically switches to back up supply. We can provide a 3V CMOS Battery for that. Communication between PIC Microcontroller and DS1307 takes place through I²C Bus.

Suggested Readings:

Circuit Diagram – Digital Clock

Digital Clock using PIC Microcontroller and DS1307 RTC - Circuit Diagram

Digital Clock using PIC Microcontroller and DS1307 RTC – Circuit Diagram

 Note: VDD , VSS of the Pic Microcontroller and VCC , GND of DS1307 are not shown in the circuit diagram. VDD, VCC should be connected to +5V and VSS, GND to OV as marked in the circuit diagram.

To simulate this project in Proteus you may need to connect I2C Debugger. SCL and SDA of I2C Debugger should be connected in parallel to SCL and SDA of DS1307. I2C Debugger can be found where CRO can be found in Proteus.

You can download the MikroC Source Code and Proteus Files etc at the end of this article. Here I explains the Source Code and different functions used in it.

The Three points to be noted while editing or creating program for this project:

  • DS1307 RTC is fully Binary Coded Decimal (BCD) clock/calender. So the data read from DS1307 should be converted to required format according to our needs and data to be written to DS1307 should be in BCD format.
  • Library for Interfacing LCD With PIC Microcontroller of MikroC needs Character or String Data. So data to be displayed in the LCD Screen should be converted to Character.
  • Addition and Subtraction cannot be directly applied on BCD. Here I first convert BCD to Binary. Then addition and subtraction can be simply applied on Binary. Then the Binary is converted back to BCD.

Functions that are used for reading and writing data from DS1307 are explained in the article Interfacing DS1307 with PIC Microcontroller please refer it. BCD2UpperCh() and BCD2LowerCh() are the two functions used to convert BCD to Character. Hour, Minute, Second, Date, Month and Year data are stored in DS1307 in separate 8-bit registers in BCD format. We read the data of these registers to access time/date. BCD2UpperCh() converts most significant 4 bits to corresponding character and BCD2LowerCh() converts least significant 4 bits to corresponding character.

The BCD2Binary() converts the BCD data read from the RTC to corresponding Binary for addition or subtraction and Binary2BCD converts the Binary back to BCD.

Bit 6 of Hour register is defined as the 24-hour or 12-hour mode selection bit.  When this bit is made high, 12-hour mode is selected and Bit 5 will represent AM/PM (Logic High represents PM).

If you have any doubts regarding the Program please do Comment.

Digital Clock

  • marielennaa

    tienes los codigos de c compiler’?

  • lijoppans

    Yes. the compiler…..MikroC Pro for PIC

  • Ahmed_Ramadan7551

    Good job

    as for Proteus File, which version you used?, because MikroC Pro v4.6 can not open this file. waiting you ASAP.

    Thanks
    Ahmed Ramadan

  • lijoppans

    I am using MikroC Pro v4.60 …………..Open the dc.c file in text editor and copy paste, then compile……….

  • disqus_VBCQpU941s

    it is OK now, but i want to enhance on my project “Smart incubator
    Machine with Temp, Humidity, Fresh Airing, Motor for flipping, serial
    Interfacing and RTC for calculating Hatching Time Interval”.

    But the mikroc pro V4.6 with me is Limited in Code editing, is there any idea instead of buying Mikroc Pro, could you help me.

    Thanks for replying

    Ahmed Ramadan

  • http://www.electrosome.com/ Ligo George

    Some of my friends said that, crack is available… …search for it………

  • Ahmed_Ramadan7551

    Thanks

    it is OK now, i replace my version by cracked one.

    Ahmed Ramadan

  • dani

    hi,
    characters for set is not blanked ?

  • http://www.electrosome.com/ Ligo George

    I don’t understand you…Please elaborate…….

  • http://pulse.yahoo.com/_6S3YF4HUAKFQGKFTKZ6SFP2QHE Ken

    I successfully loaded this program on a PIC16F877a chip and it does work so long as the PICKIT 3 that is used to program the chip remains connected. As soon as I disconnect the PICKIT, the time on the LCD stays fixed. If if flip the power off and back on (on the PIC16) then it will refresh the time back up to date. My question is why doesn’t the PIC16 seem to run when the PICKIT is no longer connected. I’ve tried playing around with the registers and so far have no luck :( Any ideas? Mycircuit is the same as the one in the article.

  • http://pulse.yahoo.com/_6S3YF4HUAKFQGKFTKZ6SFP2QHE Ken

    I just figured out what the problem was, I was using a PAD trainer for my 5 volt power supply. When I switched to another variable power supply I have, the clock now works as it should with or without the PICKIT connected. FINALLY!

  • http://www.facebook.com/lijoppans Ligo George

    It may be the problem of power supply………PICKIT3 supply’s the 5v supply needed for the pic……..when it is disconnected the sudden change in the supply voltage affects it working………Use a 100uF capacitor across the VCC and VSS…….I think it will solve it.

  • Capm90

    i have this p`roblem
    in the line 1
    function main not definied

  • http://www.facebook.com/lijoppans Ligo George

    Are you using MikroC Pro..?? ……..

    if yes……..open a new project, copy paste the code there and compile……

  • rahul

    HELLO SIR…..i have done my proteus simulation and and it works properly but in hardware lcd doesnot show any thing ….is there any precaution or special arrangement to connect DS1307 IC to 8051 kit…..

    thank you.

  • http://www.electrosome.com/ Ligo George

    to 8051 kit ? …….. DS1307 and its crystal are very sensitive to noise…So please read the datasheet of DS1307 before designing the pcb ……

  • rahul

    i have read sir ..i am getting where is prob….8051 kit is working properly but only DS1307 kit is not working properly as i think …….please suggest me what can i do.

  • Akeem

    pls, can you tell me the size of crystal for the PIC .

  • http://www.electrosome.com/ Ligo George

    8MHz………….. you can use 4 or 8 or 20…..

  • http://www.electrosome.com/ Ligo George

    check whether the sda & scl pins are connected properly and pull up resistor is connected properly………..
    i

  • Khamarul Azman Mustafa

    sir, can you tell me what is the specific component of i2c in proteus…where i can get that hardware… help me sir… thanks

  • http://www.electrosome.com/ Ligo George

    It is I2C Debugger………………… No need of it for hardware implementation ….

  • Khamarul Azman Mustafa

    can I put the alarm for this circuit but I don’t know how to program.. can you help me on this…

  • http://www.electrosome.com/ Ligo George

    Yes, for alarm you may connect a Buzzer to PIC Microcontroller…..

  • Khamarul Azman Mustafa

    but i don’t know which pin that connect to PIC… help me sir…pliss

  • Khamarul Azman Mustafa

    what is pullup and pulldown in proteus??

  • http://www.electrosome.com/ Ligo George

    You can connect a buzzer to any of the output pin of the PIC…………and program it………For eg: if you connect buzzer to 1st pin of PORTD (19th pin)
    PORTD.F0 = 1 makes its output 5v while
    PORTD.F0 = 0 makes it output 0V

  • http://www.electrosome.com/ Ligo George

    Pull up and Pull Down are two resistors………….. As its name indicates pull up is used to make a pin (or node) high while Pull Down is used to make a pin (or node) low……..But in the circuit I used 10K ohm resistors instead of Pull up and pull down…..

  • Khamarul Azman Mustafa

    PORTD.F0 = 1 makes its output 5v while
    PORTD.F0 = 0 makes it output 0V

    there is the program that you mentioned or such to understand me about connection. PORTD.F0 = 0 makes it output 0V… I don’t understand this…

  • http://www.electrosome.com/ Ligo George

    to make a pin high for 3 seconds at a particular time :

    if(hour == 5 && min == 40 && sec == 00)

    {
    PORTD.F0 = 1;
    Delay_ms(3000);
    PORTD.F0 = 0;
    }

    This code makes 19th pin of microcontroller high at 5:40

  • Khamarul Azman Mustafa

    I will try that… hope you can follow up my question because I don’t know about PIC… there is the hardware for pullup and pulldown resistor or just use the common resistor??

  • http://www.electrosome.com/ Ligo George

    Pull up and pull down are common resistors ………..A resistor is called pull up when it is used to make HIGH a node……… while a resistor is called pull down when it is used to make a node LOW

  • Mrunal Ahirrao

    sir is BCD2upperCh/lowerCh are library routines present in mikroC?

  • http://www.electrosome.com/ Ligo George

    No they are used defined libraries……….. You can download it above..

    The program is lengthy so I didn’t included in the article………

  • kikouu

    good morning sir
    i was fortunate by downloading this project and im very happy for that but i got a little problem thats when the lcd dispalying time i see that theres a difference between the real time and your time on lcd in second digit its not running at the same time with the real second clock please can you help me to figure out this problem and realize my personal seven segment clock??

  • http://www.electrosome.com/ Ligo George

    Sorry, it was a problem with proteus design…..

    I edited and upload the new file….

    Please redownload the file……..

  • imah

    good afternoon sir..
    can i know what version of proteus that you use?
    because the proteus file cannot be open at my laptop..
    tq sir.

  • http://www.electrosome.com/ Ligo George

    I created the file using proteus 7.6… but before 1 week I edited the file with proteus 7.9 ……….

  • imah

    ok..tq sir..

  • Mrunal

    Sir how to change time using Set,+,- switches? when I try to change date changes!

  • http://www.electrosome.com/ Ligo George

    Time settings are like a digital watch….
    1 press at set switch then you can change hour..
    then 1 more press you can change minute..
    and so on

  • daryl

    Sir..i have tried your program in my project.,but i am using a pic18f4550 so i modified a little with the ports in the code..i was able to get the time running in my lcd but the problem is it seems that the rtc was synchronized with the clock in my pc.,i use my pickit2 in supplying voltage,but when i use a separate power supply, time in rtc does not display on lcd..what seems to be the problem?

  • http://www.electrosome.com/ Ligo George

    Proteus simulation will show the time in your PC.
    When comes to actual hardware, when you switch on the power supply it will show time 00-00-80… then you need to set the time..

  • daryl

    Ah i see..thank you very much..i’ll try it out..i’ll update you if it is ok now..thanks a lot!

  • Mrunal Ahirrao

    Sir

    I have tried to make alarm in this project by modifying your code. Like I have used a int variable and converted your hour variable data to Decimal by using BCDTODEC16 in Mikroc as shown below:

    void Check_time(){

    hour=read_ds1307(2);

    Dhour=Bcd2Dec16(hour);

    }

    The Dhour is variable and I have used it in Main :

    Check_time();

    if(Dhour==12) // checking if its 12 o clock

    {

    Light=1; then switch on light

    }

    But practically its not working! will you please help me sir?

  • daryl

    Sir..I have tried it with my actual hardware..it doesn’t show anything on the LCD only a series of small rectangles (like this ==============) on top of the LCD..i am very sure i followed everything in your program and schematic..help me pls.thanks!

  • http://www.electrosome.com/ Ligo George

    Check your LCD data pins connections…… interchanging data pins results in unexpected characters…
    Pull UP resistors of SCL and SDA should be close to 2K…..

  • daryl

    Yes, i used 1.8K resistor as pull ups for my SCL and SDA..im curious because when i use PICkit2 as a supply voltage,it displays time but not stable (the time and date changes almost every second) and as i said earlier when i supply it with an external power supply for my actual hardware,the time doesn’t appear anymore..thanks by the way for the help..i’ll try checking again my pin connections. thank you.

  • http://www.electrosome.com/ Ligo George

    You may use a good power supply,,,,,,,, or Try after adding a 100uF capacitor near to pic microcontroller to filter ac voltages…

    Keep the length of I2C (SCL & SDA) between pic and RTC as minimum as possible…

  • http://www.electrosome.com/ Ligo George

    It is difficult to find out the problem of your code by evaluating this segment only…

    Where did you turn off the light..??

  • daryl

    Ok sir..i’ll try it then..ill update again if i encounter problems..thank you very much,.i had a wire connection for SCL &SDA almost 12inches..is it ok?

  • http://www.electrosome.com/ Ligo George

    12 inch is very large……. Make it short as possible..

  • daryl

    Ok sir..thank you very much..by the way what is the value of the 3 pull ups on the switches and the Var Resistor? i followed the value on your proteus design, thanks again for the help.

  • Mrunal Ahirrao

    I didn’t turn off the light first I am trying to make just a routine which will check time and if it matches the routine time then it should make the “light” to logic one. but its always on!

  • http://www.electrosome.com/ Ligo George

    You can use 10K.. as in proteus design.

  • http://www.electrosome.com/ Ligo George

    Try this: first turn off the light……….. then turn on it when needed…

  • Mrunal Ahirrao

    sir I have set light to 0 or off and then burned the program but its again on always but not at that time!

  • http://www.electrosome.com/ Ligo George

    I can’t find the real problem without the circuit and entire program….

    Try this : turn light off when needed and on otherwise..

  • daryl

    sir..i wasn’t able to come up with the output you were saying earlier (00-00-80) the time and date displays on the lcd as soon as i plug it in with a supply..but the time and date was not stable,.keep on changing almost every second. plss help..thanks..

  • http://www.electrosome.com/ Ligo George

    It may be due to noise, these devices are very sensitive to noise. First make your power supply free of noise by adding filtering capacitors………. Shorten the distance of I2C lines and place it close to pic microcontroller..

  • daryl

    Ok sir..thanks,i’ll try fixing it again,hopefully it would work now..thank u very much for the help..i already made the I2C lines shorter,,about 1.5 inch now..by the way with your code in converting BCD, do i still have to change anything there coz im using a pic18f4550(4mhz crystal)..and in your proj you used a pic16f..thanks,

  • sanjeev

    is it possible to display month and day with its name ?

  • Mrunal Ahirrao

    sir here is the modified code: sir I have tried your code with PIC16f628A using soft I2C and was succesful. But not working what I am trying.

    // Software I2C connections

    sbit Soft_I2C_Scl at RB4_bit;

    sbit Soft_I2C_Sda at RB1_bit;

    sbit Soft_I2C_Scl_Direction at TRISB4_bit;

    sbit Soft_I2C_Sda_Direction at TRISB1_bit;

    // End Software I2C connections

    // LCD module connections

    sbit LCD_RS at RB0_bit;

    sbit LCD_EN at RA1_bit;

    sbit LCD_D4 at RB2_bit;

    sbit LCD_D5 at RB3_bit;

    sbit LCD_D6 at RB5_bit;

    sbit LCD_D7 at RB6_bit;

    sbit LCD_RS_Direction at TRISB0_bit;

    sbit LCD_EN_Direction at TRISA1_bit;

    sbit LCD_D4_Direction at TRISB2_bit;

    sbit LCD_D5_Direction at TRISB3_bit;

    sbit LCD_D6_Direction at TRISB5_bit;

    sbit LCD_D7_Direction at TRISB6_bit;

    // End LCD module connections

    sbit Sett at RA2_bit;

    sbit Inc at RA3_bit;

    sbit Dec at RA4_bit;

    unsigned short read_ds1307(unsigned short address)

    {

    unsigned short r_data;

    INTCON.GIE=0;

    Soft_I2C_Start();

    Soft_I2C_Write(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 –> 0xD0

    Soft_I2C_Write(address);

    Soft_I2C_Start();

    Soft_I2C_Write(0xD1); //0x68 followed by 1 –> 0xD1

    r_data=Soft_I2C_Read(0);

    Soft_I2C_Stop();

    return(r_data);

    INTCON.GIE=1;

    }

    void write_ds1307(unsigned short address,unsigned short w_data)

    {

    INTCON.GIE=0;

    Soft_I2C_Start(); // issue I2C start signal //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 –> 0xD0

    Soft_I2C_Write(0xD0); // send byte via I2C (device address + W)

    Soft_I2C_Write(address); // send byte (address of DS1307 location)

    Soft_I2C_Write(w_data); // send data (data to be written)

    Soft_I2C_Stop(); // issue I2C stop signal

    INTCON.GIE=1;

    }

    unsigned char BCD2UpperCh(unsigned char bcd)

    {

    return ((bcd >> 4) + ‘0’);

    }

    unsigned char BCD2LowerCh(unsigned char bcd)

    {

    return ((bcd & 0x0F) + ‘0’);

    }

    /** Bcd2Dec**/

    unsigned short Bcd2Dec(unsigned short bcdnum) {

    unsigned short tmp = 0;

    tmp = (bcdnum >> 4) * 10;

    tmp += (bcdnum & 0x0F);

    return tmp;

    }//

    int Binary2BCD(int a)

    {

    int t1, t2;

    t1 = a%10;

    t1 = t1 & 0x0F;

    a = a/10;

    t2 = a%10;

    t2 = 0x0F & t2;

    t2 = t2 <> 4;

    t = 0x0F & t;

    r = t*10 + r;

    return r;

    }

    int second;

    int minute;

    int hour;

    int hr;

    int day;

    int dday;

    int month;

    int year;

    int ap;

    unsigned short set_count = 0;

    unsigned short set;

    unsigned short i;

    unsigned short Dhour;

    unsigned short Hcnt;

    char time[] = “00:00:00 PM”;

    char date[] = “00-00-00″;

    void Check_time() //here I am converting bcdtodec

    {

    Hcnt=read_ds1307(2);

    Dhour=Bcd2Dec(Hcnt); //saving the value in variable

    }

    unsigned short counter;

    void interrupt(){

    if (INTCON.T0IF) {

    if (counter >= 20) {

    Soft_I2C_Break();

    counter = 0; // reset counter

    }

    else

    {

    counter++; // increment counter

    INTCON.T0IF = 0; // Clear Timer0 overflow interrupt flag

    }

    }

    }

    void main()

    {

    CMCON = 0x07; // To turn off comparators

    // ADCON1= 0x06; // To turn off analog to digital converters

    TRISA = 0b00011100;

    TRISB = 0x00;

    //ANSEL=0;

    OPTION_REG=0x07;

    INTCON=0b10100000;

    PORTB.RB7=0;

    Lcd_Init(); // Initialize LCD

    Lcd_Cmd(_LCD_CLEAR); // Clear display

    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

    Lcd_out(1,1,”Time:”);

    Lcd_out(2,1,”Date:”);

    Soft_I2C_Init(); // Initialize Soft I2C communication

    do{

    set = 0;

    if(Sett == 0)

    {

    Delay_ms(100);

    if(Sett == 0)

    {

    set_count++;

    if(set_count >= 7)

    {

    set_count = 0;

    }

    }

    }

    if(set_count)

    {

    if(Inc == 0)

    {

    Delay_ms(100);

    if(Inc == 0)

    set = 1;

    }

    if(Dec == 0)

    {

    Delay_ms(100);

    if(Dec == 0)

    set = -1;

    }

    if(set_count && set)

    {

    switch(set_count)

    {

    case 1:

    hour = BCD2Binary(hour);

    hour = hour + set;

    hour = Binary2BCD(hour);

    if((hour & 0x1F) >= 0x13)

    {

    hour = hour & 0b11100001;

    hour = hour ^ 0x20;

    }

    else if((hour & 0x1F) = 60)

    minute = 0;

    if(minute = 0x32)

    day = 1;

    if(day 0x12)

    month = 1;

    if(month <= 0)

    month = 0x12;

    write_ds1307(5,month); // write month 6 June

    break;

    case 6:

    year = BCD2Binary(year);

    year = year + set;

    year = Binary2BCD(year);

    if(year = 0x50)

    year = 0;

    write_ds1307(6, year); // write year

    break;

    }

    }

    }

    second = read_ds1307(0);

    minute = read_ds1307(1);

    hour = read_ds1307(2);

    hr = hour & 0b00011111;

    ap = hour & 0b00100000;

    dday = read_ds1307(3);

    day = read_ds1307(4);

    month = read_ds1307(5);

    year = read_ds1307(6);

    time[0] = BCD2UpperCh(hr);

    time[1] = BCD2LowerCh(hr);

    time[3] = BCD2UpperCh(minute);

    time[4] = BCD2LowerCh(minute);

    time[6] = BCD2UpperCh(second);

    time[7] = BCD2LowerCh(second);

    date[0] = BCD2UpperCh(day);

    date[1] = BCD2LowerCh(day);

    date[3] = BCD2UpperCh(month);

    date[4] = BCD2LowerCh(month);

    date[6] = BCD2UpperCh(year);

    date[7] = BCD2LowerCh(year);

    if(ap)

    {

    time[9] = ‘P';

    time[10] = ‘M';

    }

    else

    {

    time[9] = ‘A';

    time[10] = ‘M';

    }

    Lcd_out(1, 6, time);

    Lcd_out(2, 6, date);

    Delay_ms(50);

    Check_time(); //here i am checking time

    if(Dhour==11) //if its 11 then make RB7=1

    {

    PORTB.RB7=1;

    }

    }while(1);

    }

    but its not working

  • MVA

    means only month and day or with time and date? if yes then its possible…

  • http://www.electrosome.com/ Ligo George

    for conversion you can also use the built in library routines of mikroc pro

  • http://www.electrosome.com/ Ligo George

    Yes… just program it… if(month == 1) print January etc…

  • daryl

    Ok sir..thank you very much..

  • http://www.electrosome.com/ Ligo George

    PORTB.RB7 ?? use PORTB.F7 = 1;
    or RB7_bit = 1;

    use like this
    if(Dhour == 11)
    {
    PORTB.F7 = 1;
    Delay_ms(3000);
    PORTB.F7 = 0;
    }

  • Mrunal Ahirrao

    Thank you sir! but I am using PIC16F628A so its PORTB.RB7. I will try with it.

  • http://www.electrosome.com/ Ligo George

    No, PORTB.RB7 is not valid, for every pic microcontroller PORTB.F7 is valid… in mikroc.. pro.

  • Mrunal Ahirra

    Sir its not working.its just showing logic 0.

  • daryl

    Sir..it worked!!thank you very much..just want to ask,how do you configure the clock to be 12-hour clock not 24 hour clock? thank you..

  • Mrunal Ahirrao

    Sir, Now its working. but not the correct way. I have tried to put the”hr” variable in Bcd2dec(hr) instead of direct reading from RTC and then made portB.F7=1; and its now working. But then is the data from RTC is not BCD? or “hr” variable is BCD? I did’nt understood! I guess in hr = hour & 0b00011111; you are masking some bits of BCD…

  • mrunal ahirrao

    this is the comment that I gave before but now its working.

  • http://www.electrosome.com/ Ligo George

    Yes you are right, we need only the last 5 bits of hour…

  • http://www.electrosome.com/ Ligo George

    Press the set button 1st, then you can change hour using + & – buttons, then press set button, then you can change minute & so on,

  • http://www.electrosome.com/ Ligo George

    DS1307 can work in 12 / 24 hour mode by using the bit 6 of hour register..

  • daryl

    Hey mr. George,,thanks again with the help..will you please help me again,i would like to count every change of minute on the time..but i’m confused how to make a somewhat like counter variable which will hold/count the value of the change in minute..ex,time is 9:00:00..if min became 01..the counter should count 1..and so on..please help me..thanks…

  • http://www.electrosome.com/ Ligo George

    Actually I don’t get your question,…… I think you may store minute in a separate variable….

  • daryl

    i already did..i tried storing minute in a separate variable..(like j = minute..) but im confused how to count the next change of minute.,sorry if you dont understand my question..i want to make a counter variable which will increment everytime the value of minute changes..i’m really confused..please help me..thanks..

  • http://www.electrosome.com/ Ligo George

    Store current and previous minutes in two variables….
    if(current != previous)
    count++;

  • daryl

    Ok then..i’ll try it…thank you very much sir!

  • daryl

    it doesn’t seem to work..here is what im trying to do..example: if a port is high at 9:00:00, i want to store the 00min in a variable and when it changes to 01 and the port is still in high state, a counter variable will increment(count++)..my problem is how will i know if the minute has already changed from 00 to 01?i tried storing minutes in separate variables but it seems like it does not recognize the minute data i stored..please help me…thanks.

  • daryl

    it doesn’t seem to work..i tried storing minute in 2 separate variables like what you said..here’s what im trying to do..example..if a pin goes high at 9:00:00, i will store 00min in a variable and if it changes to 01,and the pin is still in high state, a counter variable will increment(count++),..my problem is how will i know if the minute has already changed from 00 to 01?,,im really confused..i tried what u said, but not working..(maybe i did not do it correctly)..please help me..thank you.

  • http://www.electrosome.com/ Ligo George

    if(previous != current && port == 1)
    count ++;

  • daryl

    thank you sir..i’ll do it again..hope this time it will work..ill update again with the progress..thank you very much!

  • daryl

    Sir..i just tried a piece of code,but it does not work..pls inspect:

    ////////code//////////////////////////

    if(PORTB.F4 == 1 && PORTB.F5 == 1)
    {
    minute = read_ds1307(1); //i tried to read the time where the 2 ports where high
    previousMin = minute; ///stored min to previous
    Lcd_cmd(_LCD_CLEAR);
    Lcd_out(1, 1, “detected”);
    delay_ms(1000);
    if(PORTB.F4 == 1 && PORTB.F5 == 1)///check again if the ports are high
    {
    minute = read_ds1307(1); //read again the minute
    currentMin = minute;//stored the minute to current
    if(previousMin != currentMin && (PORTB.F4 == 1 && PORTB.F5 == 1)) //compare the 2 variables and check 2 ports state
    {
    counterM++; //increment the counter
    if(PORTB.F4 == 1 && PORTB.F5 == 1) //check again the ports
    {
    if(counterM == 2) ///check the value of counter
    {
    Lcd_cmd(_LCD_CLEAR);
    Lcd_out(1, 1, “overloaded”);
    delay_ms(2000);
    //break;
    }
    }
    }
    }
    }
    //////////end of code/////////////////

    pls help me..i really wasn’t sure with the part (minute = read_ds1307(1))..pls help me,.thank you..

  • http://www.electrosome.com/ Ligo George

    previousMin = minute; this statement wil execute more than one time when f4 & f5 of portb are high….. It should be executed once to work this program correctly…. ..

  • Whindkhan

    Instead of using of 2 buttons for increment and decrement can I use 4×3 keypad in your code? BTW thank you to your site I learned alot. :D

  • http://www.electrosome.com/ Ligo George

    Yes, of-course you can use,,,.. but a keypad just for setting a time makes an unwanted expense …

  • Whindkhan

    ok thank you, question sir. how can I see the cursor while doing the setting mode in LCD and how can I set an alarm in ur prog.

  • http://www.electrosome.com/ Ligo George

    Try after removing Lcd_Cmd(_LCD_CURSOR_OFF);
    from the code…..

    For setting alarm you may store the time in PICs EEPROM, and compare with time reading from RTC..

  • Whindkhan

    I tried that before my cursor blink at end of the date, basically it appear in 2nd row, 9th col. do you have a simple example of EEPROM. if I’m mistaken it is like NVRAM store data when the power is lost. BTW thank you! :D

  • http://www.electrosome.com/ Ligo George

    Lcd_Cmd(_LCD_CURSOR_OFF); ,,
    Use other cursor control commands…
    _LCD_FIRST_ROW –
    Move cursor to the 1st row_LCD_SECOND_ROW
    – Move cursor to the 2nd row

    _LCD_BLINK_CURSOR_ON –
    Blink cursor on

    _LCD_MOVE_CURSOR_LEFT
    – Move cursor left without changing display data RAM

    _LCD_MOVE_CURSOR_RIGHT
    – Move cursor right without changing display data RAM

    For using EEPROM..you can use mikroc functions.. Eeprom_read() and Eeprom_write() …
    http://www.electrosome.com/internal-eeprom-pic-microcontroller/

  • Whindkhan

    Still not blinking. First I Use the command _LCD_BLINK_CURSOR_ON and put the _LCD_MOVE_RIGHT.

  • http://www.electrosome.com/ Ligo George

    The cursor will be shown on the LCD if you don’t turn off it..

  • Whindkhan

    ………………………………

    void main()
    {
    IC1_Init(100000); //DS1307 I2C is running at 100KHz
    CMCON = 0x07;
    TRISD = 0x0F;
    PORTD = 0x00;
    TRISB.F0 = 0;
    Lcd_Init(); // Initialize LCD
    Lcd_Cmd(_LCD_CLEAR); // Clear display
    Lcd_Cmd(_LCD_BLINK_CURSOR_ON); // Blink Cursor
    Lcd_out(1,1,”TIME:”);
    Lcd_out(2,1,”DATE:”);

    ………………………………
    if(set_count && set)
    {
    switch(set_count)
    {
    case 1:
    hour = BCD2Binary(hour);
    hour = hour + set;
    hour = Binary2BCD(hour);
    Lcd_Cmd(_LCD_FIRST_ROW);
    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
    if((hour & 0x1F) >= 0x13)
    {
    hour = hour & 0b11100001;
    hour = hour ^ 0x20;
    }
    else if((hour & 0x1F) <= 0x00)
    {
    hour = hour | 0b00010010;
    hour = hour ^ 0x20;
    }

    write_ds1307(2, hour); //write hour
    break;

    …………..

    that's my code and still not working…

  • http://www.electrosome.com/ Ligo George

    Isn’t you tried in Proteus?…

    I think it will work in real hardware…. try it…….

  • Whindkhan

    Yes, I’ll work on real hardware but the output is the same, not blinking when setting the HH:MM:SS, MO/DD/YR.

  • daryl

    Sir will i be able to use the CCP module of pic16f877A with the ds1307 as the timer?

  • lila

    hi sir. is it possible to replace ds1307 with vs1307. i looked up the pin config of the two and it’s just sam bu when i tried to use it the lcd, it only display (Time:10:=0:=0 AM) (Date:=0-=0-=0)

  • http://www.electrosome.com/ Ligo George

    I think you can use vs1307. Error may be due to noise…. Try shortening the distance from PIC to RTC.
    Which are the pull up resistors that you used?

    You should use 2.2K or 1.8K…

  • http://www.electrosome.com/ Ligo George

    My proteus shows blinking cursor when I remove Lcd_Cmd(_LCD_CURSOR_OFF);

  • http://www.electrosome.com/ Ligo George

    Yes.. of-course..

  • Whindkhan

    Sir. how can I message you privately so that you can see my code It’s kind a long when I post it here…?

  • lila

    sir i still got the same output..what should i do

  • lila

    Sir I got the output but it is not stable and continuously changing.

  • lila

    time 00-00-80 i got this output but i cant edit the time using button

  • daryl

    ok i’ll try..i didn’t succeed with my other experiment with storing the minute, and i realized maybe i could use this CCP module..but im a little bit confused because it says there that it uses the internal timer of pic..now ill try expermenting it with the ds1307..if you have any sample programs for these hope you can help me..thanks.

  • http://www.electrosome.com/ Ligo George

    DS1307 RTC doesn’t use internal timer of PIC, so you can use the internal timer for other purposes.

  • http://www.electrosome.com/ Ligo George

    This is the first time output when you power the clock….. Check connections of switches… When you reset the second to 00 the clock will start running..

  • http://www.electrosome.com/ Ligo George

    You may have some noise problems……….

    Shorten the distance between pic and rtc and use recommended pull up resistors..

  • daryl

    Ah, i see..but can i store (example) a minute data in EEPROM?and later i will retrieve the minute data and compare it with the another one? is EEPROM capable of storing the time data of the ds1307?..thank you very much again mr. george!

  • http://www.electrosome.com/ Ligo George

    yes you can store time date etc in pic’s eeprom….

  • daryl

    Ok sir..as in store the time date data of ds1307?im still newbie with this EEPROM,ill try it out,hope it will work.thank you very much.ill post an update later..

  • daryl

    Sir, is it possible to take control of a specific interval of time in ds1307?(example) i want my device to be checking a sensor from 9:00:00am until 1:00:00pm..i want that my sensor should recognize the 4hr- time interval on which it would be checked over and over by the system..plss help me,thank you very much.

  • http://www.electrosome.com/ Ligo George

    You can compare the time from RTC with a predetermined time for that..

  • daryl

    done sir..thank you..by the way how do you set the rtc into 12hr mode??im having a hard time understanding the 6bit and 5bit of the registers..

  • http://www.electrosome.com/ Ligo George

    There is a bit in the hour register to set 12/24 hour mode.

  • GAZ

    good morning sir
    please can you help me to realize it with 4 sevent segment??

  • http://www.electrosome.com/ Ligo George
  • GAZ

    thank you sir its ikay now
    but when i copare seconds of this project and seconds on my laptop i see that theres a differnce and thats can effect the shown time can you correct it please to make no difference between this RTC and all otherRTC??

  • http://www.electrosome.com/ Ligo George

    I don’t get your question..

  • GAZ

    the time on this project it not running same like real time
    seconds on this projects is running very slowly when we copare it with the clock of my house or my laptop and later it will effect the minutes my question is can you try to make seconds running like real time
    and thank you again because you are the only one who answer my stupid questions

  • http://www.electrosome.com/ Ligo George

    You are welcome..
    It is running real time in my laptop… When did you download the zip file?
    Before a month there is a problem with the proteus design.. and I corrected it…
    Check the proteus: Which are the Pull Up resistors connected to SCL and SDA ?…. use 2.2K or 1.8k…. in proteus better to use the PULL UP resistor from their library…

  • GAZ

    ok sir
    thank you

  • pra

    sir, i download your new zip file and it running slowly than normal time.please kindly help me and re-post the proteus design.

  • http://www.electrosome.com/ Ligo George

    It is working fine on my proteus.. It may the problem with proteus version.. I am using 7.10 ….. Just try after changing the value of I2C pull up resistors to 1.8K or 2.2K..

  • pra

    yes sir,it is proteus problem.proteus animating time depend on animating options frames per second value.sorry for my mistake.your project is very useful.

    To GAZ; change the frame value and run the project :)

  • http://www.electrosome.com/ Ligo George

    Thanks for your feedback..

  • imah

    hello sir..

    i have construct the circuit in stripboard. When I put the IC and test the circuit, I found that the circuit didn’t work. When I press the push button there is no output in the lcd. I have troubleshoot the circuit and I found that there is connection at DS1307, between pin 5 and pin 6. I have reconstruct the circuit and the problem is still same. Can u give me the opinion how to solve the problem? Hope u can help me.

  • http://www.electrosome.com/ Ligo George

    There is no connection between 5 and 6… check the pull up resistors that you have used for SCL (6) and SDA (5). Use 2.2K or 1.8K…

  • imah

    what about the resistor at DS1307. Which resistor that i should use?Is it okay if I use 20 KH for crystal value.

  • http://www.electrosome.com/ Ligo George

    I don’t get your questions.. Which resistor??

  • imah

    sorry sir. resistor that connected with pin 5 and pin 6 at DS1307. For crystal value is it okay If I used 20 000 H for both IC?

  • http://www.electrosome.com/ Ligo George

    Use Standard 32.768kHz Quartz Crystal for DS1307 and 8MHz crystal for PIC…

    Use 2.2K resistors…

  • imah

    Thank u so much sir for ur information..i really appreciate it..

  • Imah

    hye sir..sorry for interrupting..
    Sir can u send me the hex file?It is because the existing hex file not working in my proteus.

  • http://www.electrosome.com/ Ligo George

    It is working fine…. What is the problem in your proteus?

  • Imah

    when i click the push button there is no changes at the LCD.

  • http://www.electrosome.com/ Ligo George

    1st click on the set button.. then use other buttons to adjust hour… then click on the set button again.. then use other buttons to adjust minute… and so on..

  • pra

    Sir ,make your clock and pic 16f877a does not out put in hardware.why? sir can read ds1307 without write time ,date, in first time.

  • http://www.electrosome.com/ Ligo George

    It is 100% working.. we have tested it… In the first time.. the default time will be 00-00-80. .. and the clock will not be running..
    We want to reset the second, to make the clock running..
    What is the output of your hardware..??

  • pra

    seven segment sir.

  • http://www.electrosome.com/ Ligo George

    What is the error in output?

  • pra

    no pic out put signal.i write PORTD.F0=1; like code with clock code after power supply i check the F0 pin it is no out put signal.kindely help me.

  • http://www.electrosome.com/ Ligo George

    Did you connected Vdd and Vss of pic microcontroller which are not shown in the above circuit diagram?
    First check your pic by doing LED blinking..

  • pra

    thank you sir for your reply, i write your clock code under the while loop.i dont use do-while loop.that is affect my programme?what is the different of do-while and while?

    but my code work in proteus.

  • http://www.electrosome.com/ Ligo George

    You can use while or.. do while.. for making infinite loop.. It will not affect the program..
    The only difference between them is.. condition is checked before entering the loop in while() .. but in.. do..while() .. it is checked after the execution of the segment…. The program segment will execute once in do..while() even if the condition is false..

  • pra

    sir clock work perfectly.thanks for your help.sir i try to make 60 led (circle led indicate second) clock and led drive with 74hc595 but 60 led not indicate anything. how to write in mikro c code for this(led on one by one according to second). hope your help.

  • Sunil Gahlot

    i need this project .but i need a lil modification to it ,,, if u can help me with this i will be thank full to u …
    i need an additional output port which will be triggered from 8.00 am to 8.00 pm or 8.00-20.00hrs

    i need to trigger a 5v relay with the time ..
    if any1 can help please i m not into programming but if i get the code and hex with crt diagram i will be thankfull

  • Sunil Gahlot

    i need this project .but i need a lil modification to it ,,, if u can help me with this i will be thank full to u …
    i need an additional output port which will be triggered from 8.00 am to 8.00 pm or 8.00-20.00hrs

    i need to trigger a 5v relay with the time ..
    if any1 can help please i m not into programming but if i get the code and hex with crt diagram i will be thankfull

  • sunil

    Ligo, absolutely amazing circuits with great explanation. This is one of the best site in digital electronics world. It’s a great service to the community. Well Done!!!

  • http://www.electrosome.com/ Ligo George

    Thank you very much ..

  • Ruben Alcazar

    Hi thank you very much, your website is awesome, i would like to feed this circuit with a 3.7v battery using a 3v LCD, but I’ve search a lot on internet regarding PIC’s on batteries but there’s little information about that, every one wants to use voltage regulators but that’s very inefficient, i think using a 3.7 v battery would be the best approach but there’s almost no information about the considerations of doing that, what do you think?

  • http://www.electrosome.com/ Ligo George

    Use PIC 16LF877A, a lower voltage version of pic 16f877a…. It can work at 3.7V…. For operating LCD, you can drop about 0.7V by connecting a 1N4007 diode in series..

  • Ruben Alcazar

    Hi, thanks. I read that Li ion batteries doesn’t have to go below certain voltage 3.2v aprox to prevent over discharge, so i think i need some under voltage lockout circuit to prevent that.. or not?

  • http://www.electrosome.com/ Ligo George

    It is better to add over discharge protection…. You may use a voltage comparator for that…

  • ramlz

    how much the value of the crystal for the microcontroller and the DS1307?

  • http://www.electrosome.com/ Ligo George

    Use 8MHz crystal for PIC and use Standard 32.768kHz Quartz Crystal for DS1307…

  • Andrey Blanter

    Dear Sir,

    First, thanks for your explanations.

    I tried to use your code on pic16F887 (instead of 877) it gives me compilation error on include library – include p16f887.inc (line 4)

    I tried to eraze this line,finaly looking on my LCD I don’t see anything (resistors forSDA and SCL attached).

    I use as power an old Nokia charger (5v output)

    Can you , please, help me

  • http://www.electrosome.com/ Ligo George

    This code is meant for PIC 16F877A not for PIC 16F877 and the compiler used here is MikroC. If you want to use 16F877, I hope that no code modifications are required but you should compile it after changing the microcontroller in project settings…

  • Jayanth D

    What are the values that has to be written to hours register if 12 hour format is selected. Mention all the values for 12 AM to 11 AM and 12 PM to 11 PM

  • http://www.electrosome.com/ Ligo George

    DS1307 is a BCD (Binary Coded Decimal) Clock.. so you should write hours as BCD..6th bit of the hour register determines whether the clock is 12/24 hour….. and 5th bit determines whether it is AM or PM when it is running in 12 hour mode…
    The program given along this article is 100% working…

  • Wannawut Pinit

    thx for you

  • Emanuel

    I have this error when compile c: “Error 128 dc.c, Line 2(1,3): A #DEVICE is required before this line”

  • http://www.electrosome.com/ Ligo George

    The code is written for MikroC Pro for PIC Microcontrollers…
    I think you are using some other compiler..

  • Emanuel

    Ups, I´m programming with PICC CCS :(

  • wan cine

    i am using your micro c code,but lcd only display character “Time:” and “Date:” only…can you help me to correct the code and solve my problem to make this simulation successful? i really need your help…i am using pic16f877 and crystal 11.0592Mhz…

  • http://www.electrosome.com/ Ligo George

    Did you tried my project files..?? It is 100% working in proteus and hardware….. You needn’t connect I2C debugger if you are using correct pull up resistors… Use resistors that labeled PULL UP in proteus libraries.. check… Check stage by stage instead of testing it after wiring the whole circuit… fist time… then add motor.. etc..

  • Belal

    Can you help ds1307 with 7 segment 6 digit

  • Cosmin Pavel

    sir, why button doesnt work? when i push each button nothig happen. but clock is running, in proteus i mean.

  • Cosmin Pavel

    im using your code and schematic

  • Cosmin Pavel

    i found it, button must be pressed much longer than usual because of “delay”, in proteus, i think…… :)

  • http://www.electrosome.com/ Ligo George

    I will help you, but please don’t ask me to design it completely.. I will help you technically…….. Use 4511 7 segment decoder..

  • http://www.electrosome.com/ Ligo George

    Yes.. you are right.. .reduce the delay in the program as per your requirement..

  • Cosmin Pavel

    sir, i want to combine two shematics: digital clock and digital thermometer. i adjusted the shematic(in proteus) fot adding lm35, the buttons are now in portd(porta.ra0 for lm35) and i maked the right setting for activating analog imput, But i dont know where in program of clock to add the thermometer sequence. i’ve tried tu use external interupt orTMR0 fot interupt routine but didnt work. were in program can i put the thermometer sequence? is something else to add?

  • wan cine

    yes..i have tried and it is working..thank you…but,how i want to combine my motor and led program with this clock? for example, i want the led on for 5 seconds and then the motor is moving for 10 seconds when the clock is at 8:00 a.m, 1:00 p.m and 9:00 p.m ??

  • http://www.electrosome.com/ Ligo George

    Do like this…void interrupt()
    {
    clear display;
    read adc vlaue;
    print the value on display;
    }

    you can also do it without using interrupt..

  • http://www.electrosome.com/ Ligo George

    just use if conditions …and splitting the delay…(to avoid interruption in the lcd display)
    if(requied time)
    {
    flag = 50;
    }
    if(flag>0)
    {
    LED ON;
    SMall delay, 100ms
    }
    else
    LED OFF;
    flag = flag – 1;

    thus when the time is reached.. led will be ON for about 5 seconds (50x100ms)

  • wan cine

    but i want to use this program..how to combine it as i said before?? and the button to set,up and down still doesn’t working after i change the delay of button..
    void main()
    {
    TRISD = 0; // PORT B as output port
    PORTD = 0; // Set RB0 to high
    do
    {
    // To turn led
    PORTD.F7 = 1;
    Delay_ms (10000); //10 seconds delay
    // To off led
    PORTD.F7 = 0;
    // To turn motor clockwise
    PORTD.F0 = 1;
    PORTD.F1 = 0;
    Delay_ms (1000); //1 seconds delay
    // To stop motor
    PORTD = 0; // PORTB = 3
    Delay_ms (1000);
    // To turn motor anticlockwise direction
    PORTD.F0 = 0;
    PORTD.F1 = 1;
    Delay_ms (1000);
    // To Stop motor
    PORTD = 0; // (3=0b00000011)
    Delay_ms (1000);
    // To turn motor clockwise
    PORTD.F2 = 1;
    PORTD.F3 = 0;
    Delay_ms (1000); //1 seconds delay
    // To stop motor
    PORTD = 0; // PORTB = 3
    Delay_ms (1000);
    // To turn motor anticlockwise direction
    PORTD.F2 = 0;
    PORTD.F3 = 1;
    Delay_ms (1000);
    // To Stop motor
    PORTD = 0; // (3=0b00000011)
    Delay_ms (1000);
    }while(0);
    }

  • http://www.electrosome.com/ Ligo George

    I already said that .. you should split the delay of motor rotation as I indicated in the previous algorithm.. otherwise the time updation process will be interrupted..

  • Uthman Oluwatosin Kingofhustle

    pls can i have the hexfile? “[email protected]

  • http://www.electrosome.com/ Ligo George

    You can download the program and proteus files through the above download link..

  • Uthman Oluwatosin Kingofhustle

    i download it but i need hexfile pls send the hexfile only to my mail please.

  • Tracey Cruzat Punzalan

    hi sir! thats a nice project, :) sir how can i identify if my rtc is really working or damaged?.. i did ur project and it really works, but on the second time around, i cannot set the corresponding time, the switches are not working and my rtc became very hot.. what should i do?..

  • http://www.electrosome.com/ Ligo George

    Hello, all the program files including the hex file is present in the downloaded zif file..

  • http://www.electrosome.com/ Ligo George

    I thing your circuit might have a short circuit… Please recheck the whole wiring………
    Note that the you should press and hold the switch for some time (less than 500ms) to work…

  • vijay

    hi sir! for 5v supply can i use (4) 1.5v battery.

  • http://www.electrosome.com/ Ligo George

    4×1.5 = 6V……
    use a 5.1V zener diode OR. put two 1n4007 diode in series to drop excess voltage..

  • Krishnappa Gururaj

    Dear sir,
    I am planning to buy pic compiler which compiler you suggest mikro c or Hi tech c.
    Thanking you
    with regards
    Gururaj

  • Krishnappa Gururaj

    sir,
    my first project using your code is working well, thanks a lot.
    Gururaj

  • Uthman Oluwatosin Kingofhustle

    sorry can the hex file work for microcontroller 16f877 because i cant get microcontroller 16f877A here. thanks

  • http://www.electrosome.com/ Ligo George

    Try after rebuilding the code by changing the microcontroller to pic 16f877 in the mikroc project settings..

  • http://www.electrosome.com/ Ligo George

    Thanks for your feedback..

  • http://www.electrosome.com/ Ligo George

    I think your display is damaged.. try replacing with another..

  • Visal Chey

    Give me code in mikroBasic please,….f

  • http://www.electrosome.com/ Ligo George

    Sorry, I haven’t yet used mikroBasic..

  • shankar

    hello sir,
    i want to display the numbers from 1 t0 1000 in pic16f690 in LCD but it is not displaying.can you give some ideas to work with ascii characters

  • wan cine

    sir,, i wrote the program you give to me…is that need to wrote if(8:00:00) after clock program or any specific column? for example

    }while(1);

    if(8:00:00)

    when i run, the error said 260 406 ‘)’ expected, but ‘:’ found ..

    can you explain more details to me..

  • http://www.electrosome.com/ Ligo George

    That errors are simple syntax errors….. check syntax errors in your program……

    and all the code should be written in that while loop… not outside it… even though it is not a syntax error…

  • Puneeth

    Hi Ligo,

    I need your suggestion to make my project success,

    In my project i am using PIC16F72, monitoring(RB0) a signal from RF transmitter and its comes once in 10Sec of 1 Sec width. I need to run my code until RF signal present. If RF signal not received with in 15 sec code should not run further. How can i use the interrupts in this case

    Please help me

  • joeykhoo

    how can i get the code???

  • Ahmad Abed

    Good Job, I hope for you the best ^_^.

  • Fethi Farhat

    i can not get the code.

  • Uthman Oluwatosin Kingofhustle
  • Uthman Oluwatosin Kingofhustle

    thanks..

  • http://www.electrosome.com/ Ligo George

    Sorry for the delay in replay.. I was busy ..

    Try this link :http://www.electrosome.com/interrupt-pic-microcontroller/

    I hope that mikroBasic should also have built in function for interrupt..

  • http://www.electrosome.com/ Ligo George

    You can download it at above download link….
    It was not working for some days.. Now the problem is solved..
    Sorry for the inconvenience caused..

  • http://www.electrosome.com/ Ligo George

    Thanks for the feedback.. :)

  • http://www.electrosome.com/ Ligo George

    You can download it at above download link….

    It was not working for some days.. Now the problem is solved..

    Sorry for the inconvenience caused..

  • http://www.electrosome.com/ Ligo George

    Please try it yourself….
    1. Open the MikroC Project
    2. Go to Project >> Edit Project
    3. Change the MCU name to 16F877
    4. Build >> Rebuild
    Now your new hex file will be generated..

  • Uthman Oluwatosin Kingofhustle

    honestly i dont know how to use micro c, i use pic c compiler, i try download micro c am having problem with it, if u dont mind doing this project for me on a pcb board i want to buy it and defend it for my final year project by december please..am ready to pay all the cost..

  • thickey1

    The Download button just takes me to the profile page!!! :-(

  • http://www.electrosome.com/ Ligo George

    Please try it now.. we have some server problems…

  • thickey1

    Still not working. Any chance you could just email me the code, please?

  • Ahmad

    I want to down load the file also and the button up doesn’t work, help me please or send it to my email .

  • Ahmad

    Thanks alot the the link work succesfully ^_^

  • http://www.electrosome.com/ Ligo George

    Sorry… now we have a lot of server problems… please try now…
    .. If it is not working… give me your mail id.. I will mail you..
    sorry for the inconvenience caused..

  • http://www.electrosome.com/ Ligo George

    Thanks for the feedback..

  • Ahmad

    Sir, DS1307 ,It needs 5V to it’s Vcc pin???

  • Ahmad

    Can you said for me practically ,at the first switch on to the curuit what will be displayed?

    thank you very much for your help

  • http://www.electrosome.com/ Ligo George

    Hello.. please try it .. it is simple..

    Just download mikroc pro.. from http://www.mikroe.com/ and try.. Now we can ship projects only to India..

  • Uthman Oluwatosin Kingofhustle

    only to india? please i will pay the shipping fee….i dont mind the cost…i need it very urgent…

  • http://www.electrosome.com/ Ligo George

    Your Country??
    When did you need this??

  • Uthman Oluwatosin Kingofhustle

    northern cyprus, close to turkey….i need it by january 3…

  • http://www.electrosome.com/ Ligo George

    Sorry… I am very busy… Try searching for someone near to you…
    Give the above circuit diagram and codes to someone who have experience with project development….

  • Uthman Oluwatosin Kingofhustle

    i cant find one please help me

  • ikm104

    Sir I didn’t get the logic behind Changing HOUR mode and AM/PM indication.
    Will you elaborate, please !!!

  • http://www.electrosome.com/ Ligo George

    DS1307 can work in 12 or 24 hour mode… you can set it..
    When it is in 12 hour mode… you need to specify AM or PM

  • Uthman Oluwatosin Kingofhustle

    Krishnappa Gururaj please can u help me build this project on pcb i want to buy it from you..thanks..

  • Abdulhakeem Dimapinto

    sir, I would like to ask some help about setting the time in the ds1307 using keypad, can you help me sir?

  • Abdulhakeem Dimapinto

    and also to be displayed in the LCD.

  • http://www.electrosome.com/ Ligo George
  • hakeem

    Sir, I’m asking help from you. i’m a graduating student but my problem is my thesis. it is a time based project.. i would like to ask some help that deals with time and uses pic16F877a. thank you

  • ikm104

    I’m sorry but i felt weird when i looked the program and tried to extract the procedure of setting hour mode and AM/PM.I didn’t get case:1 for set_count and condition(year>=50) in case :6.
    I urgently need help !

  • http://www.electrosome.com/ Ligo George

    Hello, we don’t have time to do projects for each and everyone commenting here… If you have any doubts related to above article.. ask us..

  • http://www.electrosome.com/ Ligo George

    DS1307 is a BCD clock.. but .. C operators are decimal.. that steps are used to convert decimal to bcd.. and the if conditions checks for upper and lower limits of hour…
    similarly in case 6 it checks for upper and lower limits of year … not year>=50.. it is year>=0x50

  • ikm104

    Could you elaborate ,please ! I still feel confused around if-else condition in case 1 for set_count .

  • Vaishakh Mohan

    Sir, i would like to know how to generate a PWM signal with duty-cycle adjustment according to real time. Can i use the DS1307 RTC??

  • http://www.electrosome.com/ Ligo George

    It is difficult to explain.. please see the datasheet of DS1307… and see the hour register……
    All bits of hour register are not used to indicated the value of hour…

  • http://www.electrosome.com/ Ligo George

    Yes you can use it… use mikroc’s PWM functions to generate pwm…
    See our pwm tutorials..

  • Uthman Oluwatosin Kingofhustle

    PLEASE IS THERE ANY VIDEO TO WATCH ABOUT THIS PROJECT?

  • http://www.electrosome.com/ Ligo George

    Nop..

  • Uthman Oluwatosin Kingofhustle

    ok, but it will be better if theres a video, for better illustration….so i can follow the video…

  • http://www.electrosome.com/ Ligo George

    I will try to include it.. in future projects..

  • Uthman Oluwatosin Kingofhustle

    THANKS……

  • Uthman Oluwatosin Kingofhustle

    SORRY I WANT TO ASK IF LCD HAVE ANY COMMAND THAT IS INCLUDED IN THE DIRECTORY

  • Uthman Oluwatosin Kingofhustle

    LIKE IS THERE ANY LCD HEADER FILE IN THE DIRECTORY?

  • Uthman Oluwatosin Kingofhustle

    AND ALSO I WANT TO KNOW IF THE TWO CAPACITORS ARE 4MHZ..(4 MEGA HZ)

  • Uthman Oluwatosin Kingofhustle

    please from the schematic above am seeing one 10k variable resistor, three 10k resistor, and two 22k resistor, but from what you implemented above i mean the picture you took above, am seein more than the number of resistors theirs one additional resistor am seeing in the picture please can you tell me why and how comes?

  • http://www.electrosome.com/ Ligo George

    I used 8MHz..
    You needn’t any header files.. Those functions are predefined in the MikroC Pro Compiler.. .. You may change the crystal frequency in the MikroC Project Settings..

  • http://www.electrosome.com/ Ligo George

    1. 10K resistors are used as PULL UP resistors… When the switch is not pressed.. the input pin of PIC will read it as HIGH… When the switch is pressed it becomes LOW..

    2. 10K Variable Resistor is used to adjust the contrast of the LCD

    3. 2.2K resistors are used as the PULL UP resistors for I2C Bus.. SCL and SDA are open collector outputs..

  • Uthman Oluwatosin Kingofhustle

    ok 8mega for both ds1307 and pic right?

  • Uthman Oluwatosin Kingofhustle

    what is 12c bus is it the same thing as ds1307? and also u connect a resistor to pin1 of the pic what is the value?

  • Uthman Oluwatosin Kingofhustle

    also i dont see any capacitor connected to the pic and also no crystal connected to the ds1307..

  • http://www.electrosome.com/ Ligo George

    nop…. 8MHz for pic and 32.768kHz for DS1307

  • http://www.electrosome.com/ Ligo George

    The crystal of DS1307 is very small… It is hidden below a wire.

  • http://www.electrosome.com/ Ligo George

    I2C is a serial communication interface…

  • Uthman Oluwatosin Kingofhustle

    thank you very much the project is working perfect…..but if i switch of the power and on it again the clock starts from the default, but it shouldnt bcos of the ds1307, or am confusing the function of ds1307?

  • Uthman Oluwatosin Kingofhustle
  • farfan just

    Hi Ligo, first of all this is a very good project, well done.

    I have one problem with it. When I want to bush a button, or just put my finger close to the circuit, the time and date values from DS1307 changes to “?”.on the LCD. The fix datas from PIC stays stable(“Time”,”Date”). I try almost everything, but I could’nt solve this problem. The clock is running well and the LCD shows good values, until I don’t put my hand close to the cicuit. But when i tap the gnd somwhere it’s get stable???

    Do You have some advice, or tip?

    Thank You

  • codingzfun

    hie there you have a nice working code, may you please add uart interface to let the user correct time using the computer…..please sir

  • Venkidesh

    how is 0x68 followed by 0=D0?

  • Sid Tryzbiak

    Hi,
    I converted your code for use on my PIC32MX795F512L chip but all I get is ? for time and date. I have RTC2 with 1307 RTC on SDA1 – RA15 and SCL1 on RA14. I’m not using the EEPROM so there should not be a conflict. I have RA14 and RA15 pulled high. I am using a mikroElecktronika Fusion 7 development board.
    Any Ideas?
    Thanks
    Sid
    // LCD module connection EasyPic Fusion V7 PIC32MX795F512L
    sbit LCD_RS at LATD9_bit;
    sbit LCD_EN at LATD10_bit;
    sbit LCD_D4 at LATE4_bit;
    sbit LCD_D5 at LATE5_bit;
    sbit LCD_D6 at LATE6_bit;
    sbit LCD_D7 at LATE7_bit;

    sbit LCD_RS_Direction at TRISD9_bit;
    sbit LCD_EN_Direction at TRISD10_bit;
    sbit LCD_D4_Direction at TRISE4_bit;
    sbit LCD_D5_Direction at TRISE5_bit;
    sbit LCD_D6_Direction at TRISE6_bit;
    sbit LCD_D7_Direction at TRISE7_bit;
    // End LCD module connections

    unsigned short read_ds1307(unsigned short address)
    {
    unsigned short r_data;
    I2C1_Start();
    I2C1_Write(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 –>0xD0
    I2C1_Write(address);
    I2C1_Restart();
    I2C1_Write(0xD1); //0x68 followed by 1 –> 0xD1
    r_data=I2C1_Read(0);
    I2C1_Stop();
    return(r_data);
    }

    void write_ds1307(unsigned short address,unsigned short w_data)
    {
    I2C1_Start(); // issue I2C start signal
    //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 –> 0xD0
    I2C1_Write(0xD0); // send byte via I2C (device address + W)
    I2C1_Write(address); // send byte (address of DS1307 location)
    I2C1_Write(w_data); // send data (data to be written)
    I2C1_Stop(); // issue I2C stop signal
    }

    unsigned char BCD2UpperCh(unsigned char bcd)
    {
    return ((bcd >> 4) + ‘0’);
    }

    unsigned char BCD2LowerCh(unsigned char bcd)
    {
    return ((bcd & 0x0F) + ‘0’);
    }

    int Binary2BCD(int a)
    {
    int t1, t2;
    t1 = a%10;
    t1 = t1 & 0x0F;
    a = a/10;
    t2 = a%10;
    t2 = 0x0F & t2;
    t2 = t2 <> 4;
    t = 0x0F & t;
    r = t*10 + r;
    return r;
    }

    int second;
    int minute;
    int hour;
    int hr;
    int day;
    int dday;
    int month;
    int year;
    int ap;

    unsigned short set_count = 0;
    short set;

    char time[] = “00:00:00 PM”;
    char date[] = “00-00-00″;

    void main()
    {
    I2C1_Init(100000); //DS1307 I2C is running at 100KHz

    CHECON = 0x32; // Prefetch cache pic32mx
    AD1PCFG = 0xFFFF; // Configure AN pins as digital I/O pic32mx

    TRISA = 0x07;
    PORTA = 0x00;

    Lcd_Init(); // Initialize LCD
    Lcd_Cmd(_LCD_CLEAR); // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
    Lcd_out(1,1,”Time:”);
    Lcd_out(2,1,”Date:”);

    do
    {
    set = 0;

    if(PORTA.F0 == 0)
    {
    Delay_ms(100);

    if(PORTA.F0 == 0)
    {
    set_count++;
    if(set_count >= 7)
    {
    set_count = 0;
    }
    }
    }
    if(set_count)
    {
    if(PORTA.F1 == 0)
    {
    Delay_ms(100);
    if(PORTA.F1 == 0)
    set = 1;
    }

    if(PORTA.F2 == 0)
    {
    Delay_ms(100);
    if(PORTA.F2 == 0)
    set = -1;
    }
    if(set_count && set)
    {
    switch(set_count)
    {
    case 1:
    hour = BCD2Binary(hour);
    hour = hour + set;
    hour = Binary2BCD(hour);
    if((hour & 0x1F) >= 0x13)
    {
    hour = hour & 0b11100001;
    hour = hour ^ 0x20;
    }
    else if((hour & 0x1F) = 60)
    minute = 0;
    if(minute = 0x32)
    day = 1;
    if(day 0x12)
    month = 1;
    if(month <= 0)
    month = 0x12;
    write_ds1307(5,month); // write month 6 June
    break;
    case 6:
    year = BCD2Binary(year);
    year = year + set;
    year = Binary2BCD(year);
    if(year = 0x50)
    year = 0;
    write_ds1307(6, year); // write year
    break;
    }
    }
    }

    second = read_ds1307(0);
    minute = read_ds1307(1);
    hour = read_ds1307(2);
    hr = hour & 0b00011111;
    ap = hour & 0b00100000;
    dday = read_ds1307(3);
    day = read_ds1307(4);
    month = read_ds1307(5);
    year = read_ds1307(6);

    time[0] = BCD2UpperCh(hr);
    time[1] = BCD2LowerCh(hr);
    time[3] = BCD2UpperCh(minute);
    time[4] = BCD2LowerCh(minute);
    time[6] = BCD2UpperCh(second);
    time[7] = BCD2LowerCh(second);

    date[0] = BCD2UpperCh(day);
    date[1] = BCD2LowerCh(day);
    date[3] = BCD2UpperCh(month);
    date[4] = BCD2LowerCh(month);
    date[6] = BCD2UpperCh(year);
    date[7] = BCD2LowerCh(year);

    if(ap)
    {
    time[9] = ‘P';
    time[10] = ‘M';
    }
    else
    {
    time[9] = ‘A';
    time[10] = ‘M';
    }

    Lcd_out(1, 6, time);
    Lcd_out(2, 6, date);
    Delay_ms(100);

    }while(1);
    }

  • Vlad

    Hey man, can you please modify the code to run in 24 hour mode? because i don’t know how, i am not good on software, only hardware. Thanks.

  • muhammad asyraf bin kamaruddin

    please give me codding for digital clock using pic 16f877a and ds1307

  • Sohaib

    Hi,
    Thanks for the program
    I want to edit it and put three buttons to make an alarm, but each time I add a piece of code, the LCD flashes with date and time words without showing any date or time or doesn’t show anything at all
    so is there something I should do to make adjustment to the code or where should I put the new code segments in such a way that it won’t affect the original code and will allow the user to enter the alarm time he wants

    thanks in advance
    regards,

  • jyoti

    we can implement this on pcb?

  • S vishnu Vardhan

    Sir,I m trying to build this code in microc ,but it showing that variables t2 and t are eliminated by optimizer and even i dump yhe code in my controller ,it is just displaying the names time and date,nothing more than this,please help me sir

  • ravan nazeer

    why this , hr = hour & 0b00011111; is used bcs second & minute directly taken pls explain.pls be kind enough to write comment difficult functions i am continuosly following your tute

  • http://www.electrosome.com/ Ligo George

    Check the datasheet of DS1307. You can see that last 5 bits of HOUR memory are only used to represent magnitude time in 12 hour mode.