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

I2c search problem

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



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

I2c search problem
PostPosted: Fri Oct 30, 2020 11:19 am     Reply with quote

hi,
When i run i2c search() in dspic, dspic hang up.
But if i reset slave via mclr, master will find slave device and hang up again!!!

master:
dspic33ep128gp506
ccs 5.093
Code:

#include <33EP128GP506.h>
#device ICSP=1
#include <ConfigureClock.h>

#pin_select U1TX = PIN_F1
#pin_select U1rX = PIN_F0
#use rs232(UART1, baud=9600,xmit=pin_f1,rcv=pin_f0)
#use i2c(MASTER,I2C1,Force_HW,slow)

unsigned int8 status;

unsigned int8 get_ack_status(unsigned int8 address)
{
   unsigned int8 status;
   
   i2c_start();
   status = i2c_write(address);
   i2c_stop();
   
   if(status==0)  return(TRUE);
   else           return(FALSE);
}

void Search_i2c()
{
   printf("\n\rStart:\n\r");
   delay_ms(200);
   unsigned int8 count = 0;
   for(unsigned int8 i=0x10; i < 0xF0; i+=2)
   {
    status = get_ack_status(i);
    if(status == TRUE)
      { 
         printf("Addr:%X\n\r",i);
         count++;
      //   delay_ms(100);
      }
   }
   if(count == 0)  printf("Nothing Found\n\r");
   else            printf("Chips Found=%u\n\r",count);
}


void main()
{
setup_pll();
printf("Test\r\n");delay_ms(50);
             
     
while(True)
   {   
   Search_i2c();             
   }

}


Slave:
pic16f1829
Code:

#include <16f1829.h>
#FUSES NOWDT//,NOPUT,NOBROWNOUT,NOMCLR,INTRC_IO

#use delay(internal=32Mhz)

#use i2c(Slave,sda=PIN_B5,scl=PIN_B7,address=0x40,force_hw)
//#use i2c(Slave,I2C2,address=0x40,force_hw)

unsigned int8 data[2];

#INT_SSP
void  SSP_isr(void)
{
   int8 state = i2c_isr_state();
   
   if(state < 0x80)     // Master is sending data
   {   
      i2c_read();
   }

   if(state >= 0x80)    // Master is requesting data from slave
   {   
      i2c_write(data[state-0x80]);
   }
   
}


void main()
{   
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
 
   while(TRUE){output_high(pin_b6);}
}

temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Oct 30, 2020 2:18 pm     Reply with quote

hmmm
1) what does '#include <ConfigureClock.h> ' contain ??


2) have you run PCMP's I2C Scanner program from the Code Library ?

3) what value I2C pullups are you using ??

4) what is VDD of both PICs ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Sat Oct 31, 2020 1:15 am     Reply with quote

There is a huge issue with what is posted. The second I2C slave setup line
'remmed out', is actually the correct line to setup I2C2 which is what is needed
for the pins involved. Then however the interrupt will not be INT_SSP,
but INT_SSP2.

So the slave needs:
Code:

#include <16f1829.h>
#FUSES NOWDT//,NOPUT,NOBROWNOUT,NOMCLR,INTRC_IO

#use delay(internal=32Mhz)

//#use i2c(Slave,sda=PIN_B5,scl=PIN_B7,address=0x40,force_hw)
//This actually sets up I2C2, but does not say so amywhere
#use i2c(Slave,I2C2,address=0x40,force_hw, STREAM=PORT2)
//This physically reminds us this uses I2C2

unsigned int8 data[2];

//So we now know we have to use INT_SSP2
#INT_SSP2
void  SSP2_isr(void)
{
   int8 state = i2c_isr_state(PORT2);
   
   if(state < 0x80)     // Master is sending data
   {   
      i2c_read(PORT2);
   }

   if(state >= 0x80)    // Master is requesting data from slave
   {   
      i2c_write(PORT2, data[state-0x80]);
   }
   
}


void main()
{   
   enable_interrupts(INT_SSP2);
   enable_interrupts(GLOBAL);
 
   while(TRUE){output_high(pin_b6);}
}

Currently what is happening the SSP2 hardware is triggering, but there
is no handling software for this, so the SCL line gets held low, hanging
the I2C bus. The master then sits waiting for the bus to release.
Resetting the slave releases this.

When working with devices with multiple hardware, you must always
make sure that you are handling the right hardware. Any 'keys' you
can use (like specifying UART2, or I2C2), are helpful reminders of
which hardware is involved.
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

PostPosted: Sat Oct 31, 2020 9:54 am     Reply with quote

temtronic wrote:
hmmm
1) what does '#include <ConfigureClock.h> ' contain ??


2) have you run PCMP's I2C Scanner program from the Code Library ?

3) what value I2C pullups are you using ??

4) what is VDD of both PICs ?


what is PCMP's I2C Scanner program?
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sat Oct 31, 2020 10:30 am     Reply with quote

hamid9543 wrote:


what is PCMP's I2C Scanner program?


It's free code in the library here from PCM Programmer (I didn't look - but I'm guessing that who Ttelmah is talking about)

Go look in the code library of this forum for "I2C Scanner"

Cheers,

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

PostPosted: Sat Oct 31, 2020 10:35 am     Reply with quote

Ttelmah wrote:
There is a huge issue with what is posted. The second I2C slave setup line
'remmed out', is actually the correct line to setup I2C2 which is what is needed
for the pins involved. Then however the interrupt will not be INT_SSP,
but INT_SSP2.

So the slave needs:
Code:

#include <16f1829.h>
#FUSES NOWDT//,NOPUT,NOBROWNOUT,NOMCLR,INTRC_IO

#use delay(internal=32Mhz)

//#use i2c(Slave,sda=PIN_B5,scl=PIN_B7,address=0x40,force_hw)
//This actually sets up I2C2, but does not say so amywhere
#use i2c(Slave,I2C2,address=0x40,force_hw, STREAM=PORT2)
//This physically reminds us this uses I2C2

unsigned int8 data[2];

//So we now know we have to use INT_SSP2
#INT_SSP2
void  SSP2_isr(void)
{
   int8 state = i2c_isr_state(PORT2);
   
   if(state < 0x80)     // Master is sending data
   {   
      i2c_read(PORT2);
   }

   if(state >= 0x80)    // Master is requesting data from slave
   {   
      i2c_write(PORT2, data[state-0x80]);
   }
   
}


void main()
{   
   enable_interrupts(INT_SSP2);
   enable_interrupts(GLOBAL);
 
   while(TRUE){output_high(pin_b6);}
}

Currently what is happening the SSP2 hardware is triggering, but there
is no handling software for this, so the SCL line gets held low, hanging
the I2C bus. The master then sits waiting for the bus to release.
Resetting the slave releases this.

When working with devices with multiple hardware, you must always
make sure that you are handling the right hardware. Any 'keys' you
can use (like specifying UART2, or I2C2), are helpful reminders of
which hardware is involved.


Thx. solved for slave pic16f1829. but when slave is pic18f46k22 master not found slave
Code:

#include <18F46K22.h>
#FUSES NOWDT,NOBROWNOUT,NOPUT,PLLEN,INTRC_IO
#device ADC=8

//#use delay(internal=64mhz)
#use delay(clock=64000000)

#define b_high output_high(PIN_c0)
#define b_low  output_low(PIN_c0)
#define l_high output_high(PIN_a4)
#define l_low  output_low(PIN_a4)
#define r_high output_high(PIN_a6)
#define r_low  output_low(PIN_a6)
#define f_high output_high(PIN_a7)
#define f_low  output_low(PIN_a7)

#define b_toggle output_toggle(PIN_c0)
#define l_toggle output_toggle(PIN_a4)
#define r_toggle output_toggle(PIN_a6)
#define f_toggle output_toggle(PIN_a7)

#define I2C_INT pin_C1 //D2


#define cal_key !input(pin_e2)


//#use i2c(SLAVE,SCL=PIN_C3,SDA=PIN_C4,address=0xA0,FORCE_SW)
#use i2c(SLAVE,I2C1,address=0xA0,FORCE_SW,STREAM=PORT1)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=pin_a4,bits=8,stream=blt)//bluetooth terminal//password:1234


unsigned int8 data[2];

#INT_SSP
void ssp1_interrupt()
{
   int state = i2c_isr_state();
   
   if(state < 0x80)     // Master is sending data
   {
    i2c_read();
   }

   if(state >= 0x80)    // Master is requesting data from slave
   {
      i2c_write(data[state-0x80]);
   }
}


void main()
{
setup_oscillator(OSC_64MHZ, OSC_PLL_ON);
/*   output_low(I2C_INT);
   set_analog_pins(PIN_A0,PIN_A1,PIN_A2,PIN_A3,PIN_A5,
                   PIN_B0,PIN_B1,PIN_B2,PIN_B3,PIN_B4,
                   PIN_B5,PIN_C5,PIN_C7,PIN_D0,PIN_D1,
                   PIN_D2,PIN_D3,PIN_D4,PIN_D5,PIN_D6);
   setup_adc(ADC_CLOCK_DIV_8|ADC_TAD_MUL_0);*/
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);

   

                   
 
   
   while(True)
   {   
 
         for(int j=0;j<3;j++){
   f_high;delay_ms(60);l_high;delay_ms(60);b_high;delay_ms(60);r_high;delay_ms(60);
   f_low; delay_ms(60);l_low; delay_ms(60);b_low; delay_ms(60);r_low; delay_ms(60);}
   b_low; f_low; l_low; r_low;
   delay_ms(500);
             
   }
}
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Oct 31, 2020 11:06 am     Reply with quote

this..
#use i2c(SLAVE,I2C1,address=0xA0,FORCE_SW,STREAM=PORT1)

...may be the problem...

You're telling the compiler to use software for the I2C transactions and NOT the internal MSSP peripheral. By doing this you probably do NOT have I2C interrupt ability.
others will know for sure
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Sat Oct 31, 2020 11:27 am     Reply with quote

You cannot use software for a slave. Look at the manual. No, it
will not work.

Quote:

CCS offers support for the hardware-based I2CTM and a software-based master I2CTM


The software I2C implementation is for _master only_.

and obviously, software I2C 100% guarantees no interrupt ability,
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

PostPosted: Sat Oct 31, 2020 1:02 pm     Reply with quote

Ttelmah wrote:
You cannot use software for a slave. Look at the manual. No, it
will not work.

Quote:

CCS offers support for the hardware-based I2CTM and a software-based master I2CTM


The software I2C implementation is for _master only_.

and obviously, software I2C 100% guarantees no interrupt ability,


how config i2c?
#use i2c(SLAVE,I2C1,address=0x42,FORCE_HW,STREAM=PORT1)


Last edited by hamid9543 on Sat Oct 31, 2020 1:39 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 31, 2020 1:17 pm     Reply with quote

Also, this stuff doesn't work:
Quote:
set_analog_pins(PIN_A0,PIN_A1,PIN_A2,PIN_A3,PIN_A5,
PIN_B0,PIN_B1,PIN_B2,PIN_B3,PIN_B4,
PIN_B5,PIN_C5,PIN_C7,PIN_D0,PIN_D1,
PIN_D2,PIN_D3,PIN_D4,PIN_D5,PIN_D6);

Look in the .h file for your PIC, and look in the section for:
Quote:
set_analog_pins( )

and it will show you the parameters that it accepts.
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

PostPosted: Sat Oct 31, 2020 1:26 pm     Reply with quote

PCM programmer wrote:
Also, this stuff doesn't work:
Quote:
set_analog_pins(PIN_A0,PIN_A1,PIN_A2,PIN_A3,PIN_A5,
PIN_B0,PIN_B1,PIN_B2,PIN_B3,PIN_B4,
PIN_B5,PIN_C5,PIN_C7,PIN_D0,PIN_D1,
PIN_D2,PIN_D3,PIN_D4,PIN_D5,PIN_D6);

Look in the .h file for your PIC, and look in the section for:
Quote:
set_analog_pins( )

and it will show you the parameters that it accepts.


working properly!!
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