CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

cannot read from an input (switch) over i2c...(SOLVED)
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
elizabeth ann



Joined: 16 Feb 2008
Posts: 33

View user's profile Send private message

PostPosted: Wed May 07, 2008 7:27 pm     Reply with quote

it worked! Very Happy Very Happy Very Happy

here is the final code that can successfully display the bytes (in read mode) of MAX7318 in the LCD display.

Code:


byte setme[5]={0xFE,0xFD,0xFB,0xF7,0xEF};

while(1)
{
      see_switch();
     
      printf(lcd_putc,"b1:%Xb2:%X\n",byte1,byte2);
   
      delay_ms(100);
}


void see_switch()
{
   i2c_start();           
   i2c_write(0xA0);
   i2c_write(0x00);
   i2c_start();         //START READING REGISTER
   i2c_write(0xA1);     
   byte1=i2c_read(); //if switch1 is pressed, byte1=1111 1110
   byte2=i2c_read(0);    //READ 2ND BYTE
   i2c_stop();

//however, with the second part of the i2c, (writing to the
//MAX7318 of the LEDs), this is the code that worked (and not
//the previously posted one...)

      switch(byte1)
      {
      case 0xFE:
         {i2c_start();
         i2c_write(0x32);
         i2c_write(0x02);
         i2c_write(setme[0]);
         i2c_write(0x00);
         i2c_stop();
         break;}
         
       case 0xFD:{
         i2c_start();
         i2c_write(0x32);
         i2c_write(0x02);
         i2c_write(setme[1]);
         i2c_write(0x00);
         i2c_stop();
         break;}
         
      case 0xFB:{
         i2c_start();
         i2c_write(0x32);
         i2c_write(0x02);
         i2c_write(setme[2]);
         i2c_write(0x00);
         i2c_stop();
         break;}
         
      case 0xF7:{
         i2c_start();
         i2c_write(0x32);
         i2c_write(0x02);
         i2c_write(setme[3]);
         i2c_write(0x00);
         i2c_stop();
         break;}
     
     case 0xEF:{
         i2c_start();
         i2c_write(0x32);
         i2c_write(0x02);
         i2c_write(setme[4]);
         i2c_write(0x00);
         i2c_stop();
         break;}
      }
}
         

i don't know what is wrong with the bytes then...so i started thinking of doing it the LONG way ( using array setme[5] ) and it worked. however that would be very impratical with my case because i have 10 switches. the program would be too long and redundant, it won't be a good code, right?...is there any better way to do it?

the LCD ouput displays only FE, FD, FB, F7 and FF...
can someone tell me why is it that i can't feed that value to the MAX7318? and why is it that when i completed these values to be 0XFE, 0XFD, 0xFB, 0xF7, 0xFF.... that's only when everything worked...is this something to do with the BYTE FORMAT? Crying or Very sad thanks....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 07, 2008 7:54 pm     Reply with quote

Try this code instead of the switch-case code. It should do the same
thing, and do it with a lot less code:
Quote:

i2c_start();
i2c_write(0x32);
i2c_write(0x02);
i2c_write(byte1);
i2c_write(0x00);
i2c_stop();
elizabeth ann



Joined: 16 Feb 2008
Posts: 33

View user's profile Send private message

PostPosted: Thu May 08, 2008 2:03 am     Reply with quote

thanks, PCM Programmer....

i already tried that one but it's not working on my case. that would have made things quite easier....i don't know what is wrong but it just won't work. Crying or Very sad

are there any possible solutions?

thanks again...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 08, 2008 11:30 am     Reply with quote

The two methods effectively do the same thing. It should work.
The switch-case introduces a small delay before the next i2c operation
begins, but it's only a few microseconds. You could try adding a delay
between the two i2c operations. Example:
Quote:

i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
byte1=i2c_read();
byte2=i2c_read(0);
i2c_stop();

delay_ms(1);

i2c_start();
i2c_write(0x32);
i2c_write(0x02);
i2c_write(byte1);
i2c_write(0x00);
i2c_stop();

I don't claim that this will fix it. I'm not sure what the problem is.
elizabeth ann



Joined: 16 Feb 2008
Posts: 33

View user's profile Send private message

PostPosted: Fri May 09, 2008 12:22 am     Reply with quote

by the way, here is the schematic diagram...i hope this link works....



or this one.....

elizabeth ann



Joined: 16 Feb 2008
Posts: 33

View user's profile Send private message

PostPosted: Mon May 12, 2008 7:52 am     Reply with quote

hello everyone....

here's the final code that works for my MAX7318 Switch and LED application....

Code:

void see_switch()
{
   while ( 1 )
   {
       i2c_start();
       i2c_write(0xA0);
       i2c_write(0x00);
       i2c_start();
       i2c_write(0xA1);
       byte1=i2c_read();
       byte2=i2c_read(0);
       i2c_stop();
     
       delay_ms(10);
       
       while ( (byte1==~portA) && (byte2==~portB) )
       {
       i2c_start();
       i2c_write(0x32);
       i2c_write(0x02);
       i2c_write(byte1);
       i2c_write(byte2);
       i2c_stop();
       } 
     
   }
}


the code will not only turn the LED off for a certain time but permanently (unless reset, ofcourse Wink )

problem solved!

thank you everyone for the attention and help....
this is such a great forum...and the first time i made my code work! Very Happy

thanks!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group