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 support@ccsinfo.com

led blink triggered with i2c

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ihsan



Joined: 30 Nov 2023
Posts: 9

View user's profile Send private message

led blink triggered with i2c
PostPosted: Thu Jan 18, 2024 12:29 am     Reply with quote

Hello dear firends,

I want to turn the led on when start i2c comunication and I want to turn the led off when stop i2c communication. I use i2c interrupt and I can not use led blink with i2c. I need help. Can you help me?

Code:

#INT_SSP
void  SSP_isr(bus2)
{
   output_high(pin_a2);
   int8 t_state, t_addr, i = 0;
   
   t_state = i2c_isr_state();
   state = t_state;
   if(t_state <= 0x80)
   {   
      if (t_state == 0)
      {
         t_addr = i2c_read();
         addr = t_addr;     
      }
      if (t_state == 1)
      {
         do
         {
            if(i2c_poll())
            {
               command = i2c_read();
               i++;
            }
         }while(i < 1);
      }     
   }
   if(t_state >= 0x80)
   {
      if(command == 1)
      {
         switch(state)
         {
            case 0x80:
            i2c_write(QueryData[0]);
            break;
            case 0x81:
            i2c_write(QueryData[1]);
            break;
            case 0x82:
            i2c_write(QueryData[2]);
            break;
         }
      }
      else if(command == 2)
      {
         switch(state)
         {
            case 0x80:
            i2c_write(XchangeData[0]);
            break;
            case 0x81:
            i2c_write(XchangeData[1]);
            break;
            case 0x82:
            i2c_write(XchangeData[2]);
            break;
            case 0x83:
            i2c_write(XchangeData[3]);
            break;
            case 0x84:
            i2c_write(XchangeData[4]);
            break;
            case 0x85:
            i2c_write(XchangeData[5]);
            break;
            case 0x86:
            i2c_write(XchangeData[6]);
            break;
            case 0x87:
            i2c_write(XchangeData[7]);
            break;
            case 0x88:
            i2c_write(XchangeData[8]);
            break;
            case 0x89:
            i2c_write(XchangeData[9]);
            break;
         }
      }
   }
   output_low(pin_a2);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19231

View user's profile Send private message

PostPosted: Thu Jan 18, 2024 8:32 am     Reply with quote

There are enormous problems with what you ant to do, and post.
First of all you are misusing the interrupt. You are staying inside the
interrupt handler while receiving multiple bytes. Not how the interrupt
is used or works.
You are mishandling the i2c_read, potentially releasing the clock before
the byte is read.
Then the hardware does not generate an interrupt for the stop. It is
the end of transaction, triggered by the master sending the last byte
with a NACK, that could be used, not the stop.
You don't tell us what chip you are using.
ihsan



Joined: 30 Nov 2023
Posts: 9

View user's profile Send private message

PostPosted: Fri Jan 19, 2024 1:45 am     Reply with quote

Thanks Ttelmah for your response.
I use 16f88. I learn and apply i2c communication this project. When I do not use output_high and output_low commands, communication is succesful.
I do not know, why incorrect are i2c_poll() and this loop that I use in interrupt.
Ttelmah



Joined: 11 Mar 2010
Posts: 19231

View user's profile Send private message

PostPosted: Fri Jan 19, 2024 1:53 am     Reply with quote

Beacause you an=re negating the 'point' of the interrupt.
You get an interrupt for each character. Hence handle that character
and get back to your main code. Looping an polling in the interrupt
meass your code stays inside the interrupt for the whole communcation.
Makes using the interrupt pointless.
Look at the example code. This shows how the interrupt is meant to
be used, and how the i2c_read shpuld be handled.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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