PIC16F877 up down counter code and Proteus simulation

This PIC16F877 microcontroller tutorial answers the question,
” How to implement a up down counter using PIC16F877 ? ”

 

Using PIC16 simulator (Proteus) you can verify thisΒ counterΒ code and change it according to your needs. Using one push button (Labeled as Count, as shown in figure below) you can increment (When SW1 switch is towards up position) or decrementΒ (When SW1 switch is towards down position)Β count value (displayed on LCD) as you desire. This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the β€˜Downloadsβ€˜Β section at the bottom of this page.

In this article, it is assumed that you know, how to interface LCD with PIC16F877 microcontroller. If you don’t then please readΒ this page.

The following diagram (made inΒ Proteus) shows the PIC microcontroller circuit diagram.

PIC16F877 up down counter code and Proteus simulation Schematic

The above figure was taken after setting time to 03:59:38, timer0 is used as the base for digital clock generation. Timer0 is used here to generate 1msec interrupts. After every 1msec a global variable namedΒ msCounterΒ increments. WhenΒ msCounterΒ reaches a value of 1000 then another global variable namedΒ secCounterΒ increments and this process repeats itself. Similarly, whenΒ secCounterΒ reaches 60, then Β minCounterΒ increments. And whenΒ minCounterΒ reaches 60 thenΒ hrCounterΒ increments. This process continues untilΒ hrCounterΒ reaches 24 then all of these variables reset their values.Β LCD is updated with the new values ofΒ hrCounter,Β minCounterΒ andΒ secCounterΒ after every second.

You can set time using three push buttons attached onΒ RE0,Β RE1Β andΒ RE2Β pins (As shown in the above figure). By pressing β€˜Set Timeβ€˜ button one time, code enters in configuration state.Β HoursΒ value starts to blink and you can modify it usingΒ UpΒ andΒ DownΒ buttons. PressingΒ UpΒ button increments the value and pressingΒ DownΒ button decrements the value. When you are done settingΒ HoursΒ value, pressΒ β€˜Set Timeβ€˜ button again, thenΒ MinutesΒ value will start to blink and you can adjust this value usingΒ UpΒ andΒ DownΒ buttons. Similarly, after setting Minutes value, you canΒ pressΒ β€˜Set Timeβ€˜ button again, thenΒ SecondsΒ value will start to blink and you can adjust this value usingΒ UpΒ andΒ DownΒ buttons. When you are done adjusting the time, then pressΒ β€˜Set Timeβ€˜ button for the last time and this clock will start to work normally.

A crystal of 4MHz value is used in this circuit, which makes this PIC16F877A run at a speed of 1MIPS (Million of instructions per second).

Code

The main function code is shown below.

Figure 2. Β  Main function code for digital clock on PIC16F877A

In the main function, firstly PORTE pins are made input and ADC is turned off on these pins. Then LCD is initialized usingΒ InitLCD()Β function. ThenΒ Timer0Β is initialized to generate 1msec interrupts.

After that, in theΒ while(1)Β loop, there is a state machine running. A variable namedΒ StateΒ is used to keep track of the current state of the clock. When this code starts, then State has a value ofΒ NORMAL_STATE. In the normal state, clock value is incremented after every second andΒ wheneverΒ msCounterΒ reaches a value of zero[1], then new values ofΒ hrCounter,Β minCounterΒ andΒ secCounterΒ are updated on the LCD.Β UpdateTimeCounters()Β function is called every time and it corrects the values of every counter variable depending upon the value ofΒ msCounter,Β whichΒ is incremented in the ISR function of timer0.

In the normal state, code keeps checkingΒ RE0Β pin value, if β€˜Set Timeβ€˜ button is pressed thenΒ RE0Β pin value becomes zero. This is detected usingΒ if(!RE0)Β statement in the code. WhenΒ β€˜Set Timeβ€˜ button is pressed then state is changed to β€˜CONFIG_HR_STATEβ€˜ (i-e configure hours value state). In this state,Β UpΒ andΒ DownΒ buttons are constantly monitored andΒ hrCounterΒ value can be adjusted. Similarly, by pressing β€˜Set Time’ button again, state is changed toΒ β€˜CONFIG_MIN_STATEβ€˜ (i-e configure minutes value state). In this stateΒ minCounterΒ value can be adjusted usingΒ UpΒ andΒ DownΒ buttons andΒ by pressing β€˜Set Timeβ€˜ button again, state is changed toΒ β€˜CONFIG_SEC_STATEβ€˜ (i-e configure seconds value state). In this stateΒ secCounterΒ value can be adjusted usingΒ UpΒ andΒ DownΒ buttons.Β By pressing β€˜Set Timeβ€˜ button again, state is changed toΒ β€˜NORMAL_STATEβ€˜ and clock starts to run normally.

 

Source : PIC16F877 up down counter code and Proteus simulation


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.