CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

software I2C 18F2550 problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
eng.mohamedtoumi



Joined: 13 Jan 2018
Posts: 23
Location: Tun

View user's profile Send private message Visit poster's website

software I2C 18F2550 problem
PostPosted: Mon Sep 10, 2018 7:17 pm     Reply with quote

Hello !! Very Happy Very Happy
i want to connect two MCUs 18F2550 Via I2C (Slave writes data / Master reads the data)
Yet there is a problem when i use the software I2C on Slave MCU (i need SW I2C because i need to use the external interrupts )...the data sent is wrong (data received =0xFF) and when i switch to HW_I2C the data becomes Correct ... any solutions?

Master Code
Code:
 #include <18F2550.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay (internal=8M)
#use I2C(MASTER, SDA=PIN_B6, SCL=PIN_B7,FAST=450000 ,FORCE_SW)
#include "lcd.c"
int8 data ;

void clear_screen(){   //to clear the LCD
lcd_gotoxy(1,1);
printf(lcd_putc,"                           ") ;
lcd_gotoxy(1,2);
printf(lcd_putc,"                           ") ;
}

void main(){
 lcd_init();
 setup_oscillator(OSC_8MHZ);
 while(true){                   
        i2c_start();   // start condition
        delay_ms(10); 
        i2c_write(0x12+1);
        delay_ms(10);
        data = i2c_read(0);
        delay_ms(10);
        i2c_stop();
        clear_screen();
        lcd_gotoxy(1,1);
        printf (lcd_putc,"I'M THE MASTER"); 
        lcd_gotoxy(1,2);
        printf (lcd_putc,"I RECEIVED %d ", data); 
        delay_ms(500);
       
}
}


Slave code (slave writes DATA)

Code:
#include <18F2550.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay (internal=8M)
#use I2C(slave,SDA=PIN_B6, SCL=PIN_B7, address=0x12 ,FAST=450000 ,FORCE_SW)
#include "lcd.c"
int8 data=15 ;

#INT_SSP
void ssp_interrupt()

int8 incoming, state ;
state = i2c_isr_state();   
if(state < 0x80)     // Master is sending data
  {
   incoming = i2c_read();
  }

if(state >= 0x80)   // Master is requesting data from slave
  {
   i2c_write(data);
  }
}

void clear_screen(){   //to clear the LCD
lcd_gotoxy(1,1);
printf(lcd_putc,"                           ") ;
lcd_gotoxy(1,2);
printf(lcd_putc,"                           ") ;
}

void main(){
 lcd_init();
 setup_oscillator(OSC_8MHZ);
 enable_interrupts(INT_SSP);
 enable_interrupts(GLOBAL);
 while(true){                   
        lcd_gotoxy(1,1);
        printf (lcd_putc,"I'M SLAVE"); 
        lcd_gotoxy(1,2);
        printf (lcd_putc,"I'M SENDING %d",data); 
        delay_ms(500);
}
}


Note : I'm using 4.7kohms as pullups
elcrcp



Joined: 11 Mar 2016
Posts: 62
Location: izmir / Turkey

View user's profile Send private message

PostPosted: Mon Sep 10, 2018 9:48 pm     Reply with quote

I'm not sure about this but I don't think you can use ssp interrupt if you are not using hardware dedicated ssp pins.
_________________
There is nothing you can't do if you try
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Sep 11, 2018 4:56 am     Reply with quote

I'd have to agree, interrupts are only available for the internal HW peripherals. Same holds true for the UARTS. If you create a SW UART, you don't have an ISR for it. If you dump the listing ,you'll see the assembly code and the related calls to PIC registers.
Possible workarounds are
1) reconfigure the PIC so you can use the HW I2C
2) recode to have a very tight(fast and small) loop in main to check the I2C incoming data.

#1 would be the best, #2 will work providing not much else is going on...

Jay
eng.mohamedtoumi



Joined: 13 Jan 2018
Posts: 23
Location: Tun

View user's profile Send private message Visit poster's website

PostPosted: Tue Sep 11, 2018 6:37 am     Reply with quote

Thank you for your reply.
1) reconfigure the PIC so you can use the HW I2C does it mean that i lose the external interrupts ... if not HOW ?!
2) recode to have a very tight (fast and small) loop in main to check the I2C incoming data. (for this solution i think that this can cause bus collision problems the slave and the master should be synchronized)
Any other solutions ?
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Sep 11, 2018 7:10 am     Reply with quote

That PIC has 4 interrupt-on-change pins, RB4,5,6,7. Perhaps you can use them ?
Without knowing what your total requirements are, it's hard to give you the 'perfect solution'.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Tue Sep 11, 2018 11:18 am     Reply with quote

Seriously then manual is your friend. If You read the #use I2C section, yoiu have it saying:

Quote:

The SLAVE mode should only be used with the built-in SSP.


The software library does not properly implement slave operation. To do slave, and especially to use I2C interrupts, you need to be using the hardware peripheral.
Now you say you need external interrupts. How many?.
INT2 is available even when you use the hardware I2C.
If you need more, then as others have said, consider using the INT_RB capability.
eng.mohamedtoumi



Joined: 13 Jan 2018
Posts: 23
Location: Tun

View user's profile Send private message Visit poster's website

PostPosted: Tue Sep 11, 2018 3:22 pm     Reply with quote

Thank you for your reply ... Smile Smile
Yes i need all the external interrupts ... I'm trying to communicate 18F2550 master and Raspberry pi 3 as master (note the Raspberry pi does not support slave mode nor multimaster mode). So i found another solution, which is making an i2c multiplexer.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group