This is a multi-part message in MIME format. ------=_NextPart_000_0091_01C2B44C.0EF2C2E0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit > PLEASE SOME ONE GIVE ME THE CODE TO INTERFACE 24C02 eeprom with > pic 16F73.SOS........SOS.........SOS It's always a good idea to tell people what you have tried so far if you want them to help. The information you want is available at www.piclist.com ALWAYS a good starting point for PIC information. Search the archives and you will find lots of material related to PICs and I2C interfacing, which is what you need for this. Also, standard Microchip Application Notes may be adequate. These are sometimes less than perfect (I'm told :-) ) so looking for comments at PICLIST.COM on any application note that looks OK is probably a good idea. www.microchip.com and www.microchip.com/download/appnote AN536 is a good starting place but has no actual code ____________________________________ The following post from last year may be useful: > > Does anyone have any examples of a 12F629 (or other non-I2C > > labelled PIC) > > being used to write to a I2C EEPROM or serial LCD? My bit-banged code is at: http://www.piclist.com/techref/microchip/i2c-dv.htm --Andrew ____________________________________ Um - the link seems dead so hopefully Andrew will be happy if I attach his code. Russell McMahon -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details. ------=_NextPart_000_0091_01C2B44C.0EF2C2E0 Content-Type: application/octet-stream; name="picgua~1.dat" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="picgua~1.dat" ;------------------------------------------------------------------------= ; ; I^2C master send/receive routines for 24Cxx EEPROM memory chips ; by Andrew D. Vassallo ; ; email: snurple@hotmail.com ; ; Timing set for 4MHz clock, 4.7K pullup resistors to SDA and SCL lines. ; Checks provided for failed ACK during EEPROM write access. ; Optimized for speed and clarity of function. ; Full comments provided. ; ;------------------------------------------------------------------------= CBLOCK GenCount ; generic counter/temp register (use with caution) Mem_Loc ; EEPROM memory address to be accessed Data_Buf ; byte read from EEPROM is stored in this register OutputByte ; used for holding byte to be output to EEPROM flags ; flag bit register ENDC ;------ Define port pins: RB2=3DSDA=3Ddata, RB1=3DSCL=3Dclock ;------ Pins are connected via 4.7K pullup resistors for passive control #define SDA TRISB, 2 #define SCL TRISB, 1 ;------ The memory address is predefined as Mem_Loc coming in to this = routine to read from EPROM. ;------ Note that using the 24Cxx requires an address for bits <1:3> for = control bytes. For a one- ;------ memory circuit used here, just use address 000. This routine = uses "random addressing." ;------ This code has been optimized for speed at 4MHz, assuming = Temperature is -40< T <85 deg. C. ; ; Call with: EEPROM address in Mem_Loc ; Returns with: byte in Data_Buf ReadEPROM bcf STATUS, RP0 movf PORTB, 0 ; for EEPROM operation, andlw 0xF9 ; load zero into RB1 and RB2 movwf PORTB ; for passive control of bus bsf STATUS, RP0 ; select Bank 1 for TRISB access (passive SCL/SDA = control) bsf SDA ; let SDA line get pulled high bsf SCL ; let SCL line get pulled high bcf SDA ; START - data line low movlw 0xA0 ; send "dummy" write (10100000) to set address call Byte_Out btfsc flags, 0 goto Error_Routine ; NOTE: MUST USE "RETURN" FROM THERE movf Mem_Loc, 0 call Byte_Out btfsc flags, 0 goto Error_Routine bcf SCL ; pull clock line low in preparation for 2nd START bit nop bsf SDA ; pull data line high - data transition during clock low bsf SCL ; pull clock line high to begin generating START bcf SDA ; 2nd START - data line low movlw 0xA1 ; request data read from EPROM (read=3D10100001) call Byte_Out btfsc flags, 0 goto Error_Routine ;------ Note that Byte_Out leaves with SDA line freed to allow slave to = send data in to master. call Byte_In movf Data_Buf, 0 ; put result into W register for returning to CALL bcf SCL ; extra cycle for SDA line to be freed from EPROM nop bcf SDA ; ensure SDA line low before generating STOP bsf SCL ; pull clock high for STOP bsf SDA ; STOP - data line high bcf STATUS, RP0 ; leave with Bank 0 active as default return ;------ Save each byte as it's written (not page write mode). ;------ We can speed this up to ~1.5ms for fast page write capable = EEPROMs, but in case we want to ;------ use another slower memory chip, the default is 10ms delay per = write. ; ; Call with: EEPROM address in Mem_Loc, byte to be sent in Data_Buf ; Returns with: nothing returned WriteEPROM bcf STATUS, RP0 movf PORTB, 0 ; for EEPROM operation, andlw 0xF9 ; load zero into RB1 and RB2 movwf PORTB ; for passive control of bus bsf STATUS, RP0 ; select Bank 1 for TRISB access (passive SCL/SDA = control) bsf SDA ; ensure SDA line is high bsf SCL ; pull clock high bcf SDA ; START - data line low movlw 0xA0 ; send write (10100000) to set address call Byte_Out btfsc flags, 0 goto Error_Routine ; NOTE: MUST USE "RETURN" FROM THERE movf Mem_Loc, 0 call Byte_Out btfsc flags, 0 goto Error_Routine movf Data_Buf, 0 ; move data to be sent to W call Byte_Out btfsc flags, 0 goto Error_Routine bcf SCL ; extra cycle for SDA line to be freed from EPROM nop bcf SDA ; ensure SDA line low before generating STOP bsf SCL ; pull clock high for STOP bsf SDA ; STOP - data line high call Delay10ms ; 10ms delay max. required for EPROM write cycle bcf STATUS, RP0 ; leave with Bank 0 active by default return ;------ This routine reads one byte of data from the EPROM into Data_Buf Byte_In clrf Data_Buf movlw 0x08 ; 8 bits to receive movwf GenCount ControlIn bsf STATUS, RP0 ; select Bank 1 to access TRISB rlf Data_Buf, 1 ; shift bits into buffer bcf SCL ; pull clock line low nop bsf SCL ; pull clock high to read bit bcf STATUS, RP0 ; select Bank 0 to read PORTB bits directly! btfsc SDA ; test bit from EPROM (if bit=3Dclear, skip because = Data_Buf is clear) bsf Data_Buf, 0 ; read bit into 0 first, then eventually shift to 7 decfsz GenCount, 1 goto ControlIn bsf STATUS, RP0 ; leave Bank 1 active (don't have to wait for ACK = here) return ;------ This routine sends out the byte in the W register and then waits = for ACK from EPROM (256us timeout period) Byte_Out movwf OutputByte movlw 0x08 ; 8 bits to send movwf GenCount rrf OutputByte, 1 ; shift right in preparation for next loop ControlOut rlf OutputByte, 1 ; shift bits out of buffer bcf SCL ; pull clock line low nop btfsc OutputByte, 7 ; send current "bit 7" goto BitHigh bcf SDA goto ClockOut BitHigh =09 bsf SDA ClockOut=09 bsf SCL ; pull clock high after sending bit decfsz GenCount, 1 goto ControlOut bcf SCL ; pull clock low for ACK change bsf SDA ; free up SDA line for slave to generate ACK nop nop nop ; wait for slave to pull down ACK bsf SCL ; pull clock high for ACK read bcf STATUS, RP0 ; select Bank0 to test SDA PORTB input directly! clrf GenCount ; reuse this register as a timeout counter (to 256us) = to test for ACK WaitForACK incf GenCount, 1 ; increase timeout counter each time ACK is not = received btfsc STATUS, Z goto No_ACK_Rec btfsc SDA ; test pin. If clear, EEPROM is pulling SDA low for ACK goto WaitForACK ; ...otherwise, continue to wait bcf flags, 0 ; clear flag bit (if desired) bsf STATUS, RP0 ; select Bank1 as default during these routines return ;------ No ACK received from slave (must use "return" from here) ;; Typically, set a flag bit to indicate failed write and check for it = upon return. No_ACK_Rec bsf flags, 0 ; set flag bit bsf STATUS, RP0 ; select Bank1 as default during these routines return ; returns to Byte_Out routine ;------ No ACK received from slave. This is the error handler. Error_Routine ; Output error message, etc. here return ; returns to INITIAL calling routine Delay10ms movlw 0x0A movwf GenCount Delay_Start nop movlw 0x07 ; 249 cycles * 4us per cycle + 5us =3D 1.000ms Delay addlw 0x01 btfss STATUS, Z goto Delay decfsz GenCount, 1 goto Delay_Start return ------=_NextPart_000_0091_01C2B44C.0EF2C2E0--