写一个STM32单片机控制8个LED灯的包含属性、对象、状态、控制封装结构。要给出最优的封装的全部代码,而且只能使用STM32标准库,不能使用HAL库或者LL库
时间: 2024-05-12 10:18:47 浏览: 29
属性:
- 灯的数量:8
- 灯的引脚:PA0~PA7
对象:
- LED灯
状态:
- 开/关状态
控制封装结构:
```c
#include "stm32f10x.h"
// 灯数量
#define LED_NUM 8
// 灯引脚
#define LED_PIN_0 GPIO_Pin_0
#define LED_PIN_1 GPIO_Pin_1
#define LED_PIN_2 GPIO_Pin_2
#define LED_PIN_3 GPIO_Pin_3
#define LED_PIN_4 GPIO_Pin_4
#define LED_PIN_5 GPIO_Pin_5
#define LED_PIN_6 GPIO_Pin_6
#define LED_PIN_7 GPIO_Pin_7
// 灯端口
#define LED_PORT GPIOA
// 灯状态
typedef enum {
LED_OFF = 0,
LED_ON = 1
} LEDState;
// LED灯对象
typedef struct {
GPIO_TypeDef* port;
uint16_t pin;
LEDState state;
} LED;
// 初始化LED灯
void LED_Init(LED* led) {
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = led->pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(led->port, &GPIO_InitStructure);
LED_Off(led);
}
// 打开LED灯
void LED_On(LED* led) {
led->state = LED_ON;
GPIO_SetBits(led->port, led->pin);
}
// 关闭LED灯
void LED_Off(LED* led) {
led->state = LED_OFF;
GPIO_ResetBits(led->port, led->pin);
}
// 反转LED灯状态
void LED_Toggle(LED* led) {
if (led->state == LED_OFF) {
LED_On(led);
} else {
LED_Off(led);
}
}
// 初始化所有LED灯
void LEDs_Init(LED leds[]) {
for (int i = 0; i < LED_NUM; i++) {
LED_Init(&leds[i]);
}
}
// 打开所有LED灯
void LEDs_On(LED leds[]) {
for (int i = 0; i < LED_NUM; i++) {
LED_On(&leds[i]);
}
}
// 关闭所有LED灯
void LEDs_Off(LED leds[]) {
for (int i = 0; i < LED_NUM; i++) {
LED_Off(&leds[i]);
}
}
// 反转所有LED灯状态
void LEDs_Toggle(LED leds[]) {
for (int i = 0; i < LED_NUM; i++) {
LED_Toggle(&leds[i]);
}
}
int main(void) {
LED leds[LED_NUM] = {
{ .port = LED_PORT, .pin = LED_PIN_0 },
{ .port = LED_PORT, .pin = LED_PIN_1 },
{ .port = LED_PORT, .pin = LED_PIN_2 },
{ .port = LED_PORT, .pin = LED_PIN_3 },
{ .port = LED_PORT, .pin = LED_PIN_4 },
{ .port = LED_PORT, .pin = LED_PIN_5 },
{ .port = LED_PORT, .pin = LED_PIN_6 },
{ .port = LED_PORT, .pin = LED_PIN_7 }
};
LEDs_Init(leds);
while (1) {
LEDs_Toggle(leds);
for (int i = 0; i < 1000000; i++);
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)