I'm designing a click counter for a microcontroller and I'm using C for developing the code.
What happens here is when I click first button it will count the number of button presses. Then I have to press the second button to display the count in binary lighting up the LEDs. For example, if I press first button 10 times, it will light up second LED and fourth LED.
Since there are 8 LEDs I'm using 2 different ports.( 6 bits from PORTB and 2 bits from PORTD). For that I have developed a code using if/else.But I want to implement this operation without multiple if/else operations because this method doesn't seem like much efficient.
while(PIND & 0b00100000){ //while PD5 switch is not pressed
if(clickCount>=128){ //if click count>=128
PORTB = PORTB | 0b00100000; //set PB5 HIGH
clickCount-=128; //deduct 128 from clickCount
}else if(clickCount>=64){
PORTB = PORTB | 0b00010000;
clickCount-=64;
}else if(clickCount>=32){
PORTB = PORTB | 0b00001000;
clickCount-=32;
}else if(clickCount>=16){
PORTB = PORTB | 0b00000100;
clickCount-=16;
}else if(clickCount>=8){
PORTB = PORTB | 0b00000010;
clickCount-=8;
}else if(clickCount>=4){
PORTB = PORTB | 0b00000001;
clickCount-=4;
}else if(clickCount>=2){
PORTD = PORTD | 0b10000000;
clickCount-=2;
}else if(clickCount==1){
PORTD = PORTD | 0b01000000;
clickCount = 0;
}
}
And I want to make this code in less number of bytes. So is there any way to develop this code segment using a for loop or any other method?