本帖最后由 南来之风 于 2023-7-31 12:57 编辑
本文是系列测评的第二篇文章,从简到繁,一步一步的使用GPIO。
开发板上面有三颗供用户使用的LED灯,我们关注第三颗LD3:
在硬件上,有一个地方需要特别注意:SW1,它必须处于3.3V的位置,否则整个开发板的核心将失去供电!
正常上电后,检查电脑设备管理器中,是否有ST LINK端口,如下所示COM12即为开发板对应的端口。
核心代码:
- /* 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_ICACHE_Init();
- /* USER CODE BEGIN 2 */
- /* -1- Enable GPIO Clock (to be able to program the configuration registers) */
- LD1_GPIO_CLK_ENABLE();
- /* -2- Configure IO in output push-pull mode to drive external LEDs */
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Pin = LD3_PIN;
- HAL_GPIO_Init(LD3_GPIO_PORT, &GPIO_InitStruct);
-
- uint32_t i,j=200;
-
- HAL_GPIO_WritePin(LD1_GPIO_PORT, LD1_PIN,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LD2_GPIO_PORT, LD2_PIN,GPIO_PIN_SET);
之后在程序中:
- while (1)
- {
- HAL_GPIO_WritePin(LD3_GPIO_PORT, LD3_PIN,GPIO_PIN_RESET);
- delay_us(1000);
-
- HAL_GPIO_WritePin(LD3_GPIO_PORT, LD3_PIN,GPIO_PIN_SET);
- delay_us(1000);
- }
程序运行的时候发现,自己写的延迟函数被编译器给优化掉了!
解决办法:
|