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

No ACK from Slave (I2C communication PIC16f1459 & LM75)

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



Joined: 12 Oct 2015
Posts: 1

View user's profile Send private message

No ACK from Slave (I2C communication PIC16f1459 & LM75)
PostPosted: Mon Oct 12, 2015 9:37 am     Reply with quote

I am unable to receive an ACK from the slave. I send the initial address with write bit, but do not get an acknowledgement.
--main.c
Code:

 #include <xc.h>
#include <stdlib.h>
#include "I2C.h"
#define CDC_DATA_OUT_EP_SIZE    64
#define CDC_DATA_IN_EP_SIZE     64
#define _XTAL_FREQ 32000000
unsigned char Byte1;
    unsigned char Byte2;
    unsigned int Int1;
unsigned char readBuffer[CDC_DATA_IN_EP_SIZE];

void main(void)
{
   
     
    while(1)
    {
       
       I2CInit();
      //  __delay_ms(5);
        I2CStart();
      //  __delay_ms(5);
       
        //I2CWait();
       
        I2CSend(0x48|0x27);
        I2CAck();
        I2CSend(0x00);
        I2CAck();
        I2CRestart();
        I2CSend(0x49|0x27);
        I2CAck();
        Byte1 = I2CRead();
        I2CNak();
        I2CStop();
    }
   
}

I2C lib
Code:

/** INCLUDES *******************************************************/
#include <xc.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stddef.h>
#include "I2C.h"


#define BUFF_SIZE 64
#define SDA     TRISBbits.TRISB4 // I2C bus
#define SCL     TRISBbits.TRISB6 //
#define SCL_IN  PORTBbits.RB6    //
#define SDA_IN  PORTBbits.RB4    //
#define CDC_DATA_OUT_EP_SIZE    64
#define CDC_DATA_IN_EP_SIZE     64


/** VARIABLES ******************************************************/
typedef struct buffer{
    float buff[BUFF_SIZE];
    int readIndex;
    int writeIndex;
}buffer;

static char buttonMessage[] = "Button pressed.\r\n";
static char buttonMessage2[] = "Button pressed2.\r\n";
static uint8_t readBuffer[CDC_DATA_OUT_EP_SIZE];
static uint8_t writeBuffer[CDC_DATA_IN_EP_SIZE];
unsigned char readBuffer1[CDC_DATA_IN_EP_SIZE];
unsigned char Byte1;
    unsigned char Byte2;
    unsigned int Int1;
    float float1;

   
   
/*I2C Implementation*/
    void I2CInit(void)
{
      SDA = 1;
    SCL = 1;
    SDA_IN = 0;   
    SCL_IN = 0;
     
    SSPADDbits.SSPADD = 0x8;
   SSPSTAT = 0xA8;//0b10101000;      //No slew rate
    SSPADD = 0x27;
   SSPCON1bits.SSPM = 0x08;
   SSPCON1 = 0x28;
   SSPSTATbits.SMP = 1;
   SSPSTATbits.CKE = 0;
      //Disable SMBus inputs
  // SSPADD = 0x27;         //100 KHz
 //   SSPCON1 = 0b00101000;  //I2C Master mode
   SSPCON3 = 0x00;  //Rien de slave
   

}
 
/*
Function: I2CStart
Return:
Arguments:
Description: Send a start condition on I2C Bus
*/
void I2CStart()
{
   
   SSPCON2bits.SEN = 1;         /* Start condition enabled */
   //while(SSPCON2bits.SEN);      /* automatically cleared by hardware */
    //LATCbits.LATC0 = 1;                 /* wait for start condition to finish */
}
 
/*
Function: I2CStop
Return:
Arguments:
Description: Send a stop condition on I2C Bus
*/
void I2CStop()
{
   SSPCON2bits.PEN = 1;         /* Stop condition enabled */
   //while(SSPCON2bits.PEN);      /* Wait for stop condition to finish */
    LATCbits.LATC0 = 1;                /* PEN automatically cleared by hardware */
}
 
/*
Function: I2CRestart
Return:
Arguments:
Description: Sends a repeated start condition on I2C Bus
*/
void I2CRestart()
{
   SSPCON2bits.RSEN = 1;        /* Repeated start enabled */
   //while(SSPCON2bits.RSEN);     /* wait for condition to finish */
}
 
/*
Function: I2CAck
Return:
Arguments:
Description: Generates acknowledge for a transfer
*/
void I2CAck()
{
   
   SSPCON2bits.ACKDT = 0;       /* Acknowledge data bit, 0 = ACK */
   SSPCON2bits.ACKEN = 1;       /* Ack data enabled */
   //while(SSPCON2bits.ACKEN);  /* wait for ack data to send on bus */
}
 
/*
Function: I2CNck
Return:
Arguments:
Description: Generates Not-acknowledge for a transfer
*/
void I2CNak()
{
   SSPCON2bits.ACKDT = 1;       /* Acknowledge data bit, 1 = NAK */
   SSPCON2bits.ACKEN = 1;       /* Ack data enabled */
   //while(SSPCON2bits.ACKEN);    /* wait for ack data to send on bus */
}
 
/*
Function: I2CWait
Return:
Arguments:
Description: wait for transfer to finish
*/
void I2CWait()
{
   while ((SSPCON2 == 0x1F ) || ( SSPSTAT == 0x04 ) );
    /* wait for any pending transfer */
}
 
/*
Function: I2CSend
Return:
Arguments: dat - 8-bit data to be sent on bus
           data can be either address/data byte
Description: Send 8-bit data on I2C bus
*/
void I2CSend(unsigned char dat)
{
   
   SSPBUF = dat;    /* Move data to SSPBUF */
   //while(BF);       /* wait till complete data is sent from buffer */
   I2CWait();       /* wait for any pending transfer */
}
 
/*
Function: I2CRead
Return:    8-bit data read from I2C bus
Arguments:
Description: read 8-bit data from I2C bus
*/
unsigned char I2CRead(void)
{
   unsigned char temp;
/* Reception works if transfer is initiated in read mode */
   SSPCON2bits.RCEN = 1;        /* Enable data reception */
   //while(!SSPSTATbits.BF);      /* wait for buffer full */
   temp = SSPBUF;   /* Read serial buffer and store in temp register */
   I2CWait();       /* wait to check any pending transfer */
   return temp;     /* Return the read data from bus */
}
   
/*end implementation functions*/

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 12, 2015 9:47 am     Reply with quote

Quote:
#include <xc.h>

This is XC8 code. You're on the wrong forum. Every PIC C compiler
has its own forum. Try these two forums:
Microchip XC8 forum:
http://www.microchip.com/forums/f249.aspx
Microchip i2c forum:
http://www.microchip.com/forums/f10.aspx
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