From c6b07ae2f7cb8664981d5cf9c2a52713ff3565f9 Mon Sep 17 00:00:00 2001 From: "Phani Pavan K: iDellToast" Date: Sat, 23 Nov 2024 12:15:49 +0530 Subject: [PATCH] add binary counter example --- 1_Basic_GPIO/digitalOut.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/1_Basic_GPIO/digitalOut.md b/1_Basic_GPIO/digitalOut.md index dc2c9c8..eb9aec4 100644 --- a/1_Basic_GPIO/digitalOut.md +++ b/1_Basic_GPIO/digitalOut.md @@ -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`.