View previous topic :: View next topic |
Author |
Message |
imatshego
Joined: 12 May 2010 Posts: 6 Location: South Africa
|
PIC 18F2220 I2C |
Posted: Wed May 12, 2010 1:40 am |
|
|
Hi Guys
I am new to CCS and hardware design. I have done some basic C programming on PIC 18F2220 with LCD and 7Segment displays. I would like to up my game by using the same pic but using I2C communication. I would like to start off with the PIC being the master and any other device as a slave. My concern is that how would I know which PICs and Devices can communicate using I2C? What are the basics that one would check to see if the PIC or device is I2C comparable using CCS and Proteus 7 Professional?
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 12, 2010 12:12 pm |
|
|
Quote: | My concern is that how would I know which PICs and Devices can
communicate using I2C? |
To be an i2c slave, the PIC must have an SSP or MSSP module. This
will be listed in the Features section, near the start of the PIC data sheet.
Any PIC can do a software i2c Master, using normal i/o pins. To do a
hardware i2c master, it must have an MSSP module.
You can use this Microchip Parts Selector page to find such PICs:
http://www.microchip.com/maps/microcontroller.aspx
Set the Device Prefix (at the top) to 16F, for example.
Then set the first column for I2C to 1. The screen will automatically
update itself a few seconds after you change each setting. Then go look
at the Search Results box at the top to see the selected PICs. This will
show all PICs that at least have an SSP module. |
|
|
imatshego
Joined: 12 May 2010 Posts: 6 Location: South Africa
|
|
Posted: Thu May 13, 2010 3:13 am |
|
|
Thanks PCM programmer. I followed the steps and came out ok. I also downloaded data sheets for the pics and going through them.
Another thing that I would like to know is, when using PIC CCS wizard to setup my pic, lets say for example PIC18f2220 and I forget to select I2C under Communication or forget to set Timers or Oscillator, can I change my code after the code has been generated by the wizard or would I have to re-run the wizard and make the necessary changes? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Thu May 13, 2010 4:26 am |
|
|
Most long term users, don't use the 'Wizard' at all....
Yes, you can change the code.
The big problem with the Wizard, is that it fairly often generates 'pointless' setups, for things that are already setup OK at boot, and even worse, quite frequently uses settings, that are wrong.
Best Wishes |
|
|
imatshego
Joined: 12 May 2010 Posts: 6 Location: South Africa
|
|
Posted: Thu May 13, 2010 4:53 am |
|
|
Thanks Thelmah I had noticed that myself that code is useless and not very self explanatory.
I have a clear understanding of I2C and devices being used and their connection. If I were to connect a PIC18f2220 as a master to a DS307 as a slave using I2C communication, how would I know what the address of my slave is.
1. Do I have to look the datasheet?
2. Do I have to recode the I2C header or can I include I1C.h or #I2C in my header and just cal its functions like keypad driver, or LCD driver?
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Thu May 13, 2010 5:20 am |
|
|
Yes, device data sheet.
You need to have a 'caveat' about this though. The first byte sent in an I2C transaction, is a 7bit address, plus one 'direction' bit. Historically, when I2C was first designed, devices were referred to by this 7bit address, which you had to left shift one bit, to get the actual 'number' you needed to send. Later, some manufacturers, then switched to giving the address already left shifted. So you need to be very careful, to make sure whether they are refering to the 7bit address, or an 8bit one....
It is just like the RS232. You can just have the I2C setup line, then call the functions. Remember that a master, does not have an address, so you just have the master setup line, then to talk to a device on the bus using a 'write' address byte of '0x78', you would just use:
Code: |
I2C_START();
I2C_WRITE(0x78); //The first byte sent after the 'start', is the address
I2C_WRITE(0x1); //On most chips, the second byte, is the register
I2C_WRITE(23); //put '23', into register 1, on the chip at 0x78
I2C_STOP(); //finish the transaction
|
You can then talk to another device on the same bus, at a different address by just sending it's address byte after the 'start'.
Best Wishes |
|
|
imatshego
Joined: 12 May 2010 Posts: 6 Location: South Africa
|
|
Posted: Thu May 13, 2010 5:35 am |
|
|
Ok You have lost me. lets say that I have my main.c and ds1307.c.
Would I include the code in the main or do I include another driver?
I2C_WRITE(0x78); //The first byte sent after the 'start', is the address
Is the 0x78 address the default for all slaves?
What if I have more slaves, how would I know their addresses?
I2C_WRITE(0x1); //On most chips, the second byte, is the register
Is the 0x1 address the default for all registers?
Please re-explain, or direct me to a link that might break it down for me, maybe I have missed something during by studying about the I2C.
Thanks you for your time. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu May 13, 2010 7:30 am |
|
|
No,
he means the first byte is an address of some device on the bus.
It's going to (actually MUST) be different for every device on that bus.
To my knowledge, there is no "broadcast address"...
So one moment, you might be addressing the DS1307, the next your home brew I2C slave.
You get to know those addresses as listed on the datasheet for the device.
Some devices like the DS1721 can handle multiple addresses via hardware address select. And it's listed on the datasheet. There's no guessing involved.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
imatshego
Joined: 12 May 2010 Posts: 6 Location: South Africa
|
|
Posted: Thu May 13, 2010 7:36 am |
|
|
But how do you know what's the address for any device is on the bus? If u have one master and four slaves. how would you know each address of the slaves?
how does one get to that hex address, 0x..? Is it something that the master automatically assigns? Does each slave have it's default manufacture address?
Thanks |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu May 13, 2010 8:08 am |
|
|
You read the datasheet for the device.
So far, we've talked about the DS1307 and I've mentioned the DS1721.
Have you looked at the datasheets for either device!?
The device's address is declared in the datasheet.
Typically the datasheet also describes the communication protocol (in terms of what bytes are sent after the address byte to access multiple registers. Never assume all devices are the same.)
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Thu May 13, 2010 8:11 am |
|
|
As I said.
Data sheet.
Devices differ in how this is handled. Some have just one address. A lot more, have two or three addresses, selectable by a pin on the device. Normally pin open, pin high, and pin low, select three different addresses. Yet more have a number of address pins (the common I2C EEPROM chips, have two address pins, allowing four different addresses to be selected). Then a few, have a number of different addresses, specified by programming at the last stage of manufacture, and indicated by an extra character in the part number.
Best Wishes |
|
|
imatshego
Joined: 12 May 2010 Posts: 6 Location: South Africa
|
|
Posted: Fri May 14, 2010 4:25 am |
|
|
Thank you for a detailed explanation. I see that you did say that the data sheet will give me the address in you previous reply. Sorry my bad.
Going through CCS drivers I see that there's a function for 24LC16B, it looks like the I2C code has already been taken care of.
Can I copy this function into my program, change the relevant address, and #define EEPROM_SDA PIN_C4
#define EEPROM_SCL PIN_C3
to the correct ones and just call this in my main.c by using #include?
Thanks. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Fri May 14, 2010 11:18 am |
|
|
Yup. Just change the pin definitions and go..
That's typically all that's needed with some of those older/mature drivers that have been tested for so long.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|