Сканер шины i2c для PIC

Это диагностическая программа, которая ищет любое ведомое устройство на шине i2c и сообщает адрес для каждого найденного устройства. Эта программа полезна, если вы хотите убедиться, что ваш микроконтроллер действительно видит ведомое устройство, или убедиться в правильности адреса ведомого устройства. Данные выводятся через UART. Но программу можно изменить и для вывода на ЖКИ.

 

Код программы для компилятора CCS Compiler:

#include <16F887.h> 
#fuses INTRC_IO,NOWDT,NOPROTECT,PUT,NOLVP,NOBROWNOUT
#use delay(clock=4M)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

// This function writes the slave address to the i2c bus.
// If a slave chip is at that address, it should respond to
// this with an "ACK".   This function returns TRUE if an
// ACK was found.  Otherwise it returns FALSE.
int8 get_ack_status(int8 address)
{
int8 status;

i2c_start();
status = i2c_write(address);  // Status = 0 if got an ACK
i2c_stop();

if(status == 0)
   return(TRUE);
else
   return(FALSE);
}


//=================================
void main()
{
int8 i;
int8 status;
int8 count = 0;

printf("\n\rStart:\n\r");

delay_ms(1000);

// Try all slave addresses from 0x10 to 0xEF.
// See if we get a response from any slaves
// that may be on the i2c bus.
for(i=0x10; i < 0xF0; i+=2)
   {
    status = get_ack_status(i);
    if(status == TRUE)
      { 
       printf("ACK addr: %X\n\r", i);
       count++;
       delay_ms(2000);
      }
   }

if(count == 0)
   printf("\n\rNothing Found");
else
   printf("\n\rNumber of i2c chips found: %u", count);

while(1);
}

Пример вывода при обнаружении двух датчиков температуры TC74:

Start:
ACK addr: 9A
ACK addr: A0

Number of i2c chips found: 2

Загрузка...