​ ​ ​ ​ Bit banging i2c on 08M - Page 4
Page 4 of 4 FirstFirst ... 2 3 4
Results 31 to 31 of 31

Thread: Bit banging i2c on 08M

  1. #31
    Senior Member
    Join Date
    Apr 2013
    Location
    north dakota
    Posts
    462
    Blog Entries
    2

    Default

    Hi
    After testing,retesting,reading,rereading here is I2C bit-banged version AA
    Code:
     'I2C bit_banged to read(only) time from the DS3231 RTC  Version AA 
    #picaxe 20X2
    #NO_DATA
    #terminal 38400
    setfreq m32
    
      Symbol  i2cDataByte     = b6     ' send/read byte
      Symbol  i2cBitCount     = b7     ' loop for byte data (8)bits
      SYMBOL  secs            = b8     ' $00-$59
      SYMBOL  mins            = b9     ' $00-$59
      SYMBOL  hours           = b10    ' $00-$23
      SYMBOL  day             = b11    ' $01-$07   
      SYMBOL  date            = b12    ' $01-$31
      SYMBOL  month           = b13    ' $01-$12
      SYMBOL  year            = b14    ' $00-$99
    
      Symbol  SCL             = B.0    ' picaxe-B.0 to DS3231-SCL  with 4k7-ohm pullup resistor (+5vdc)
      Symbol  SDA             = B.1    ' picaxe-B.1 to DS3231-SDA  with 4k7-ohm pullup resistor (+5vdc)
      Symbol  SCL_PIN         = pinB.0 
      Symbol  SDA_PIN         = pinB.1 
       
     
      Symbol  I2C_ADR     = %11010000             ;Slave Address for DS3231
    
    #Define BcdToDec(bcd) bcd / $10 * 250 + bcd   ;switch bcd to decimal
     
    Power_Reset: 
     Gosub I2C_Stop  
    Main: '==========================Main Program====================================================
                                Gosub I2C_Start
    i2cDataByte = I2C_ADR | 0 : Gosub SendByteAndReadAck                       ; Write to slave device $D0
    
    i2cDataByte = $00         : Gosub SendByteAndReadAck                       ; Set address to $00
    
                                Gosub I2C_Start                
    i2cDataByte = I2C_ADR | 1 : Gosub SendByteAndReadAck                       ; Read from slave device $D1
    
                                Gosub ReadByteAndSendAck :secs  = i2cDataByte  ; Data Address $00         
                                Gosub ReadByteAndSendAck :mins  = i2cDataByte  ; Data Address $01
                                Gosub ReadByteAndSendAck :hours = i2cDataByte  ; Data Address $02                         
                                Gosub ReadByteAndSendAck :day   = i2cDataByte  ; Data Address $03                         
                                Gosub ReadByteAndSendAck :date  = i2cDataByte  ; Data Address $04
                                Gosub ReadByteAndSendAck :month = i2cDataByte  ; Data Address $05                         
                                Gosub ReadByteAndSendNAk :year  = i2cDataByte  ; Data Address $06 (last byte read) 
                                Gosub I2C_Stop 
                                Gosub Sertxd_Time     'sertxd out all 6 bytes of time/date     
    Goto Main 
    
    Sertxd_Time:   
    
     secs   = BcdToDec( secs  )
     mins   = BcdToDec( mins  )
     hours  = BcdToDec( hours )
     day    = BcdToDec( day   )
     date   = BcdToDec( date  )
     month  = BcdToDec( month )
     year   = BcdToDec( year  )
    
      sertxd ("----Start----",  cr,lf) 
      sertxd ("secs  = ",#secs, cr,lf) 
      sertxd ("mins  = ",#mins, cr,lf) 
      sertxd ("hours = ",#hours,cr,lf) 
      sertxd ("day   = ",#day,  cr,lf) 
      sertxd ("date  = ",#date, cr,lf) 
      sertxd ("month = ",#month,cr,lf) 
      sertxd ("year  = ",#year, cr,lf)
      sertxd ("-----End------", cr,lf)
      pause 8000   
    Return 
    SendByteAndReadAck:   ' sending slave/data addresses to the DS3231   
      For i2cBitCount = 0 TO 7
        If i2cDataByte < $80 Then
          Low SDA                                ; SDA = 0
        Else
          Input SDA                              ; SDA = 1
        End If
        Input SCL                                ; SCL = 1
        Do : Loop Until SCL_PIN = 1
        Low SCL                                  ; SCL = 0            
        i2cDataByte = i2cDataByte * 2             
      Next     
      Input SDA
      Input SCL                                  ; SCL = 1
      Do : Loop Until SCL_PIN = 1
      Low SCL                                    ; SCL = 0            
    Return 
    
    ReadByteAndSendAck: ' reading data bytes from the DS3231
      Input SDA      
      For i2cBitCount = 0 TO 7
        Input SCL                                ; SCL = 1
        Do : Loop Until SCL_PIN = 1
        i2cDataByte = i2cDataByte * 2 | SDA_PIN            
        Low SCL                                  ; SCL = 0            
      Next
      Low SDA                                    ; SDA = 0
      Input SCL                                  ; SCL = 1
      Do : Loop Until SCL_PIN = 1
      Low SCL                                    ; SCL = 0            
      Input SDA                                  ; SDA = 1
    Return
    
    ReadByteAndSendNak: ' reading last data byte from the DS3231
      Input SDA                                  ; SDA = 1
      For i2cBitCount = 0 TO 7
        Input SCL                                ; SCL = 1
        Do : Loop Until SCL_PIN = 1
        i2cDataByte = i2cDataByte * 2 | SDA_PIN            
        Low SCL                                  ; SCL = 0            
      Next
      Input SCL                                  ; SCL = 1
      Do : Loop Until SCL_PIN = 1
      Low SCL                                    ; SCL = 0            
    Return
    I2C_Start:   
                  Input SCL                       ; SCL = 1
                  Input SDA                       ; SDA = 1
                  Low SDA                         ; SDA = 0
                  Low SCL                         ; SCL = 0   
    Return 
    I2C_Stop:
                  Low SCL                         ; SCL = 0 
                  Low SDA                         ; SDA = 0
                  Input SCL                       ; SCL = 1
                  Input SDA                       ; SDA = 1            
    Return
    Last edited by newplumber; 18-04-2018 at 20:54.
    programming a picaxe is some what like playing chess....and I'm horrible at chess

Bookmarks

Posting Permissions

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