CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC 18F2220 I2C

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
imatshego



Joined: 12 May 2010
Posts: 6
Location: South Africa

View user's profile Send private message MSN Messenger

PIC 18F2220 I2C
PostPosted: Wed May 12, 2010 1:40 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 12, 2010 12:12 pm     Reply with quote

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

View user's profile Send private message MSN Messenger

PostPosted: Thu May 13, 2010 3:13 am     Reply with quote

Very Happy

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: 19359

View user's profile Send private message

PostPosted: Thu May 13, 2010 4:26 am     Reply with quote

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

View user's profile Send private message MSN Messenger

PostPosted: Thu May 13, 2010 4:53 am     Reply with quote

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: 19359

View user's profile Send private message

PostPosted: Thu May 13, 2010 5:20 am     Reply with quote

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

View user's profile Send private message MSN Messenger

PostPosted: Thu May 13, 2010 5:35 am     Reply with quote

Confused

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: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Thu May 13, 2010 7:30 am     Reply with quote

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

View user's profile Send private message MSN Messenger

PostPosted: Thu May 13, 2010 7:36 am     Reply with quote

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: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Thu May 13, 2010 8:08 am     Reply with quote

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: 19359

View user's profile Send private message

PostPosted: Thu May 13, 2010 8:11 am     Reply with quote

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

View user's profile Send private message MSN Messenger

PostPosted: Fri May 14, 2010 4:25 am     Reply with quote

Very Happy

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: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Fri May 14, 2010 11:18 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group