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 support@ccsinfo.com

Not able to re-enable I2C interrupts after using spi.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Newuser
Guest







Not able to re-enable I2C interrupts after using spi.
PostPosted: Fri Apr 11, 2008 5:13 pm     Reply with quote

Hi All,

I have I2C & SPI sharing same lines.

I am using to I2C bus to communicate with another pic, and spi bus to read/write ext eeprom.

To write/read from eeprom using SPI, i am disabling I2C interrupts and then i am able to read/ write to EEPROM. But i am not able to bring back my I2C to communicate to the other PIC.
Here, my processor acting as a I2C slave.

So, my question is
How to bring up my i2c after using SPI??

Thanks a lot in advance.

Regards
Dwarag.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 6:05 pm     Reply with quote

You didn't specify your PIC, so I did a test for the 18F452.
The code which configures the MSSP module for hardware i2c
or SPI is shown below. Notice that there are certain things
in common between the two modes. The setup_spi() statement
changes some of the settings when it sets up the SPI mode.

You could create a routine called setup_i2c(), and simply
duplicate the #use i2c() setup code shown below. Use
the output_float() function to set the pins to inputs. Also
use #byte statements to define the addresses of the SSPCON
and SSPSTAT registers. Then write a line of code for each
one to load it with the correct value for i2c mode.
When you duplicate the i2c code, you might want to put in
a line at the start of it, to disable the SSP module, the same
way CCS does it in the setup_spi() function. This will reset
the module.

Then call the setup_i2c() function when you want to switch
back to using the MSSP module in i2c mode.

This is the initialization code that is inserted at the start of main()
when you specify the following #use i2c() statement.
Code:

#use i2c(Master, sda=PIN_C4, scl=PIN_C3, FORCE_HW)

002A:  BSF    TRISC.3   // SCL = input   (pin C3)
002C:  BSF    TRISC.4   // SDA = input  (pin C4)

002E:  MOVLW  09
0030:  MOVWF  SSPADD   // Set i2c baud rate

0032:  MOVLW  28
0034:  MOVWF  SSPCON1  // Set mode

0036:  BSF    SSPSTAT.SMP   // Set SMP bit = 1
0038:  BCF    SSPSTAT.CKE   // Set CKE bit = 0



This is what the setup_spi() statement does:
Code:

... setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_16);
0022:  BCF    SSPCON1.SSPEN   // Disable SSP module

0024:  BCF    TRISC.5   // SDO = output  (pin C5)
0026:  BSF    TRISC.4   // SDI = input  (pin C4)
0028:  BCF    TRISC.3   // SCK = output   (pin C3)

002A:  MOVLW  21   // Set mode = SPI Master, and enable SSP
002C:  MOVWF  SSPCON1 

002E:  MOVLW  40    // Set CKE bit = 1, all others = 0
0030:  MOVWF  SSPSTAT
Ttelmah
Guest







PostPosted: Sat Apr 12, 2008 2:09 am     Reply with quote

As one more comment, after setting up the I2C, I'd clear the interrupt flag. Remember the hardware for both functions is 'common', so this interrupt will occur for SPI character transfers, as well as for I2C transfers. After the SPI transactions, the flag will probably be set, but the contents of the registers will make little 'sense' to an I2C interrupt handler, and might lead to this getting very confused. Hence change the mode to I2C, then clear the interrupt, before re-enabling this.

Best Wishes
New user
Guest







PostPosted: Mon Apr 14, 2008 10:16 am     Reply with quote

Hi,

Thanks for the reply.

I am using PIC18f4680, i have tried your suggestion in my code. But, i couldn't re-enable the i2c interrupt yet.

Is it because of different processor?
If you have some examples in C code, please send it to me.

Thanks for your kind help


Regards
Dwarag.
Newuser
Guest







PostPosted: Fri Apr 18, 2008 12:29 pm     Reply with quote

Thanks for your kind help.

After using spi, i overwritten the SSPCON register with value 0x36.
I could receive messages from the other pic, but was not able reply.

This problem is because of the overflow bit is set in the sspcon register, so in the interrupt handler i clear this bit, it worked for me.

So, if somebody facing this problem can try this method, it may help you.

Regards
Dwarag.
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