
本文来源:http://www.lpqz.cn/news/124531
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。
1. 一个STM32开源项目该有的样子搞STM32开发的人多少都有过这种经历从GitHub或者各种论坛扒下来一个项目压缩包解压一看代码是能编译但引脚对不上自己的板子原理图是截图糊得看不清仿真文件压根没有。想改吧不知道从哪下手想复现吧缺东少西。这种“半开源”的项目说实话比不开源还折磨人。我这次要聊的是一个我自己从头到尾整理并开源的STM32项目。它不是什么高精尖的工业级产品而是一个功能完整、资料齐全、可以直接复现的嵌入式小系统。核心交付物就三样完整的工程代码、可编辑的原理图、能跑起来的仿真文件。这三样东西缺一不可因为它们分别对应了嵌入式项目落地的三个关键环节——软件逻辑、硬件设计、前期验证。这个项目适合谁如果你是刚学完STM32基础外设、想找一个完整项目练手的学生它很适合你如果你是需要做毕业设计但不知道怎么把代码、原理图、仿真串起来的同学它也能给你一套可参考的模板甚至如果你是一个已经工作的嵌
入式工程师想看看别人是怎么组织一个开源项目的资料结构的也可以拿来翻翻。我先把话说在前头这篇文章不会只给你甩一个网盘链接就完事。我要做的是把这个项目从设计思路到代码结构、从原理图绘制到仿真配置一层一层拆开讲清楚。你看完之后不仅能复现这个项目更重要的是能掌握一套自己做开源项目的方法论。2. 项目整体设计与方案选型2.1 为什么选STM32F103C8T6作为主控STM32的型号多如牛毛从F0到H7从C0到G4选型的时候很容易挑花眼。这个项目我最终选了STM32F103C8T6原因很实在第一资料多、社区活跃。这颗芯片可以说是STM32入门的“国民芯片”网上能找到的教程、例程、踩坑记录是最丰富的。对于开源项目来说这一点极其重要——用户遇到问题能搜到答案项目的维护成本就低。第二成本低、易获取。最小系统板十几块钱就能买到自己打板的话芯片单价也就几块钱。开源项目如果门槛太高参与的人就少。第三外设够用。这
个项目用到了GPIO、USART、TIM、ADC、I2C这几个常用外设F103C8T6完全覆盖不需要上更高端的型号。第四仿真支持好。这一点经常被忽略。不是所有STM32型号都能在主流仿真软件里找到对应的模型而F103系列在Proteus等仿真平台上的支持是最成熟的。注意如果你手头只有STM32F401或者别的型号代码移植的工作量其实不大主要是时钟配置和部分寄存器差异。但仿真部分可能需要换模型这个后面会细说。2.2 代码、原理图、仿真三件套的协同关系很多人做项目是“先画板子再写代码”或者“先写代码再补原理图”最后仿真干脆不做。这种流程在个人练手时问题不大但一旦要开源给别人用就会暴露出一堆问题代码里的引脚定义和原理图对不上、仿真模型的外围电路和实际板子不一致、别人拿到你的资料根本不知道怎么验证。我的做法是三者同步设计具体流程是这样的先确定功能需求和引脚分配。拿出一张纸或者Excel把所有外设列
出来分配好引脚标注每个引脚的功能和电气特性。根据引脚分配画原理图。原理图不是随便连的每个引脚的外围电路都要有依据——上拉电阻要不要加、去耦电容放几个、晶振负载电容选多大这些都要在原理图阶段确定。代码按照引脚分配来写。所有引脚定义用宏定义集中管理方便修改。仿真验证核心逻辑。在打板之前用仿真软件把关键功能跑一遍确认逻辑没问题。这套流程的好处是任何一环出问题都能快速定位到源头。比如仿真跑不通先检查原理图连接对不对原理图没问题再看代码配置。而不是像无头苍蝇一样到处改。2.3 开源项目资料结构的组织方式一个合格的开源项目目录结构应该是清晰的。我见过太多项目把所有文件往根目录一扔找起来费劲。这个项目的目录结构是这样的STM32-Project/ ├── Docs/ # 文档目录 │ ├── 设计说明.md │ ├── 引脚分配表.md │ └── 常见问题.md ├── Hardware/ # 硬件资
为三层硬件抽象层HAL Layer直接操作寄存器和HAL库函数封装成统一的接口。比如LED控制、按键读取、串口收发都在这一层实现。上层不需要知道用的是哪个GPIO口只需要调用LED_On()、LED_Off()这样的函数。功能模块层Module Layer实现具体的功能逻辑。比如温湿度采集模块、数据显示模块、通信协议解析模块。每个模块是一个独立的.c/.h文件对对外只暴露必要的接口。应用层App Layer主循环和任务调度。这一层负责把各个功能模块串起来实现完整的业务逻辑。这样分层的好处是可移植性和可测试性。比如你要把温湿度传感器从DHT11换成SHT30只需要改功能模块层的实现应用层代码完全不用动。代码里所有引脚定义都集中在board_config.h文件里用宏定义管理// board_config.h #define LED1_PIN GPIO_PIN_13 #define LED1_
PORT GPIOC #define KEY1_PIN GPIO_PIN_0 #define KEY1_PORT GPIOA #define USART1_TX_PIN GPIO_PIN_9 #define USART1_RX_PIN GPIO_PIN_10这样别人拿到代码如果引脚不一样只需要改这一个文件就行不用满工程搜索。3.2 原理图设计那些容易踩坑的地方原理图这部分我想重点讲几个新手最容易出错的地方。这些坑我都踩过有些甚至导致板子打回来直接不能用。第一个坑去耦电容的放置。STM32的每个电源引脚旁边都应该放一个100nF的去耦电容而且这个电容要尽量靠近引脚。我见过有人把所有去耦电容集中放在板子一角结果芯片工作不稳定偶尔复位。原理图上看不出问题但PCB布局的时候就暴露了。所以画原理图的时候就要有意识地把去耦电容和对应的电源引脚画在一起。第二个坑晶振负载电容的计算。STM32外部晶振的负
载电容不是随便选一个20pF就完事。计算公式是CL (C1 × C2) / (C1 C2) Cstray其中CL是晶振手册上标注的负载电容Cstray是PCB走线的寄生电容一般取3~5pF。假设晶振的CL是12pFCstray取4pF那么C1和C2应该选16pF左右。如果选错了晶振可能起振困难或者频率偏移。第三个坑BOOT引脚的处理。STM32的BOOT0和BOOT1引脚决定了启动模式。如果你用串口下载程序BOOT0需要拉高正常运行的时候BOOT0要拉低。很多人在原理图上把BOOT0直接接地结果想用串口下载的时候发现进不去。正确的做法是加一个跳线帽或者拨码开关方便切换。第四个坑复位电路。STM32的NRST引脚内部有上拉电阻但为了可靠复位建议外部还是加一个10kΩ上拉电阻和一个100nF电容到地。有些开发板省掉了这个电容结果按键复位的时候偶尔不灵。原理图我用的是嘉立创EDA绘制的原因
很简单免费、元件库全、可以直接导出BOM和PCB。当然你也可以用Altium Designer或者KiCad原理是相通的。3.3 仿真配置Proteus还是其他仿真工具的选择上我主要用Proteus。原因有三个一是它对STM32F103系列的支持比较成熟二是可以直接加载.hex文件运行和实际烧录的效果接近三是元件库里有常用的外设模型比如LCD1602、DHT11、按键、LED等。不过Proteus也不是万能的。它的时序仿真精度有限对于一些对时序要求严格的应用比如高速SPI通信、精确的PWM输出仿真结果只能作为参考不能完全替代实际测试。另外Proteus的STM32模型不支持某些高级外设比如USB、CAN、以太网等。如果你要做更精确的仿真可以考虑用STM32CubeIDE自带的仿真功能或者用Keil MDK的软件仿真。Keil的软件仿真可以模拟寄存器级别的行为适合调试底层驱动代码。但它没有外
围电路的模型只能验证代码逻辑不能验证硬件连接。我的建议是Proteus用来验证整体功能逻辑Keil软件仿真用来调试底层驱动两者结合使用。实际打板之前再用示波器和逻辑分析仪验证关键信号。4. 实操过程与核心环节实现4.1 从零搭建工程CubeMX配置要点如果你是从零开始第一步是用STM32CubeMX生成工程框架。这个工具虽然有人吐槽它生成的代码臃肿但不可否认它大大降低了初始化配置的门槛。具体配置步骤如下第一步选择芯片型号。在CubeMX里搜索STM32F103C8T6选中后开始配置。第二步配置时钟源。在RCC配置里把HSE高速外部时钟设为Crystal/Ceramic Resonator。这样CubeMX会自动配置外部晶振相关的寄存器。第三步配置时钟树。F103C8T6的最高主频是72MHz。输入晶振频率一般填8MHz然后通过PLL倍频到72MHz。具体路径是HSE 8MHz → PLL
×9 → 72MHz → AHB Prescaler /1 → APB1 Prescaler /2 (36MHz) → APB2 Prescaler /1 (72MHz)。第四步配置外设。根据项目需求依次配置GPIO、USART、TIM、ADC、I2C等。每个外设的配置界面里都有详细的参数设置比如USART的波特率、数据位、停止位、校验位等。第五步配置中断优先级。如果用到中断需要在NVIC配置里设置优先级。STM32F103的中断优先级分为抢占优先级和响应优先级数值越小优先级越高。一般来说对实时性要求高的中断比如串口接收设置更高的抢占优先级。第六步生成代码。在Project Manager里设置工程名称、路径、工具链MDK-ARM或STM32CubeIDE然后点击Generate Code。实操心得CubeMX生成的代码里用户代码要写在/* USER CODE BEGIN */和/* US
ER CODE END */之间这样重新生成代码的时候不会被覆盖。这个习惯一定要养成否则改一次配置就丢一次代码。4.2 关键功能模块的代码实现这个项目的核心功能包括LED状态指示、按键输入检测、串口数据收发、定时器PWM输出、ADC电压采集、I2C温湿度读取。我挑几个有代表性的讲一下实现细节。按键消抖是嵌入式开发里的经典问题。机械按键在按下和松开的瞬间会产生抖动如果不处理一次按下可能被识别成多次。常见的消抖方法有两种硬件消抖加RC电路和软件消抖延时采样。这个项目用的是软件消抖在定时器中断里每10ms采样一次按键状态连续3次采样结果一致才认为是有效按键。// 按键扫描函数在10ms定时器中断里调用 void Key_Scan(void) { static uint8_t key_cnt 0; uint8_t key_now HAL_GPIO_ReadPin(KEY1_PORT, KEY1
_PIN); if (key_now GPIO_PIN_RESET) { if (key_cnt 3) key_cnt; if (key_cnt 3) { key_pressed 1; // 确认按下 } } else { key_cnt 0; key_pressed 0; } }串口数据收发用的是中断方式而不是轮询。轮询方式会阻塞主循环影响其他任务的执行。中断方式下每收到一个字节就触发一次中断把数据存到缓冲区里主循环再慢慢处理。// 串口接收中断回调函数 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart-Instance USART1) { // 把接收到的数据存入环形缓冲区 RingBuffer_Write(rx_buffer, rx_byte); // 重新开启接收中断 HAL_UAR
T_Receive_IT(huart1, rx_byte, 1); } }PWM输出用的是定时器TIM3的通道1和通道2。PWM频率计算公式是PWM频率 定时器时钟 / ((Prescaler 1) × (Period 1))假设定时器时钟是72MHzPrescaler设为71Period设为999那么PWM频率 72000000 / (72 × 1000) 1000Hz。占空比通过修改比较寄存器CCR的值来调节CCR从0到Period对应占空比从0%到100%。4.3 原理图绘制实操以最小系统为例画原理图这件事说难不难说简单也不简单。关键是养成规范的习惯。我以最小系统部分为例讲一下绘制流程。第一步放置主控芯片。在嘉立创EDA的元件库里搜索STM32F103C8T6放置到画布上。注意检查引脚编号和封装是否对应有些库里的符号引脚排列和实际芯片不一致。第二步连接电源和地。VDD引脚接
3.3VVSS接GND。每个VDD引脚旁边放一个100nF去耦电容。VDDA和VSSA是模拟电源也要接3.3V和GND并且额外加一个1μF的电容滤波。第三步连接晶振电路。8MHz晶振接在PD0和PD1OSC_IN和OSC_OUT之间两端各接一个16pF电容到地。注意晶振的外壳要接地减少干扰。第四步连接复位电路。NRST引脚接一个10kΩ上拉电阻到3.3V同时接一个100nF电容到地再并联一个复位按键。第五步连接BOOT引脚。BOOT0通过一个10kΩ电阻接地同时引出一个跳线帽需要串口下载时可以短接到3.3V。BOOT1直接接地。第六步引出调试接口。SWD接口需要连接SWDIOPA13和SWCLKPA14再加上3.3V和GND。建议用标准的4针排针间距2.54mm。画完之后一定要用ERC电气规则检查功能检查一遍看看有没有未连接的引脚、电源冲突等问题。4.4 仿真文件配置与运行Proteus仿

本文来源:http://www.lpqz.cn/news/124531
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。
1. What an STM32 open-source project should look like. Anyone who has worked with STM32 has had a version of this experience: you download a project archive from GitHub or some forum, unzip it, and find that the code compiles but the pins don't match your board, the schematic is a blurry screenshot, and there is no simulation file at all. You want to modify it but don't know where to start; you want to reproduce it but something is always missing. To be honest, this kind of 'half-open-source' project is more torturous than no open source at all. What I want to talk about this time is an STM32 project I organized and open-sourced from start to finish. It is not some cutting-edge industrial-grade product, but a fully featured, well-documented, directly reproducible small embedded system. There are exactly three core deliverables: complete project code, an editable schematic, and a simulation file that actually runs. None of the three can be missing, because they correspond to the three key stages of bringing an embedded project to life — software logic, hardware design, and early verification. Who is this project for? If you are a student who has just learned the basics of STM32 peripherals and want a complete project to practice on, it's a great fit; if you are working on a graduation design but don't know how to stitch together code, schematic, and simulation, it can give you a reference template; and even if you are an already-working embedded
engineer who wants to see how someone else organizes an open-source project's documentation, you can take a look as well. Let me be clear up front: this article won't just toss you a cloud-drive link and call it a day. What I want to do is take this project apart layer by layer — from design thinking to code structure, from schematic drawing to simulation configuration — and explain it all clearly. When you finish reading, you will not only be able to reproduce this project; more importantly, you will have mastered a methodology for building open-source projects of your own. 2. Overall project design and solution selection. 2.1 Why choose the STM32F103C8T6 as the MCU. There are countless STM32 models, from F0 to H7 and from C0 to G4, and it's easy to be dazzled when selecting one. For this project I finally chose the STM32F103C8T6 for very practical reasons. First, abundant documentation and an active community. This chip can be called the 'people's chip' for STM32 entry; the tutorials, example code, and troubleshooting records you can find online are the richest of all. For an open-source project this matters enormously: when users hit a problem they can search and find an answer, and the maintenance burden stays low. Second, low cost and easy availability. A minimum-system board costs just over ten yuan, and if you make your own board the chip itself is only a few yuan. An open-source project with a high barrier to entry attracts fewer participants. Third, sufficient peripherals. This
project uses GPIO, USART, TIM, ADC, and I2C — common peripherals that the F103C8T6 covers completely, with no need for a higher-end model. Fourth, good simulation support. This point is often overlooked. Not every STM32 model has a corresponding model in mainstream simulation software, while the F103 series enjoys the most mature support on platforms such as Proteus. Note that if you only have an STM32F401 or another model on hand, porting the code is not much work — it's mainly clock configuration and a few register differences. The simulation part, though, may require a different model; I'll go into detail later. 2.2 How the code, schematic, and simulation trio work together. Many people do a project by 'drawing the board first, then writing the code', or 'writing the code first, then adding the schematic', and skip the simulation altogether. That flow is fine for personal practice, but once you open-source it for others to use, a whole pile of problems surfaces: pin definitions in the code don't match the schematic, the simulation model's peripheral circuits don't match the real board, and others have no idea how to verify your materials. My approach is to design all three in sync, with a concrete flow like this. First determine the functional requirements and pin allocation. Take a sheet of paper or open Excel and list all the peripherals
out, assign the pins, and annotate each pin's function and electrical characteristics. Draw the schematic based on the pin allocation. A schematic isn't something you just wire up arbitrarily: the peripheral circuit for every pin needs a rationale — whether to add a pull-up resistor, how many decoupling capacitors to place, how large the crystal load capacitors should be — all of that must be decided at the schematic stage. Write the code according to the pin allocation. Use macros to centralize all pin definitions for easy modification. Verify the core logic in simulation. Before fabricating the board, run the key functions in simulation software to confirm the logic is sound. The benefit of this flow is that if any link goes wrong you can quickly trace it back to the source. For example, if the simulation doesn't work, first check whether the schematic connections are right; if the schematic is fine, then look at the code configuration — rather than groping around changing things at random. 2.3 How to organize an open-source project's documentation structure. A decent open-source project should have a clear directory structure. I've seen far too many projects dump all their files in the root directory, making them a pain to navigate. This project's directory structure is as follows: STM32-Project/ ├── Docs/ # documentation directory │ ├── 设计说明.md (design notes) │ ├── 引脚分配表.md (pin assignment table) │ └── 常见问题.md (FAQ) ├── Hardware/ # Hardware resources
The code is organized into three layers. HAL Layer (Hardware Abstraction Layer) directly manipulates registers and wraps HAL library functions into a unified interface — LED control, key reading, and serial transmit/receive are all implemented here. Upper layers don't need to know which GPIO port is used; they just call functions like LED_On() and LED_Off(). Module Layer implements specific functional logic — for example, a temperature/humidity acquisition module, a data display module, and a communication protocol parsing module. Each module is an independent .c/.h file pair that exposes only the necessary interfaces. App Layer handles the main loop and task scheduling; it stitches the functional modules together into complete business logic. The benefit of this layering is portability and testability. For example, to swap the temperature/humidity sensor from a DHT11 to an SHT30 you only need to change the Module Layer implementation — the App Layer code doesn't change at all. All pin definitions in the code are centralized in board_config.h using macros: // board_config.h #define LED1_PIN GPIO_PIN_13 #define LED1_
PORT GPIOC #define KEY1_PIN GPIO_PIN_0 #define KEY1_PORT GPIOA #define USART1_TX_PIN GPIO_PIN_9 #define USART1_RX_PIN GPIO_PIN_10. That way, when someone else gets the code and their pins differ, they only need to change this one file instead of searching through the whole project. 3.2 Pitfalls in schematic design. For the schematic I want to focus on several mistakes that beginners are most likely to make. I've fallen into every one of these pits myself, and some even resulted in boards that were unusable straight out of fabrication. Pitfall one: placement of decoupling capacitors. Every power pin of an STM32 should have a 100 nF decoupling capacitor next to it, placed as close to the pin as possible. I've seen people cluster all the decoupling capacitors in one corner of the board, resulting in an unstable chip that resets occasionally. You can't see the problem in the schematic, but it shows up during PCB layout. So while drawing the schematic, deliberately place each decoupling capacitor next to its corresponding power pin. Pitfall two: calculating the crystal load capacitance. The load capacitance of the STM32's external crystal
isn't something you just pick as 20 pF and call it a day. The formula is CL = (C1 × C2) / (C1 + C2) + Cstray, where CL is the load capacitance specified in the crystal's datasheet and Cstray is the stray capacitance of the PCB traces, usually taken as 3–5 pF. Assuming the crystal's CL is 12 pF and Cstray is 4 pF, then C1 and C2 should be around 16 pF. Get it wrong and the crystal may struggle to start oscillating or drift in frequency. Pitfall three: handling the BOOT pins. The STM32's BOOT0 and BOOT1 pins determine the boot mode. If you download programs over serial, BOOT0 must be pulled high; during normal operation BOOT0 must be pulled low. Many people tie BOOT0 directly to ground in the schematic and then find they can't get into serial download mode when they need it. The correct approach is to add a jumper cap or DIP switch for easy switching. Pitfall four: the reset circuit. The STM32's NRST pin has an internal pull-up, but for reliable reset it is still advisable to add an external 10 kΩ pull-up resistor and a 100 nF capacitor to ground. Some development boards omit that capacitor, and then button resets occasionally fail. I drew the schematic in JLCEDA (嘉立创EDA) for a very simple
reason: it's free, has a complete component library, and can export BOM and PCB data directly. Of course, you can also use Altium Designer or KiCad — the principles are the same. 3.3 Simulation configuration. For simulation tools I mainly use Proteus, for three reasons: first, its support for the STM32F103 series is fairly mature; second, you can directly load a .hex file and run it, which is close to the effect of actual flashing; third, its component library includes common peripheral models such as LCD1602, DHT11, keys, and LEDs. Proteus is not omnipotent, though. Its timing simulation accuracy is limited; for timing-critical applications such as high-speed SPI communication or precise PWM output, the simulation results can only serve as a reference and cannot fully replace real testing. In addition, Proteus's STM32 model does not support certain advanced peripherals such as USB, CAN, and Ethernet. If you need more accurate simulation, you can consider the simulation features built into STM32CubeIDE or the software simulation in Keil MDK. Keil's software simulation can model register-level behavior, making it suitable for debugging low-level driver code. But it has no models for peripheral
circuits — it can only verify code logic, not hardware connections. My recommendation is to use Proteus to verify overall functional logic and Keil software simulation to debug low-level drivers, combining the two. Before actually fabricating the board, verify the key signals with an oscilloscope and a logic analyzer. 4. Hands-on process and implementation of the core stages. 4.1 Building the project from scratch: key CubeMX configuration points. If you're starting from zero, the first step is to generate the project framework with STM32CubeMX. Although some people complain that the code it generates is bloated, there's no denying that it greatly lowers the barrier to initialization configuration. The specific steps are as follows. Step one, select the chip model: search for STM32F103C8T6 in CubeMX, select it, and begin configuring. Step two, configure the clock source: in the RCC configuration, set HSE (high-speed external clock) to Crystal/Ceramic Resonator, and CubeMX will automatically configure the registers related to the external crystal. Step three, configure the clock tree: the F103C8T6's maximum core frequency is 72 MHz. Enter 8 MHz as the input crystal frequency and multiply it up to 72 MHz through the PLL. The concrete path is HSE 8 MHz → PLL
×9 → 72 MHz → AHB Prescaler /1 → APB1 Prescaler /2 (36 MHz) → APB2 Prescaler /1 (72 MHz). Step four, configure the peripherals: configure GPIO, USART, TIM, ADC, I2C, and so on in order according to the project's needs. Each peripheral's configuration screen has detailed parameters — for USART, baud rate, data bits, stop bits, parity, and so on. Step five, configure interrupt priorities: if you use interrupts, set their priorities in the NVIC configuration. STM32F103 interrupt priorities are divided into preemption priority and subpriority, with smaller numbers meaning higher priority. Generally, interrupts with high real-time requirements, such as serial reception, get a higher preemption priority. Step six, generate the code: in Project Manager set the project name, path, and toolchain — MDK-ARM or STM32CubeIDE — then click Generate Code. A practical tip: in CubeMX-generated code, write user code between /* USER CODE BEGIN */ and /* US
ER CODE END */ so it won't be overwritten when the code is regenerated. You must get into this habit, or you'll lose your code every time you change a configuration. 4.2 Code implementation of key functional modules. This project's core functions include LED status indication, key input detection, serial data transmit/receive, timer PWM output, ADC voltage acquisition, and I2C temperature/humidity reading. I'll pick a few representative ones and go into implementation detail. Key debouncing is a classic problem in embedded development. A mechanical key bounces at the instant of press and release; without handling it, a single press may be recognized as multiple presses. There are two common debouncing methods: hardware debouncing with an RC circuit, and software debouncing with delayed sampling. This project uses software debouncing, sampling the key state every 10 ms in a timer interrupt and treating it as a valid press only after three consecutive consistent samples. // Key scan function, called in the 10 ms timer interrupt void Key_Scan(void) { static uint8_t key_cnt 0; uint8_t key_now HAL_GPIO_ReadPin(KEY1_PORT, KEY1
_PIN); if (key_now GPIO_PIN_RESET) { if (key_cnt 3) key_cnt; if (key_cnt 3) { key_pressed 1; // press confirmed } } else { key_cnt 0; key_pressed 0; } } Serial transmit/receive uses interrupts rather than polling. Polling blocks the main loop and affects other tasks. With interrupts, each received byte triggers an interrupt that stores the data in a buffer, and the main loop processes it at its leisure. // Serial receive interrupt callback void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart-Instance USART1) { // store the received data in the ring buffer RingBuffer_Write(rx_buffer, rx_byte); // re-enable the receive interrupt HAL_UAR
T_Receive_IT(huart1, rx_byte, 1); } } PWM output uses channels 1 and 2 of timer TIM3. The PWM frequency formula is: PWM frequency = timer clock / ((Prescaler + 1) × (Period + 1)). Assuming the timer clock is 72 MHz, Prescaler is 71, and Period is 999, then the PWM frequency is 72,000,000 / (72 × 1000) = 1000 Hz. The duty cycle is adjusted by changing the value of the compare register CCR: CCR from 0 to Period corresponds to a duty cycle from 0% to 100%. 4.3 Drawing the schematic in practice: the minimum system as an example. Drawing a schematic is neither hard nor easy — the key is to develop disciplined habits. I'll use the minimum-system part as an example to walk through the drawing process. Step one, place the main chip: search for STM32F103C8T6 in JLCEDA's component library and place it on the canvas. Check that the pin numbers match the footprint — some libraries have symbol pin arrangements that don't match the actual chip. Step two, connect power and ground: connect the VDD pins to
3.3 V and VSS to GND. Place a 100 nF decoupling capacitor next to every VDD pin. VDDA and VSSA are the analog supply and must also connect to 3.3 V and GND, with an additional 1 μF capacitor for filtering. Step three, connect the crystal circuit: the 8 MHz crystal goes between PD0 and PD1 (OSC_IN and OSC_OUT), with a 16 pF capacitor to ground at each end. Ground the crystal's metal case to reduce interference. Step four, connect the reset circuit: the NRST pin gets a 10 kΩ pull-up resistor to 3.3 V, a 100 nF capacitor to ground, and a reset button in parallel. Step five, connect the BOOT pins: BOOT0 goes to ground through a 10 kΩ resistor and also breaks out to a jumper cap so it can be shorted to 3.3 V when serial download is needed. BOOT1 ties directly to ground. Step six, break out the debug interface: the SWD interface needs SWDIO (PA13) and SWCLK (PA14), plus 3.3 V and GND. A standard 4-pin header with 2.54 mm pitch is recommended. After drawing, always run the ERC (electrical rule check) to look for unconnected pins, power conflicts, and similar issues. 4.4 Simulation file configuration and running: Proteus sim-