#include <Wire.h> #include "gw_grayscale_sensor.h" #define GW_GRAY_GPIO_DAT A4 #define GW_GRAY_GPIO_CLK A5 /* 读取8 bit的传感器数据 */ static uint8_t gw_gray_serial_read() { uint8_t ret = 0; for (int i = 0; i < 8; ++i) { digitalWrite(GW_GRAY_GPIO_CLK, 0); ret |= digitalRead(GW_GRAY_GPIO_DAT) << i; digitalWrite(GW_GRAY_GPIO_CLK, 1); delayMicroseconds(5); } return ret; } void setup() { pinMode(GW_GRAY_GPIO_CLK, OUTPUT); pinMode(GW_GRAY_GPIO_DAT, INPUT_PULLUP); digitalWrite(GW_GRAY_GPIO_CLK, 0); // 初始化串口 Serial.begin(115200); } void loop() { uint8_t sensor_status = 0; uint8_t sensor1, sensor2, sensor3, sensor4, sensor5, sensor6, sensor7, sensor8; // 读取传感器串行输出 sensor_status = gw_gray_serial_read(); // 把读取到的传感器数据打印到公屏上 Serial.print(sensor_status, HEX); Serial.print(" : "); for (int i = 0; i < 8; ++i) { Serial.print((sensor_status >> i) & 0x1); Serial.print(" "); } Serial.println(); /* 其他使用方法 */ // 取第一位探头数据 sensor1 = GET_NTH_BIT(sensor_status, 1); // 取第二位探头数据 sensor2 = GET_NTH_BIT(sensor_status, 2); // 单独分到变量里 SEP_ALL_BIT8(sensor_status, sensor1, sensor2, sensor3, sensor4, sensor5, sensor6, sensor7, sensor8); // sensor1~8 有传感器1~8的数据 delay(500); }