# Creating a counter using other way

Recently I created a simple board to test the HCS08 chip used on my wash machine, it is a very simple board with just the microcontroller and two LEDs (wired to PTE1 and PTE3).

Then I created a simple binary counter program to test it:

```#define LED1 PTED_PTED1
#define LED2 PTED_PTED3

void main(void)
{
int i = 0;
MCU_init();
PTEDD = 0x0F;
LED1 = 0;
LED2 = 0;
while(1){
i++;
LED1 = i & 1;
LED2 = (i & 2) >> 1;
mdelay(500);
}
}
```

It worked fine, but then I started to thing another way to count without using a counter (i++).
I realized the LED1 (bit 0) always goes some sequence (0->1->0->1…), it could be done easily using a Exclusive OR (XOR) logic: LED1 = LED1 ^ 1 or Not Logic: LED1 = ~LED1;
Also I noticed which LED2 (bit 1) only turns ON or OFF after a LED1 transition from 1 to 0. Then I could create a variable T (Temporary) to save previous LED1 state and compare it with current status. Case previous state is 1 and current status is 0 it should inverts the LED2 state, then just do that: LED2 = LED2 ^ (T & ~LED1), then I got it:

```#define LED1 PTED_PTED1
#define LED2 PTED_PTED3

void main(void)
{
int i = 0, T = 0;
MCU_init();
PTEDD = 0x0F;
LED1 = 0;
LED2 = 0;
while(1){
LED1 = LED1 ^ 1;
LED2 = LED2 ^ (T & ~LED1);
T = LED1;
mdelay(500);
}
}
```

It worked, but just after doing this way I realized my mistake: I don’t need to compare previous state, because LED1 just was 1 before it turns 0 (except the first time, but it doesn’t matter), then the above code could be reduced to:

```#define LED1 PTED_PTED1
#define LED2 PTED_PTED3

void main(void)
{
MCU_init();
PTEDD = 0x0F;
LED1 = 0;
LED2 = 0;
while(1){
LED1 = LED1 ^ 1;
LED2 = LED2 ^ ~LED1;
mdelay(500);
}
}
```

It fact I created a counter without using an ADD instruction. To be honest I never saw a microcontroller without ADD instruction, but if some day I got one I could do a counter easily 😉