PIC18F452, I2C and SEEPROM

Discussion in 'Embedded Systems and Microcontrollers' started by Bejitto, Nov 4, 2009.

  1. Bejitto

    Thread Starter New Member

    Nov 4, 2009

    This is my first post. I'm a student taking a course in PIC microcontrollers. We are using the PIC18F452 with a 10MHz crystal. I'm currently working on interfacing my PIC18F452 to a SEEPROM (24C04) using I2C. I've having quite a difficult time, and in a nutshell, I can't seem to set the Start (SEN) bit of SSPCON2. I'm sure of the rules and etiquette of posting on here, but hopefully I don't do anything wrong.

    First off, here's a schematic of the PIC board I'm using:


    Most of the connections are made for us. All I had to finish off was connecting SCL to RC3 and SDA to RC4. Not a lot of hardware, so I'll jump right into coding. I'm not going to post the entire thing, just the relevant parts. Most of this is taken from a datasheet for "Overview and Use of the
    PICmicro® MSSP I2C Interface with a 24xx01x EEPROM". Though I've gone through it all myself several times now so I can understand it. I don't like just copy/pasting code.

    First, a few defines:

    Code ( (Unknown Language)):
    1. LC01CTRLIN     equ        H'A0'         ;I2C value for CONTROL BYTE when
    2.                                 ; INputing data to the EEPROM
    3. LC01CTRLOUT    equ        H'A1'         ;I2C value for CONTROL BYTE when
    4.                                 ; requesting OUTput from the EEPROM
    5. LC01ADDR    equ        H'12'         ;Sample value for ADDRESS BYTE
    6. LC01DATA    equ        H'34'         ;Sample data to write to EEPROM
    7. BAUD        equ        D'100'        ;Desired Baud Rate in kbps
    8. FOSC        equ        D'10000'    ;Oscillator Clock in kHz
    A simple setup of TRISC:

    Code ( (Unknown Language)):
    1. movlw    b'00011000'            ;RC3, RC4 are inputs for PORTC
    2. movwf    TRISC                ;Remaining PORTC I/O lines are outputs
    This is the initialization routine for setting up I2C:

    Code ( (Unknown Language)):
    1. ;########## SetupI2C subroutine #########
    2. ; This subroutine initializes the I2C bus for 100 kHz transfers.
    4. SetupI2C
    6. ; Configure Baud Rate
    7.     movlw    (FOSC/(4*BAUD))-1    ;Calculates SSPADD Setting for
    8.     movwf    SSPADD                ; desired Baud rate and sets up SSPADD
    10. ; Configure MSSP module for Master Mode
    11.     movlw    b'00101000'            ;Enables MSSP and uses appropriate
    12.                                 ; PORTC pins for I2C mode (SSPEN set) AND
    13.                                 ; Enables I2C Master Mode (SSPMx bits)
    14.     movwf    SSPCON1                ;This is loaded into SSPCON1
    16. ; Configure Input Levels and slew rate as I2C Standard Levels
    17.     movlw    b'10000000'            ;Slew Rate control (SMP) set for 100kHz
    18.     movwf    SSPSTAT                ;Mode and input levels are I2C spec,
    19.                                 ; loaded in SSPSTAT
    22.     return
    Here's the beginning of the I2CWrite routine:

    Code ( (Unknown Language)):
    1. ;########## I2CWrite subroutine #########
    2. ; Begin I2C Data Transfer Sequences
    4. I2CWrite
    6. ; Send START condition and wait for it to complete
    7.     bsf        SSPCON2,SEN            ;Generate START Condition
    8.     call    WaitMSSP            ;Wait for I2C operation to complete
    I don't need to post the rest of the routine, because this is where the program is getting stuck. When I try to set the SEN bit, SSPCON2 does not set it. Finally, here's the wait routine:

    Code ( (Unknown Language)):
    1. ;########## WaitMSSP subroutine #########
    2. ; This routine waits for the last I2C operation to complete.
    3. ; It does this by polling the SSPIF flag in PIR1.
    5. WaitMSSP
    7.     btfss    PIR1,SSPIF            ;Check if done with I2C operation
    8.     goto    $-1                    ;I2C module is not ready yet
    9.     bcf        PIR1,SSPIF            ;I2C module is ready, clear flag.
    11.     return
    I'm really at a loss of what to do here, and I've been trying to figure this out for days now. This isn't the only version of I2C interfacing I've attempted. I've tried looking around online to see how others do it. I've used a version from our course book, "Embedded Design with the PIC18F452, by Peatman". I've even tried writing my own from scratch. They all end up stuck at this SEN bit.

    Again, I'm very new to PIC programming, so I don't really know all the ins and outs. I'm more or less seeking troubleshooting help, not just a flat out answer.

    Thank you for any help anyone can provide.
  2. Markd77

    Senior Member

    Sep 7, 2009
    Is it possible that SEN is not defined? (maybe there should be an include file or something.) Try swapping:
    bsf SSPCON2,SEN
    bsf SSPCON2,3 (or whatever bit number it should be)
    and see if that works.
  3. Bejitto

    Thread Starter New Member

    Nov 4, 2009
    Sorry, I should have mentioned I'd already tried bsf SSPCON2,0 (SEN is bit 0).

    After further investigation, I'm able to set the SEN bit up until I load SSPCON1, but I can't see anything wrong with the way I set it up.