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 CCS Technical Support

i2c over 16F877A doesn't work

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



Joined: 27 Apr 2008
Posts: 167

View user's profile Send private message Send e-mail MSN Messenger

i2c over 16F877A doesn't work
PostPosted: Tue Feb 16, 2010 6:58 am     Reply with quote

Hi everybody;

Trying to use I2C over 16F877A @ 20Mhz, but it simple doesn't work at all.

Here is my code:

Code:
#include <16f877a.h>
#fuses HS,PUT,WDT,PROTECT,NOBROWNOUT,NOLVP,CPD
#use delay(clock=20000000, RESTART_WDT)
#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3)
#define SLAVE1   0x10
#define SLAVE2   0x20
.
.
.
.
void i2c_send()
   {
   i2c_start();
   i2c_write(SLAVE1);
   i2c_write(0X00);
   i2c_write(0X01);
   i2c_stop();
                deb=1;
   }

I already take a look at .lst file, and didn’t see any register associated with I2C. What’s going wrong?
Ttelmah
Guest







PostPosted: Tue Feb 16, 2010 7:45 am     Reply with quote

Unless you comment out the #nolist line, near the top of the processor include file, No assembly will be generated in the list file, for the 'internal' CCS functions, so nothing would be shown accessing the I2C peripheral, if you were doing so.
However, as shown you are not using the I2C peripheral, but software functions. You need to add 'FORCE_HW' to the I2C declaration to use the hardware peripheral. Note the line in the manual:
"Software functions are generated unless the FORCE_HW is specified".

Not working, number of things:
1) Have you got suitable pull-up resistors on the lines?.
2) What is the peripheral chip?.

Best Wishes
rudy



Joined: 27 Apr 2008
Posts: 167

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Tue Feb 16, 2010 9:08 am     Reply with quote

Thanks Sr.

It’s working now. Just to the records, the peripheral chips are two more 16F877A.

Now, I ask you how do the slaves talk to the MASTER? Is only a matter of change TRISC directions and disable SSP interruption while transmit? Right now I got perfect conversation, but only MASTER to SLAVE direction, and I will, for sure, need to receive some "WORDS" from THE SLAVES.
Ttelmah
Guest







PostPosted: Tue Feb 16, 2010 9:32 am     Reply with quote

Look at the examples. EX_slave.c, shows how to do this. You don't change anything, except the 'direction' bit at the bottom of the slave address, written by the master. So the master writes to the slave, saying 'I want to read', by setting this bit, and the slave then writes the required byte(s) into the I2C buffer. The master then reads, and receives this.

Best Wishes
rudy



Joined: 27 Apr 2008
Posts: 167

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Feb 17, 2010 3:46 pm     Reply with quote

Hi.

I really tried a lot to receive some "words" from my slave, but it seems to me a little bit confuse.

Can anyone "translate" to me what exactly each line of the below code means?
Code:

#INT_SSP
void i2c_isr()
{
state = i2c_isr_state();
if((state == 0 ) || (state == 0x80)) i2c_read();
if(state >= 0x80) i2c_write(send_buffer[state - 0x80]);
else if(state > 0) rcv_buffer[state - 1] = i2c_read();
}

The communication flow normally from master to slave.
I didn’t realize when may I send data for the master, can anybody help me?
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