15275285755 发表于 2016-5-7 10:41:43

8575移位检测问题

我用两块8575 I0扩展板得到一组10进制的数据,然后转换为2进制后正好是32个0,对应上两块扩展板的32个口,高电平为1,低电平为0,我现在要完成一个项目,就是当外部有连续的4个IO的输入,就点亮一个LED。

我本来是打算用一个for循环,从0到32进行数据循环,然后读取每一位的二进制数据,当读到1的时候累计一次,当读到0则进行清空,然后进行判断,当连续4个数据为1的时候进行输出,但不知为什么一直实现不了,求大师帮助



#include <Wire.h>
int address = 0x20; //0100000 (7bit)
int address1 = 0x21;
uint16_t dataReceive; //16bit data received
uint16_t dataSend;//16bit data sent
uint16_t dataReceive1; //16bit data received
uint16_t dataSend1;//16bit data sent

int LED8=10;

int a;//
int b;
int i;
int c;

uint16_t z;

void setup()
{
Wire.begin();
Serial.begin(19200);
pcf8575_write(dataSend); //turn the pcf8575 pins to input
pcf8575_write(dataSend1);

pinMode(LED8,OUTPUT);
}
void loop()
{
pcf8575_write(0xFF);
dataReceive = pcf8575_read(); //read the pcf8575 pins
dataReceive1 = pcf8575_read1();

z=dataReceive1;



//------------------------------
   
    {
   c=(dataReceive,HEX);
   b=0;
   for (i=1;i<16;i++)
   
    {
   
      a = bitRead(c,i);
      delay(5);
      if(a==1)
      {
      b=b+1;
      }
      
      if(a==0)
      {
      b=0;
      }
      
      if(b==4)
      {
      digitalWrite(LED8,HIGH);
      }
      else
      {
      digitalWrite(LED8,LOW);
      }
   // delay(50);
   
   }
//-------------------------------------------   
}
Serial.print(dataReceive,BIN); //~ inverses the results, this removes the ambiguity of leading zero
Serial.print("");
Serial.print(dataReceive1,BIN);
Serial.print("   z= ");
Serial.print(z);
Serial.print("   c= "); //~ inverses the results, this removes the ambiguity of leading zero
Serial.print(c);
Serial.print("   a= "); //~ inverses the results, this removes the ambiguity of leading zero
Serial.print(a);
   Serial.print("   i= "); //~ inverses the results, this removes the ambiguity of leading zero
Serial.println(i);
delay(50);
}


void pcf8575_write(uint16_t dt)
{
Wire.beginTransmission(address);
Wire.write(0xFF);
Wire.write(0xFF);
Wire.endTransmission();
delayMicroseconds(16);
}

uint16_t pcf8575_read()
{
uint8_t a,b;
Wire.beginTransmission(address);
Wire.endTransmission();
Wire.requestFrom(address,5);
if(Wire.available())
{
    a = Wire.read();
    b = Wire.read();
    return(word(a,b));
}
}



void pcf8575_write1(uint16_t dt)
{
Wire.beginTransmission(address1);
Wire.write(0xFF);
Wire.write(0xFF);
Wire.endTransmission();
delayMicroseconds(16);
}
uint16_t pcf8575_read1()
{
uint8_t c,d;
Wire.beginTransmission(address1);
Wire.endTransmission();
Wire.requestFrom(address1,5);
if(Wire.available())
{
    c = Wire.read();
    d = Wire.read();
    return(word(c,d));
}
}


15275285755 发表于 2016-5-7 10:48:32

关键是c的值无法得到2进制,上面的是换16进制,结果通过串口看到他的值就是16,而不是16进制的值,改成BIN,返回数值就是2

15275285755 发表于 2016-5-7 11:27:43

我用移位完成了上面的设计,但是现在新的问题来了,我无法确定到底是那几个IO的输入

我需要判断

原野动力 发表于 2016-5-7 15:20:40

为啥验证码这么难输对
页: [1]
查看完整版本: 8575移位检测问题