Example

 avatar
tuhuuduc
c_cpp
a month ago
2.9 kB
2
Indexable
Never
##GPIO
#include "main.h"

void SystemClock_Config(void);
static void MX_GPIO_Init(void);

int main(void) {
    // HAL initialization
    HAL_Init();
    
    // Configure the system clock
    SystemClock_Config();
    
    // Initialize all configured peripherals
    MX_GPIO_Init();

    // Main loop
    while (1) {
        // Toggle the GPIO pin (PA5 in this case)
        HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

        // Insert delay
        HAL_Delay(500);
    }
}

void SystemClock_Config(void) {
    // Configure the system clock
    // This code is generated by STM32CubeMX and should be left unchanged
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLM = 1;
    RCC_OscInitStruct.PLL.PLLN = 8;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
    RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
    RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
        Error_Handler();
    }
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                  | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
        Error_Handler();
    }
}

static void MX_GPIO_Init(void) {
    // GPIO Ports Clock Enable
    __HAL_RCC_GPIOA_CLK_ENABLE();

    // Configure GPIO pin output level
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);

    // Configure GPIO pin: PA5
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void Error_Handler(void) {
    // Error handler
    while (1) {
    }
}

##


##EEPROM
#include <EEPROM.h>

#define DEVICE_ID_ADDRESS 0 // Address in EEPROM where the device ID is stored

void setup() {
    Serial.begin(9600);

    // Check if the EEPROM contains a valid device ID
    byte deviceID = EEPROM.read(DEVICE_ID_ADDRESS);
    if (deviceID == 0xFF) { // Assuming 0xFF indicates uninitialized EEPROM
        deviceID = 42; // Assign a default device ID
        EEPROM.write(DEVICE_ID_ADDRESS, deviceID);
    }

    Serial.print("Device ID: ");
    Serial.println(deviceID);
}

void loop() {
    // Main code loop
}
Leave a Comment