1. Light up an LED lamp
Software programming often starts with outputting "Hello World". In embedded development, we often start learning by lighting up a running light. However, due to the limited onboard resources, there is only one green LED lamp on the Nucleo development board. Here we use lighting up this LED lamp as an example to develop the first program. First, the configuration of Cube is shown in the figure below, which is the initial configuration without modification.
Enter the code in while(1)
HAL_GPIO_TogglePin(GPIOA,LD4_Pin);
HAL_Delay(1000);
The green LED light will flash every second. After compiling, you can download the demonstration video as follows
2cb61de732ea018baecfe9bf6b2de912
2. OLED module test
OLED is a commonly used intuitive display module for programming. Here we introduce the use of I2C for OLED configuration on STM32L452RE. First, the configuration of Cube is shown in the figure below.
After generating the bottom layer, we also need to introduce three files: oled.c, oled.h, and oledfont.h. Those who often develop must be familiar with them, so I won't go into details here. I use a four-pin oled screen to connect GND to ground and VCC to 3.3V. Here, I configure the I2C2 channel, so SCL connects to PB10 and SDA connects to PB11. After connecting, I wrote a test code for testing.
int main(void)
{
/* USER CODE BEGIN 1 */
u8 t=0;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_I2C2_Init();
/* USER CODE BEGIN 2 */
OLED_Init();
OLED_ShowString(0,0,"STM32L452",24);
OLED_ShowString(0,24, "0.96' OLED TEST",16);
OLED_ShowString(0,40,"ATOM 2023/9/17",12);
OLED_ShowString(0,52,"ASCII:",12);
OLED_ShowString(64,52,"CODE:",12);
OLED_Refresh_Gram();
t=' ';
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
OLED_ShowChar(36,52,t,12);
OLED_ShowNum(94,52,t,3,12);
OLED_Refresh_Gram();
t++;
if(t>'~')t=' ';
HAL_Delay(500);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
The test results are as follows
It should be noted that the output pins are changed when using different I2C channels. STM32L452RE can configure four I2Cs, of which I2C1 corresponds to PA9 and PA10, I2C2 corresponds to PB10 and PB11, I2C3 corresponds to PC0 and PC1, and I2C3 corresponds to PB6 and PB7. After configuration, find the corresponding pins. If the pins on the board are not fully marked, please refer to the figure below.