View previous topic :: View next topic |
Author |
Message |
denis_11
Joined: 13 Jul 2010 Posts: 45
|
i2c_isr_state for writing |
Posted: Wed Mar 02, 2011 10:05 am |
|
|
Hi, I'm testing the i2c Slave with a 18F14k50. For now, I simulate an 256Bytes eeprom. Now trying IC-Prog and an eeprom programmer connected with the PIC, reading is working, but the writing does not work.
Which may be the problem? This is the code:
Code: |
#include <18F14k50.h>
#fuses INTRC_IO, NOWDT,NOMCLR, PUT, BROWNOUT, NOLVP, NOPLLEN, CPUDIV1
#use delay(clock=4000000)
#use i2c(SLAVE,fast, SDA=PIN_B4, SCL=PIN_B6, address=0xa0,FORCE_HW)
BYTE address=0,buffer[256];
#byte SSPSTAT = 0xFC7
#byte SSPBUF = 0xFC9
#bit DA_BIT = SSPSTAT.5
#bit RW_BIT = SSPBUF.0
#inline
int16 my_i2c_isr_state(void)
{
static int16 i2c_state = 0x03;
int16 retval;
if(!DA_BIT) // If address byte was received
{
i2c_state = 0; // Then clear state.
if(RW_BIT) // If it is a Read address
bit_set(i2c_state, 7); // Then set bit 7 of state.
}
retval = i2c_state;
i2c_state++;
return(retval);
}
#INT_SSP
void i2c_isr() {
BYTE incoming;int16 state;
state = my_i2c_isr_state();
if(state<0x80){
incoming=i2c_read();
if(state==1){
address=incoming;
}
if(state>1){
buffer[address++] =incoming;
}
}
if(state>=0x80){
i2c_write(buffer[address++]);
}
}
void main ()
{
setup_oscillator(OSC_4MHZ);
int16 i;
for(i=0;i<256;i++){
buffer[i]=0xFF;
}
BUFFER[0]=0xEE; //
BUFFER[1]=0xE1; // test reading
BUFFER[2]=0xE2; //
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while (TRUE) {}
}
|
I use an internal clock, can adversely affect this? thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 02, 2011 5:36 pm |
|
|
Post the test program that for the Master PIC that talks to this i2c slave PIC.
Post your compiler version.
Also, I think you are trying to do page mode. Your Master PIC test
program will show this, if so.
Also, is this a Proteus project, or is it in real hardware ? |
|
|
denis_11
Joined: 13 Jul 2010 Posts: 45
|
|
Posted: Wed Mar 02, 2011 8:46 pm |
|
|
hi, it is in real hardware... I'm testing this in real hardware. My compiler version is 4.119.
The program I use is IC-Prog v1.06B and an eeprom programmer that already works with normal type eeprom 24LC16. I have tried both with the page write then without the page write but the result does not change...but reading from IC-Prog works well...wrong setting of crystal in my code?? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 02, 2011 11:10 pm |
|
|
Do you have a 2nd PIC, which is the i2c Master ?
If so, post the source code for it.
For example, here is a test program for an i2c Master PIC, that talks
to the CCS Ex_Slave.c code:
http://www.ccsinfo.com/forum/viewtopic.php?t=32368&start=3
Post your program for your i2c Master PIC. |
|
|
denis_11
Joined: 13 Jul 2010 Posts: 45
|
|
Posted: Thu Mar 03, 2011 5:58 am |
|
|
no, I do not use a pic as a master ... I use the eeprom programmer and IC-Prog as a master ... IC-Prog uses 0xA0 as device address, for reading SEQUENTIAL RANDOM READ mode, and writing or PAGE WRITE or WRITE BYTE. I would like to emulate an EEPROM...is this possible? |
|
|
denis_11
Joined: 13 Jul 2010 Posts: 45
|
|
Posted: Thu Mar 03, 2011 6:47 pm |
|
|
No one knows more help? I tried with different frequencies of internal clock and I see that change the speed of i2c slave, in fact I have also set the delay in the configuration of IC-Prog to get a good reading ... this only happens with less than 4 MHz internal crystal oscillator... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Mar 03, 2011 6:52 pm |
|
|
Some things to do:
1. Try your code with a different PIC such as the 18F452.
2. Try it with the CCS example file, Ex_slave.c, which emulates a 24LC01
eeprom. Program that file into an 18F452 and see if you can talk to it
with icprog. |
|
|
denis_11
Joined: 13 Jul 2010 Posts: 45
|
|
Posted: Fri Mar 04, 2011 8:44 am |
|
|
ok thanks. Why do I suggest to use the PIC 18F452? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Fri Mar 04, 2011 10:17 am |
|
|
As one other little comment, try increasing your clock rate.
Though the I2C transaction should be 'held' by the slave as it takes quite a long time to enter the interrupt handler, it has been documented in the past that problems result, when you have a PIC I2C slave running a significantly lower clock rate than the corresponding master.
On the 452, 'known quantity'. A lot of the newer chips have issues with some peripherals (haven't checked if yours is one of these), but the I2C on the 452, is a well known 'working' version.
Best Wishes |
|
|
|