笔者跟着B站铁头山羊视频学习 STM32-HAL库 开发教程。下面对HAL库有关 SPI 课程知识和应用做一个梳理。
无流可省
1、SPI 总线基本原理
SPI总线(Series Peripheral Interface)串行外设接口,适用于高速、双向数据传输场景。










2、示例——按钮实验
实验现象:按键松开,翻转LED,在按键按下期间不翻转。
void main()
{
uint8_t pre = 1; // 设置两个标志来记录按钮按下的电平状态
uint8_t cur = 1; //
uint8_t led_state = 0; // 记录LED的状态
while (1)
{
pre = cur; // 在按下 或者 松开的长时间内将两个状态变为一致
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) == GPIO_PIN_SET) // 判断按键是否有电平变化
{
cur=0;
}
else
{
cur=1;
}
if(pre!=cur) // 说明按键在瞬变的时刻
{
if(cur==0){} // 按键被按下的瞬间
else // 按键被松开的瞬间
{
if(led_state == 0) // 如果LED为灭 将其点亮 并重新设置状态
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
led_state = 1;
}
else // 如果LED为亮 将其熄灭 并重新设置状态
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
led_state = 0;
}
}
}
}