0% found this document useful (0 votes)
21 views2 pages

Port

Uploaded by

einas mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Port

Uploaded by

einas mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

To use a for loop to iterate over pins from different ports, you can create an

array of pointers to the GPIO ports and then iterate over both the ports and pins.
Here's how you can do it:

Example: Iterating Over Pins and Ports

#include <stdint.h>

// Define GPIO base address (example for STM32)


#define GPIO_BASE 0x40020000

// Define GPIO port structure


typedef struct {
volatile uint32_t MODER; // Mode register
volatile uint32_t OTYPER; // Output type register
volatile uint32_t OSPEEDR; // Output speed register
volatile uint32_t PUPDR; // Pull-up/pull-down register
volatile uint32_t IDR; // Input data register
volatile uint32_t ODR; // Output data register
volatile uint32_t BSRR; // Bit set/reset register
volatile uint32_t LCKR; // Configuration lock register
volatile uint32_t AFR[2]; // Alternate function registers
} GPIO_TypeDef;

// Define GPIO port addresses


#define GPIOA ((GPIO_TypeDef *)(GPIO_BASE + 0x000))
#define GPIOB ((GPIO_TypeDef *)(GPIO_BASE + 0x400))
// Add other ports as needed...

// Macros for pin operations


#define GPIO_PIN_SET_MODE_OUTPUT(GPIOx, PIN) \
((GPIOx)->MODER = ((GPIOx)->MODER & ~(0x3 << ((PIN) * 2))) | (0x1 << ((PIN) *
2)))

#define GPIO_PIN_SET(GPIOx, PIN) ((GPIOx)->ODR |= (1 << (PIN)))


#define GPIO_PIN_CLEAR(GPIOx, PIN) ((GPIOx)->ODR &= ~(1 << (PIN)))

// Array of GPIO ports


GPIO_TypeDef *gpio_ports[] = {GPIOA, GPIOB}; // Add more ports as needed

int main() {
const int num_ports = sizeof(gpio_ports) / sizeof(gpio_ports[0]);
const int num_pins = 16; // Assume 16 pins per port

// Configure all pins as outputs


for (int port = 0; port < num_ports; port++) {
for (int pin = 0; pin < num_pins; pin++) {
GPIO_PIN_SET_MODE_OUTPUT(gpio_ports[port], pin);
}
}

// Toggle each pin in sequence


while (1) {
for (int port = 0; port < num_ports; port++) {
for (int pin = 0; pin < num_pins; pin++) {
// Set pin high
GPIO_PIN_SET(gpio_ports[port], pin);
for (volatile int i = 0; i < 100000; i++); // Delay
// Set pin low
GPIO_PIN_CLEAR(gpio_ports[port], pin);
for (volatile int i = 0; i < 100000; i++); // Delay
}
}
}

return 0;
}

Explanation:

1. Array of Ports:

The gpio_ports array stores pointers to the GPIO port structures (e.g., GPIOA,
GPIOB).

You can extend this array to include more ports if needed.

2. Pin Iteration:

The outer loop iterates over the ports, and the inner loop iterates over pins (0 to
15).

3. Dynamic Configuration:

All pins in all specified ports are configured as outputs in the setup loop.

Each pin is toggled in sequence with a delay in the main loop.

4. Customization:

Modify the gpio_ports array or the num_pins variable to match your hardware.

Notes:

Ensure the GPIO peripheral clocks are enabled before accessing GPIO registers
(specific to your microcontroller).

This approach is scalable and works well for controlling multiple pins across
multiple ports.

You might also like