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 unsigned int div_a = (
unsigned int) (
I2C_CLOCK /1000);
110 unsigned int div_b = (4 *bus_baud_rate_KHZ);
111 SSPADD = (
unsigned char)(div_a/div_b)-1;
114 #ifdef I2C_MODULE_TYPE_2
115 void I2C2_baud_rate (
unsigned int bus_baud_rate_KHZ){
116 unsigned int div_a = (
unsigned int) (
I2C_CLOCK /1000);
117 unsigned int div_b = (4 *bus_baud_rate_KHZ);
118 SSP2ADD = (
unsigned char) (div_a/div_b)-1;
128 SSPADD = (
unsigned char) device_address;
131 #ifdef I2C_MODULE_TYPE_2
132 void I2C2_set_slave_address (
unsigned int device_address){
133 SSP2ADD = (
unsigned char) device_address;
147 while(SSPCON2bits.SEN);
151 #ifdef I2C_MODULE_TYPE_2
152 void I2C2_start_bit (
void){
155 while(SSP2CON2bits.SEN);
166 while(SSPCON2bits.PEN);
170 #ifdef I2C_MODULE_TYPE_2
171 void I2C2_stop_bit (
void){
174 while(SSP2CON2bits.PEN);
184 SSPBUF = byte_to_send;
187 if ( SSPCON1bits.WCOL )
192 if( ((SSPCON1&0x0F)!=0x08) && ((SSPCON1&0x0F)!=0x0B) ) {
198 while ( !PIR1bits.SSPIF );
201 if ( ( !SSPSTATbits.R_W ) && ( !SSPSTATbits.BF )){
211 if( ((SSPCON1&0x0F)==0x08) || ((SSPCON1&0x0F)==0x0B) ) {
214 while( SSPSTATbits.BF );
219 if (SSPCON2bits.ACKSTAT )
230 #ifdef I2C_MODULE_TYPE_2
231 signed char I2C2_write_byte (
unsigned char byte_to_send){
234 SSP2BUF = byte_to_send;
237 if ( SSP2CON1bits.WCOL )
242 if( ((SSP2CON1&0x0F)!=0x08) && ((SSP2CON1&0x0F)!=0x0B) ) {
244 SSP2CON1bits.CKP = 1;
247 while ( !PIR3bits.SSP2IF );
250 if ( ( !SSP2STATbits.R_W ) && ( !SSP2STATbits.BF )){
260 if( ((SSP2CON1&0x0F)==0x08) || ((SSP2CON1&0x0F)==0x0B) ) {
263 while( SSP2STATbits.BF );
265 I2C2_wait_bus_IDLE();
268 if ( SSP2CON2bits.ACKSTAT )
288 #ifdef I2C_MODULE_TYPE_2
289 void I2C2_reset_write_collision_flag (
void){
304 if ( PIR2bits.BCLIF ){
315 if ( !SSPCON2bits.ACKSTAT )
322 if ( !SSPCON2bits.ACKSTAT ){
340 if ( PIR2bits.BCLIF ){
347 #ifdef I2C_MODULE_TYPE_2
348 signed char I2C2_write_byte_to_external_device (
unsigned char control_byte,
unsigned char register_address,
unsigned char data){
350 I2C2_wait_bus_IDLE();
354 if (PIR3bits.BCL2IF){
359 if ( I2C2_write_byte(control_byte)){
365 if (!SSP2CON2bits.ACKSTAT){
367 if (I2C2_write_byte(register_address)) {
373 if ( !SSP2CON2bits.ACKSTAT ){
374 if (I2C2_write_byte(data)) {
391 if (PIR3bits.BCL2IF){
418 if (!SSPCON2bits.ACKSTAT){
426 if (!SSPCON2bits.ACKSTAT) {
431 if ( PIR2bits.BCLIF ){
442 if (!SSPCON2bits.ACKSTAT ){
444 SSPCON2bits.RCEN = 1;
446 while ( SSPCON2bits.RCEN );
455 if (PIR2bits.BCLIF) {
480 #ifdef I2C_MODULE_TYPE_2
481 signed char I2C2_read_byte_from_external_device (
unsigned char control_byte,
unsigned char register_address,
unsigned char *data){
482 I2C2_wait_bus_IDLE();
486 if (PIR3bits.BCL2IF){
489 if (I2C2_write_byte(control_byte)) {
495 if (!SSP2CON2bits.ACKSTAT){
497 if (I2C2_write_byte(register_address)){
503 if (!SSP2CON2bits.ACKSTAT) {
505 I2C2_restart_communication();
508 if (PIR3bits.BCL2IF){
513 if (I2C2_write_byte(control_byte+1)){
519 if (!SSP2CON2bits.ACKSTAT){
521 SSP2CON2bits.RCEN = 1;
523 while (SSP2CON2bits.RCEN);
532 if (PIR3bits.BCL2IF) {