/* STM32 Blue Pill: Light up 10 LEDs sequentially on PB0, PB1, PB3-PB10 */
#include "stm32f1xx_hal.h"
// Define the pins for the 10 LEDs
#define LED1_PIN GPIO_PIN_0 // PB0
#define LED2_PIN GPIO_PIN_1 // PB1
#define LED3_PIN GPIO_PIN_3 // PB3
#define LED4_PIN GPIO_PIN_4 // PB4
#define LED5_PIN GPIO_PIN_5 // PB5
#define LED6_PIN GPIO_PIN_6 // PB6
#define LED7_PIN GPIO_PIN_7 // PB7
#define LED8_PIN GPIO_PIN_8 // PB8
#define LED9_PIN GPIO_PIN_9 // PB9
#define LED10_PIN GPIO_PIN_10 // PB10
#define LED10_PIN GPIO_PIN_10 // PB10
// Define the port for the LEDs
#define LED_GPIO_PORT GPIOB
// Enable the clock for the port
#define LED_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
int main(void)
{
HAL_Init();
// Enable the clock for GPIOB
LED_GPIO_CLK_ENABLE();
// GPIO initialization structure
GPIO_InitTypeDef GPIO_InitStruct;
// Initialize PB0, PB1, PB3-PB10 as output
GPIO_InitStruct.Pin = LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN |
LED5_PIN | LED6_PIN | LED7_PIN | LED8_PIN | LED10_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
/*Configure GPIO pin : PA9 */
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL; // <----- This Option
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
while (1)
{
// Sequentially light up LEDs on GPIOB
// HAL_GPIO_WritePin(LED_GPIO_PORT, LED1_PIN, GPIO_PIN_SET);
// HAL_Delay(100);
// HAL_GPIO_WritePin(LED_GPIO_PORT, LED1_PIN, GPIO_PIN_RESET);
// HAL_Delay(100);
// HAL_GPIO_WritePin(LED_GPIO_PORT, LED9_PIN, GPIO_PIN_SET);
// HAL_Delay(100);
// HAL_GPIO_WritePin(LED_GPIO_PORT, LED9_PIN, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(LED_GPIO_PORT, LED10_PIN, GPIO_PIN_SET);
// HAL_Delay(100);
// HAL_GPIO_WritePin(LED_GPIO_PORT, LED10_PIN, GPIO_PIN_RESET);
// IF Button Is Pressed
if(HAL_GPIO_ReadPin(LED_GPIO_PORT, LED9_PIN)==GPIO_PIN_SET)
{
HAL_GPIO_WritePin(LED_GPIO_PORT, LED10_PIN, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LED_GPIO_PORT, LED10_PIN, GPIO_PIN_RESET);
HAL_Delay(100);
}
// else
// {
// // Else .. Turn LED OFF!
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
// }
}
}
void SysTick_Handler(void)
{
HAL_IncTick();
}
void NMI_Handler(void)
{
}
void HardFault_Handler(void)
{
while (1) {}
}
void MemManage_Handler(void)
{
while (1) {}
}
void BusFault_Handler(void)
{
while (1) {}
}
void UsageFault_Handler(void)
{
while (1) {}
}
void SVC_Handler(void)
{
}
void DebugMon_Handler(void)
{
}
void PendSV_Handler(void)
{
}
Loading
stm32-bluepill
stm32-bluepill