单片机代码实现四路开关测控系统
时间: 2023-05-12 13:04:42 浏览: 71
非常感谢您的提问。以下是一个简单的单片机代码实现四路开关测控系统的示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SWITCH_COUNT 4
int main()
{
int switches[SWITCH_COUNT] = {0};
int i;
while (1) {
printf("Please enter a command (e.g. 'switch 1 on', 'switch 2 off', 'status'):\n");
char input[100];
fgets(input, sizeof(input), stdin);
char *token = strtok(input, " ");
if (token == NULL) {
printf("Invalid command.\n");
continue;
}
if (strcmp(token, "switch") == 0) {
token = strtok(NULL, " ");
if (token == NULL) {
printf("Invalid command.\n");
continue;
}
int switch_num = atoi(token);
if (switch_num < 1 || switch_num > SWITCH_COUNT) {
printf("Invalid switch number.\n");
continue;
}
token = strtok(NULL, " ");
if (token == NULL) {
printf("Invalid command.\n");
continue;
}
if (strcmp(token, "on") == 0) {
switches[switch_num - 1] = 1;
printf("Switch %d turned on.\n", switch_num);
} else if (strcmp(token, "off") == 0) {
switches[switch_num - 1] = 0;
printf("Switch %d turned off.\n", switch_num);
} else {
printf("Invalid command.\n");
continue;
}
} else if (strcmp(token, "status") == 0) {
for (i = 0; i < SWITCH_COUNT; i++) {
printf("Switch %d is %s.\n", i + 1, switches[i] ? "on" : "off");
}
} else {
printf("Invalid command.\n");
continue;
}
}
return 0;
}
```
这个程序可以通过命令行输入控制四个开关的状态,并且可以查询当前开关的状态。如果您有任何问题,请随时问我。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)