Hardware I2C


Closed Thread
Results 1 to 12 of 12

Thread: Hardware I2C

Hybrid View

  1. #1
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    I don't know what PIC you are using, so I can't promise anything, but I've attached a file containing some portions of code I have used with the hardware I2C on a PIC 18F4620.

    The file is a compilation of cut and past from a few include files, and has only been minimally "cleaned up" for public viewing. It has 4 sections:
    1) Basic PIC and HSEROUT setup (mostly to reinforce what pic and how it's configured)
    2) The meat of the I2C commands
    3) Samples of Large Block Reading/Writing to an EEPROM
    4) Samples of Reading/Writing to an RTC

    Hopefully this will get you started.
    Best of luck,
    Steve B

    Disclaimer: Looking a little closer I recall this being one of my first PBP-ASM items I tackled. It worked, and with other things pressing, I haven't gone back and cleaned things up, like unused variables (DelayCtr1 and DelayCtr2) and rements of HSEROUT commands used to debug.
    Attached Files Attached Files
    Last edited by SteveB; - 20th January 2007 at 05:50. Reason: Added disclaime

  2. #2
    Join Date
    Jun 2005
    Posts
    29


    Did you find this post helpful? Yes | No

    Default

    Thank you SteveB, looks like what I need. I will take a closer look and adapt it to my needs. I'm using a PIC16F886, but I think all MSSP modules work the same way, and I only need to read from the EEPROM.

  3. #3
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Toley00 View Post
    Thank you SteveB, looks like what I need. I will take a closer look and adapt it to my needs. I'm using a PIC16F886, but I think all MSSP modules work the same way, and I only need to read from the EEPROM.
    A couple of things right off the top of my head. The buffer size will need to be reduced for a 16F. Also, the way I allocated variables to "BANKA" or "BANKX" in the declaration statements will also need changing. If I get a chance later I'll try to give it a look-over for anything else. As I recall, I think I got the basics of the ASM routines from a Microchip document/source, and adapted them for the 18F. So that might also be an avenue to persue.

    Steve B

  4. #4
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Thumbs up Thanks SteveB

    Hi,

    SteveB your routines are neat (at least than mine) and I learnt from it as well. Thanks to you and Toley for raising the topic.
    Regards

    Sougata

  5. #5
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    I made a quick run through the code (while my kids took a nap). Changed all the ASM to PBP. Also modified the buffer size, and some SFR lables to correspond the 16F. It compiles OK, but no 16F's to test it (nor anything to setup the circut without canibalizing something else). Keep in mind, on many of the 18Fs, you've got RAM to burn, so the buffers reflect that. On the 16Fs, you don't have that luxary.

    Hopefully, if you are working it on your end, you can compare/use what I've done to move further down the road.

    I did notice a number of areas where things could be improved or I would do thing differently. The error handling is especially weak. However, I never got any errors with the code and my setup, so never needed it to be very robust. So, I will put this on my *LONG* list of things to do when I get around to it.

    SteveB
    Attached Files Attached Files
    Last edited by SteveB; - 21st January 2007 at 04:58.

  6. #6
    Join Date
    Jun 2005
    Posts
    29


    Did you find this post helpful? Yes | No

    Thumbs up Project finished!

    Thank you SteveB, with your help (and a lot of time) I finally make it work. I've changed some things in your Routines. First I changed the Labels for shorter names. I've also changed the lines refer to PIR1.3 for SSPSTAT.2 to indicate when a transmit is over. And I fixed a little error in your code RCEN is SSPCON2.3 instead of SSPCON2.4.

    In Fact all that is need is to correctly set the MSSP module :

    SSPSTAT.7 = 0 'High Speed Filter
    SSPADD = $0C '400 kHz @ 20 MHz
    SSPCON = %00101000 'I2C Master Mode Enable

    And in your main program use the 6 next sub routines as needed :

    ;-----------------------------------------------------------
    ; Process Routines
    ;-----------------------------------------------------------
    I2CSTART:
    SSPCON2.0 = 1 ; SEN - Start Condition Enable Bit
    WHILE SSPCON2.0 = 1 : WEND ; Wait for Start to complete
    RETURN
    ;-----------------------------------------------------------
    I2CRD:
    SSPCON2.3 = 1 ; RCEN - Enable receive mode
    WHILE SSPCON2.3 = 1 : WEND ; Wait for Read to complete
    I2CDATA = SSPBUF
    Return
    ;-----------------------------------------------------------
    I2CWR:
    SSPBUF = I2CDATA ; Move data to SSPBUF
    WHILE SSPSTAT.2 = 1 : WEND ; SSPSTAT = 1 Transmit in progress
    While SSPCON2.6 = 1 : WEND ; Wait for Acknowledge from slave 1=NotAck
    RETURN ; Acknowledge recieved
    Return
    ;-----------------------------------------------------------
    I2CSTOP:
    SSPCON2.2 = 1 ; PEN - send stop bit
    While SSPCON2.2 = 1 : Wend ; Wait for SSP to complete
    Return
    ;-----------------------------------------------------------
    I2CNOACK:
    SSPCON2.5 = 1 ; ACKDT - Set Ack bit to NotAck
    SSPCON2.4 = 1 ; ACKEN - send ACKDT bit
    While SSPCON2.4 = 1 : Wend ; Wait for SSP to complete
    Return
    ;-----------------------------------------------------------
    I2CACK:
    SSPCON2.5 = 0 ; ACKDT - Set Ack bit to Ack
    SSPCON2.4 = 1 ; ACKEN - send ACKDT bit
    While SSPCON2.4 = 1 : Wend ; Wait for SSP to complete
    Return
    ;-----------------------------------------------------------

    Now By using thoses sub routines, I'm able to access an EEPROM fast enough to read a 8 sec. wav files at 8 kHz. Everything is written in PicBasic Pro (no assembly) and is at least 3 times faster than standard I2CREAD statement.

    Ok I know it's not really "User friendly" but my programming skills are limited and it work fine for me like this. Maybe someone can write something easier to use, it could be very handy.

  7. #7
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    It's great to here when someone has success. Especially when they have put in the work (like getting into the datasheet) that you have.

    SteveB

Similar Threads

  1. I2C Master/Slave 16F88/16F767 working code
    By DanPBP in forum Code Examples
    Replies: 2
    Last Post: - 23rd October 2012, 22:31
  2. HARDWARE I2C SAMPLE CODE question
    By Michael Wakileh in forum Code Examples
    Replies: 2
    Last Post: - 16th June 2009, 21:07
  3. I2C Master Slave issues.
    By cpayne in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 29th March 2008, 19:33
  4. Hardware I2C
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 21
    Last Post: - 13th March 2008, 13:24
  5. Please help with i2cslave i2c slave
    By cycle_girl in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 1st December 2005, 13:55

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts