PIC 16F872 I2C problem

Hi all,

I'm a newbie to Microchip PICs having used other 8-bit controllers and I've run into a problem I don't quite understand.

I simply cannot get I2C communication to work. I want to use the 16F872 as an I2C slave device, but it just doesn't respond.

I've tried both Application Note 734 assembler program and the example EX_SLAVE.C that comes with the CCS C Compiler but with the same result.

As a master I've tried both an ETX-format PC and a PC with I2C USB adapter. When the master tries to address the PIC at address 0x22, there is no response at all according to the oscilloscope.

As the other Philips chips on the bus work and the problem still remains when I disconnect everything except the pull-up:s and the PIC I guess the problem isn't electrical.

Is there any special considerations/settings when programming the PIC from MPLAB to enable SSP or can you suggest any other possible error sources?

Reply to
k.renaud
Loading thread data ...

schreef in bericht news: snipped-for-privacy@z14g2000cwz.googlegroups.com...

Lack of usefull information, please enter your code!

Reply to
Alexander

C Application example:

#if defined(__PCM__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12

9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #endif

#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x22)

typedef enum {NOTHING, CONTROL_READ, ADDRESS_READ, READ_COMMAND_READ} I2C_STATE;

I2C_STATE fState; BYTE address, buffer[0x10];

#INT_SSP void ssp_interupt () { BYTE incoming;

if (i2c_poll() == FALSE) { if (fState == ADDRESS_READ) { //i2c_poll() returns false on the i2c_write (buffer[address]);//interupt receiving the second fState = NOTHING; //command byte for random read operation } } else { incoming = i2c_read();

if (fState == NOTHING){ fState = CONTROL_READ; } else if (fState == CONTROL_READ) { address = incoming; fState = ADDRESS_READ; } else if (fState == ADDRESS_READ) { buffer[address] = incoming; fState = NOTHING; } } }

void main () { int i;

fState = NOTHING; address = 0x00; for (i=0;i

Reply to
k.renaud

You have several infinite loops in your code (I checked the ASM). This may only work if you know exactly what you are doing, in this case you need to have set at least the watchdog. The best way to handle the infinite loop in the ISR is to check every intterupt used, and after that if non of this caused the jump to the ISR, reset all interrupts that were also enabled with another. Disable all unused intterrupts if possible.

Why use an infinite loop for an I2C error???

Reply to
Alexander

The assembler code is supplied on Microchip's website in Application Note AN734 so I can't say why they chose to loop infinitely causing a WDT reset in case of an I2C error. But this surely doesn't interfere with normal I2C communication without errors.

The C code is supplied by CCS as an example with their compiler.

Because I have used supplied example code I strongly suspect that the code isn't the problem but rather some kind of error during programming although every other program that has been put into the PIC works ok. The latest version of MPLAB is used and ICD2 as a programmer.

Reply to
k.renaud

schreef in bericht news: snipped-for-privacy@g44g2000cwa.googlegroups.com...

Where I have read this I don't know. But somewhere I heard that the Microchip code was not very well suited or maybe buggy for some (or all) chips.

Reply to
Alexander

Try posting the code so otheres can test it.

Reply to
Hopkins

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.