Here's an example code that utilizes the GPIOX_IDR and ODR registers to control the built-in LED based on the state of the blue built-in button. This code assumes you're using a microcontroller with GPIOX_IDR and ODR registers. You need to replace 'GPIOX' with the appropriate GPIO port name (e.g., GPIOA, GPIOB, etc.) according to your microcontroller.

#include <stdint.h>

// Function to initialize GPIO pins
void gpio_init() {
    // Enable clock for GPIOX (replace X with the appropriate port number)
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOXEN;

    // Set the built-in LED pin as output (replace X and PIN with the appropriate port and pin numbers)
    GPIOX->MODER |= GPIO_MODER_MODER_PIN << (PIN * 2);
}

// Function to read the state of the blue built-in button
uint8_t read_button_state() {
    // Read the state of the blue button pin (replace X and PIN with the appropriate port and pin numbers)
    return (GPIOX->IDR >> PIN) & 0x01;
}

// Function to turn on the built-in LED
void turn_on_led() {
    // Set the built-in LED pin high (replace X and PIN with the appropriate port and pin numbers)
    GPIOX->ODR |= GPIO_ODR_ODR_PIN << PIN;
}

// Function to turn off the built-in LED
void turn_off_led() {
    // Set the built-in LED pin low (replace X and PIN with the appropriate port and pin numbers)
    GPIOX->ODR &= ~(GPIO_ODR_ODR_PIN << PIN);
}

int main() {
    // Initialize GPIO pins
    gpio_init();

    while (1) {
        // Read the state of the blue button
        uint8_t button_state = read_button_state();

        // If the button is pressed, turn on the LED; otherwise, turn it off
        if (button_state) {
            turn_on_led();
        } else {
            turn_off_led();
        }
    }
    return 0;
}

Remember, this code is a basic example. You might need to modify it based on your specific microcontroller and pin assignments.

Control Built-in LED with GPIOX_IDR and ODR: Blue Button Example

原文地址: https://www.cveoy.top/t/topic/pbD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录