1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Oshonsoft Basic Low Level I2C

Discussion in 'Oshonsoft' started by ericgibbs, Feb 17, 2015.

  1. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    This modified example program is from the Oshonsoft manual, its uses the Basic low level I2C statements.
    It will write the test data to the simulator EEPROM, but will not Read it back, it gets all FF's. I also get garbage in the real hardware.

    If the 'Goto ep_read' is un remmed and the program re-run in the simulator it will read the test data OK.

    If any Oshonsoft user has the time to check out this example and let me know what they find it would be appreciated.
    I have sent a bug report to Vladimir.
    Eric

    Code (text):

    'example 2:
    '17Feb2015
    'PIC 18F2550
    'EEPROM 24C256

    Define CLOCK_FREQUENCY = 20
    AllDigital

    Define I2CREAD_DELAYUS = 50
    Define I2CCLOCK_STRETCH = 50

    Dim addr As Word
    Dim data(31) As Byte
    Dim cnt1 as Byte

    Symbol sda = PORTC.2
    Symbol scl = PORTC.3

    addr = 0

    'goto ep_read
    ep_write:
    I2CPrepare sda, scl
    I2CStart
    I2CSend 0xa0
    I2CSend addr.HB
    I2CSend addr.LB
    For cnt1 = 0 To 30 ' write 0 to 0xff to eeprom
    I2CSend cnt1
    Next cnt1
    I2CStop

    ep_read:
    I2CStart
    I2CSend 0xa1
    For addr = 0 To 30
        I2CReceiveAck data(addr)
    Next addr
    I2CRecN data(31)
    I2CStop

     
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    I don't know if the simulator actually simulates the EEprom.. but after writing you have to wait at least 5ms before you read or else the write fails....
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Just simulated this on ISIS and it works fine..

    Code (basic):

    'example 2:
    '17Feb2015
    'PIC 18F2550
    'EEPROM 24C256

    Define CLOCK_FREQUENCY = 20
    AllDigital

    Define I2CREAD_DELAYUS = 50
    Define I2CCLOCK_STRETCH = 50

    Dim addr As Word
    Dim data(31) As Byte
    Dim cnt1 As Byte

    Symbol sda = PORTC.2
    Symbol scl = PORTC.3

    addr = 0

    ep_write:
    I2CPrepare sda, scl
    I2CStart
    I2CSend 0xa0
    I2CSend addr.HB
    I2CSend addr.LB
    For cnt1 = 0 To 30  'write 0 to 0xff to eeprom
    I2CSend cnt1
    Next cnt1
    I2CStop

    WaitMs 20

    ep_read:
    I2CPrepare sda, scl
    I2CStart
    I2CSend 0xa0
    I2CSend addr.HB
    I2CSend addr.LB
    I2CStop
    I2CStart
    I2CSend 0xa1
    For addr = 0 To 30
    I2CReceiveAck data(addr)
    Next addr
    I2CRecN data(31)
    I2CStop

     
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. zas

    zas New Member

    Joined:
    Feb 11, 2015
    Messages:
    12
    Likes:
    0

    Hello.
    are the Port Pins for SDA and SCL correct. In the Datasheet it have to be PORTB.
    I becom an error at the Line "I2CRecN data(31)". The Array have 31 Elements (0-30)
     
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Ian,
    Thanks for the feedback, if I hear from Vladimir I will post.
    The Oshonsoft EEPROM sim tool works OK with the PORTB.0/1 , using the PIC's internal I2C module.
    I have played with the delays without any success.
    As I step thru the IDE, PICs buffer registers for the data(31) Array, during a Read operation, it loads a 0xFF into the buffer as it Reads the data.!

    Eric
     
  7. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    Hi zas,
    I am using the PORTB.0/1 for SPI [I2C pins] with a FAT32 SD card, that is why I want to use the low level I2C for a DS3231 RTC
    The data(31) Array, is 32 Bytes long, that is data(0) thru data(31)

    Eric
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Have you tried the code I have written? Your "Read" routine was missing a few elements aswell...
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    One other thing.... Time arrays on the fat32 are backwards to the RTC.... Seconds are placed last!!!
     
  10. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    Hi Ian,
    I have not tried your code, but I will.

    I have the DS3231 RTC working OK, time set/read, temperature etc,,, with the PIC's I2C internal module OK, its the Oshonsoft low level thats a pain.

    That code I pasted is from the Oshonsoft manual.:)
    E

    EDIT:
    Your code works, I was able to get the result I wanted using that type of coding in previous tests I had done.
    Its the Oshonsoft example/demo code that will not work, thats why I am querying it with Vladimir.
    E
     
    Last edited: Feb 18, 2015
  11. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    After a great deal of testing/trying it appears that the High level statements for I2C can apply to any pair of pins of a PIC, not just to the internal I2C module.!
    The way the documentation reads is that low level statements should be used for PIC's that do not have have an internal I2C module.
    I took this literally, as if I used pins other than the dedicated I2C pins I would have to use low level statements.

    For other users of the DS3231 this Read on 1sec interrupt works OK.
    Thanks All! :angelic:

    Code (text):


    Define I2CREAD_DELAYUS = 400
    Define I2CCLOCK_STRETCH = 10
    'define the SDA and SCL channels ,  The actual internal I2C pins are PORTB.0 and B.1 which I am using for SD Cards and  radio transceivers.
    Symbol sda = PORTC.1
    Symbol scl = PORTC.2
    'interrupt
    On Low Interrupt
    Save System
    INTCON.TMR0IF = 0
    iflag = 1
    read_rtc:
    I2CStart
    I2CSend 0xd0  'DS3231 slave addr wr
    WaitUs 10
    I2CSend 0x00  'first word addr wr
    I2CStop
    WaitUs 10
    I2CStart
    I2CRead sda, scl, 0xd1, 0, secs
    I2CRead sda, scl, 0xd1, 1, mins
    I2CRead sda, scl, 0xd1, 2, hrs
    I2CRead sda, scl, 0xd1, 3, day
    I2CRead sda, scl, 0xd1, 4, date
    I2CRead sda, scl, 0xd1, 5, mnth
    I2CRead sda, scl, 0xd1, 6, year
    I2CRead sda, scl, 0xd1, 0x11, temph
    I2CRead sda, scl, 0xd1, 0x12, templ
    I2CStop
    TMR0 = 0xff
    INTCON.TMR0IE = 1
    Resume  
     
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Yeah! Vladimir has only implemented bit banging I2C and SPI.. I have stopped using Oshonsoft now as I have better compilers... I still use it here on the forum to help others...
     
    • Agree Agree x 1
  13. languer

    languer Active Member

    Joined:
    Oct 26, 2010
    Messages:
    193
    Likes:
    25
    Location:
    Arizona
    Yeap, it has been like that since day one. If you check the LST file you will quickly realize that the MSSP registers are never touched with the Oshonsoft routines. One can always create their own PROC/FN using direct register calls.

    I also stopped using the compiler for the most part. I still like the simulator, but it is so far behind in terms of device support that I use it less and less all the time. Pity, since it held such great promise.
     
  14. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi lanquer,
    Have you had a chance to try the SD Card sim IDE of Oshonsoft.?

    I can get the Block statements to write/read OK.
    The FAT16 and FAT32 statements work OK ,with the exception of the FileAppend and FileExist statements.
    A PC, SD Card reader slot can read the Oshonsoft FAT's OK and a Formatter program off the web is available.

    To use FAT the PIC must have at least 1024 RAM Bytes, I use a 18F2550 off the shelf.

    Sent a bug report to Vladimir, he suggested I download the latest versions, which I already had! also to make sure I had sufficient decoupling on my circuit!!
    How he considers that would only effect Append and Exist when every other statements works OK is puzzling.
    Also in I2C and assembler the circuit boards work OK.

    My intention was to continue postings/blogs that used only Oshonsoft basic, so that other users could benefit, rather than try to decipher the OSH manuals.

    Eric
     

Share This Page