0% found this document useful (0 votes)
45 views

Header Files I2C.H

This document contains header files and code for I2C communication on a microcontroller. It defines the I2C pins and speed, includes header files, and contains functions for initializing I2C, sending/receiving data, and start/stop conditions. The main function initializes I2C, sends/receives a byte as an example, and loops continuously.

Uploaded by

RASMITHA R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Header Files I2C.H

This document contains header files and code for I2C communication on a microcontroller. It defines the I2C pins and speed, includes header files, and contains functions for initializing I2C, sending/receiving data, and start/stop conditions. The main function initializes I2C, sends/receives a byte as an example, and loops continuously.

Uploaded by

RASMITHA R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

HEADER FILES

I2C.H
#ifndef __I2C_H
#define __I2C_H

// Define i2c pins


#define SDA RC4 // Data pin for i2c
#define SCK RC3 // Clock pin for i2c
#define SDA_DIR TRISC4 // Data pin direction
#define SCK_DIR TRISC3 // Clock pin direction

// Define i2c speed


#define I2C_SPEED 100 // kbps

//Function Declarations
void InitI2C(void);
void I2C_Start(void);
void I2C_ReStart(void);
void I2C_Stop(void);
void I2C_Send_ACK(void);
void I2C_Send_NACK(void);
bit I2C_Write_Byte(unsigned char);
unsigned char I2C_Read_Byte(void);
#endif

INCLUDE.H
#ifndef __INCLUDES_H
#define __INCLUDES_H
// Define CPU Frequency
// This must be defined, if __delay_ms() or
// __delay_us() functions are used in the code
#define _XTAL_FREQ 20000000

#include <htc.h>
#include "I2C.h"

#endif

MAIN.C
#include"Includes.h"

// Main function
void main(void)
{
unsigned char RxByte = 0;

InitI2C(); // Initialize i2c pins

I2C_Start(); // Send start bit on i2c


I2C_Write_Byte(0xA0); // Send 0xA0 on i2c
RxByte = I2C_Read_Byte(); // Read value from i2c
I2C_Send_ACK(); // Send ACK bit on i2c
I2C_Stop(); // Send stop bit on i2c

while(1)
{
}
}

I2C.C
#include "Includes.h"
// Function Purpose: Configure I2C module
void InitI2C(void)
{
SDA_DIR = 1; // Make SDA and
SCK_DIR = 1; // SCK pins input

SSPADD = ((_XTAL_FREQ/4000)/I2C_SPEED) - 1;
SSPSTAT = 0x80; // Slew Rate control is disabled
SSPCON = 0x28; // Select and enable I2C in master mode
}

// Function Purpose: I2C_Start sends start bit sequence


void I2C_Start(void)
{
SEN = 1; // Send start bit
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
}

// Function Purpose: I2C_ReStart sends start bit sequence


void I2C_ReStart(void)
{
RSEN = 1; // Send Restart bit
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
}

//Function : I2C_Stop sends stop bit sequence


void I2C_Stop(void)
{
PEN = 1; // Send stop bit
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
}

//Function : I2C_Send_ACK sends ACK bit sequence


void I2C_Send_ACK(void)
{
ACKDT = 0; // 0 means ACK
ACKEN = 1; // Send ACKDT value
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
}

//Function : I2C_Send_NACK sends NACK bit sequence


void I2C_Send_NACK(void)
{
ACKDT = 1; // 1 means NACK
ACKEN = 1; // Send ACKDT value
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
}
// Function Purpose: I2C_Write_Byte transfers one byte
bit I2C_Write_Byte(unsigned char Byte)
{
SSPBUF = Byte; // Send Byte value
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit

return ACKSTAT; // Return ACK/NACK from slave


}

// Function Purpose: I2C_Read_Byte reads one byte


unsigned char I2C_Read_Byte(void)
{
RCEN = 1; // Enable reception of 8 bits
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
return SSPBUF; // Return received byte
}

You might also like