Update. I inserted a delay after the first function call like this: // Write Slave address and set master for transmission I2CResult=MasterWriteI2C1((I2CDevAddress << 1) & 0xfe); asm("nop"); for (temp=0; temp<1000; temp++) { } Now I am getting the 9th (ACK) clock. Inserting these delays throughout allowed me to get all the write function calls to complete. Maybe it's some kind of interaction with the Picket 3 and MPlab. Or maybe I'm checking the wrong flag? Mark Mark E. Skeels wrote: > Hi, List, > > I am working with the PIC24HJ64GP210, C30 and the 16 bit Peripheral > library I2C functions. > > I am trying to write/read a Maxim DS1337 RTC chip. > > I started with some sample code for interfacing I2C EEPROMs and now have > the code below. > > I have read the data sheet and user's guide but I don't yet fully > understand how to configure all of the control bits, so I gave it my > best shot. > > /_*My problem:*_/ At line /_*1*_/ below, the I2C peripheral clocks only > 8 times after start condition, so I never see the ACK from the slave DS1337. > > Both of the following checks for TBF and ACK fall through and at line > */_2_/* the function MasterWriteI2C1 returns a -1, indicating a bus > collision. > > /_*My Question:*_/ How do I get the I2C peripheral to clock out the 9th > time so I can get the ACK bit from the DS1337? > > Many thanks, > Mark > > /*************************************************************** > * Write a block of chars into the Maxim DS1337 RTC I2C device. > * > * I2CAddress contains internal starting memory location of RTC > * p_I2CWriteBlock points to first location of a block of chars > * BlockLen contains number of characters in the data block > * I2CDevAddress contains I2C device address > ***************************************************************/ > void I2CWriteBlock(unsigned char I2CAddress, unsigned char > *p_I2CWriteBlock, unsigned int BlockLength, unsigned char I2CDevAddress) > { > unsigned char i, I2CResult; > unsigned int config1; > > //**** begin test code to fill the array with know data > unsigned char *p_TempBlock; > p_TempBlock=p_I2CWriteBlock; > > for (i=0; i { > *p_TempBlock = 0xad; > p_TempBlock++; > } > //*** end test code. > > /* Configure I2C for 7 bit address mode 400 KHz operation.....??*/ > config1 = ( > I2C1_ON & I2C1_IDLE_CON & /*I2C1_CLK_HLD &*/ > I2C1_IPMI_DIS & I2C1_7BIT_ADD & > I2C1_SLW_EN & I2C1_SM_DIS & > I2C1_GCALL_DIS & I2C1_STR_DIS & > I2C1_NACK & I2C1_ACK_EN & I2C1_RCV_DIS & > I2C1_STOP_DIS & I2C1_RESTART_DIS & > I2C1_START_DIS > ); > > //Enable I2C module 1 for use. > OpenI2C1( config1 , BRG_VAL ); //BRG_VAL is a constant;currently gives > ~330 KHz bus speed > asm("nop"); //asm's help with breakpoint debugging using Pickit 3 > > //Wait for bus to become available > IdleI2C1(); > asm("nop"); > > // Set a start condition on the bus > StartI2C1(); > asm("nop"); > > //wait for start sequence to finish... > while(I2C1CONbits.SEN ); > asm("nop"); > > // Write Slave address and set master for transmission > /_*1*_/ I2CResult=MasterWriteI2C1((I2CDevAddress << 1) & 0xfe); > asm("nop"); > > // Wait till I2C device address is transmitted > while(I2C1STATbits.TBF); > asm("nop"); > > //wait for the ACK...(it should be low so wait while high) > while(I2C1STATbits.ACKSTAT); > asm("nop"); > > // Set the starting location in the I2C device to be written > /_*2*_/ I2CResult=MasterWriteI2C1(I2CAddress); > asm("nop"); > > //Wait till I2C device address is transmitted > while(I2C1STATbits.TBF); > asm("nop"); > > //wait for the ACK...(it should be low so wait while high) > while(I2C1STATbits.ACKSTAT); > asm("nop"); > > //write the data > for (i=0; i<(BlockLength); i++) > { > asm("nop"); > //Write a byte > I2CResult=MasterWriteI2C1(*p_I2CWriteBlock); > asm("nop"); > > /* Wait till I2C device address is transmitted */ > while(I2C1STATbits.TBF); > //wait for the ACK...(it should be low so wait while high) > asm("nop"); > > //wait for ACK > while(I2C1STATbits.ACKSTAT); > asm("nop"); > > //next byte > p_I2CWriteBlock++; > } > asm("nop"); > > // Set a stop condition on the bus > StopI2C1(); > asm("nop"); > > //Close I2C module 1 > CloseI2C1(); > asm("nop"); > } > > -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist