add 2 more projs

This commit is contained in:
2024-11-17 23:10:35 +05:30
parent d89f54c6e8
commit ff51a6ee7c

View File

@@ -131,3 +131,71 @@ sleep_ms(500);
- `gpio_set_drive_strength(LED[i], GPIO_DRIVE_STRENGTH_2MA)` sets the **Drive Strength**/Current Limit of GPIO port. The RP2040 C SDK provides 4 drive strengths, 2mA, 4mA, 8mA and 12mA.
The rest of the code is similar to the prior examples, which toggles the LEDS one by one, in series and turns them off, creating a loading effect. Visual output can be seen below.
## ToggleBlinky
This example uses `gpio_get_out_level` to to get the output level for gicen given gpio port. The above example can be simplified by using this function call, as below.
```c
int LED1 = 11;
int LED2 = 12;
int LED3 = 13;
int LED4 = 14;
int LED5 = 15;
int LEDS[5] = {LED1, LED2, LED3, LED4, LED5};
for (int i = 0; i < 5; i++)
{
gpio_init(LEDS[i]);
gpio_set_dir(LEDS[i], GPIO_OUT);
gpio_set_drive_strength(LEDS[i], GPIO_DRIVE_STRENGTH_2MA);
}
while (true)
{
for (int i = 0; i < 5; i++)
{
gpio_put(LEDS[i], !gpio_get_out_level(LEDS[i]));
sleep_ms(100);
}
}
sleep_ms(500);
```
## ParallelBlinky
use gpio_put_masked and gpio_set_dir_masked, set_dir_in_masked and set_dir_out_masked
In this example, the init and set dir operations are performed simultaneously using `gpio_init_mask` and `gpio_set_dir_masked` calls. These take a integer value called mask, where each bit represents one GPIO pin. The above example can be simplified and replicated with the code below.
```c
int LED1 = 11;
int LED2 = 12;
int LED3 = 13;
int LED4 = 14;
int LED5 = 15;
int LEDS[5] = {LED1, LED2, LED3, LED4, LED5};
int pinMask = 0b1111100000000000;
int dirMask = 0b1111100000000000;
gpio_init_mask(pinMask);
gpio_set_dir_masked(pinMask, dirMask);
while (true)
{
for (int i = 0; i < 5; i++)
{
gpio_put(LEDS[i], !gpio_get_out_level(LEDS[i]));
sleep_ms(100);
}
}
sleep_ms(500);
```
Here, the `pinMask` variable represents the gpio pins. The position from the right represents the the gpio pin with that number and putting a `1` in that position means it is highlighted. So the 0th bit maps to GP0, 1st bit maps to GP1 and so on. We need GP11 to GP15, so the bits in 11th to 15th positions are set to `1`, denoting they are highlighted.
`gpio_init_mask` will initialize only the pins highlighted in the function's input, which is its first argument `pinMask`.
`gpio_set_dir_masked` will set the IO direction of only the highlighted pins in its first argument `pinMask` to the direction specified in its second argument `dirMask`. In dirMask, `1` denoted output and `0` denotes input.
For this example, the init mask functions will initialize GP11 to GP15, because they are highlighted by `pinMask`. The set dir mask function will focus on GP11 to GP15, because they are highlighted by `pinMask`, take the bits in highlighted locations from `dirMask` and set the direction if they are 1 or 0.
## Line Control
This experiment showcases how digital output pin can control a DC appliance, a fan in this case, using a particular IC. The code stays the same as `Blinky`, only the hardware changes. This example showcases real world use case for digital output.
For this example, a `IRLZ44N` MOSFET is used for controlling a 12v DC fan. A 12v voltage module is used as power source for the fan. Positive of the module is connected to the fan's positive pin. The `Gate` pin of the mosfet is connected to a GPIO pin, `Source` is connected to ground and `Drain` is connected to -ve pin of the fan. It is important to note that ALL THE GROUND MUST BE CONNECTED, forming whats knows as **Common Ground**. [Explanaion provided in Arduino Forum](https://forum.arduino.cc/t/common-ground-and-why-you-need-one/626215). The ground of the 12v module is connected to ground of the pico.