8-BIT UP or DOWN COUNTER USING PIC16F877A

This is an example which shows how to connect a push button to a microcontroller and control some operations. The main pitfall for a beginner in this case is, he/she may not be bothered about the switch bounce effect.

When a mechanical switch is closed/opened, for a short interval , it will continue ON-OFF cycle. This means, the switch will be perfectly ON/OFF only after this interval. So we should check the state of switch only after this interval. In programming part, we should also consider this to avoid errors. The below program is for a  two switch controlled 8-bit UP/DOWN  counter using PIC16F877A.

PROGRAM (for HI-TECH C compiler)

#include <pic.h>
#define _XTAL_FREQ 20e6
__CONFIG(0x3F3A);
unsigned char count=0;
main()
{
ADCON1=0b00001111;    //to make PORTE as digital I/O pins.
TRISD=0;
TRISE0=1;
TRISE1=1;
while(1)
    {
    if(RE0==1)
        {
        if(count<255){count++;}
        __delay_ms(50);
        while(RE0==1);
        __delay_ms(50);
        }
   
    if(RE1==1)
        {
        if(count>0){count--;}
        __delay_ms(50);
        while(RE1==1);
        __delay_ms(50);
        }
    PORTD=count;
    }
}

HEX  CODE (save as .hex)

:060000000A128A11A42F70
:100F4800F30183010A128A11A92F0F3083169F001B
:100F58008801091489148312091CD52F7308FF3AD4
:100F68000319B72FF30A0230F2004430F100A93018
:100F7800F000F00BBD2FF10BBD2FF20BBD2F64005D
:100F8800831203130918C42F0230F2004430F10011
:100F9800A930F000F00BCE2FF10BCE2FF20BCE2F95
:100FA800640083120313891CFB2F73080319DD2FB8
:100FB800F3030230F2004430F100A930F000F00BE6
:100FC800E32FF10BE32FF20BE32F640083120313DB
:100FD8008918EA2F0230F2004430F100A930F000FD
:100FE800F00BF42FF10BF42FF20BF42F64007308BD
:080FF800831203138800AF2FE0
:02400E003A3F37
:00000001FF

CIRCUIT DIAGRAM



5 comments :

  1. hyh... is this circuit and program working? because i have already try making this one and it is not working.. maybe there's something wrong with our circuit or something.. pls can u help me?

    ReplyDelete
  2. #include
    delay ()
    {
    for (int i=0; i<20000; i++);
    }
    #define _XTAL_FREQ 20e6
    __CONFIG(0x3F3A);
    unsigned char count=0;
    main()
    {
    ADCON1=0b00001111; //to make PORTE as digital I/O pins.
    TRISD=0;
    TRISE0=1;
    TRISE1=1;
    while(1)
    {
    if(RE0==1)
    {
    int k=0x80;
    while(1){
    PORTD=k;
    k=(k>>1);
    delay();
    if (k<0x01)
    {
    k=0x80;
    }
    }
    }
    }
    }
    try to this one....

    ReplyDelete
  3. #include
    delay ()
    {
    for (int i=0; i<20000; i++);
    }
    #define _XTAL_FREQ 20e6
    __CONFIG(0x3F3A);
    unsigned char count=0;
    main()
    {
    ADCON1=0b00001111; //to make PORTE as digital I/O pins.
    TRISD=0;
    TRISE0=1;
    TRISE1=1;
    while(1)
    {
    if(RE0==1)
    {
    int k=0x80;
    while(1){
    PORTD=k;
    k=(k>>1);
    delay();
    if (k<0x01)
    {
    k=0x80;
    }
    }
    }
    }
    }
    try to this one....

    ReplyDelete