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;
138 while ((SSPCON2 & 0x1F) | (SSPSTATbits.R_W));
142 #ifdef I2C_MODULE_TYPE_2
143 void I2C2_wait_bus_IDLE (
void){
144 while ((SSP2CON2 & 0x1F) | (SSP2STATbits.R_W));
156 while(SSPCON2bits.SEN);
160 #ifdef I2C_MODULE_TYPE_2
161 void I2C2_start_bit (
void){
164 while(SSP2CON2bits.SEN);
175 while(SSPCON2bits.PEN);
179 #ifdef I2C_MODULE_TYPE_2
180 void I2C2_stop_bit (
void){
183 while(SSP2CON2bits.PEN);
193 SSPBUF = byte_to_send;
196 if ( SSPCON1bits.WCOL )
201 if( ((SSPCON1&0x0F)!=0x08) && ((SSPCON1&0x0F)!=0x0B) ) {
207 while ( !PIR1bits.SSPIF );
210 if ( ( !SSPSTATbits.R_W ) && ( !SSPSTATbits.BF )){
220 if( ((SSPCON1&0x0F)==0x08) || ((SSPCON1&0x0F)==0x0B) ) {
223 while( SSPSTATbits.BF );
228 if (SSPCON2bits.ACKSTAT )
239 #ifdef I2C_MODULE_TYPE_2
240 signed char I2C2_write_byte (
unsigned char byte_to_send){
243 SSP2BUF = byte_to_send;
246 if ( SSP2CON1bits.WCOL )
251 if( ((SSP2CON1&0x0F)!=0x08) && ((SSP2CON1&0x0F)!=0x0B) ) {
253 SSP2CON1bits.CKP = 1;
256 while ( !PIR3bits.SSP2IF );
259 if ( ( !SSP2STATbits.R_W ) && ( !SSP2STATbits.BF )){
269 if( ((SSP2CON1&0x0F)==0x08) || ((SSP2CON1&0x0F)==0x0B) ) {
272 while( SSP2STATbits.BF );
274 I2C2_wait_bus_IDLE();
277 if ( SSP2CON2bits.ACKSTAT )
297 #ifdef I2C_MODULE_TYPE_2
298 void I2C2_reset_write_collision_flag (
void){
313 if ( PIR2bits.BCLIF ){
324 if ( !SSPCON2bits.ACKSTAT )
331 if ( !SSPCON2bits.ACKSTAT ){
349 if ( PIR2bits.BCLIF ){
356 #ifdef I2C_MODULE_TYPE_2
357 signed char I2C2_write_byte_to_external_device (
unsigned char control_byte,
unsigned char register_address,
unsigned char data){
359 I2C2_wait_bus_IDLE();
363 if (PIR3bits.BCL2IF){
368 if ( I2C2_write_byte(control_byte)){
374 if (!SSP2CON2bits.ACKSTAT){
376 if (I2C2_write_byte(register_address)) {
382 if ( !SSP2CON2bits.ACKSTAT ){
383 if (I2C2_write_byte(data)) {
400 if (PIR3bits.BCL2IF){
427 if (!SSPCON2bits.ACKSTAT){
435 if (!SSPCON2bits.ACKSTAT) {
440 if ( PIR2bits.BCLIF ){
451 if (!SSPCON2bits.ACKSTAT ){
453 SSPCON2bits.RCEN = 1;
455 while ( SSPCON2bits.RCEN );
464 if (PIR2bits.BCLIF) {
484 return ((
unsigned char) SSPBUF);
487 #ifdef I2C_MODULE_TYPE_2
488 signed char I2C2_read_byte_from_external_device (
unsigned char control_byte,
unsigned char register_address){
489 I2C2_wait_bus_IDLE();
493 if (PIR3bits.BCL2IF){
496 if (I2C2_write_byte(control_byte)) {
502 if (!SSP2CON2bits.ACKSTAT){
504 if (I2C2_write_byte(register_address)){
510 if (!SSP2CON2bits.ACKSTAT) {
512 I2C2_restart_communication();
515 if (PIR3bits.BCL2IF){
520 if (I2C2_write_byte(control_byte+1)){
526 if (!SSP2CON2bits.ACKSTAT){
528 SSP2CON2bits.RCEN = 1;
530 while (SSP2CON2bits.RCEN);
539 if (PIR3bits.BCL2IF) {
558 return ((
unsigned char) SSP2BUF);