/* The scheme of counting button clicks with the output of the number of clicks on the seven-segment indicator (up to 99 clicks) */
int count = 0; //counter
int razr;
bit oldstate; //button state
char seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //7seg digits
void setup() // setip i/o
{
TRISB = 0;
TRISD.RD0 = 0;
TRISD.RD1 = 0;
TRISD.RD2 = 1;
PORTD.RD0 = 0;
PORTD.RD1 = 0;
PORTB = 0;
}
void setMode(int c) //*
{
if (c < 10)
{
delay_ms(20);
PORTD.RD1 = 1;
PORTB = seg[c%10];
}
if (c >= 10)
{
PORTD.RD0 = 1;
PORTB = seg[(c/10)%10];
}
}
void main()
{
setup();
while(1)
{
if (Button (&PORTD, 2, 5, 1))//Debouncing
{
oldstate = 1; //flag button state
}
if (oldstate && Button (&PORTD, 2, 5, 0))
{
count++;
oldstate = 0;
}
if (count == 100)
{
count = 0;
}
setMode(count);
}
}