Bascom komunikasi I2C

$regfile = "reg51.dat"
$crystal = 11059200
$baud = 9600

Dim Address As Byte , Alamat As Byte
Dim Reg_sec As Byte , Reg_min As Byte , Reg_hour As Byte
Dim Num_hour As Byte , Num_min As Byte , Num_sec As Byte
Dim Reg_date As Byte , Reg_month As Byte , Reg_year As Byte
Dim Num_date As Byte , Num_month As Byte , Num_year As Byte
Dim Pm_bit As Bit
Dim Masuk As String * 1

'------------------------------------------
' config I2C bus
Config Scl = P2.0
' I2C Clock
Config Sda = P2.1
' I2C Data
'-------------------------------------------

Const Ds1307w = &HD0
' Write
Const Ds1307r = &HD1
' Read

Const Sec_addmap = &H00
'Const Min_addmap = &H01
'Const Hour_addmap = &H02

Const Date_addmap = &H04
'Const Month_addmap = &H05
'Const Year_addmap = &H06

Declare Sub Siapkan(alamat As Byte)

Do
   Input Masuk
   Select Case Masuk
   Case "t" : Call Read_time
              Print Num_hour ; ":" ; Num_min ; ":" ; Num_sec
   Case "d" : Call Read_date
              Print Num_date ; "/" ; Num_month ; "/" ; Num_year
   Case "i" : Print "made by me!"
   Case Else
      Print "er"
   End Select
Loop
End

'-=-=-=-=-=-=-=-=-=-=-=-=-=
Sub Read_time
'-=-=-=-=-=-=-=-=-=-=-=-=-=
   Call Siapkan(sec_addmap)
   I2crbyte Reg_sec , Ack
   I2crbyte Reg_min , Ack
   I2crbyte Reg_hour , Nack
   I2cstop
' Generate Stop
' check for am/pm
   If Reg_hour.5 = 1 Then
      Pm_bit = 1
   Else
      Pm_bit = 0
   End If
 'reg_hour.6=1(12 hrmode) =0(24 hourmode)
'=-=-=-=-=-=-=-=-=-=-=-
   Reg_hour = Reg_hour And &B00011111
' remove 12/24 and am/pm bits
' set numeric variables
   Num_hour = Makedec(reg_hour)
   Num_min = Makedec(reg_min)
   Num_sec = Makedec(reg_sec)
End Sub

'-=-=-=-=-=-=-=-=-=-=-=-=-=
Sub Read_date
'-=-=-=-=-=-=-=-=-=-=-=-=-=
   Call Siapkan(date_addmap)
   I2crbyte Reg_date , Ack
   I2crbyte Reg_month , Ack
   I2crbyte Reg_year , Nack
   I2cstop
' Generate Stop
' set numeric variables
   Num_date = Makedec(reg_date)
   Num_month = Makedec(reg_month)
   Num_year = Makedec(reg_year)
End Sub

Sub Siapkan(alamat As Byte)
' Read Clock Data
   I2cstart
' Generate start
   I2cwbyte Ds1307w
' Slave adsress
   I2cwbyte Alamat
' Address of SECONDS REG
   I2cstop
' Generate Stop
   I2cstart
' Repeated start
   I2cwbyte Ds1307r
' Slave address (read)
End Sub

Postingan populer dari blog ini

LCD 2x16 via ESP32

Perencanaan awal ESP32 S3 testing

winsocket vb.net