构建嵌入式系统借助AI 智能
AI 智能
UnityMbed 由 AI 驱动,提供从编码到开发板烧录的一站式开发生态,让嵌入式系统开发比以往更智能、更高效。
在同一会话中完成编写、构建和烧录
一个真实的 N32G031 开发会话:配置 GPIOA 时钟、连接 DHT11,一键完成烧录。
UM
N32G031_OLED_DHT11-mainn32g031 已保存到磁盘N32G031_OLED_DHT11-main
>已连接到磁盘 · ~/Projects/N32G031_OLED_DHT11
flash ok · 240 bytes · 0.8s · OpenOCD n32g031文件
文件
src
7misc.c
n32g031_it.c
oled.c
system_n32g031.c
utils.c
drivers
51filemain
2inc
10工具
硬件
2工具链
1云服务
3账户
4高级
2⌘K快速搜索
main.c✕
src/main.c
AI 已开启
1#include "n32g031.h"
2#include "n32g031_gpio.h"
3#include "n32g031_rcc.h"
4#include "core_cm0.h"
5#include "dht11.h"
6#include "oled.h"
7#include "utils.h"
8#include <stdio.h>
9
10// --- 新增:LED 初始化 ---
11void leds_init(void)
12{
13 // 启用 GPIOA 时钟(APB2PCLKEN 的第 2 位)
14 RCC->APB2PCLKEN |= (1U << 2);
15
16 // 将 PA2(黄)、PA3(绿)、PA4(红)配置为输出(01)
17 GPIOA->PMODE &= ~((3U << 4) | (3U << 6) | (3U << 8));
18 GPIOA->PMODE |= ((1U << 4) | (1U << 6) | (1U << 8));
19
20 // 配置为推挽输出(0)
21 GPIOA->POTYPE &= ~((1U << 2) | (1U << 3) | (1U << 4));
22
23 // 配置为无上拉/下拉(00)
24 GPIOA->PUPD &= ~((3U << 4) | (3U << 6) | (3U << 8));
25
26 // 初始关闭所有 LED(向端口位清除寄存器写入 1)
27 GPIOA->PBC = (1U << 2) | (1U << 3) | (1U << 4);
28}
29// ---------------------------------
30
31int main(void)
32{
33 uint8_t current_temp = 0;
34 uint8_t current_humi = 0;
35
36 uint8_t last_temp = 255;
37 uint8_t last_humi = 255;
38 uint8_t was_error = 1;
39
40 uint8_t error_count = 0;
41
42 char buffer[20];
43
44 SystemInit();
45 SystemCoreClockUpdate();
46
47 leds_init(); // 初始化 3 个 LED
48
49 delay_ms(500);
50 OLED_Init();
51 OLED_Clear();
52 DHT11_Init();
53
54 OLED_ShowString(10, 24, "System Ready", 16);
55 delay_ms(1500);
56
57 OLED_Clear();
58
59 while (1)
60 {
61 if (DHT11_Read_Data(¤t_temp, ¤t_humi) == 1)
62 {
63 error_count = 0;
64
65 if (was_error)
66 {
67 OLED_Clear();
68 OLED_ShowString(0, 0, "Temp: ", 8);
69 OLED_ShowString(0, 32, "Humidity: ", 8);
70 was_error = 0;
71 last_temp = 255;
72 last_humi = 255;
73 }
74
75 if (current_temp != last_temp)
76 {
77 sprintf(buffer, "%02d C ", current_temp);
78 OLED_ShowString(0, 12, buffer, 16);
79 OLED_ShowString(24, 12, "o", 8);
80 last_temp = current_temp;
81 }
82
83 if (current_humi != last_humi)
84 {
85 sprintf(buffer, "%02d %% ", current_humi);
86 OLED_ShowString(0, 44, buffer, 16);
87 last_humi = current_humi;
88 }
89
90 // --- 新增:基于湿度的 LED 逻辑 ---
91 if (current_humi < 40)
92 {
93 GPIOA->PBSC = (1U << 3); // 绿灯亮(PA3)
94 GPIOA->PBC = (1U << 2) | (1U << 4); // 黄灯、红灯灭
95 }
96 else if (current_humi >= 40 && current_humi <= 60)
97 {
98 GPIOA->PBSC = (1U << 2); // 黄灯亮(PA2)
99 GPIOA->PBC = (1U << 3) | (1U << 4); // 绿灯、红灯灭
100 }
101 else
102 {
103 GPIOA->PBSC = (1U << 4); // 红灯亮(PA4)
104 GPIOA->PBC = (1U << 2) | (1U << 3); // 绿灯、黄灯灭
105 }
106 // ------------------------------------------
107 }
108 else
109 {
110 error_count++;
111
112 if (error_count >= 3)
113 {
114 if (!was_error)
115 {
116 OLED_Clear();
117 was_error = 1;
118 }
119 OLED_ShowString(0, 0, "Sensor Error! ", 16);
120 OLED_ShowString(0, 24, "Check Wiring. ", 16);
121
122 // 可选:发生错误时关闭所有 LED
123 GPIOA->PBC = (1U << 2) | (1U << 3) | (1U << 4);
124 }
125 }
126
127 delay_ms(2000);
128 }
129}
AI 助手
N32G031 已连接Pro ctx 0.0k
烧录至 N32G031 开发套件
Starter、Pro 和 Learning 套件均采用同款 Cortex-M0+ 开发板。旋转查看套件,然后通过下方表单订购。
Starter Kit 开发板
采用经典的 Uno 兼容外形,可广泛兼容市面上的标准硬件扩展板。
新手友好即插即用一体化设计
Partners
































选择方案
已经订阅?登录以管理套餐 →
AI 模型因方案而异,请查看各方案包含的模型
| 方案对比 | Free | Starter | Pro 最受欢迎 | Advance | Enterprise |
|---|---|---|---|---|---|
| 价格 | 免费 | ฿249/月 | ฿550/月 | ฿990/月 | 定制 |
| 席位 | 1 个席位 | 1 个席位 | 1 个席位 | 1 个席位 | 按组织架构自定义席位 |
| 积分 | 200 积分 | 2,000 积分 | 7,000 积分 | 13,000 积分 | 自定义积分额度 |
| AI 模型访问权限 | Fast | Fast | Fast · Balanced · Smart | Fast · Balanced · Smart · Pro | Fast · Balanced · Smart · Pro |
| 适用场景 | 基础试用 | 日常使用 | 常规开发 | 高级开发 | 可根据组织需求定制 高级支持 |
安装 UnityMbed 桌面 IDE
随时与我们联系
工作时间
周一至周五:09:00–20:00
周末及法定节假日休息




