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.
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