View previous topic :: View next topic |
Author |
Message |
jemmyduc
Joined: 22 May 2013 Posts: 18
|
I2C in 18F4431 |
Posted: Fri Dec 27, 2013 11:15 am |
|
|
This is an interrupt I2C function in slave
Code: | #INT_SSP //gui 1 byte
void i2c_isr()
{
int8 state;
state = i2c_isr_state();
if(state>=0x80)
i2c_write(8);
} |
This is a Read I2C function in master
Code: | int8 read_slave()
{
int8 f;
i2c_start();
i2c_write(slave_address + 1);
f = i2c_read();
i2c_stop();
return f;
} |
when i set time to read I2C from slave.It didn't work.
Nobody can help me fix it. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 27, 2013 11:37 am |
|
|
You're missing a parameter in the call to i2c_read() in the master code:
Quote: | int8 read_slave()
{
int8 f;
i2c_start();
i2c_write(slave_address + 1);
f = i2c_read();
i2c_stop();
return f;
} |
Read the i2c User's manual:
http://www.nxp.com/documents/user_manual/UM10204.pdf
Quote: |
3.1.6 Acknowledge (ACK) and Not Acknowledge (NACK)
There are five conditions that lead to the generation of a NACK:
.
.
.
.
5. A master-receiver must signal the end of the transfer to the slave transmitter.
|
How to do that in CCS ? Read the CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Quote: |
i2c_read(ack) parameters:
ack - Optional, defaults to 1.
0 - indicates do not ack.
1 - indicates to ack.
2 - slave only, indicates to not release clock at end of read. Use when
i2c_isr_state () returns 0x80. stream - specify the stream defined in #USE I2C
|
Fix your code and add the correct parameter. |
|
|
RXLA105
Joined: 27 Dec 2013 Posts: 1
|
|
Posted: Fri Dec 27, 2013 2:15 pm |
|
|
bed bug attorney in Los Angeles wrote: | You're missing a parameter in the call to i2c_read() in the master code:
Quote: | int8 read_slave()
{
int8 f;
i2c_start();
i2c_write(slave_address + 1);
f = i2c_read();
i2c_stop();
return f;
} |
Read the i2c User's manual:
http://www.nxp.com/documents/user_manual/UM10204.pdf
Quote: |
3.1.6 Acknowledge (ACK) and Not Acknowledge (NACK)
There are five conditions that lead to the generation of a NACK:
.
.
.
.
5. A master-receiver must signal the end of the transfer to the slave transmitter.
|
How to do that in CCS ? Read the CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Quote: |
i2c_read(ack) parameters:
ack - Optional, defaults to 1.
0 - indicates do not ack.
1 - indicates to ack.
2 - slave only, indicates to not release clock at end of read. Use when
i2c_isr_state () returns 0x80. stream - specify the stream defined in #USE I2C
|
Fix your code and add the correct parameter. |
Were you able to fix your code as identified above? I wanted to be sure this worked for you as I am having concerns as well.
Last edited by RXLA105 on Wed Feb 05, 2014 2:48 pm; edited 1 time in total |
|
|
jemmyduc
Joined: 22 May 2013 Posts: 18
|
|
Posted: Fri Dec 27, 2013 8:34 pm |
|
|
I fixed follow your guide, but it didn't work. I think my pull up resister is wrong. I used 2.7k resister.
I know that value of pull up resister relative to speed of I2C transmission. If i use 20MHz crystal, you can tell me what is the speed of I2C transmission? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Sat Dec 28, 2013 4:43 am |
|
|
No.....
The speed of the I2C, has nothing (really) to do with the speed of the CPU crystal. You specify the speed in the I2C setup function. Keyword 'slow' implies 100KHz, while 'fast' gives 400KHz. You can also specify your own rate, so: 'fast=200000' sets the bus up to use 200KHz transmission.
2.7K should be fine for any default rate (100K or 400K). What affects the resistor value required is the combination of bus speed, and bus capacitance. The pull up, is the only thing taking the line high. If a bus has a lot of capacitance, and you try to run it fast, then smaller resistors are needed. The minimum you can use depends on the drive capabilities of the devices.
Now you have not told us what device you are trying to talk to.
Most devices support 100KHz (standard I2C). A few old devices only support slower rates. Many newer devices support 400KHz (fast), while some devices support even higher speeds.
So start by reading the data sheet for your device, and finding what bus rate it supports, and selecting this in the I2C setup. If it gives 'min', and 'max' rates select a rate somewhere in the middle.
Best Wishes |
|
|
jemmyduc
Joined: 22 May 2013 Posts: 18
|
|
Posted: Sat Dec 28, 2013 10:19 pm |
|
|
Thank your helping. I got it.
Thank you so much. |
|
|
jemmyduc
Joined: 22 May 2013 Posts: 18
|
|
Posted: Sat Dec 28, 2013 10:36 pm |
|
|
I have a question. Can you answer me?
I use 18F4431, and i set up my I2C is that
Code: | #use i2c(master, sda=PIN_C4, scl=PIN_C5) |
If i don't use FAST or SLOW control word, what is the speed of I2C?
I hope your answer. Thank you. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Dec 28, 2013 10:45 pm |
|
|
The compiler will use software i2c and it depends upon your PIC's
oscillator frequency and possibly on your CCS compiler version.
Please post both of those. |
|
|
jemmyduc
Joined: 22 May 2013 Posts: 18
|
|
Posted: Sun Dec 29, 2013 7:01 am |
|
|
I use 20MHz crystal and 4.114 CCS compiler.
Thank your attention. |
|
|
|