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 failure

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







i2c failure
PostPosted: Mon Jan 06, 2003 10:23 am     Reply with quote

Hi, we are having much trouble getting two PIC 16F877A's to talk to each other via i2c. We just want to read information form a slave to the master.

Please could some1 have a peek at our (small) code and suggest where we are going wrong.

Using software implementation, the number we are sending from the slave to the master is F0, however, upon each iteration the number cycles, like a shift register with a bit of wire between both ends. F0, E1, C3, 87, 0F, 1F, 3E, 7C, F8, F0, E1 ......

The hardware implementation works for one interation then just outputs FF for all time.

If anyone has some CCS working PIC master-slave code we would be most grateful.

MASTER CODE
---------------
/* Standrd PIC C Headers */

#include <16f877a.h>
#include <stdlib.h>

/* Device Fuse Settings */

#fuses HS,NOWDT,NOPROTECT,PUT

/* Device Clock Settings */

#use delay(clock=20000000)

/* Device IO Settings */

#use fast_io(A) /* inputs */
#use fast_io(D) /*inputs */
#use fast_io(E) /*unused, set to all out anyway */

#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(master, scl=PIN_B6, sda=PIN_B7, SLOW)

#define READ_ADDRESS 0xa1

int main(void)
{

int number = 0;

disable_interrupts(GLOBAL);
disable_interrupts(INT_SSP);

delay_ms(100);

while(1 == 1)
{

printf("tx i2c \n\r");

i2c_start();

// printf("i2c START Complete \n\r ");

i2c_write(READ_ADDRESS);

// printf("i2c Address Sent \n\r");

number = i2c_read(0);

// printf("number: \%d \n\r", number);

delay_ms(5);

i2c_stop();

printf("i2c stopped, \%x \n\r", number);

delay_ms(100);

}

}

SLAVE CODE
-----------
/* Standrd PIC C Headers */

#include <16f877a.h>
#include <stdlib.h>

/* Device Fuse Settings */

#fuses HS,NOWDT,NOPROTECT,PUT

/* Device Clock Settings */

#use delay(clock=20000000)

/* Device IO Settings */

#use fast_io(A) /* inputs */
#use fast_io(B) /*inputs */
#use fast_io(D) /*inputs */
#use fast_io(E) /*unused, set to all out anyway */

#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(slave, scl=PIN_B6, sda=PIN_B7, address = 0xa0, SLOW)

#byte PORTA = 5

int main(void)
{
int temp = 0;

delay_ms(80);
set_tris_a(0);

disable_interrupts(GLOBAL);
disable_interrupts(INT_SSP);

while(1 == 1)
{
i2c_write(0xF0);
}

}
SOrry for putting entire code up but we really are stuck!
___________________________
This message was ported from CCS's old forum
Original Post ID: 10463
Tomi
Guest







Re: i2c failure
PostPosted: Mon Jan 06, 2003 10:45 am     Reply with quote

I don't think that a bitbanged I2C slave could work anyway.
Use the HW I2C and the interrupt-driven mechanism. I use something like this to send out a 10-byte answer for a simple read:

#byte SSPCON1= 0x14
#byte SSPBUF = 0x13
#byte SSPSTAT= 0x94

#define RECORDSIZE 10
char SSP_data[RECORDSIZE],phasecnt;

#int_ssp
void ssp_isr()
{
if (!bit_test(SSPSTAT,5)) {
phasecnt = 0;
}
if (phasecnt < RECORDSIZE) {
SSPBUF = SSP_data[phasecnt];
bit_set(SSPCON1,4);
phasecnt++;
if (phasecnt == RECORDSIZE) {
// end of transmission
}
}
}

void main()
{
........
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
.........
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 10465
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