- #include <reg51.h>
-
- // 你的IO定义
- sbit aj_x1 = P3^6;
- sbit aj_x2 = P3^7;
- sbit aj_x3 = P1^1;
- sbit aj_x4 = P1^0;
- sbit aj_y1 = P1^4;
- sbit aj_y2 = P1^5;
- sbit aj_y3 = P1^6;
- sbit aj_y4 = P1^7;
-
- // 函数功能:读取8个IO,封装成 1 个 BYTE
- unsigned char IO_To_Byte(void)
- {
- unsigned char byte_data = 0; // 定义一个字节变量
-
- // 按位读取每个IO,拼进字节里
- if(aj_x1) byte_data |= 0x01; // bit0
- if(aj_x2) byte_data |= 0x02; // bit1
- if(aj_x3) byte_data |= 0x04; // bit2
- if(aj_x4) byte_data |= 0x08; // bit3
- if(aj_y1) byte_data |= 0x10; // bit4
- if(aj_y2) byte_data |= 0x20; // bit5
- if(aj_y3) byte_data |= 0x40; // bit6
- if(aj_y4) byte_data |= 0x80; // bit7
-
- return byte_data; // 返回完整字节
- }
复制代码 通过AI找到答案,有机会试试。
|