Digital UP and DOWN counter based on Microcontroller PIC16f877
by Ivan
Event counter based on PIC16f877 microcontroller:-
One of my student e-mail me to guide him in the development of an event counter using microcontroller PIC16f877. So this project is for those who are interested in making a simple counter.
Feature of DIGITAL up and down counter:-
- There are only two push button used for input. One is “UP button” when pressed the counter is incremented by one. and the other is “DOWN button” when pressed counter is decremented by one.
- An alphanumeric liquid crystal display is used for the reading of counts.
- It is programmed for counting of events from 0 event to 15000 event. But student can modify it very easily in the code.
Uses of Event counter:-
- It can be used for event counting.
- It can be used for passenger counting in a bus.
- It can be used for counting of visitors in a place.
Interface of event counter:-
currently in the circuit diagram there are two buttons are shown for input of event one for increment and one for decrement. But user can use optocouplers or red switched instead of simple push buttons. It depends on the use and application of event counter.
The inputs should be so that when active give logic zero and when inactive give logic one.
Circuit diagram of event counter:-
below is the circuit diagram of the event counter.
code of the event counter:-
the program for microcontroller pic16f877 is written in mikroC language and the program listing is shown here below.
sbit LCD_RS at RD4_bit;
sbit LCD_EN at RD5_bit;
sbit LCD_D4 at RD0_bit;
sbit LCD_D5 at RD1_bit;
sbit LCD_D6 at RD2_bit;
sbit LCD_D7 at RD3_bit;
sbit LCD_RS_Direction at TRISD4_bit;
sbit LCD_EN_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD3_bit;
int counts,oldCounts;
char txt1[] = “Event Counter”;
char txt2[] = “Project”;
char txt3[] = “using PIC16f877″;
char txt4[] = “two button interface”;
char txt[6];
void showdata(unsigned int);
void main() {
// OPTION_REG = 0×48;
PORTC=0;
TRISC = 0; //set for ouput
TRISB = 0x3f; // set for input
PORTB = 0xFF;
PORTD = 0xFF;
TRISD = 0×0; // set for ouput
TRISA = 0×0; // PORTA is output
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,txt1); // Write text in first row
Lcd_Out(2,1,txt2); // Write text in second row
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,txt3); // Write text in first row
Lcd_Out(2,1,txt4); // Write text in second row
Delay_ms(2000);
counts = 0;
oldCounts = 1;
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1, 1, “Press Up or down”);
while(1) {
if (Button(&PORTB, 6, 1000, 0)) {counts–;if(counts<=0)counts =0;} if (Button(&PORTB, 7, 1000, 0)) {counts++;if(counts>=15000)counts=15000;}
if(oldCounts!=counts)
{
showdata(counts);
oldCounts = counts;
}
}
}
void showdata(unsigned int ia)
{
WordToStr(ia, txt); // Transform counter value to string
Lcd_Out(2, 1, “counts”);
Lcd_Out(2, 8, txt);
}
Tags:-
PIC 16F877, 18F452 and 18F4550,PIC16F877, PIC18F452,PIC18F4550,16f876,16f84,microcontroller projects,lm35,lm34,lm335,temperature sensor,MikroC Projects
4 Responses
Hello,
It’s rather simple. The counter is set to limiting the count to 15000 so if the count goes beyond 15000, it will reset it back to 15000 as if the event didn’t occur. As for the “if(oldCounts!=counts)” bit, it’s for making sure the new count is displayed before updating it.
Kind regards.
let say that i want to program like calculator.
eg. 16 x 2
the user will push button 1 & 6. how to program that is 16(sixteen).
can you help me?
the image is too small i cant see the pins numbers
plz help
hi,
i’m a bit confusing when u write
” if (Button(&PORTB, 6, 1000, 0)) {counts–;if(counts=15000)counts=15000;}
if(oldCounts!=counts)”
could u explain more?
TQ