Oj Guest
|
Re-enable i2c |
Posted: Tue May 20, 2008 11:08 am |
|
|
I am sharing the HW SPI/i2c bus with an external EEPROM and another PIC. The problem is that I cannot get the i2c to re-enable. Initially if I read data from the PIC using the i2c bus it returns the correct data. Then I read data from the SPI EEPROM... works fine but when I try to read data from the PIC again.. I just get FF back. What am I doing wrong?
I have read several posts on how to re-enable i2c after using spi but none seem to work. Can someone please provide example code?
Thanks in advance!
I am using the CCS compiler ver 4.057
SLAVE CODE:
Code: |
#include <18F87J60.h>
#fuses NOWDT, NODEBUG, HS, NOIESO, NOFCMEN, PRIMARY, ETHLED
#use delay(clock=25M)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xE0)
#use rs232(baud=9600, parity=N ,xmit=PIN_C6, rcv=PIN_C7, stream=UART,bits=8)
int buffer[0x10] = {0x41,0x42,0x43,0x44,0x45,0x46} ;
#byte SSPCON=0xFC6
#bit SSPEN=SSPCON.5
#bit SSPOV=SSPCON.6
#bit WCOL =SSPCON.7
/******************** Enable i2c **********************************************/
void enable_i2c()
{
output_float(PIN_C3);
output_float(PIN_C4);
SSPEN=1;
if(SSPOV){SSPOV=0;}
if(WCOL){WCOL=0;}
clear_interrupt(INT_SSP);
}
/******************** UART Receive Event ***************************************/
#int_rda
void UART_Receive()
{
if(fgetc(UART)=='S')
{
//HERE I READ DATA FROM A SPI EEPROM USING THE FOLLOWING:
//setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_XMIT_L_TO_H|SPI_CLK_DIV_4);
//gets called for every a read/write
//CLK PIN_C3
//EEPROM_DO PIN_C4
//EEPROM_DI PIN_C5
// fprintf(UART,"data=%u\r",read_eeprom_(0));
enable_i2c();
}
}
/******************** I2C Receive Event ***************************************/
#INT_SSP
void ssp_interupt ()
{
int state,temp;
state = i2c_isr_state();
if(state >= 0x80)
{
i2c_write(buffer[state - 0x80]);
}
else
{
temp=i2c_read();
}
}
/******************** Main Loop ************************************************/
void main ()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
enable_interrupts(INT_RDA);
while (TRUE) {}
}
|
MASTER CODE:
Code: |
#include <18F87J60.h>
#fuses NOWDT, NODEBUG, HS, NOIESO, NOFCMEN, PRIMARY, ETHLED
#use delay(clock=25M)
#use i2c(MASTER,SDA=PIN_C4, SCL=PIN_C3)
#use rs232(baud=9600, parity=N ,xmit=PIN_C6, rcv=PIN_C7, stream=UART,bits=8)
/******************** UART Receive Event ***************************************/
#int_rda
void UART_0_Receive()
{
if(fgetc(UART)=='C')
{
int8 data;
fprintf(UART,"Writing data to i2c\n\r");
i2c_start();
i2c_write(0xE1);
data=i2c_read();
fprintf(UART,"read %x \n\r", data);
data=i2c_read();
fprintf(UART,"read %x \n\r", data);
data=i2c_read(0);
i2c_stop();
fprintf(UART,"read %x \n\r", data);
}
}
/******************** Main Loop ************************************************/
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
while(TRUE){}
}
|
|
|