while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET) { message[i++] = USART_ReceiveData(USART1); } // ???????? if(strstr(message, "LIGHT ON") != NULL) { GPIO_SetBits(GPIOC, GPIO_Pin_13); } else if(strstr(message, "LIGHT OFF") != NULL) { GPIO_ResetBits(GPIOC, GPIO_Pin_13); } else if(strstr(message, "TEMPERATURE") != NULL) { // ?????? float temperature = 0; // TODO: ?????? // ?????? char str[50]; sprintf(str, "Temperature: %.2f", temperature);这段代码的错误如下:main.c(12): error: #268: declaration may not appear after executable statement in block uint8_t i = 0; main.c(17): error: #268: declaration may not appear after executable statement in block char message[100]; main.c(20): error: #20: identifier "NULL" is undefined if(strstr(message, "LIGHT ON") != NULL),请依次修改他们
时间: 2024-02-14 07:16:39 浏览: 110
这些错误与之前的类似,都是关于变量和语句的声明位置问题,以及标准库头文件的引用问题。下面是修改后的代码:
```
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "stm32f10x.h"
int main() {
uint8_t i = 0;
char message[100] = {0};
float temperature = 0.0f;
char str[50] = {0};
// ...
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET) {
message[i++] = USART_ReceiveData(USART1);
}
// ...
if(strstr(message, "LIGHT ON") != NULL) {
GPIO_SetBits(GPIOC, GPIO_Pin_13);
}
else if(strstr(message, "LIGHT OFF") != NULL) {
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
}
else if(strstr(message, "TEMPERATURE") != NULL) {
// TODO: 计算温度值
sprintf(str, "Temperature: %.2f", temperature);
}
// ...
return 0;
}
```
建议在代码中添加注释,以便理解代码的功能和实现细节。
阅读全文