49 unsigned char dummy_read;
61 SSPCON1 |= device_type;
67 SSPCON1 |= 0b00100000;
74 #ifdef I2C_MODULE_TYPE_2
76 void I2C2_open (
unsigned char device_type){
78 unsigned char dummy_read;
90 SSP2CON1 |= device_type;
96 SSP2CON1 |= 0b00100000;
99 dummy_read = I2C2_read_byte ();
109 SSPADD = ((
I2C_CLOCK /1000)/(4 *bus_baud_rate_KHZ))-1;
112 #ifdef I2C_MODULE_TYPE_2
113 void I2C2_baud_rate (
unsigned int bus_baud_rate_KHZ){
114 SSP2ADD = ((
I2C_CLOCK /1000)/(4 *bus_baud_rate_KHZ))-1;
124 SSPADD = (
unsigned char) device_address;
127 #ifdef I2C_MODULE_TYPE_2
128 void I2C2_set_slave_address (
unsigned int device_address){
129 SSP2ADD = (
unsigned char) device_address;
143 while(SSPCON2bits.SEN);
147 #ifdef I2C_MODULE_TYPE_2
148 void I2C2_start_bit (
void){
151 while(SSP2CON2bits.SEN);
162 while(SSPCON2bits.PEN);
166 #ifdef I2C_MODULE_TYPE_2
167 void I2C2_stop_bit (
void){
170 while(SSP2CON2bits.PEN);
180 SSPBUF = byte_to_send;
183 if ( SSPCON1bits.WCOL )
188 if( ((SSPCON1&0x0F)!=0x08) && ((SSPCON1&0x0F)!=0x0B) ) {
194 while ( !PIR1bits.SSPIF );
197 if ( ( !SSPSTATbits.R_W ) && ( !SSPSTATbits.BF )){
207 if( ((SSPCON1&0x0F)==0x08) || ((SSPCON1&0x0F)==0x0B) ) {
210 while( SSPSTATbits.BF );
215 if (SSPCON2bits.ACKSTAT )
226 #ifdef I2C_MODULE_TYPE_2
227 signed char I2C2_write_byte (
unsigned char byte_to_send){
230 SSP2BUF = byte_to_send;
233 if ( SSP2CON1bits.WCOL )
238 if( ((SSP2CON1&0x0F)!=0x08) && ((SSP2CON1&0x0F)!=0x0B) ) {
240 SSP2CON1bits.CKP = 1;
243 while ( !PIR3bits.SSP2IF );
246 if ( ( !SSP2STATbits.R_W ) && ( !SSP2STATbits.BF )){
256 if( ((SSP2CON1&0x0F)==0x08) || ((SSP2CON1&0x0F)==0x0B) ) {
259 while( SSP2STATbits.BF );
261 I2C2_wait_bus_IDLE();
264 if ( SSP2CON2bits.ACKSTAT )
284 #ifdef I2C_MODULE_TYPE_2
285 void I2C2_reset_write_collision_flag (
void){
300 if ( PIR2bits.BCLIF ){
311 if ( !SSPCON2bits.ACKSTAT )
318 if ( !SSPCON2bits.ACKSTAT ){
336 if ( PIR2bits.BCLIF ){
343 #ifdef I2C_MODULE_TYPE_2
344 signed char I2C2_write_byte_to_external_device (
unsigned char control_byte,
unsigned char register_address,
unsigned char data){
346 I2C2_wait_bus_IDLE();
350 if (PIR3bits.BCL2IF){
355 if ( I2C2_write_byte(control_byte)){
361 if (!SSP2CON2bits.ACKSTAT){
363 if (I2C2_write_byte(register_address)) {
369 if ( !SSP2CON2bits.ACKSTAT ){
370 if (I2C2_write_byte(data)) {
387 if (PIR3bits.BCL2IF){
414 if (!SSPCON2bits.ACKSTAT){
422 if (!SSPCON2bits.ACKSTAT) {
427 if ( PIR2bits.BCLIF ){
438 if (!SSPCON2bits.ACKSTAT ){
440 SSPCON2bits.RCEN = 1;
442 while ( SSPCON2bits.RCEN );
451 if (PIR2bits.BCLIF) {
471 return ((
unsigned char) SSPBUF);
474 #ifdef I2C_MODULE_TYPE_2
475 signed char I2C2_read_byte_from_external_device (
unsigned char control_byte,
unsigned char register_address){
476 I2C2_wait_bus_IDLE();
480 if (PIR3bits.BCL2IF){
483 if (I2C2_write_byte(control_byte)) {
489 if (!SSP2CON2bits.ACKSTAT){
491 if (I2C2_write_byte(register_address)){
497 if (!SSP2CON2bits.ACKSTAT) {
499 I2C2_restart_communication();
502 if (PIR3bits.BCL2IF){
507 if (I2C2_write_byte(control_byte+1)){
513 if (!SSP2CON2bits.ACKSTAT){
515 SSP2CON2bits.RCEN = 1;
517 while (SSP2CON2bits.RCEN);
526 if (PIR3bits.BCL2IF) {
545 return ((
unsigned char) SSP2BUF);