LTlib LaurTec Library  4.0.1
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
module_I2C.h File Reference
#include "LTlib.h"

Go to the source code of this file.

Macros

#define I2C_SLAVE_7_BIT_ADDRESS   0b00000110
 
#define I2C_SLAVE_10_BIT_ADDRESS   0b00000111
 
#define I2C_MASTER   0b00001000
 
#define I2C_SLEW_ON   0b00000000
 
#define I2C_SLEW_OFF   0b10000000
 
#define I2C1_read_byte()   (SSPBUF)
 
#define I2C1_wait_bus_IDLE()   while((SSPCON2 & 0x1F) | (SSPSTATbits.R_W))
 
#define I2C1_check_data_ready()   (SSPSTATbits.BF)
 
#define I2C1_check_read_write_operation()   (SSPSTATbits.R_W)
 
#define I2C1_check_stop_bit()   (SSPSTATbits.P)
 
#define I2C1_check_start_bit()   (SSPSTATbits.S)
 
#define I2C1_check_bus_collision()   (PIR2bits.BCLIF)
 
#define I2C1_check_ACK()   (SSPCON2bits.ACKSTAT)
 
#define I2C1_restart_communication()   SSPCON2bits.RSEN=1;while(SSPCON2bits.RSEN)
 
#define I2C1_negative_ACK()   SSPCON2bits.ACKDT=1, SSPCON2bits.ACKEN=1;while(SSPCON2bits.ACKEN)
 
#define I2C1_set_master_as_receiver()   SSPCON2bits.RCEN = 1;while (SSPCON2bits.RCEN)
 

Functions

void I2C1_open (unsigned char device_type)
 
void I2C1_baud_rate (unsigned int bus_baud_rate_KHZ)
 
void I2C1_set_slave_address (unsigned int device_address)
 
signed char I2C1_write_byte (unsigned char byte_to_send)
 
void I2C1_start_bit (void)
 
void I2C1_stop_bit (void)
 
void I2C1_reset_write_collision_flag (void)
 
signed char I2C1_write_byte_to_external_device (unsigned char control_byte, unsigned char register_address, unsigned char data)
 
signed char I2C1_read_byte_from_external_device (unsigned char control_byte, unsigned char register_address)
 

Macro Definition Documentation

#define I2C1_check_ACK ( )    (SSPCON2bits.ACKSTAT)

This macro checks if the ACK has been received.

Parameters
void
Returns
flag It returns the ACK status. 0: ACK has been received 1: ACK has not been received

Definition at line 356 of file module_I2C.h.

#define I2C1_check_bus_collision ( )    (PIR2bits.BCLIF)

This macro checks if a bus collision happened.

Parameters
void
Returns
flag It returns the bus collision status. 0: Normal transmission 1: Bus collision has been detected

Definition at line 340 of file module_I2C.h.

#define I2C1_check_data_ready ( )    (SSPSTATbits.BF)

This macro checks if there is any data ready in the RX buffer.

Parameters
void
Returns
flag It returns data ready flag. 0: Buffer is empty 1: Buffer is full

Definition at line 274 of file module_I2C.h.

#define I2C1_check_read_write_operation ( )    (SSPSTATbits.R_W)

This macro checks if received command is a read or write operation.

Parameters
void
Returns
flag It returns RW flag. 0: Write Command 1: Read Command

Definition at line 291 of file module_I2C.h.

#define I2C1_check_start_bit ( )    (SSPSTATbits.S)

This macro checks if the stop bit has been detected.

Parameters
void
Returns
flag It returns Stop bit flag. 0: Start bit is not detected 1: Start bit has been detected

Definition at line 324 of file module_I2C.h.

#define I2C1_check_stop_bit ( )    (SSPSTATbits.P)

This macro checks if the stop bit has been detected.

Parameters
void
Returns
flag It returns Stop bit flag. 0: Stop bit is not detected 1: Stop bit has been detected

Definition at line 307 of file module_I2C.h.

#define I2C1_negative_ACK ( )    SSPCON2bits.ACKDT=1, SSPCON2bits.ACKEN=1;while(SSPCON2bits.ACKEN)

This macro release a negative ACK sequence NACK

Parameters
void
Returns
void

Definition at line 387 of file module_I2C.h.

#define I2C1_read_byte ( )    (SSPBUF)

This macro reads the input buffer.

Parameters
void
Returns
Input buffer value

Definition at line 134 of file module_I2C.h.

#define I2C1_restart_communication ( )    SSPCON2bits.RSEN=1;while(SSPCON2bits.RSEN)

This macro restarts the I2C module

Parameters
void
Returns
void

Definition at line 371 of file module_I2C.h.

#define I2C1_set_master_as_receiver ( )    SSPCON2bits.RCEN = 1;while (SSPCON2bits.RCEN)

This macro set the I2C module as receiver to enable the data read (read sequence).

Parameters
void
Returns
void

Definition at line 403 of file module_I2C.h.

#define I2C1_wait_bus_IDLE ( )    while((SSPCON2 & 0x1F) | (SSPSTATbits.R_W))

This function waits for the bus to be Idle before continuing.

Parameters
void
Returns
void

Definition at line 168 of file module_I2C.h.

#define I2C_MASTER   0b00001000

Definition at line 58 of file module_I2C.h.

#define I2C_SLAVE_10_BIT_ADDRESS   0b00000111

Definition at line 57 of file module_I2C.h.

#define I2C_SLAVE_7_BIT_ADDRESS   0b00000110

Definition at line 56 of file module_I2C.h.

#define I2C_SLEW_OFF   0b10000000

Definition at line 64 of file module_I2C.h.

#define I2C_SLEW_ON   0b00000000

Definition at line 61 of file module_I2C.h.

Function Documentation

void I2C1_baud_rate ( unsigned int  bus_baud_rate_KHZ)

This function sets the bus baudrate in case the device is a Master device.

Parameters
bus_baud_rate_KHZBaud Rate espressed in KHz [min: 1, max:400]
Returns
void

You must not set the buadrate for a slave device. Doing so may change the slave address.

Definition at line 107 of file module_I2C.c.

References I2C_CLOCK.

void I2C1_open ( unsigned char  device_type)

This function Opens the I2C module enabling SCL and SDA lines.

Parameters
device_typeIt specifies if the device is a Master or a Slave [I2C_MASTER, I2C_SLAVE_7_BIT_ADDRESS, I2C_SLAVE_10_BIT_ADDRESS]
Returns
void

You must use the constants I2C_MASTER, I2C_SLAVE_7_BIT_ADDRESS, I2C_SLAVE_10_BIT_ADDRESS. By default the slew rate is turned on to support communications up to 400KHz.

Definition at line 47 of file module_I2C.c.

References I2C1_read_byte, I2C_SLEW_ON, and PIN_AS_INPUT.

signed char I2C1_read_byte_from_external_device ( unsigned char  control_byte,
unsigned char  register_address 
)

This function read a byte from an external device made of 1 control byte, 1 addres byte.

Returns
error Communication error or data byte 0: No communication error -1: Bus Collision error -2: Not ACK Error -3: Write Collision
void

Definition at line 399 of file module_I2C.c.

References I2C1_negative_ACK, I2C1_restart_communication, I2C1_start_bit(), I2C1_stop_bit(), I2C1_wait_bus_IDLE, and I2C1_write_byte().

void I2C1_reset_write_collision_flag ( void  )

This function resets the write collision bit.

Parameters
void
Returns
void

Definition at line 280 of file module_I2C.c.

void I2C1_set_slave_address ( unsigned int  device_address)

This function sets I2C slave address.

Parameters
device_addressSlave Address
Returns
void

Definition at line 121 of file module_I2C.c.

void I2C1_start_bit ( void  )

This function releases the start bit and waits until Start bit is out (idle).

Parameters
void
Returns
void

This is a blocking function.

Definition at line 139 of file module_I2C.c.

void I2C1_stop_bit ( void  )

This function releases the stop bit and waits until Stop bit is out (idle).

Parameters
void
Returns
void

This is a blocking function.

Definition at line 160 of file module_I2C.c.

signed char I2C1_write_byte ( unsigned char  byte_to_send)

This function writes a byte to the output buffer and performs several error tests.

Parameters
byte_to_sendByte to be sent out
Returns
error 0: ACK received for the byte sent out -1: Bus Conflict occurs -2: NACK has been received -3: Unkown Error

Definition at line 177 of file module_I2C.c.

References I2C1_wait_bus_IDLE.

signed char I2C1_write_byte_to_external_device ( unsigned char  control_byte,
unsigned char  register_address,
unsigned char  data 
)

This function write a byte to an external device made of 1 control byte, 1 addres byte and 1 byte data.

Parameters
control_byteControl byte of the external device register_address Address to be written within the external device data Data to be wirtten within the pointed address
Returns
error Communication error 0: No communication error -1: Bus Collision error -2: Not ACK Error -3: Write Collision

Definition at line 294 of file module_I2C.c.

References I2C1_start_bit(), I2C1_stop_bit(), I2C1_wait_bus_IDLE, and I2C1_write_byte().