Trung
New Member
In this project we use an implement 2 mode button (1 button 2 mode).
Connect with microcontroller 16F877A: Button connection RB1, 7-segment LED connected to portd (RD0 ... RD7) and PORTB (RB4 ... RB7).
Description of project: When pressing the release button, each press release tang 7-segment LED unit 1, corresponding to the number of clicks the button. While holding the button, 7-segment LED displays the number is increasing. You watch this video to better understand the post.
Reference code with mikroC Pro for Pic and see more tutorials at dientudieukhien.net:
Connect with microcontroller 16F877A: Button connection RB1, 7-segment LED connected to portd (RD0 ... RD7) and PORTB (RB4 ... RB7).
Description of project: When pressing the release button, each press release tang 7-segment LED unit 1, corresponding to the number of clicks the button. While holding the button, 7-segment LED displays the number is increasing. You watch this video to better understand the post.
Reference code with mikroC Pro for Pic and see more tutorials at dientudieukhien.net:
Code:
/*
* Project name: 1 Button 2 che do
* Author: http://dientudieukhien.net
* Description: Neu nhan button va nha ra thi led 7 doan hien thi so lan nhan button,
neu nhan giu button - hold button thi led 7 doan se dem len den khi nao nha button.
* Test configuration:
MCU: PIC16F877A
Oscillator: HS, 08.0000 MHz
SW: mikroC v8.0
*/
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0xC0;
case 1 : return 0xF9;
case 2 : return 0xA4;
case 3 : return 0xB0;
case 4 : return 0x99;
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xF8;
case 8 : return 0x80;
case 9 : return 0x90;
} //case end
}
unsigned short digit_no, digit10, digit1, digit;
void interrupt() {
if (digit_no==0) {
PORTB.F7=0;
PORTB.F6 = 0; // Turn off all 7seg displays
PORTD = digit1; // send mask for ones digit to PORTD
PORTB.F7 = 1; // turn on 1st 7 seg., turn off 2nd
digit_no = 1;
} else {
PORTB.F6=0;
PORTB.F7 = 0; // Turn off all 7seg displays
PORTD = digit10; // send mask for tens digit to PORTD
PORTB.F6 = 1; // turn on 2nd 7 seg., turn off 1st
digit_no = 0;
}
TMR0 = 0; // clear TMRO
INTCON = 0x20; // clear TMR0IF and set TMR0IE
}
void display(unsigned short i){
unsigned short digit;
digit=i%10;
digit1=mask(digit); //hien thi hang don vi
digit=i/10;
digit10=mask(digit); // hien thi hang chuc
delay_ms(500);
}
void main() {
unsigned short i,j,dem;
OPTION_REG = 0x80; // Timer0 settings
TMR0 = 0;
INTCON = 0xA0; // Disable PEIE,INTE,RBIE,T0IE
trisb=0x03;
trisd=0x00;
trisa=0X00;
portb=0;
portd=0;
i=0;
j=0;
dem=0;
display(i);
for(;;){
if(Button(&portb,1,1,0)){ //Kiem tra button nhan giu hay nhan roi nha ra
dem=dem+1;
delay_ms(1);
}
if(dem==1){ //Neu nhan roi nha ra
i=i+1;
display(i);
}
if(dem>1){ //Neu nhan giu - hold button
display(j);
j=j+1;
delay_ms(30);
if(Button(&portb,1,0,1)){
i=0;
j=0;
delay_ms(1000);
display(j);
goto lap;
}
}
lap: //Kiem tra neu buton nha ra thi resest dem.
if(Button(&portb,1,0,1)){
dem=0;
}
}
}