View previous topic :: View next topic |
Author |
Message |
vtrx
Joined: 11 Oct 2017 Posts: 142
|
18F2550 I2c Software |
Posted: Tue Sep 14, 2021 4:50 pm |
|
|
I need to use a 24C08 memory with the 18F2550, but using software routines.
I use native SPI for another circuit.
Using the described methods, I can't get results in the simulation.
Is the described procedure correct?
Code: | #use I2C(sda=PIN_A1, scl=PIN_A0, FAST=100000)
...
void ext_24c08_write(unsigned int8 address,data)
{
i2c_start();
i2c_write(WRITE_ADDR);
i2c_write(address);
i2c_write(data);
i2c_stop();
}
int8 ext_24c08_read(unsigned int8 address)
{
int8 result;
i2c_start();
i2c_write(WRITE_ADDR);
i2c_write(address);
i2c_start();
i2c_write(READ_ADDR);
result = i2c_read(1);
i2c_stop();
return(result);
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Tue Sep 14, 2021 5:59 pm |
|
|
simulation' ?
What are you using as a 'simulator' ?? |
|
|
vtrx
Joined: 11 Oct 2017 Posts: 142
|
|
Posted: Tue Sep 14, 2021 7:31 pm |
|
|
Before losing more hair, I simulated with Proteus.
Are the codes correct? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Tue Sep 14, 2021 7:47 pm |
|
|
sorry, but I don't use Proteus. It's well know to not work 100% as a PIC simulator. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 14, 2021 8:04 pm |
|
|
Why don't you use the 2408.c file provided by CCS in the Drivers folder ?
It's significantly different than what you posted. You should try it. |
|
|
vtrx
Joined: 11 Oct 2017 Posts: 142
|
|
Posted: Wed Sep 15, 2021 9:08 am |
|
|
PCM programmer wrote: | Why don't you use the 2408.c file provided by CCS in the Drivers folder ?
It's significantly different than what you posted. You should try it. |
Thanks.
I'm going to use this method, anyway the problem was in emulation.
Every time I want to use a communication, which the microcontroller has implemented by hardware, and I want to use it by software, I just need to choose non-standard pins? |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Wed Sep 15, 2021 9:14 am |
|
|
You can also use the FORCE_SW switch under I2C.
See the #use I2C section in the manual _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Wed Sep 15, 2021 9:24 am |
|
|
Honestly I'd suspect the 'simulation'. It probably cannot actually handle
interpreting software I2C.
The reason the standard driver is different, is that the address for the
2408, is actually a 10bit value, and the extra 2 bits have to be masked
into the device address sent. However omitting these (provided your
value for WRITE_ADDR, and READ_ADDR, have sensible settings for these,
that match), should not matter. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 15, 2021 9:55 am |
|
|
Quote: | int8 ext_24c08_read(unsigned int8 address)
{
int8 result;
i2c_start();
i2c_write(WRITE_ADDR);
i2c_write(address);
i2c_start();
i2c_write(READ_ADDR);
result = i2c_read(1);
i2c_stop();
return(result);
} |
Also, your code above is incorrect. The last i2c_read() should have
a parameter of 0. Look at the CCS code. They have a 0. This is part
of the i2c spec. |
|
|
vtrx
Joined: 11 Oct 2017 Posts: 142
|
|
Posted: Wed Sep 15, 2021 3:00 pm |
|
|
I'm using 2408.c (write_ext_eeprom(x,x)/read_ext_eeprom(x)). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Thu Sep 16, 2021 3:53 am |
|
|
PCM is pointing out why your original code would not work.
Learn from him for any future code you write. |
|
|
|