1單片機激活一個I/O口,讓單片機輸出固定的串口信號的代碼示例如下:
#include <reg52.h>
sbit P0_1 = P0^1; // Define P0.1 as sbit type
void main()
{
P0_1 = 0; // Activate P0.1 and set its value to low
// Output "123456" via serial port
printf("123456");
while(1)
{
// Do nothing, wait for input signal
}
}
51單片機輸入串口信號123456并激活P0^1的代碼示例如下:
#include <reg52.h>
#include <stdio.h>
sbit P0_1 = P0^1; // Define P0.1 as sbit type
char input[7];
void main()
{
while(1)
{
scanf("%s", input);
if (strcmp(input, "123456") == 0)
{
P0_1 = 1; // Activate P0.1 and set its value to high
}
}
}
注意:需要包含"stdio.h"頭文件并初始化串口。
|