add binary counter example
This commit is contained in:
@@ -224,3 +224,25 @@ Code to turn the fan on and off with a 2 second delay:
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Binary LED Counter
|
||||||
|
|
||||||
|
This example uses 5 LEDS to visualize a 5 bit binary counter. Mask commands are used to set the LEDS simultaneously.
|
||||||
|
|
||||||
|
```c
|
||||||
|
int pinMask = 0b1111100000000000;
|
||||||
|
int dirMask = 0b1111100000000000;
|
||||||
|
int counter = 0;
|
||||||
|
gpio_init_mask(pinMask);
|
||||||
|
gpio_set_dir_masked(pinMask, dirMask);
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
gpio_put_masked(pinMask, counter<<11);
|
||||||
|
counter++;
|
||||||
|
sleep_ms(500);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`gpio_put_masked` has similar characteristics as other masked functions. This will set the output pins according to the second argument simultaneously, `counter<<11` in this case. Counter is left shifted by 11 bits to align with the mask.
|
||||||
|
|
||||||
|
if counter is `11001`, `counter<<11` will be `1100100000000000`.
|
||||||
|
|||||||
Reference in New Issue
Block a user