#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stm32f1xx_hal.h>
// Set STM32F103 LED RED (PC13)
#define LED_PORT_C GPIOC
#define LED_PORT_A GPIOA
#define LED_PIN_C13 GPIO_PIN_13
#define LED_PIN_C14 GPIO_PIN_14
#define LED_PIN_C15 GPIO_PIN_15
#define LED_PIN_A0 GPIO_PIN_0
#define LED_PIN_A1 GPIO_PIN_1
#define BTN_PIN_A2 GPIO_PIN_2
#define BTN_PIN_A3 GPIO_PIN_3
#define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOC_CLK_ENABLE
void Error_Handler(void);
void SystemClock_Config(void);
static void MX_USART2_UART_Init(void);
void SysTick_Handler(void)
{
static int currentState = 1;
static int delayCounter = 0;
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C13 | LED_PIN_C14 | LED_PIN_C15, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A0 | LED_PIN_A1, GPIO_PIN_RESET);
if (currentState == 0)
{
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C13, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C14, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C15, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A0, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A1, GPIO_PIN_SET);
}
else if (currentState == 1)
{
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C13, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C15, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A0, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A1, GPIO_PIN_RESET);
}
if (HAL_GPIO_ReadPin(LED_PORT_A, BTN_PIN_A2))
{
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C13, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C15, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A0, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A1, GPIO_PIN_RESET);
}
delayCounter++;
if (delayCounter >= 1000)
{
delayCounter = 0;
currentState++;
if (currentState > 1)
{
currentState = 0;
}
}
}
void initGPIO()
{ GPIO_InitTypeDef GPIO_Config;
GPIO_Config.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_Config.Pull = GPIO_NOPULL;
GPIO_Config.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_Config.Pin = LED_PIN_C13 | LED_PIN_C14 | LED_PIN_C15 | LED_PIN_A0 | LED_PIN_A1;
LED_PORT_CLK_ENABLE();
HAL_GPIO_Init(LED_PORT_C, &GPIO_Config);
HAL_GPIO_Init(LED_PORT_A, &GPIO_Config);
__HAL_RCC_GPIOA_CLK_ENABLE();
}
int main(void)
{
HAL_Init();
SystemClock_Config();
initGPIO();
while (1)
{
/* HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C13, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C13 | LED_PIN_C14 | LED_PIN_C15 , GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A0 | LED_PIN_A1 , GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C14, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C13 | LED_PIN_C14 | LED_PIN_C15 , GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A0 | LED_PIN_A1 , GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C15, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C13 | LED_PIN_C14 | LED_PIN_C15 , GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A0 | LED_PIN_A1 , GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A0, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C13 | LED_PIN_C14 | LED_PIN_C15 , GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A0 | LED_PIN_A1 , GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A1, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LED_PORT_C, LED_PIN_C13 | LED_PIN_C14 | LED_PIN_C15 , GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT_A, LED_PIN_A0 | LED_PIN_A1 , GPIO_PIN_RESET); */
}
return 0;
}
// @brief System Clock Configuration
// @retval None
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/* Initializes the CPU, AHB and APB busses clocks */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/* Initializes the CPU, AHB and APB busses clocks */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
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_0) != HAL_OK) Error_Handler();
}
void Error_Handler(void)
{ /* User can add his own implementation to report the HAL error return state */ }
#ifdef USE_FULL_ASSERT
/* @brief Reports the name of the source file and the source line number
where the assert_param error has occurred.
@param file: pointer to the source file name
@param line: assert_param error line source number
@retval None
*/
void assert_failed(uint8_t file, uint32_t line)
{ / User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) /
}
#endif / USE_FULL_ASSERT */
// @brief USART2 Initialization Function
// @param None
// @retval None
Loading
stm32-bluepill
stm32-bluepill