Page 2 of 2
Re: NXT : I2C communication with PCB
Posted: 14 Dec 2010, 23:22
by mattallen37
Instead of using:
Why not something like this:
Code: Select all
WriteBuf[1] = 0xff-(ReadBuf[0]) ; //You may need the NOT bitwise operator in there, I don't know, as your code is incomplete)
just for simplicity.
Or, you could break out the individual bits, and rewrite the output value based on their values (or whatever other values).
Re: NXT : I2C communication with PCB
Posted: 16 Dec 2010, 09:50
by angusfr
if found the solution before i saw your answer ^^
My complete code based on
"RXC touch sensor Mux" :
Code: Select all
#define I2Cport S4
#define I2CAddr8574 0x40 // I2C Address pour 8574
unsigned byte WriteBuf[]={I2CAddr8574,0x00}; // adresse - données
unsigned byte ReadBuf[]; // Lecture PCF8574
int RdCnt=5; // Nb byte a lire
unsigned byte lightLed = 0x00;
int GetTouchSwitches()
{
do
{
I2CBytes(I2Cport, WriteBuf, RdCnt, ReadBuf);
}while (ReadBuf[0] == WriteBuf[1]) // instead of do "ReadBuf[0] == 0xff", i check if the data change. That's how i can light more than 1 led at the same time (if i write 0xff, i turn off led)
}
task main()
{
ClearScreen();
SetSensorLowspeed(I2Cport);
WriteBuf[1] = 0xff; // turn off leds
while(true){
GetTouchSwitches(); // get button pressed
ReadBuf[0] >>= 4; // readbuf = 4byte for button and 4 byte for led, i only need informations about button then i delete led bytes
lightLed = ~ReadBuf[0]; // ~ operator convert 0 to 1 and 1 to 0 (button pressed is high level 1 but i need a low level 0 to ligh a led)
lightLed = (lightLed) & (WriteBuf[1]); // i use an AND operator to keep previous light leds
WriteBuf[1] = lightLed;
I2CBytes(I2Cport, WriteBuf, RdCnt, ReadBuf); // i send data to PCF8574
Wait(100);
}
}
Some comments are in french, don't blame me for that x)
Next step for me is to turn off a led when the linked button is pressed for a second time without turn off the other leds. Display wich leds are ON or OFF on NXT screen would be nice too.
I'm gonna take a look about my switch too (on A0,A1,A2) ...
See you