This is a multi-part message in MIME format. ------=_NextPart_000_004D_01C2B260.67B4C560 Content-Type: text/plain; charset="iso-8859-2" Content-Transfer-Encoding: 7bit Hi! I presume you are using hardware I2C. Here is part of my project where I read and write to PCF8583 I2C clock/calendar chip. Code may not be pretty, but it works. Regards, Samo -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads ------=_NextPart_000_004D_01C2B260.67B4C560 Content-Type: text/plain; name="i2c.txt" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="i2c.txt" ;****USEFULL MACROS BANK0 macro bcf STATUS, RP0 ;switch to bank0 00h-7Fh bcf STATUS, RP1 endm BANK1 macro bsf STATUS, RP0 ;bank1 80h-FFh bcf STATUS, RP1 endm BANK2 macro bcf STATUS, RP0 ;bank2 100h-17Fh bsf STATUS, RP1 endm BANK3 macro bsf STATUS, RP0 ;bank3 180h-1FFh bsf STATUS, RP1 endm I2C INITIALISATION FOR 4MHZ CLOCK ;I2C movlw b'00011000' ;SCL and SDA inputs movwf TRISC bsf SSPSTAT, 7 bcf SSPSTAT, 6 ;I2C, standard speed 100kHz movlw .9 movwf SSPADD ;100kHz line BANK0 movlw b'00101000' ;I2C master mode, MSSP enabled iorwf SSPCON, f movlw b'11111000' andwf SSPCON clrf I2C_BYTE ; ; init for other code was here MAIN WRITE_ONE_BYTE movlw PCF_ADDRESS movwf SLAVE_ADDRESS movlw .101 movwf I2C_BYTE call SEND_START call ADDRESS_TO_WRITE;send slave address call SEND_I2C_BYTE ;send register location movlw b'10101010' ;data to write movwf I2C_BYTE call SEND_I2C_BYTE ;send it call SEND_STOP ;initiate stop clrf I2C_FLG READ_ONE_BYTE movlw .101 ;read from location 3 movwf I2C_BYTE call SEND_START call ADDRESS_TO_WRITE;call slave and indicate write call SEND_I2C_BYTE ;send memory location to write call SEND_STOP clrf I2C_FLG call SEND_START call ADDRESS_TO_READ ;call slave to read from call READ_I2C_BYTE call SEND_NOACK ;indicate to slave read is over call SEND_STOP goto MAIN ;Go write again ;SUBROUTINES. YOU CAN SKIP FLAG CHECKING IF YOU WANT ;//////////////////////////////////////////////////////////////////// READ_I2C_BYTE btfsc I2C_FLG, ACK_ERR;ack error? if no, skip next return ;yes. return bcf I2C_FLG, ACK_ERR;no. clear error flag BANK1 bsf SSPCON2, RCEN ;initiate recieve data byte BANK0 READ1 btfss PIR1, SSPIF ;recieve completed? if yes, skip next goto READ1 ;no. try again bcf PIR1, SSPIF ;yes. clear completed flag movfw SSPBUF ;get recieved data movwf I2C_BYTE ;and store it return ;//////////////////////////////////////////////////////////////////// SEND_NOACK BANK1 bsf SSPCON2, ACKDT ;select noack bsf SSPCON2, ACKEN ;initiate ack sequence(sends NOACK) BANK0 READ2 btfss PIR1, SSPIF ;send completed? if yes, skip next goto READ2 ;no, try again bcf PIR1, SSPIF ;yes. clear completed flag return ;//////////////////////////////////////////////////////////////////// SEND_START bcf I2C_FLG, ACK_ERR;initialise acknowledge error flag bcf PIR1, SSPIF ;clear SSP interrupt flag BANK1 bsf SSPCON2, SEN ;initiate START condition BANK0 WAIT1 btfss PIR1, SSPIF ;START completed?If yes, skip next goto WAIT1 ;No.Wait here bcf PIR1, SSPIF ;Yes. Clear interrupt flag return ;////////////////////////////////////////////////////////////////// ADDRESS_TO_WRITE movfw SLAVE_ADDRESS ;get slave address andlw b'11111110' ;tag as write(last digit is 0!!!) movwf SSPBUF ;initiate send slave adress WAIT2 btfss PIR1, SSPIF ;address sent? If yes, skip next goto WAIT2 ;no. Wait here bcf PIR1, SSPIF ;clear completed flag BANK1 btfsc SSPCON2, ACKSTAT;ACK from slave recieved? If yes, skip next goto WADDR_ACK_ERROR ;No. Go service error bcf I2C_FLG, ACK_ERR;Yes.Signal everything is OK return ; WADDR_ACK_ERROR bsf I2C_FLG, ACK_ERR;ack error. Abort. bsf I2C_FLG, ERR ;signal main to repeat transfer bsf I2C_FLG, W_ADDR ;error sending address call SEND_STOP ;and send STOP condition return ;//////////////////////////////////////////////////////////////////// SEND_I2C_BYTE btfsc I2C_FLG, ACK_ERR;Aborted transmission?If no, skip next return ;Yes. Return BANK0 ; bcf I2C_FLG, ACK_ERR;No.Clear ack error flag. movfw I2C_BYTE ;Fetch data to send movwf SSPBUF ;and initiate transfer WAIT3 btfss PIR1, SSPIF ;Done? If yes, skip next goto WAIT3 ;No. Go try again bcf PIR1, SSPIF ;Yes. Clear completed flag BANK1 btfsc SSPCON2, ACKSTAT;ACK from slave recieved? If yes, skip next goto BYTE_ACK_ERROR ;No.Go service error bcf I2C_FLG, ACK_ERR;Yes. Everything is OK BANK0 return BYTE_ACK_ERROR bsf I2C_FLG, ACK_ERR;ack error. Abort. bsf I2C_FLG, ERR ;signal main to repeat transfer bsf I2C_FLG, W_BYTE ;error sending byte call SEND_STOP ;and send STOP condition return ;////////////////////////////////////////////////////////////////////////// SEND_STOP BANK1 bsf SSPCON2, PEN ;initiate STOP condition BANK0 WAIT4 btfss PIR1, SSPIF ;STOP done? If yes, skip next goto WAIT4 ;No, wait here bcf PIR1, SSPIF ;Yes. Clear flag and go bcf I2C_FLG, ACK_ERR;indicate stop condition was done return ;/////////////////////////////////////////////////////////////////// ADDRESS_TO_READ movfw SLAVE_ADDRESS ;get slave address iorlw b'00000001' ;tag as read (last digit is 1!!!) movwf SSPBUF ;initiate send slave adress WAIT22 btfss PIR1, SSPIF ;address sent? If yes, skip next goto WAIT22 ;no. Wait here bcf PIR1, SSPIF ;clear completed flag BANK1 btfsc SSPCON2, ACKSTAT;ACK from slave recieved? If yes, skip next goto RADDR_ACK_ERROR ;No. Go service error bcf I2C_FLG, ACK_ERR;Yes.Signal everything is OK return ; RADDR_ACK_ERROR bsf I2C_FLG, ACK_ERR;ack error. Abort. bsf I2C_FLG, ERR ;signal main to repeat transfer bsf I2C_FLG, R_ADDR ;error sending address call SEND_STOP ;and send STOP condition return ;////////////////////////////////////////////////////////////////////////// ;****INITIALISATION ROUTINES FOR LCD,REGISTERS... INIT BANK1 movlw .7 movwf ADCON1 ;PORTA and E are configured as digital I/O ! clrf TRISA clrf TRISB clrf TRISD clrf TRISE ;make all ports outputs ; bsf PIE1, TMR1IE ;enable TIMER1 interrupt ;I2C movlw b'00011000' ;SCL and SDA inputs movwf TRISC bsf SSPSTAT, 7 bcf SSPSTAT, 6 ;I2C, standard speed 100kHz movlw .9 movwf SSPADD ;100kHz line BANK0 movlw b'00101000' ;I2C master mode, MSSP enabled iorwf SSPCON, f movlw b'11111000' andwf SSPCON clrf I2C_BYTE clrf PORTA clrf PORTB clrf PORTC clrf PORTD clrf PORTE ;and clear them clrf POINTER movlw b'11111111' movwf PORTD clrf FLAG clrf DEBNCE_CTR ;clear debounce counter clrf BEEP_COUNT ;clear beeper counter clrf MNU_FLG clrf O_FLAGS movlw '2' movwf HOUR_LOW movwf HOUR_HIGH movwf MINUTES_HIGH movwf MINUTES_LOW -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads ------=_NextPart_000_004D_01C2B260.67B4C560--