#include "main.h"
UART_HandleTypeDef huart1;
uint8_t ledState[3] = {0}; // 1 - on, 0 - off
uint8_t flag_key1_press = 1;
uint32_t debounce_time = 0;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
while (1) {
// Green button pressed
if (HAL_GPIO_ReadPin(GPIOC, User_Button_Green_Pin) == GPIO_PIN_SET) {
HAL_GPIO_WritePin(GPIOA, Led_Green_Pin, GPIO_PIN_SET);
ledState[0] = !ledState[0];
HAL_Delay(10);
//while (HAL_GPIO_ReadPin(GPIOC, User_Button_Green_Pin) == GPIO_PIN_SET){}
}
else {
HAL_GPIO_WritePin(GPIOA, Led_Green_Pin, GPIO_PIN_RESET);
}
// Red button pressed
if (HAL_GPIO_ReadPin(GPIOC, User_Button_Red_Pin) == GPIO_PIN_SET) {
HAL_GPIO_WritePin(GPIOA, Led_Red_Pin, ledState[1]);
ledState[1] = !ledState[1];
HAL_Delay(10);
//while (HAL_GPIO_ReadPin(GPIOC, User_Button_Red_Pin) == GPIO_PIN_SET){}
}
else {
HAL_GPIO_WritePin(GPIOA, Led_Red_Pin, GPIO_PIN_RESET);
}
// Blue button pressed
if (HAL_GPIO_ReadPin(GPIOC, User_Button_Blue_Pin) == GPIO_PIN_SET) {
HAL_GPIO_WritePin(GPIOA, Led_Blue_Pin, ledState[2]);
ledState[2] = !ledState[2];
HAL_Delay(10);
//while (HAL_GPIO_ReadPin(GPIOC, User_Button_Blue_Pin) == GPIO_PIN_SET){}
}
else {
HAL_GPIO_WritePin(GPIOA, Led_Blue_Pin, GPIO_PIN_RESET);
}
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(Led_GPIO_Port, Led_Green_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(Led_GPIO_Port, Led_Blue_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(Led_GPIO_Port, Led_Red_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : User_Button_Green_Pin */
GPIO_InitStruct.Pin = User_Button_Green_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(User_Button_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : User_Button_Blue_Pin */
GPIO_InitStruct.Pin = User_Button_Blue_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(User_Button_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : User_Button_Red_Pin */
GPIO_InitStruct.Pin = User_Button_Red_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(User_Button_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : Led_Red_Pin */
GPIO_InitStruct.Pin = Led_Red_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : Led_Blue_Pin */
GPIO_InitStruct.Pin = Led_Blue_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : Led_Green_Pin */
GPIO_InitStruct.Pin = Led_Green_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(Led_GPIO_Port, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
}
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6