STM32常见问题解决方案分享
STM32中遇到的问题
最近在用Stm32搭建一个小系统,遇到一些问题,在这里随手记录下来:
错误索引表
序号 | 错误KeyWord | 日期 |
---|---|---|
1 | expected an expression for | 2024/07/02 |
2 | declaration may not appear after executable statement in block | 2024/07/02 |
1. …\HARDWERE\USART2\usart2.c(165): error: #29: expected an expression for (int i = 0; i < length – 1; ++i) { \HARDWERE\USART2\usart2.c(165): error: #20: identifier “i” is undefined for (int i = 0; i < length – 1; ++i) {
void clearBuffer(uint8_t *buffer, uint8_t length) {
for (int i = 0; i < length; ++i) {
buffer[i] = 0;
}
原因:不支持在 for 循环中声明变量
解决方法:函数顶部声明循环变量,然后在 for 循环中使用
int checkChecksum(uint8_t *data, uint8_t length) {
uint8_t sum = 0;
uint8_t i; // 在函数顶部声明变量
for (i = 0; i < length - 1; ++i) {
sum += data[i];
}
return (sum + data[length - 1]) == 0;
}
2. …\SYSTEM\OLED\OLED.c(13): error: #268: declaration may not appear after executable statement in block GPIO_InitTypeDef GPIO_InitStructure; …\SYSTEM\OLED\OLED.c: 0 warnings, 1 error
错误原因为:在老版本 C 语言中,所有变量声明必须出现在函数块的开头,在 C99 标准之后,局部变量可以出现在任何地方。
解决方法:更改为C99 模式
C99模式
stm32搞起来太麻烦了,放弃了。改用matlab做了一个上位机,一下子解决了,此博客暂时终止。2024/7/13
作者:汉嵚