View previous topic :: View next topic |
Author |
Message |
ihsan
Joined: 30 Nov 2023 Posts: 9
|
led blink triggered with i2c |
Posted: Thu Jan 18, 2024 12:29 am |
|
|
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: 19535
|
|
Posted: Thu Jan 18, 2024 8:32 am |
|
|
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
|
|
Posted: Fri Jan 19, 2024 1:45 am |
|
|
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: 19535
|
|
Posted: Fri Jan 19, 2024 1:53 am |
|
|
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. |
|
|
|