Page 1 of 2

Digital Clock with only PIC16F628A

Posted: Sat Mar 16, 2013 6:09 pm
by Rksk
I just wanted to test my new seven segment module :D
And thought to create something.

Then wrote this to make a Digital Clock with only PIC16F628A
Below are some photos, a video, the circuit diagram and the mikroC code I wrote for this.

Also this clock is not 100% calibrated, so you have to do it manually.
My code is not well commented. So if you got any problem, feel free to ask it here.
1.JPG
1.JPG (26.61 KiB) Viewed 18122 times
2.JPG
2.JPG (47.28 KiB) Viewed 18122 times
3.JPG
3.JPG (34.17 KiB) Viewed 18122 times
4.JPG
4.JPG (44.33 KiB) Viewed 18122 times
[media]http://www.youtube.com/watch?v=15FkKL6vbOQ[/media]
Circuit Diagram.jpg
Circuit Diagram.jpg (40.01 KiB) Viewed 18122 times

Code: Select all

unsigned short i, DD0, DD1, DD2, DD3, DD4;
char temp=0;

unsigned short mask(unsigned short num) {
 switch (num) {
 case 0 : return 0x40;
 case 1 : return 0x79;
 case 2 : return 0x24;
 case 3 : return 0x30;
 case 4 : return 0x19;
 case 5 : return 0x12;
 case 6 : return 0x02;
 case 7 : return 0x78;
 case 8 : return 0x00;
 case 9 : return 0x10;
 } //case end
}


unsigned int counter = 0;
char second = 0;
char hour = 12;
char minute = 0;

void interrupt(){

     if(PIR1.TMR1IF){
        counter++;
        TMR1H = 0xD8;
        TMR1L = 0xF0;
        if(counter >= 25) {
            counter = 0;
            second++;
            if (second >= 60){
               second = 0;
               minute++;
              if (minute >= 60){
                 minute = 0;
                 hour++;
                if (hour >= 13){
                   hour = 1;
                   /*
                   you can add calibration here,
                   just increase or discrese second or/and minute
                   Ex: minute -= 2
                       second -= 5
                   */
                }

              }
             EEPROM_Write(4,minute);
             EEPROM_Write(2,hour);
            }
        }
        PIR1.TMR1IF = 0;
      
        if (temp > 0) temp--;
      
      }
}









void main() {
  CMCON |= 7;
  TRISB = 0b00000000;
  TRISA = 0b00110000;
  PORTB = 0x00;
  PORTA = 0x00;

  hour = EEPROM_Read(2);
  minute = EEPROM_Read(4);
  
  TMR1H = 0xD8;
  TMR1L = 0xF0;

   T1CON.T1CKPS1 = 1;
   T1CON.T1CKPS0 = 0;
   PIE1.TMR1IE = 1;

   INTCON.PEIE = 1;
   INTCON.GIE = 1;
   
   T1CON.TMR1ON = 1;


  do {

     if (PORTA.B4 == 1 && temp == 0){
        
        PORTB.B7=0;
        if (PORTA.B4==0)
        {
         hour++;
         if (hour >= 13) hour = 1;
        }
        else
        {
         minute++;
         if (minute >= 60) minute = 0;
        }
        
        temp = 12;//doesn't get another button click until a half of second
     }



  DD0 = minute%10;
  DD0 = mask(DD0);
  
  DD1 = (minute/10);
  DD1 = mask(DD1);
  
  DD2 = hour%10;
  DD2 = mask(DD2);
  
  DD3 = (hour/10);
  DD3 = mask(DD3);
  
  if (counter > 12) DD4 = 1;
  else DD4 = 0;//blick the LED onece a second

  for (i = 0; i<=50; i++) {
      PORTB = DD0;
      PORTA = 1;
      delay_ms(1);
      
      PORTB = DD1;
      PORTA = 2;
      delay_ms(1);
      
      PORTB = DD2;
      PORTA = 4;
      delay_ms(1);
      
      PORTB = DD3;
      PORTA = 8;
      delay_ms(1);

      PORTB = 0xFF;
      PORTA = 0;
      PORTB.B7 = DD4; // turn on/off blicking LED
      delay_ms(1);
      
      }

  } while(1);          // endless loop
}

Please don't copy paste this on your blog or website, just add a link to this page if you are copying.
Also begginers, please try to learn without just copiyng this.

Re: Digital Clock with only PIC16F628A

Posted: Sat Mar 16, 2013 7:37 pm
by Neo
This is the best article I have ever seen on ROBOT.LK. Simply brilliant work Rksk.
I became extremely happy by seeing your wonderful achievements and this is a good lesson for the younger generation in Sri Lanka. Very well done!!!

Re: Digital Clock with only PIC16F628A

Posted: Sat Mar 16, 2013 8:22 pm
by Rksk
Neo wrote:This is the best article I have ever seen on ROBOT.LK. Simply brilliant work Rksk.
I became extremely happy by seeing your wonderful achievements and this is a good lesson for the younger generation in Sri Lanka. Very well done!!!
Oops, I never expected an appreciation like this.
Thank you very much Neo... :clap:

Both of bread board and Pickit2 are donated by you. :)

Re: Digital Clock with only PIC16F628A

Posted: Sat Mar 16, 2013 8:31 pm
by Nandika
Nice work Rksk... :)

Re: Digital Clock with only PIC16F628A

Posted: Sat Mar 16, 2013 10:34 pm
by tmadushan
Seems you have got a new cam. Nice work bro!! I thought using a dedicated clock crystal - 32768MHz would give more precise values... Good Luck!!

Re: Digital Clock with only PIC16F628A

Posted: Sun Mar 17, 2013 12:28 am
by Rksk
Nandika wrote:Nice work Rksk... :)
Thank you very much Nandika...
tmadushan wrote:Seems you have got a new cam. Nice work bro!! I thought using a dedicated clock crystal - 32768MHz would give more precise values... Good Luck!!
Nope, it's just a temp one :(
this crystal is 4.00000MHz, but it's not true. I don't know why, have to ask from Neo.
Thank you very much Tharanga...

Re: Digital Clock with only PIC16F628A

Posted: Sun Mar 17, 2013 5:48 pm
by SemiconductorCat
This issue is discussed several times in stackexchange.

There is no way easily fix this error. Even your hand watches exist this.
It depend on the temperature too.

PPM value of crystals are higher on high temperature environments like here.

Some clocks are synchronizing automatically with the GMT broadcasting signal.
But neither it's accurate that far. Because as you know there is a time to radio waves
to travel.

Unless you ready to pay for that Atomic clock alternative or crystal oven method.


A one particle suggestion is a look-up table acording to temperature.
Normally crystal manufactures give a table. PPM varies according to
temperature.


Since in industry, when there are no precise manual calibration. May be deep space communication
like industries may use those PLL like things to calibrate. Where dolper shifting will change the
frequency and need adjustments. But those things are not magic things for engineers. After it formalized
it's nothing. Same principle could apply here too, I mean lock with a atomic clock (GMT) signal and a
voltage controlled osc will generate the clock signal.

any more improvement ideas ? Neo?

Re: Digital Clock with only PIC16F628A

Posted: Sun Mar 17, 2013 6:12 pm
by Neo
any more improvement ideas ? Neo?
Basically you are right. Highly accurate clock is not a main requirement to day to day lives. Just a few seconds means nothing.

However on a high speed application such as a racing car, racing boat, a rocket, a satellite, etc... a second means few kilometers may be. So this needs to be taken under consideration. If you take International Space Station, that moves 7.68 km/s . So this is not a small displacement.

The best I can suggest is a Real-Time clock IC that comes with a 32.768 KHz crystal. Those has built-in error compensation features. Have a look at DS1307 application in ROBOT.LK.
https://robot.lk/viewtopic.php?f=18&t=2578

Re: Digital Clock with only PIC16F628A

Posted: Sun Mar 17, 2013 8:38 pm
by tmadushan
Correction, not 32768MHz, 32768Hz.. Yes try this with DS1307 and I2C, It will be more precise.

Re: Digital Clock with only PIC16F628A

Posted: Mon Mar 18, 2013 2:10 pm
by SemiconductorCat
Good tutorial/question Rksk,

You guys are crazy guys. Sooner or later you guys may need this.
one alternative is radio clock.


DCF77 is a signal embedded time on it. See more info: http://en.wikipedia.org/wiki/DCF77

And it could easily communicate with PIC: http://www.picprojects.net/dcf77/index.html
Image

I don't know whether DCF77 is available here or not. But India got a radio clock transmitter there.
May be this would be your next hobby project to decode that signal and extract clock info from it.

Different time zones and different signal travel time , still may need calibration.


and about RTC module, I think they have special 10PPM crystals. But even it will leap around 5 minutes per
a one year.A normal crystal is around 20PPM in the market. So nothing much difference in high cost crystals
as I think.