Wednesday, October 10, 2012

Digital Clock Using PIC16f628a Microcontroller


This is a very basic digital clock that can be done using a microcontroller. I particularly used a PIC16f628a.

You can also check my digital clock based on 74ls90
here --> http://circuitdesolator.blogspot.com/2010/12/digital-clock-based-on-74ls90.html

Here's the picture of my digital clock prototype




This is the schematic of the device:



 Notes to remember:

1. Put resistors from 7447 to the seven segment display pins(a-g) as current limiter resistors
2. You need to start up you're clock at exactly 1200am/pm.



-------------------------------------------SOURCE CODE-----------------------------------------------
//Digital Clock using PIC16f628a microcontroller
//Design by: Parax (rlabs)
//Date: April 2011

#include

#define _XTAL_FREQ 4000000

__CONFIG(INTIO & WDTDIS & PWRTDIS & UNPROTECT & BORDIS & LVPDIS);

unsigned int hours = 0;
unsigned int mins = 59;

unsigned int timer = 0;
unsigned char mpx_cnt = 0;
static unsigned char mode = 0;

void interrupt ISR(void)
{   
       
            {       
            timer++;
           
            if(timer > 19650)
            {
                mins++;   
                if(mins == 60)
                {           
                    mins = 0;
                    hours++;
                    if(hours == 13)
                    hours == 1;                   
                }
               
                timer = 0;
            }
        }
       
        switch (mpx_cnt)
        {
            case 0:
                PORTB = hours/10;
                RA0 = 1;       
                mpx_cnt = 1;
               
            case 1:
                PORTB = hours%10;
                RA1 = 1;       
                mpx_cnt = 2;
           
            case 2:
                PORTB = mins/10;
                RA2 = 1;       
                mpx_cnt = 3;
           
            case 3:
                PORTB = mins%10;
                RA3 = 1;
                mpx_cnt = 0;
        }
   
        T0IF = 0;                //clear TMR0 interrupt flag   
}


void init_Timers(void)
{
    GIE = 0;               
   
    T0CS = 0;               
    PSA = 0;                                           
    PS2 = 0;              
    PS1 = 0;              
    PS0 = 0;

    T0IF = 0;               
    T0IE = 1;                  
    TMR0 = 6;                                                                                               
   
    GIE = 1;             
}

void main()
{
    TRISA = 0x00;
    TRISB &= ~0x0F;
    TRISB |= 0xF0;
   
    init_Timers();
   
    while(1);
}

---------------------------------------SOURCE CODE--------------------------------------------

Switch debounce circuit


 In most digital electronics projects that uses different kinds of switches, switch bounces are commonly encountered. These are extra glitches after the real switch operation. These are tiny pulses that affect the actual operation of the circuit like counters.

Here's a demo video of a switch bounce.


It is clearly seen that in certain push on the switch, the counting is not accurate. This is causee by the switch bounce. Here now is the image that shows switch bounces and a clean pulse.



As you can see in the top image, this is an image of a clean pulse without a switch bounce. On the other hand, below it, shows the bounces formed from switching.

The upper image use a circuit that is called switch debounce. A switch debounce can be software or hardware. We will create the latter.

The circuit below is a RS latch that is used as a switch debouncer. It's composed by two 2-input NAND gates with outputs feedbacks to the input of another. (other explation from malvino)

Pull-up and Pull-Down Resistor


Most of the beginners in digital electronics assume that the hanging input is a logic 0. Then, that's a misconception. They are neutral state which can be a logic 0 or a logic 1. This will bring chaos on your circuit operation.

To solve this problem, we add a pull-up and a pull-down resistor.

Below is an image of pull-up and a pull-down.



As you can see, in each case there is a default input. For a pull-up, the default input to the logic IC is 1. And for the pull-down, the logic is 0. The resistor also provide a current limiting function thus affecting power consumption of the input section. 10kohms is the commonly value but any value can be use depending on your application.