|
|
我用两块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));
}
}
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|