Files
2026-10-19 15:47:18 +08:00

91 lines
1.7 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "led.h"
// 初始化 LED
void led_init()
{
// 定义一个 GPIO 初始化啊的结构体
GPIO_InitTypeDef GPIO_InitStruct;
// 初始化时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOA, ENABLE);
// 将 PC8 和 PC9 设置成上拉输入 1000
// up 上拉
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
// 针脚
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
// 输出速度
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct);
}
// 让编号为 nu 的 LED 点亮, nu 为0,1,2 代表 LED0, LED1,LED2
void led_on(int nu)
{
switch (nu)
{
case 0:
GPIOc->ODR |= (0x1 << 1);
break;
case 1:
GPIOc->ODR |= (0x1 << 2);
break;
case 2:
GPIOc->ODR |= (0x1 << 3);
break;
default:
break;
}
}
// 让编号为 nu 的 LED 熄灭
void led_off(int nu)
{
switch (nu)
{
case 0:
GPIOc->ODR &= ~(0x1 << 1);
break;
case 1:
GPIOc->ODR &= ~(0x1 << 2);
break;
case 2:
GPIOc->ODR &= ~(0x1 << 3);
break;
default:
break;
}
}
// 获取编号为 nu 的 LED 的状态值
int led_get_status(int nu)
{
uint32_t ret;
if (nu >= 0 && nu <= 2)
{
ret = GPIOc->ODR & (0x1 << (1 + nu));
return ret != 0 ? 1:0;
}
return 0;
}
// 设置编号为 nu 的 LED 的状态值 status, 0:熄灭, 1:点亮
void led_set_status(int nu, int status)
{
if (status)
{
led_on(nu);
} else {
led_off(nu);
}
}