Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I2C driver problem with PIC16F18877.

Status
Not open for further replies.

akshaybhavsar

Full Member level 2
Joined
May 5, 2016
Messages
135
Helped
2
Reputation
4
Reaction score
2
Trophy points
18
Activity points
898
Hi I am using PIC16F18877 controller with 32 MHZ System clock.I want to develop I2C drivers for sensor APDS9960.
I have working I2C firmware for APDS9960 for PIC32MX controller which works very well..I have ported this firmware for PIOC16F18877 but it doesnt work.

Here is my code


Code:
Conversation opened. 1 unread message.

Skip to content
Using Gmail with screen readers
 
 
More 
1 of 21,952
 
i2c pic16
Inbox
 x
sanket meher
 
Attachments3:47 PM (4 minutes ago)
 
to me
PFA
2 Attachments
 
Click here to Reply or Forward
2.73 GB (18%) of 15 GB used
Manage
Terms - Privacy
Last account activity: 0 minutes ago
Details
 
 

/* I2C library*/

#include <xc.h>
#include "i2c.h"


// The flag to indicate whether an I2C error has occured.
unsigned char b_i2c_error_flag = 0;

// Function Purpose: Configure I2C module
void InitI2C(void)
{ 
        
        SDA_DIR = 1; // Make SDA and
 SCK_DIR = 1; // SCK pins input
    
 SSP1ADD = 0x4F;//((_XTAL_FREQ/4000)/I2C_SPEED) - 1; 
 SSP1STAT = 0x80; // Slew Rate control is disabled
 SSP1CON1 = 0x28; // Select and enable I2C in master mode
  
}


unsigned char b_i2c_check_error_flag(void)
{
 return b_i2c_error_flag; 
}

// Function Purpose: I2C_Start sends start bit sequence
void I2C_Start(void)
{
 SSP1CON2bits.SEN = 1; // Send start bit
 while(SSP1CON2bits.SEN == 1); // Wait for it to complete 
}

// Function Purpose: I2C_ReStart sends start bit sequence
void I2C_ReStart(void)
{
 
 SSP1CON2bits.RSEN = 1; // Send start bit
 while(SSP1CON2bits.RSEN == 1); // Wait for it to complete
}

//Function : I2C_Stop sends stop bit sequence
void I2C_Stop(void)
{
 
 SSP1CON2bits.PEN = 1; // Send start bit
 while(SSP1CON2bits.PEN == 1);
}

//Function : I2C_Send_ACK sends ACK bit sequence
void I2C_Send_ACK(void)
{
 SSP1CON2bits.ACKDT = 0; // 0 means ACK
 SSP1CON2bits.ACKEN = 1; // Send ACKDT value
 while(SSP1CON2bits.ACKEN == 1); // Wait for it to complete 
}

//Function : I2C_Send_NACK sends NACK bit sequence
void I2C_Send_NACK(void)
{
 SSP1CON2bits.ACKDT = 1; // 0 means ACK
 SSP1CON2bits.ACKEN = 1; // Send ACKDT value
 while(SSP1CON2bits.ACKEN == 1); 
}

// Function Purpose: I2C_Write_Byte transfers one byte
void I2C_Write_Byte(unsigned char Byte)
{
  // Clear the error flag before we start a new I2C operation.
 b_i2c_error_flag = 0;
 SSP1BUF = Byte; // Send Byte value
 while (SSP1STATbits.R_W == 1);// Wait for slave to acknowledge.
  
 // If slave does not acknowledge...
 if (SSP1CON2bits.ACKSTAT == 1) {
  // Send stop bit.
  SSP1CON2bits.PEN = 1;
  while (SSP1CON2bits.PEN == 1);
  
  // Set the error flag and exit.
  b_i2c_error_flag = 1;
  return;
 }
 
}

// Function Purpose: I2C_Read_Byte reads one byte
unsigned char I2C_Read_Byte(void )
{
  SSP1CON2bits.RCEN=1; // Enable to receive data 
        
    // Wait until the data is received.
 unsigned long count = 10000L;
 while (SSP1STATbits.BF == 0) {
  
  // If timeout...
  if (--count == 0) {
   // Send stop bit.
   SSP1CON2bits.PEN = 1;
   while (SSP1CON2bits.PEN == 1);
   
   // Set the error flag and exit.
   b_i2c_error_flag = 1;
   return 0;
  }
 }
 
  return SSP1BUF; // Return received byte
}

i2c.c
Displaying i2c.c.
 

Hi,

but it doesnt work
This is no error description.

-->Give a detailed error description.
Best if you additionally can give scope pictures.

Klaus
 

Stats of SDA and SCL pins doesnt change.
 

Hi,

I see you setup SCL and SDA as input, but nowhere change this...
Can´t see at which pins are the signals.

--> please give complete informations. Otherwise you risk that nobody responds.
Best is to give the schematic, too.

What did you do to debug your program?
Do other port pins work?
Can you set the I2C pins HIGH and LOW without I2C function - just ouptu HIGH and LOW at the port pins.

Klaus
 

hello,

if RC3 RC4 pins ..for I2C concerned by Peripheral Pin Select (PPS) !
see also analog ..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top