Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

I2C


  • Please log in to reply
2 replies to this topic

#1 Stari

Stari

    Member

  • Members
  • PipPip
  • 26 posts

Posted 23 February 2012 - 11:24 AM

Hello I just finished a few things, thanks to this community. I wonder if anyone has made ​​communication with the DS1307 in VB ? In C# all things are working well, but if anyone has example in Vb, i will be very happy. Stari http://www.zlatnajedra.com

#2 mimtarsden

mimtarsden

    New Member

  • Members
  • Pip
  • 8 posts

Posted 27 February 2012 - 09:24 PM

Hi,

This is the code i used, I found it in the C# forum I 'think' it was originally by Fabien Royer, I quickly translated it to VB it seems to work perfectly...

Namespace netduino.helpers.Hardware
    ''' <summary>
    ''' This class implements a complete driver for the Dallas Semiconductors / Maxim DS1307 I2C real-time clock: http://pdfserv.maxim-ic.com/en/ds/DS1307.pdf
    ''' </summary>
    Public Class DS1307
        Implements IDisposable
        ' Defines the frequency of the signal on the SQW interrupt pin on the clock when enabled
        <Flags()> _
        Public Enum SQWFreq
            SQW_1Hz
            SQW_4kHz
            SQW_8kHz
            SQW_32kHz
            SQW_OFF
        End Enum

        ' Defines the logic level on the SQW pin when the frequency is disabled
        <Flags()> _
        Public Enum SQWDisabledOutputControl
            Zero
            One
        End Enum

        ' Real time clock I2C address
        Public Const DS1307_I2C_ADDRESS As Integer = &H68
        ' I2C bus frequency for the clock
        Public Const DS1307_I2C_CLOCK_RATE_KHZ As Integer = 100

        ' Allow 10ms timeouts on all I2C transactions
        Public Const DS1307_I2C_TRANSACTION_TIMEOUT_MS As Integer = 10

        ' Start / End addresses of the date/time registers
        Public Const DS1307_RTC_START_ADDRESS As Byte = &H0
        Public Const DS1307_RTC_END_ADDRESS As Byte = &H6

        ' Square wave frequency generator register address
        Public Const DS1307_SQUARE_WAVE_CTRL_REGISTER_ADDRESS As Byte = &H7

        ' Start / End addresses of the user RAM registers
        Public Const DS1307_RAM_START_ADDRESS As Byte = &H8
        Public Const DS1307_RAM_END_ADDRESS As Byte = &H3F

        ' Total size of the user RAM block
        Public Const DS1307_RAM_SIZE As Byte = 56

        ' Instance of the I2C clock
        Private clock As I2CDevice

        Public Sub New()
            clock = New I2CDevice(New I2CDevice.Configuration(DS1307_I2C_ADDRESS, DS1307_I2C_CLOCK_RATE_KHZ))
        End Sub

        ''' <summary>
        ''' Gets the date / time in 24 hour format.
        ''' </summary>
        ''' <returns>A DateTime object</returns>
        Public Function [Get]() As DateTime
            Dim clockData As Byte() = New Byte(6) {}

            ' Read time registers (7 bytes from DS1307_RTC_START_ADDRESS)
            Dim transaction = New I2CDevice.I2CTransaction() {I2CDevice.CreateWriteTransaction(New Byte() {DS1307_RTC_START_ADDRESS}), I2CDevice.CreateReadTransaction(clockData)}

            If clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) = 0 Then
                Throw New Exception("I2C transaction failed")
            End If

            ' year
            ' month
            ' day
            ' hours over 24 hours
            ' minutes
            ' seconds
            Return New DateTime(BcdToDec(clockData(6)) + 2000, BcdToDec(clockData(5)), BcdToDec(clockData(4)), BcdToDec(clockData(2) And &H3F), BcdToDec(clockData(1)), BcdToDec(clockData(0) And &H7F))
        End Function

        ''' <summary>
        ''' Sets the time on the clock using the datetime object. Milliseconds are not used.
        ''' </summary>
        ''' <param name="dt">A DateTime object used to set the clock</param>
        Public Sub [Set](ByVal dt As DateTime)
            Dim transaction = New I2CDevice.I2CWriteTransaction() {I2CDevice.CreateWriteTransaction(New Byte() {DS1307_RTC_START_ADDRESS, DecToBcd(dt.Second), DecToBcd(dt.Minute), DecToBcd(dt.Hour), DecToBcd(CInt(dt.DayOfWeek)), DecToBcd(dt.Day), _
             DecToBcd(dt.Month), DecToBcd(dt.Year - 2000)})}

            If clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) = 0 Then
                Throw New Exception("I2C write transaction failed")
            End If
        End Sub

        ''' <summary>
        ''' Enables / Disables the square wave generation function of the clock.
        ''' Requires a pull-up resistor on the clock's SQW pin.
        ''' </summary>
        ''' <param name="Freq">Desired frequency or disabled</param>
        ''' <param name="OutCtrl">Logical level of output pin when the frequency is disabled (zero by default)</param>
        Public Sub SetSquareWave(ByVal Freq As SQWFreq, Optional ByVal OutCtrl As SQWDisabledOutputControl = SQWDisabledOutputControl.Zero)
            Dim SqwCtrlReg As Byte = CByte(OutCtrl)

            SqwCtrlReg <<= 3
            ' bit 7 defines the square wave output level when disabled
            ' bit 6 & 5 are unused
            If Freq <> SQWFreq.SQW_OFF Then
                SqwCtrlReg = CByte(SqwCtrlReg Or 1)
            End If

            SqwCtrlReg <<= 4
            ' bit 4 defines if the oscillator generating the square wave frequency is on or off.
            ' bit 3 & 2 are unused
            SqwCtrlReg = SqwCtrlReg Or CByte(Freq)
            ' bit 1 & 0 define the frequency of the square wave
            WriteRegister(DS1307_SQUARE_WAVE_CTRL_REGISTER_ADDRESS, SqwCtrlReg)
        End Sub

        ''' <summary>
        ''' Halts / Resumes the time-keeping function on the clock.
        ''' Calling this function preserves the value of the seconds register.
        ''' </summary>
        ''' <param name="halt">True: halt, False: resume</param>
        Public Sub Halt(ByVal halt__1 As Boolean)
            Dim seconds = Me(DS1307_RTC_START_ADDRESS)

            If halt__1 Then
                ' Set bit 7
                seconds = CByte(seconds Or &H80)
            Else
                ' Reset bit 7
                seconds = CByte(seconds And &H7F)
            End If

            WriteRegister(DS1307_RTC_START_ADDRESS, seconds)
        End Sub

        ''' <summary>
        ''' Writes to the clock's user RAM registers as a block
        ''' </summary>
        ''' <param name="buffer">A byte buffer of size DS1307_RAM_SIZE</param>
        Public Sub SetRAM(ByVal buffer As Byte())
            If buffer.Length <> DS1307_RAM_SIZE Then
                Throw New ArgumentOutOfRangeException("Invalid buffer length")
            End If

            ' Allocate a new buffer large enough to include the RAM start address byte and the payload
            'Address byte
            Dim TrxBuffer = New Byte(1 + (DS1307_RAM_SIZE - 1)) {}

            ' Set the RAM start address
            TrxBuffer(0) = DS1307_RAM_START_ADDRESS

            ' Copy the user buffer after the address
            buffer.CopyTo(TrxBuffer, 1)

            ' Write to the clock's RAM
            Dim transaction = New I2CDevice.I2CWriteTransaction() {I2CDevice.CreateWriteTransaction(TrxBuffer)}

            If clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) = 0 Then
                Throw New Exception("I2C write transaction failed")
            End If
        End Sub

        ''' <summary>
        ''' Reads the clock's user RAM registers as a block.
        ''' </summary>
        ''' <returns>A byte array of size DS1307_RAM_SIZE containing the user RAM data</returns>
        Public Function GetRAM() As Byte()
            Dim ram = New Byte(DS1307_RAM_SIZE - 1) {}

            Dim transaction = New I2CDevice.I2CTransaction() {I2CDevice.CreateWriteTransaction(New Byte() {DS1307_RAM_START_ADDRESS}), I2CDevice.CreateReadTransaction(ram)}

            If clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) = 0 Then
                Throw New Exception("I2C transaction failed")
            End If

            Return ram
        End Function

        ''' <summary>
        ''' Reads an arbitrary RTC or RAM register
        ''' </summary>
        ''' <param name="address">Register address between 0x00 and 0x3f</param>
        ''' <returns>The value of the byte read at the address</returns>
        Default Public ReadOnly Property Item(ByVal address As Byte) As Byte
            Get
                If address > DS1307_RAM_END_ADDRESS Then
                    Throw New ArgumentOutOfRangeException("Invalid register address")
                End If

                Dim value = New Byte(0) {}

                ' Read the RAM register @ the address
                Dim transaction = New I2CDevice.I2CTransaction() {I2CDevice.CreateWriteTransaction(New Byte() {address}), I2CDevice.CreateReadTransaction(value)}

                If clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) = 0 Then
                    Throw New Exception("I2C transaction failed")
                End If

                Return value(0)
            End Get
        End Property

        ''' <summary>
        ''' Writes an arbitrary RTC or RAM register
        ''' </summary>
        ''' <param name="address">Register address between 0x00 and 0x3f</param>
        ''' <param name="val">The value of the byte to write at that address</param>
        Public Sub WriteRegister(ByVal address As Byte, ByVal val As Byte)
            If address > DS1307_RAM_END_ADDRESS Then
                Throw New ArgumentOutOfRangeException("Invalid register address")
            End If

            Dim transaction = New I2CDevice.I2CWriteTransaction() {I2CDevice.CreateWriteTransaction(New Byte() {address, CByte(val)})}

            If clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) = 0 Then
                Throw New Exception("I2C write transaction failed")
            End If
        End Sub

        ''' <summary>
        ''' Takes a Binary-Coded-Decimal value and returns it as an integer value
        ''' </summary>
        ''' <param name="val">BCD encoded value</param>
        ''' <returns>An integer value</returns>
        Protected Function BcdToDec(ByVal val As Integer) As Integer
            Return (((val \ 16) * 10) + (val Mod 16))
        End Function

        ''' <summary>
        ''' Takes a Decimal value and converts it into a Binary-Coded-Decimal value
        ''' </summary>
        ''' <param name="val">Value to be converted</param>
        ''' <returns>A BCD-encoded value</returns>
        Protected Function DecToBcd(ByVal val As Integer) As Byte
            Return CByte(((val \ 10) * 16) + (val Mod 10))
        End Function

        ''' <summary>
        ''' Releases clock resources
        ''' </summary>
        Public Sub Dispose() Implements IDisposable.Dispose
            clock.Dispose()
        End Sub
    End Class
End Namespace

Hope that helps.

#3 Stari

Stari

    Member

  • Members
  • PipPip
  • 26 posts

Posted 28 February 2012 - 04:02 PM

Thank you. Very useful. Stari http://www.zlatnajedra.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.