Create a counter in C Show the count display in binary using
Solution
The software part in C is given below:
// 4 bit up counter
// Define Tact switch @ RC4
sbit Switch at RC4_bit;
// Define button Switch parameters
#define Switch_Pin 4
#define Switch_Port PORTC
#define Debounce_Time 20 // Switch Debounce time 20ms
unsigned short count;
void main()
{ ANSEL = 0b00000000; //All I/O pins are configured as digital
CMCON0 = 0x07 ; // Disbale comparators
TRISC = 0b00010000; // PORTC all output except RC4
TRISA = 0b00001000; // PORTA All Outputs, Except RA3
count = 0;
PORTC = count;
do
{ if (Button(&Switch_Port, Switch_Pin, Debounce_Time, 0))
{ if (!Switch)
{ count ++;
if (count ==16) count =0;
PORTC = count;
}
while (!Switch); // Wait till the button is released
}
} while(1); // Infinite Loop
}
Output of the above program:
The counter starts from 0 (all LEDs off), and is incremented by 1 on every button press. After it reaches 15, it overflows and takes the next value 0. This repeats forever.

