--=====================_1077640778==_ Content-Type: text/plain; charset="us-ascii" here's Julian Winpenny's file on doing that; At 06:26 PM 24/02/04 +0000, you wrote: >Hi Guys (& Gals ?) > >I have just bought a couple of Philips PCF8753 RTC chips from RS components, >(seemed like a good idea at the time!) but I am having a mare of a time >talking to them on my 16F877A. > >I have the wiring ok (I am absolutely 100% sure of this - after all it is >only 2 wire I2C :) I have followed some of the not-very-helpful philips >datasheets (is this me, or are all philips datasheets hard to read?). >but can't get it to talk to me... > >I am using CCSinfo's PCWH C Compiler. but I can read ASM if someone has any >ideas/code/guidelines/valium... > >Thanks >Jim > >-- >http://www.piclist.com#nomail Going offline? Don't AutoReply us! >email listserv@mitvma.mit.edu with SET PICList DIGEST in the body > -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body --=====================_1077640778==_ Content-Type: text/plain; charset="us-ascii" Content-Disposition: attachment; filename="i2c.txt" /***************************************************************************/ /* I2C.C i2c protocol implementation sources */ /* */ /* Version : 1.0 */ /* */ /* By : Julian Winpenny */ /* */ /* Date : 31/5/1999 */ /* */ /* Purpose : I2C Protocol implementation */ /* */ /* Comments : The SDA data line uses inverted logic, because */ /* if the serial data line is not connected to an */ /* open-drain output ( Port A bit 4 ), then an external */ /* transistor must be used */ /* */ /***************************************************************************/ #define EESlaveWrite 0xA0 // EEPROM Slave address and write mode #define EESlaveRead 0xA1 // EEPROM Slave address and read mode #define ClockWrite 0xA2 // PCF8583 Clock Write mode #define ClockRead 0xA3 // PCF8583 Clock Read mode /*************************************************************/ /* Start Bit Subroutine */ /* this routine generates a start bit */ /* (Low going data line while clock is high) */ /*************************************************************/ void BitStart(void) { output_low_port_b( sdataOut ); // make sure data is high ( Inverted by a Transistor !) output_low_port_a( sclk ); // make sure clock is low output_low_port_b( sdataOut ); // Ensure data is High Delay(); output_high_port_a( sclk ); // set clock high Delay(); output_high_port_b( sdataOut );// data line goes low during // high clock for start bit Delay(); output_low_port_a( sclk ); // start clock train Delay(); } /************************************************************/ /* Stop Bit Subroutine */ /* This routine generates a stop bit */ /* (High going data line while clock is high) */ /************************************************************/ void BitStop(void) { output_high_port_b( sdataOut ); // make sure data line is low nop(); output_high_port_a( sclk ); // set clock high Delay(); output_low_port_b( sdataOut ); // data goes high while clock high // for stop bit Delay(); output_low_port_a( sclk ); // set clock low again Delay(); } /**************************************************************/ /* BITIN routine reads one bit of data from the */ /* serial EE device and stores it in the bit 'di' */ /**************************************************************/ void BitIn(void) { set_bit( eeflag, di ); // assume input bit is high output_high_port_a( sclk ); // set clock line high Delay(); // just sit here a sec if ( ( input_port_b() & sdataInmask ) == 0 ) clear_bit( eeflag, di ); // input bit was low, set 'di' accordingly output_low_port_a( sclk ); // set clock line low Delay(); } /*************************************************************/ /* BITOUT routine takes the bit of data in 'do' and */ /* transmits it to the serial EE device */ /*************************************************************/ void BitOut(void) { if ( ( eeflag & domask ) == 0 ) { output_high_port_b( sdataOut );// output a low bit } else { output_low_port_b( sdataOut ); // high? set data line high } Delay(); // Small delay output_high_port_a( sclk ); // set clock line high Delay(); output_low_port_a( sclk ); // return clock line low Delay(); output_low_port_b( sdataOut );// set sdataOut line to float sDataIn } /*****************************************************************/ /* Receive data byte routine */ /* This routine reads one byte of data from the part */ /* into the 'eedata' register. It then sends a high */ /* ack bit to indicate that no more data is to be read */ /*****************************************************************/ void ReadEEByte(void) { eedata = 0; // Clear input buffer for ( Temp = 0; Temp < 8; Temp++ ) { asm rlf _eedata, F; // rotate datai 1 bit left BitIn(); // read a bit // set/clear bit 0 as necessary if ( ( eeflag & dimask ) != 0 ) set_bit( eedata, 0 ); else clear_bit( eedata, 0 ); } // data returned in eedata } /********************************************************************/ /* Write Data byte function */ /* This routine takes the byte of data stored in the */ /* 'eedata' register and transmits it to the serial EE device.*/ /* It will then send 1 more clock to the serial EE for the */ /* acknowledge bit. If the ack bit from the part was low */ /* then the transmission was sucessful. If it is high, then */ /* the device did not send a proper ack bit. */ /* */ /********************************************************************/ void WriteEEByte(void) { for ( Temp = 0; Temp < 8; Temp++ ) { clear_bit( eeflag, do ); // assume bit out is low if ( ( eedata & 0x80 ) != 0 ) // Do we send a low? set_bit( eeflag, do ); // ...no, send a high BitOut(); // Send the bit to serial EE asm rlf _eedata, F; // Rotate data left 1 bit } BitIn(); } /********************************************************************/ /* READ (read routine) */ /* In the read mode, the PIC must send the acknowledge */ /* bit after every 8 data bits from the device. When the */ /* last byte needed has been read, then the controller will */ /* send a high acknowledge bit and then a stop bit to halt */ /* transmission from the device. */ /********************************************************************/ char ReadData( char AdrHigh, char AdrLow ) { BitStart(); // generate start bit eedata = E2Device; WriteEEByte(); // Slave write mode if ( E2Device == EESlaveWrite ) // If E2 then two byte address needed { eedata = AdrHigh; WriteEEByte(); // Send high byte of address } eedata = AdrLow; WriteEEByte(); // Send low byte of address BitStart(); // generate start bit eedata = (E2Device + 1); WriteEEByte( ); // Switch to read mode ReadEEByte(); // Read 1 byte from device (eedata) set_bit( eeflag, do ); // yes, send high ack bit BitOut(); // to stop tranxmission BitStop(); // and send a stop bit return eedata; } /********************************************************************/ /* Input HighAddress, LowAddress, data. */ /* */ /********************************************************************/ void WriteData( char AdrHigh, char AdrLow, char ch ) { BitStart(); // generate start bit eedata = E2Device; WriteEEByte(); // Slave write mode if ( E2Device == EESlaveWrite ) // if EEPROM device, two byte address { eedata = AdrHigh; WriteEEByte(); // Send high byte of address } eedata = AdrLow; WriteEEByte(); // Send low byte of address eedata = ch; WriteEEByte(); // Send the byte to write BitStop(); // and send a stop bit Delay(); if ( E2Device == EESlaveWrite ) { for ( Temp = 0; Temp < 100; Temp++ ) // Wait for program cycle complete { Delay(); BitStart(); eedata = E2Device; WriteEEByte(); BitIn(); // read ack bit if ( ( eeflag & di ) == 0 ) // check ack bit break; // Break when program cycle complete. } } //;BitStop(); clear_bit( eeflag, err ); // Assume it was ok ! if ( Temp == 100 ) set_bit( eeflag, err ); // set error flag } -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body --=====================_1077640778==_ Content-Type: text/plain; charset="us-ascii" Regards Roland Jollivet JeM Electric cc PO Box 1460 Kloof 3640 Kwazulu Natal South Africa Tel: +27 31 7024412 Fax: +27 31 7011674 \o Cell: +27 83 255 6017 l> Email: enquiries@caon.co.za < \ -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body --=====================_1077640778==_--