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

20 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
  6. Hi, at first congrats for nice work and appreciate. Also i would like to use "7" like nosed "7". Included f digit at C file // 7: 0 1 0 0 1 1 1 0x07. But i couldn't convert to hex file. Mplab and MicroC always gave error for C file. Nosed 7 looks nifty in my opinion. Awaiting your suggestion and thank you..

    ReplyDelete
    Replies
    1. And what is the error Mplab gave?
      Also, did you change the 7-th value in this line of code:
      ----------
      const byte segments[11] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x00};
      ----------
      It should be 0x27

      BTW, I don't think the '7' will look better with 'nose', just my opinion :)

      Delete
    2. Thanks for fast response. Problems were BUILD FAILED (exit value 2, total time: 554 ms), space between words etc. I tried it but same results, besides nosed 7 more readable cause some A segment of displays are short. It's your opinion:) Have nice day enjoy your work..

      Delete
    3. No way unfrtntly; failed again. I changed the value as you point out: / / 7: 0 1 0 0 1 1 1 0x27
      const byte segments[11] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x27, 0x7F, 0x6F, 0x00}; as well.
      make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
      make -f nbproject/Makefile-default.mk dist/default/debug/Myproject.X.debug.elf
      make[2]: Entering directory 'E:/DIYfan clock/Digital Clock (PIC16F628A, DS1307, v2)/Myproject.X'
      "C:\Program Files\Microchip\xc8\v2.46\bin\xc8-cc.exe" -mcpu=16F628A -c -D__DEBUG=1 -mdebugger=none -mdfp="C:/Program Files/Microchip/MPLABX/v6.20/packs/Microchip/PIC16Fxxx_DFP/1.6.156/xc8" -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mno-default-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto -o build/default/debug/newmain.p1 newmain.c
      ::: advisory: (2100) using the C99 standard library with the selected device may result in larger code and data usage
      make[2]: *** [nbproject/Makefile-default.mk:95: build/default/debug/newmain.p1] Error 1
      make[1]: *** [nbproject/Makefile-default.mk:85: .build-conf] Error 2
      make: *** [nbproject/Makefile-impl.mk:39: .build-impl] Error 2
      newmain.c:1:10: fatal error: 'Digital Clock (PIC16F628A, DS1307, v2).h' file not found
      #include "Digital Clock (PIC16F628A, DS1307, v2).h"
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      1 error generated.
      (908) exit status = 1
      make[2]: Leaving directory 'E:/DIYfan clock/Digital Clock (PIC16F628A, DS1307, v2)/Myproject.X'

      BUİLD FAILED (exit value 2, total time: 84ms)

      Delete
    4. The error is "fatal error: 'Digital Clock (PIC16F628A, DS1307, v2).h' file not found", so obviously "Digital Clock (PIC16F628A, DS1307, v2).h" is missing. You should get the file from the downloaded archive and put it in the same folder were the .C file is.
      Also in Mplab you should add the file to the project - right click on "Header files" and select "Add existing item"
      And finally because the program was written and compiled in MicroC Pro, maybe there is other incompatibilities with Mplab.

      What error do you get from MicroC Pro? I don't use MicroC Pro anymore, but maybe I still can help.

      Delete
  7. At first thank you for your kind reply. I learned Mplab IDE V. 6.20 has problems. Anyway i just wanna nosed hex but could't. No problem, I'd like to tell that you spent time for nice work and reply all questions with patience. Stay well, thanks..

    ReplyDelete