PIC18F452, I2C and SEEPROM

Thread Starter

Bejitto

Joined Nov 4, 2009
2
Hello:

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:

http://img41.imageshack.us/i/picproto.jpg/

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:

Rich (BB code):
LC01CTRLIN     equ        H'A0'         ;I2C value for CONTROL BYTE when
                                ; INputing data to the EEPROM
LC01CTRLOUT    equ        H'A1'         ;I2C value for CONTROL BYTE when
                                ; requesting OUTput from the EEPROM
LC01ADDR    equ        H'12'         ;Sample value for ADDRESS BYTE
LC01DATA    equ        H'34'         ;Sample data to write to EEPROM
BAUD        equ        D'100'        ;Desired Baud Rate in kbps
FOSC        equ        D'10000'    ;Oscillator Clock in kHz
A simple setup of TRISC:

Rich (BB code):
movlw    b'00011000'            ;RC3, RC4 are inputs for PORTC
movwf    TRISC                ;Remaining PORTC I/O lines are outputs
This is the initialization routine for setting up I2C:

Rich (BB code):
;########## SetupI2C subroutine #########
; This subroutine initializes the I2C bus for 100 kHz transfers.

SetupI2C

; Configure Baud Rate
    movlw    (FOSC/(4*BAUD))-1    ;Calculates SSPADD Setting for
    movwf    SSPADD                ; desired Baud rate and sets up SSPADD

; Configure MSSP module for Master Mode
    movlw    b'00101000'            ;Enables MSSP and uses appropriate
                                ; PORTC pins for I2C mode (SSPEN set) AND
                                ; Enables I2C Master Mode (SSPMx bits)
    movwf    SSPCON1                ;This is loaded into SSPCON1

; Configure Input Levels and slew rate as I2C Standard Levels
    movlw    b'10000000'            ;Slew Rate control (SMP) set for 100kHz
    movwf    SSPSTAT                ;Mode and input levels are I2C spec,
                                ; loaded in SSPSTAT


    return
Here's the beginning of the I2CWrite routine:

Rich (BB code):
;########## I2CWrite subroutine #########
; Begin I2C Data Transfer Sequences

I2CWrite

; Send START condition and wait for it to complete
    bsf        SSPCON2,SEN            ;Generate START Condition
    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:

Rich (BB code):
;########## WaitMSSP subroutine #########
; This routine waits for the last I2C operation to complete.
; It does this by polling the SSPIF flag in PIR1.

WaitMSSP

    btfss    PIR1,SSPIF            ;Check if done with I2C operation
    goto    $-1                    ;I2C module is not ready yet
    bcf        PIR1,SSPIF            ;I2C module is ready, clear flag.

    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.
 

Markd77

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

Thread Starter

Bejitto

Joined Nov 4, 2009
2
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.
 
Top