elizabeth ann
Junior Member level 2
read switches i2c
i am trying to read from a microswitch (similar to a push button) over the i2c communication so i developed a code that will constantly read from the source (10 microswitches installed in Port Expander 1) and feed it to another i2c device (10 LEDs installed in Port Expander 2) in order to turn an LED off until all the 10 LEDs are OFF. for example, if i have pressed SWITCH 1, then LED 1 should be OFF.
however, this code seems to have a problem.... i have tried to load the program in my microcontroller but nothing happens....
also, i have a question about the byte received during the READ process, is it simply 0 or 1 and 0 means low, and 1 is high? i hope someone can enlighten me....
thank you.(by the way i am using CCS C as my compiler, and MAX7318 for my port expanders)
note:
slave addresses:
0x32=10 LEDs (preconfigured as output)
0xA0=10 switches (preconfigured as input)
perhaps something is wrong with my WRITE MODE/PROCESS? corrections are very much welcomed...
i am trying to read from a microswitch (similar to a push button) over the i2c communication so i developed a code that will constantly read from the source (10 microswitches installed in Port Expander 1) and feed it to another i2c device (10 LEDs installed in Port Expander 2) in order to turn an LED off until all the 10 LEDs are OFF. for example, if i have pressed SWITCH 1, then LED 1 should be OFF.
however, this code seems to have a problem.... i have tried to load the program in my microcontroller but nothing happens....
also, i have a question about the byte received during the READ process, is it simply 0 or 1 and 0 means low, and 1 is high? i hope someone can enlighten me....
thank you.(by the way i am using CCS C as my compiler, and MAX7318 for my port expanders)
note:
slave addresses:
0x32=10 LEDs (preconfigured as output)
0xA0=10 switches (preconfigured as input)
Code:
BYTE byte1, byte2;
do
{
while( !byte1 && !byte2 )
{
i2c_start();
i2c_write(0xA0);
i2c_write(0x02);
i2c_start(); //START READING REGISTER
i2c_write(0xA1);
byte1=i2c_read(1); //ex. if switch1 is pressed, byte1=0x7F
byte2=i2c_read(); //READ 2ND BYTE
i2c_read(0); //STOP READING
i2c_stop();
break;
}
byte1=byte1&&0xFF;
byte2=byte2&&0xFF;
i2c_start();
i2c_write(0x32); //UPDATE LEDs WITH SWITCH STATE
i2c_write(0x02); //ACCESS OUTPUT REGISTER
i2c_write(byte1); //WRITE BYTE1 (FROM READ MODE) TO LED
i2c_write(byte2); //WRITE BYTE2 (FROM READ MODE) TO LED
i2c_stop();
} while ( (byte1!=0x00) && (byte2!=0x00) );