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

16f877 i2c problems with master in interrupt.

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







16f877 i2c problems with master in interrupt.
PostPosted: Fri Dec 07, 2001 5:31 am     Reply with quote

I have wrote this code for to transmit one byte, from slave to master with master i2c in interrupt:


*********************** MASTER *************************

#include <16F877.h>
#device PIC16F877 *=16 ICD=TRUE
#use delay(clock=8000000)
#fuses HS,NOWDT,PUT,NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use i2c(master,sda=PIN_C4,scl=PIN_C3,SLOW,NOFORCE_SW)


#INT_SSP
void i2c_invia_a_slave()
{
*0x13 = 0x0F;
x=i2c_read (0);
disable_interrupts (INT_SSP);
i2c_stop ();
}


void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
enable_interrupts(global);

do
{
if (some event)
{
bit_clear (*0x0C, 3);
enable_interrupts(INT_SSP);
i2c_start();
}
} while (TRUE);
}


**************** SLAVE ********************

#include <16F877.h>
#device PIC16F877 *=16 ICD=TRUE
#use delay(clock=8000000)
#fuses HS,NOWDT,PUT,NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use i2c(slave,sda=PIN_C4,scl=PIN_C3,address=0x0E,SLOW,NOFORCE_SW)


#INT_SSP
void i2c_ricevi_da_master()
{
byte incoming;
if (i2c_poll()==TRUE) incoming = i2c_read ();
else i2c_write (25);
}

void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
enable_interrupts(INT_SSP);
enable_interrupts(global);

do{}while (TRUE);
}


The problem is that first transmission is ok, the master receives the number 25, but the slave remain blocked on the instruction i2c_write (25); and if I try to transmit an other time, the master jams also in the instraction i2c_start ();.

The same problem there is also when the master is implemented not in interrupt. The solution in this case is to add the "OPTIONAL ????" parameter zero in the instruction i2c_read(0); to cause the function to not ACK the received data.

This solution does not work with master in interrupt.


how I can make?



IDE 2.32
PCB 2.734
PCM 2.734
___________________________
This message was ported from CCS's old forum
Original Post ID: 1499
Tomi
Guest







Re: 16f877 i2c problems with master in interrupt.
PostPosted: Sat Dec 08, 2001 3:49 pm     Reply with quote

Hi Davide,
I don't find the slave address at your master side.
To read a byte, you must:
i2c_start();
i2c_write(0x0F); // 0x0E the slave address base, so send out 0x0F for reading
etc.

So you need a phase counter or same and send out the slave address (bit0=1) as the first byte from your ISR.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1520
Davide
Guest







Re: 16f877 i2c problems with master in interrupt.
PostPosted: Mon Dec 10, 2001 3:11 am     Reply with quote

:=Hi Davide,
:=I don't find the slave address at your master side.
:=To read a byte, you must:
:=i2c_start();
:=i2c_write(0x0F); // 0x0E the slave address base, so send out 0x0F for reading
:=etc.
:=
:=So you need a phase counter or same and send out the slave address (bit0=1) as the first byte from your ISR.


Hi Tomy, sooner or later I will have to offer a pizza to you, if you come in Italy, or if I come.... (where do you live?).


Instead writing i2c_write (0X0F);
I wrote *0X13 = 0x0F;
Because, else the Master don't repeat the interrupt at the end of the i2c_write instruction (this method is one of your suggestions that work fine).

At this point the problems remain the same.

*********************** MASTER *************************

#include <16F877.h>
#device PIC16F877 *=16 ICD=TRUE
#use delay(clock=8000000)
#fuses HS,NOWDT,PUT,NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use i2c(master,sda=PIN_C4,scl=PIN_C3,SLOW,NOFORCE_SW)


#INT_SSP
void i2c_master()
{
*0x13 = 0x0F; //****************************** HERE
x=i2c_read (0);
disable_interrupts (INT_SSP);
i2c_stop ();
}


void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
enable_interrupts(global);

do
{
if (some event)
{
bit_clear (*0x0C, 3);
enable_interrupts(INT_SSP);
i2c_start();
}
} while (TRUE);
}


**************** SLAVE ********************

#include <16F877.h>
#device PIC16F877 *=16 ICD=TRUE
#use delay(clock=8000000)
#fuses HS,NOWDT,PUT,NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use i2c(slave,sda=PIN_C4,scl=PIN_C3,address=0x0E,SLOW,NOFORCE_SW)


#INT_SSP
void i2c_slave()
{
byte incoming;
if (i2c_poll()==TRUE) incoming = i2c_read ();
else i2c_write (25);
}

void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
enable_interrupts(INT_SSP);
enable_interrupts(global);

do{}while (TRUE);
}


The problem is that first transmission is ok, the master receives the number 25, but the slave remain blocked on the instruction i2c_write (25); and if I try to transmit an other time, the master jams also in the instraction i2c_start ();.

The same problem there is also when the master is implemented not in interrupt. The solution in this case is to add the "OPTIONAL ????" parameter zero in the instruction i2c_read(0); to cause the function to not ACK the received data.

This solution does not work with master in interrupt.

Bye,
Davide.

IDE 2.32
PCB 2.734
PCM 2.734
___________________________
This message was ported from CCS's old forum
Original Post ID: 1537
Davide
Guest







SORRY
PostPosted: Mon Dec 10, 2001 3:17 am     Reply with quote

Excuse Tomi I mistake always your name.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1538
Davide
Guest







Re: 16f877 i2c problems with master in interrupt.
PostPosted: Mon Dec 10, 2001 7:26 am     Reply with quote

Hi Tomy, sooner or later I will have to offer a pizza to you, if you come in Italy, or if I come.... (where do you live?).


Instead writing i2c_write (0X0F);
I wrote *0X13 = 0x0F;
Because, else the Master don't repeat the interrupt at the end of the i2c_write instruction (this method is one of your suggestions that work fine).

At this point the problems remain the same.

*********************** MASTER *************************

#include <16F877.h>
#device PIC16F877 *=16 ICD=TRUE
#use delay(clock=8000000)
#fuses HS,NOWDT,PUT,NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use i2c(master,sda=PIN_C4,scl=PIN_C3,SLOW,NOFORCE_SW)


#INT_SSP
void i2c_master()
{
*0x13 = 0x0F; //****************************** HERE
x=i2c_read (0);
disable_interrupts (INT_SSP);
i2c_stop ();
}


void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
enable_interrupts(global);

do
{
if (some event)
{
bit_clear (*0x0C, 3);
enable_interrupts(INT_SSP);
i2c_start();
}
} while (TRUE);
}


**************** SLAVE ********************

#include <16F877.h>
#device PIC16F877 *=16 ICD=TRUE
#use delay(clock=8000000)
#fuses HS,NOWDT,PUT,NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use i2c(slave,sda=PIN_C4,scl=PIN_C3,address=0x0E,SLOW,NOFORCE_SW)


#INT_SSP
void i2c_slave()
{
byte incoming;
if (i2c_poll()==TRUE) incoming = i2c_read ();
else i2c_write (25);
}

void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
enable_interrupts(INT_SSP);
enable_interrupts(global);

do{}while (TRUE);
}


The problem is that first transmission is ok, the master receives the number 25, but the slave remain blocked on the instruction i2c_write (25); and if I try to transmit an other time, the master jams also in the instraction i2c_start ();.

The same problem there is also when the master is implemented not in interrupt. The solution in this case is to add the "OPTIONAL ????" parameter zero in the instruction i2c_read(0); to cause the function to not ACK the received data.

This solution does not work with master in interrupt.

Bye,
Davide.

IDE 2.32
PCB 2.734
PCM 2.734
___________________________
This message was ported from CCS's old forum
Original Post ID: 1540
Tomi
Guest







Re: 16f877 i2c problems with master in interrupt.
PostPosted: Mon Dec 10, 2001 12:00 pm     Reply with quote

<font face="Courier New" size=-1>:=Hi Tomy, sooner or later I will have to offer a pizza to you, if you come in Italy, or if I come.... (where do you live?).

Hungary. OK, Sir, let's say that you have sent a virtual pizza for me (an original, "fried-on-stone" , Quattro Staggioni or Cardinale with extra cheese). And don't worry about my name: you can write Thomas, Tomasso, Tomi, Tomassino, Tomy, etc.

:=Instead writing i2c_write (0X0F);
:=I wrote *0X13 = 0x0F;

OK, I'm a blind mouse with two eye-glasses and I forgot to change it to the right one.
And now the problem. I have tried your code in my old CCS C version (2.734) and I have seen that the optional parameter for i2c_read() doesn't matter. If you want to use the NACK as the end of the transfer, try this:

#INT_SSP
void i2c_master()
{
*0x13 = 0x0F; //****************************** HERE
bit_set(*0x91,5); // set NACK
x = i2c_read();
bit_set(*0x91,4); // set ACKEN to init an ACK cycle
while (!bit_test(*0x0C,3)) ; // wait for ACK end
bit_clear(*0x0C,3);
disable_interrupts (INT_SSP);
i2c_stop ();
}

Hope this helps. If not, I'll be here usually after 6:00PM (we are in the same time zone).
</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 1545
Guest








Re: 16f877 i2c problems with master in interrupt.
PostPosted: Sun Jun 11, 2006 9:02 am     Reply with quote

Davide wrote:
:=Hi Davide,
:=I don't find the slave address at your master side.
:=To read a byte, you must:
:=i2c_start();
:=i2c_write(0x0F); // 0x0E the slave address base, so send out 0x0F for reading
:=etc.
:=
:=So you need a phase counter or same and send out the slave address (bit0=1) as the first byte from your ISR.


Hi Tomy, sooner or later I will have to offer a pizza to you, if you come in Italy, or if I come.... (where do you live?).


Instead writing i2c_write (0X0F);
I wrote *0X13 = 0x0F;
Because, else the Master don't repeat the interrupt at the end of the i2c_write instruction (this method is one of your suggestions that work fine).

At this point the problems remain the same.

*********************** MASTER *************************

#include <16F877.h>
#device PIC16F877 *=16 ICD=TRUE
#use delay(clock=8000000)
#fuses HS,NOWDT,PUT,NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use i2c(master,sda=PIN_C4,scl=PIN_C3,SLOW,NOFORCE_SW)


#INT_SSP
void i2c_master()
{
*0x13 = 0x0F; //****************************** HERE
x=i2c_read (0);
disable_interrupts (INT_SSP);
i2c_stop ();
}


void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
enable_interrupts(global);

do
{
if (some event)
{
bit_clear (*0x0C, 3);
enable_interrupts(INT_SSP);
i2c_start();
}
} while (TRUE);
}


**************** SLAVE ********************

#include <16F877.h>
#device PIC16F877 *=16 ICD=TRUE
#use delay(clock=8000000)
#fuses HS,NOWDT,PUT,NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use i2c(slave,sda=PIN_C4,scl=PIN_C3,address=0x0E,SLOW,NOFORCE_SW)


#INT_SSP
void i2c_slave()
{
byte incoming;
if (i2c_poll()==TRUE) incoming = i2c_read ();
else i2c_write (25);
}

void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
enable_interrupts(INT_SSP);
enable_interrupts(global);

do{}while (TRUE);
}


The problem is that first transmission is ok, the master receives the number 25, but the slave remain blocked on the instruction i2c_write (25); and if I try to transmit an other time, the master jams also in the instraction i2c_start ();.

The same problem there is also when the master is implemented not in interrupt. The solution in this case is to add the "OPTIONAL ????" parameter zero in the instruction i2c_read(0); to cause the function to not ACK the received data.

This solution does not work with master in interrupt.

Bye,
Davide.

IDE 2.32
PCB 2.734
PCM 2.734
___________________________
This message was ported from CCS's old forum
Original Post ID: 1537
domdom



Joined: 06 Sep 2006
Posts: 29

View user's profile Send private message

PostPosted: Wed Sep 06, 2006 9:13 am     Reply with quote

Are they working? i mean master and slave c code?
then how you programme into your MCU?
the MCU(master) programme with the master c code? the slave MCU programme with the slave c code?
cjusto



Joined: 26 Apr 2006
Posts: 38
Location: Portugal

View user's profile Send private message

PostPosted: Thu Sep 07, 2006 1:45 pm     Reply with quote

hey ragazzo italiano.

i think you should implement the master without interrupt. if u need that contious, than it will work.

the nack in the end is very important.


cya around!
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