Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 06, 2024, 04:37:23 04:37


Login with username, password and session length


Pages: [1]
Print
Author Topic: MikroC Pro I2C Scan Tool  (Read 8253 times)
0 Members and 1 Guest are viewing this topic.
Some Anon Guy
Junior Member
**
Offline Offline

Posts: 43

Thank You
-Given: 58
-Receive: 25



« on: September 04, 2013, 05:24:27 05:24 »

Here is some code that may help someone here. It is an I2C bus scan tool that I cobbled together from other examples online.
This uses a modified I2C write command (The mikroc pro built in i2c write will wait forever if it does not receive a response...)

It will scan the I2C bus and write the results out on a serial terminal.

I am not a professional programmer, just a hobbyist. Maybe this will help some one out...

Code:

int i=0;
signed char _status=0;

signed char WriteI2C( unsigned char data_out );

void main() {
   I2C1_Init(100000); // Init I2C Bus @ 100Khz
   UART1_init(9600);  // Setup serial at 9600bps
   Delay_ms(100);     // Wait for UART module to stabilize

   while(1){
     char  buffer[15];
     UART1_Write(10);
     UART1_Write(13);
     UART1_Write_Text("Start");
     UART1_Write(10);
     UART1_Write(13);
     for ( i=1; i<=127; i++)
     {
         i2c1_start();
         _status = WriteI2C(i<<1);
         I2C1_stop();
         if (_status==0)
         {
           sprinti(buffer,"Device at:%X\n", i);
           UART1_Write_Text(buffer);
         }

     }
     sprinti(buffer,"Complete.\n", i);
     UART1_Write_Text(buffer);
     sprinti(buffer,"Pause 5 Sec.\n", i);
     UART1_Write_Text(buffer);
     Delay_ms(5000);
  }

}

signed char WriteI2C( unsigned char data_out )
{
    int counter = 0;              // Counter used in timeout for I2C operations
    SSP1BUF = data_out;           // write single byte to SSPBUF
    if ( SSPCON1.WCOL )           // test if write collision occurred
     return ( -1 );               // if WCOL bit is set return negative #
    else
    {
        if( ((SSPCON1&0x0F)==0x08) || ((SSPCON1&0x0F)==0x0B) )        //master mode only
        {
            counter=0;
            while( SSPSTAT.BF )       // wait until write cycle is complete
            {
                   // Setup timer so that we don't wait forever if there is an error
                   counter +=1;
                   if (counter >= 1000)
                   {
                      return ( -99 ); // Time out after 1000 clock cycles
                   }
            }
            I2C1_Is_Idle();           // ensure module is idle
            if ( SSPCON2.ACKSTAT )    // test for ACK condition received
             return ( -2 );                  // return NACK
            else return ( 0 );        // return ACK
        }

    }
}

Logged
Elysion
Newbie
*
Offline Offline

Posts: 14

Thank You
-Given: 27
-Receive: 1


« Reply #1 on: July 23, 2018, 02:46:35 14:46 »

Hi,

Original updated source link is below.

https://libstock.mikroe.com/projects/view/938/i2c-scan-functions

Thanks to "Some Anon Guy" for remembering it to me. It saved my life one week ago.

Ozi
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC