Wednesday, March 20, 2019

Simple Digital Clock with PIC16F628A and DS1307 and 7-Segment LED display

In this new project I am again using PIC16F628A microcontroller. The goal is simple digital clock with 7-segment LED display and the clock will have no additional functionality - no alarm, no seconds digits, no date. The latter can be added in the software though. For the RTC chip I chose DS1307. For the LED display I used Kingbright CC56-21SRWA.



This is the schematic I concocted. The microcontroller works with it's internal oscillator at 4 MHz to save 2 extra pins. Reset pin (MCLR) is also used as input for one of the buttons. All segments are connected to PORTB and COMs are connected to PORTA. RTC chip is also connected to PORTA.
The schematic is extremely simple and I assembled it on a breadbord for a quick test:


It worked as expected. The refresh rate of the digits is about 53Hz and there is no visible flickering. Because of the multiplexing the digits are dimmer and to compensate this the current through segments must be higher. I tested it with different values for current limiting resistors R1-R7 and below 220 Ohm the microcontroller starts to misbehave - some of the digits start to flicker. 220 Ohm and above seems OK. The two dots in the middle are connected to the SQW pin of the DS1307. This pin is configured as 1 Hz square wave output. It is an open drain output, so in order to work it has to have pull-up resistor. I searched and didn't find anywhere information about current capability of this pin. I tested with 330 and 470 Ohm and it didn't burn :) To be on the safe side I left the 470 Ohm one - it's a little dimmer than the rest of the segments, but still well visible.
There are two buttons  for adjusting the time - one for the hours and one for the minutes.
There is one unused pin left - RB7, which can be used for additional functionality. For example a buzzer can be connected  and an alarm function can be added to the software.

The software is written and compiled with MikroC Pro and uses its build in software I2C library for communicating with RTC chip. If someone wishes to use MPLAB software for compiling the code he should write his own I2C functionality from scratch. 
Here is a short video demonstrating how it works:



Software code and HEX file: DigitalClock
If you use PICkit 3 standalone software to upload HEX code to the microcontroller, then in the "Tools" menu check the option "Use VPP First Program Entry".


Update: June 30, 2020
This clock (with improved schematic) is available for purchase at tindie.com

14 comments:

  1. It worked right away, but it needed at least 6.5V for the digits to show up. This voltage is the absolute maximum for the MCU and close to maximum for the RTC. So, it would be nice to have this work with 5V. Did your circuit worked with 5V?

    ReplyDelete
    Replies
    1. Hi,
      The schematic work with 5V power supply. Check the values of the resistors R1-R7. If your 7-segment indicator is green maybe the forward voltage of the LEDs inside is greater and the resulting current trough them is smaller. If this is the case, you can try reducing the vaues of the R1-R7

      Delete
    2. I am using the exact same display as yours (CC56-21SRWA) in RED color. I will try to reduce R1-R7 a bit to see what happens. Could you share the Eagle files or libraries for the parts? I would like to design a PCB for this.

      Delete
  2. hi the code please?
    link not working

    ReplyDelete
  3. Good day, sorry for my english, but i tried to compile the .c file, and there is an error, i dont know why, 39 324 Undeclared identifier 'Soft_I2C_Start' in expression. i want to use a comun annode display, but even the original file does not compile in mikro c pro for pic v.7.6.0, i just dont what to do. thanks

    ReplyDelete
    Replies
    1. You should go to "Library manager" (usually it is on the right side) and check "Software I2C". This will tell the compiler to include this library and there should not be errors when compiling the code.

      Delete
  4. can i replace DS1307 with ds3231 with out any changes ? if changes requires can you provide me the drawing and hex code?
    thanks,
    vijayan

    ReplyDelete
    Replies
    1. Did you even read the datasheets of these two chips? Not everything on the internet is free - you must do some work yourself.

      Delete
  5. what are the changes for codes to 12hr format and if could AM/PM indicator in pin13

    ReplyDelete
    Replies
    1. The code calculating the hour is this:

      if (h & 0b01000000) { // if 12h mode (bit 6 == 1)
      if (h & 0b00100000) // if PM (bit 5 == 1)
      h = 12 + ((h >> 4) & 1)*10 + (h & 0b00001111);
      else
      h = ((h >> 4) & 1)*10 + (h & 0b00001111);
      }
      else
      h = ((h >> 4) & 3)*10 + (h & 0b00001111);

      You should modified it like this:

      if (h & 0b01000000) { // if 12h mode (bit 6 == 1)
      // if (h & 0b00100000) // if PM (bit 5 == 1)
      // h = 12 + ((h >> 4) & 1)*10 + (h & 0b00001111);
      // else
      h = ((h >> 4) & 1)*10 + (h & 0b00001111);
      }
      else
      h = ((h >> 4) & 3)*10 + (h & 0b00001111);

      Delete
    2. You can add a button to pin 13 to switch between 24/12 mode but it is unnecessary as most of the people prefer one of the two modes and rarely will switch between them. If you want it anyway, you should have some variable which will store the current mode, track the status of pin13 and change this variable accordingly. If you want this variable to retain its value when the power is down, then you can write the value of this variable in the free memory of the DS1307 (addresses 0x08..0x3F) and read it from there at startup.

      Delete
    3. thank you sir replying me. in my second question actually i need attach led to pin 13 to indicate AM/PM,not a switch. what codes to add?
      and sir, 12 hr/24 hr any format how can remove blinking digit 1 (hr first digit/first digit from left) is zero (eg in time 1.20 am its indicate 01.20 actually i need 1.20/ in time 13.30 should 1.30 not 01.30 (12hr format)).can you teach what codes add to remove that zero in 12hr format.

      Delete
    4. And More questions(ha ha) what codes modify for common anode and blue 1.2 " 7 segment.

      Delete