笔者跟着B站铁头山羊视频学习 STM32-HAL库 开发教程。有一说一,这个教程自诩为“最佳教程,没有之一~”,确实有点东西。像我这种看视频想睡觉的入门小白来说,感觉捡到宝了。下面对这些课程的应用做一个梳理。
省流:
HAL_Delay(500);
延时函数:延时500ms 。
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_RESET);
设置IO引脚:将GPIOG的G6引脚置低电平GPIO_PIN_RESET。
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13) == GPIO_PIN_RESET)
读取IO引脚:判断 GPIOC的C13引脚 是否为 低电平GPIO_PIN_RESET(GPIO_PIN_SET)。
1、电源MOS命名
2、GPIO口的通用和复用


IO复用就是同一个IO引脚具备多个不同的功能,它既可以被用户直接编程,也可以给芯片的其他模块使用。

3、GPIO工作模式

3.1 推挽输出和开漏输出

推挽输出模式下,一对MOS管交替导通,对外输出低电压或高电压。
开漏输出模式下,PMOS保持关断,可以对外输出低电压或高阻抗。
3.2 最大输出速度选择

3.3 输入模式


4、示例
4.1 LED灯闪烁
(1)LED灯输出配置

PC13 阳极接高电压(Vdd),需要使用开漏输出,写0输出低电压,写1输出高阻态。
PA9 阴极接低电压(Vss),需要使用推挽输出,写0输出低电压,写1输出高电压。

(2)编写程序

int main(void)
{
/* USER CODE BEGIN 1 */
/* 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();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET); // 点亮LED,C13置0,开漏输出低电压
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); // 点亮LED,C9置1,推挽输出高电压
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET); // 熄灭LED,C13置1,开漏输出高阻态
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); // 熄灭LED,C9置0,推挽输出低电压
HAL_Delay(500);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
4.2 按键控制LED灯亮灭
(1)按键输入配置


(2)程序

int main(void)
{
/* USER CODE BEGIN 1 */
/* 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();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13) == GPIO_PIN_RESET) // 判断 按键输入引脚C13 是否为低电平
{
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_RESET); // 置 LED输出引脚G6 为低电平
}
else if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13) == GPIO_PIN_SET)
{
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_SET);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}