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

i2c between 2 PICS

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







i2c between 2 PICS
PostPosted: Mon Mar 10, 2008 2:36 pm     Reply with quote

I am in trouble with getting 2 PICs communicating through i2c interface.
I observed SDA and SCL signals with a scope. I saw the first WR command
and the following 2 bytes going through and ACKed by the slave. Then the
RD command is sent and ACKed by the slave and after that SDA and SCL
signals freeze at 0V. Any ideas of what I did wrng? Thanks in advance.


Masre code:

#include <16F886.h>
#device adc=10

#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES MCLR //Master Clear pin used for I/O
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOWRT //Program memory not write protected
#FUSES BORV40 //Brownout reset at 4.0V

#use delay(clock=8000000)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(MASTER, scl = PIN_C3, sda = PIN_C4, FORCE_HW)


#BYTE PORTB = 0x06 //Hardware config registers...
#BYTE TRISA = 0x85
#BYTE TRISB = 0x86
#BYTE TRISC = 0x87
#BYTE SSPADD = 0x93


#define SCAN_BYTE1 10
#define SCAN_BYTE2 11

int16 slaves[16];
int8 slave_count = 0;

void detect_slaves(){
int32 i = 0;
int8 addrh, addrl, temp;

while(i < 0xFFFF){
addrl = (int8)(i & 0xFF);
addrh = (int8)(i >> 8);
i2c_start();
i2c_write(0xA0);
i2c_write(SCAN_BYTE1);
i2c_write(addrh);
//i2c_stop();
i2c_start();
i2c_write(0xA1);
//while(i2c_poll() == 0);
temp = i2c_read();
i2c_stop();
if(temp != 0xC3)
i = i + 256;
else{
i2c_start();
i2c_write(0xA0);
i2c_write(SCAN_BYTE2);
i2c_write(addrl);
//i2c_stop();
i2c_start();
i2c_write(0xA1);
//while(i2c_poll() == 0);
temp = i2c_read();
i2c_stop();
if(temp == 0xC3){
slaves[slave_count] = i;
slave_count++;
}
i++;
}
}
printf("\r\nSlaves found: %d\r\n", slave_count);
for(addrh=0;addrh<slave_count;addrh++)
printf("nSlave #%d Slave ID %LX\r\n", addrh, slaves[addrh]);
}


void init_ports(){
TRISA = 0x03;
TRISB = 0x00;
TRISC = 0x98;
}



void main(){
init_ports();
SSPADD = 0x7F; //The slowest possible SCL
detect_slaves();
for( ; ; ){
PORTB++;
}
}



Slave code:

#include <16F883.h>
#device adc=8

#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES MCLR //Master Clear pin enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOWRT //Program memory not write protected
#FUSES BORV40 //Brownout reset at 4.0V

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(SLAVE, ADDRESS = 0xA0, scl = PIN_C3, sda = PIN_C4, FORCE_HW)


#BYTE PORTB = 0x06 //Hardware config registers...
#BYTE TRISA = 0x85
#BYTE TRISB = 0x86
#BYTE TRISC = 0x87
#BYTE SSPADD = 0x93

#define SCAN_BYTE1 10
#define SCAN_BYTE2 11

int8 state;
int8 i2c_buffer[16];
extern int8 state;

#INT_SSP
void i2c_isr(){
output_high(PIN_B7);
state = i2c_isr_state();
if(state >= 0x80)
i2c_write(0xC3);
else
i2c_buffer[1] = i2c_read();

output_low(PIN_B7);
}


void init_ports(){
TRISA = 0x03;
TRISB = 0x00;
TRISC = 0x98;
}


void main(){
init_ports();
PORTB = 0;
clear_interrupt(INT_SSP);
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);

for( ; ; ){
//printf("%X \r\n", state);
}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 10, 2008 3:36 pm     Reply with quote

This series of PICs have bugs with the i2c slave mode.
I was never able to make it work. Later, Microchip released an errata
notice on the MSSP module. They give a work-around for the slave,
but I never tried it. In other words, you have do some some coding
beyond using the CCS functions. I don't have time to do this right now.
http://ww1.microchip.com/downloads/en/DeviceDoc/80302D.pdf
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Mon Mar 10, 2008 8:13 pm     Reply with quote

Microchip's errata are a least an admission of something but I don't understand the Microchip business model of not recalling these chips and either replacing them with modified silicon or substituting a working device.
They cost pennies to manufacture and the cost of having customers feel it is their loss if Microchip errs must be higher. I know the US medical model is never to disclose mistakes and have the patient pay for the medical mistake when it happens. It has led to angry patients and massive legal bills for the medical profession. Microchip runs the risk of a serious malpractice law suit.
nicko
Guest







PostPosted: Tue Mar 11, 2008 10:16 am     Reply with quote

Thank You PCM programmer,


I migrated to PIC16F876A and I am fine now.
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