Downloader Serial EEPROM i2c Programmer dgn Visual basic VB6

Aplikasi untuk mengisi IC memory serial / I2C EEProm

Aplikasi I2C EEProm downloader

Rangkaian interface dari LPT ke IC EEPROM:

Code Program dalam Visual Basic 6:

Private Sub cmdreadbyte_Click()
‘ A 0 0 A 1
‘kirim start->1010.000.0->0000.0000->start->1010.000.1->bacadata->nack->stop

Dim val, ack, tampung As Byte
val = 0
Dim I, l, DATAX, controlbyte As Integer

i2cstart
controlbyte = writecontrolbyte Or (Geser_Kanan(((“&H” & txtalamatedit) And &H700), 7))
If (i2csendbyte(controlbyte)) <> 0 Then
i2cstop
MsgBox “ada kesalahan sewaktu menulis 1 read byte”
End
End If

If (i2csendbyte((“&H” & txtalamatedit) And &HFF)) <> 0 Then
i2cstop
MsgBox address, , “ada kesalahan sewaktu menulisalamat”
End
End If

‘issue read
i2cstart
controlbyte = readcontrolbyte Or (Geser_Kanan(((“&H” & txtalamatedit) And &H700), 7))

If (i2csendbyte(controlbyte)) <> 0 Then
i2cstop
MsgBox “ada kesalahan sewaktu menulis 3 readbyte”
End
End If

‘=====i2cread byte======
txtdataedit = Hex(i2creadbyte(1))
i2cstop

End Sub

Private Sub Command1_Click()
Dim I As Integer

If txtalamatedit = “” Then
Exit Sub
End If

MsgBox Hex(txtalamatedit), , Hex(txtbytesize – 1)
If txtalamatedit > txtbytesize – 1 Then
MsgBox txtbytesize – 1, , “MAXIMUM ALAMAT =”
Exit Sub
End If

For I = 0 To txtbytesize – 1
If Hex(txtalamatedit) = Hex(I) Then
txtdataedit = Hex(buffer1(I))
End If

Next I

End Sub

Private Sub Command2_Click()
‘ A 0 0 A 1
‘kirim start->1010.000.0 ->0000.0000->start->1010.000.1->bacadata->nack->stop
‘ start – deviceaddress – word add – data-stop

Dim I, l, DATAX, controlbyte As Integer

i2cstart
‘=====device address====
controlbyte = writecontrolbyte Or (Geser_Kanan(((“&H” & txtalamatedit) And &H700), 7))
If (i2csendbyte(controlbyte)) <> 0 Then
i2cstop
MsgBox “ada kesalahan sewaktu menulis command2″
End
End If

‘===word adresss======
If (i2csendbyte((“&H” & txtalamatedit) And &HFF)) <> 0 Then
i2cstop
MsgBox address, , “ada kesalahan sewaktu menulisalamat”
End
End If

‘===write byte—-

If (i2csendbyte((“&H” & txtdataedit) And &HFF)) <> 0 Then
i2cstop
MsgBox address, , “ada kesalahan sewaktu menulisalamat”
End If
i2cstop

tunda 10 ‘ beri dia kesempatan selama 0.5 detik menuliskannya
‘txtalamatedit = Hex(((grid.ColSel – 1) + (16 * (grid.RowSel – 1))))
Call cmdreadbyte_Click
tunda 10
grid.TextMatrix(grid.RowSel, grid.ColSel) = txtdataedit

MsgBox “write 1 byte ok”

End Sub

Private Sub Command4_Click()
Dim I As Integer
For I = 0 To txtbytesize – 1
Combo2.AddItem buffer1(I)
Combo3.AddItem Hex(buffer2(I))
Next I
End Sub

Private Sub Command6_Click()
If txteepromfile = 0 Then
grid.TextMatrix(txtrow, txtcol) = txtdataedit
End If
MsgBox “cheksum belum dihitung ulang …!”
End Sub

Private Sub Form_Load()
Dim I As Integer

cmdreadbyte.Visible = False
Command2.Visible = False
Command6.Visible = False
writecontrolbyte = &HA0
readcontrolbyte = &HA1
Out &H378, &HFC
Out &H37A, ((Inp(&H37A) And &HF0) Or &H4)
Opoff.value = True
control_byte = &HA4
Setup_output
tunda 0.1
txtbytesize.Text = 2048
txtnumber.Text = 1
addr.Text = 2
s$ = “|^00 |^01 |^02 |^03 |^04 |^05 |^06 |^07 |^08 |^09 |^0A |”
s$ = s$ & “^0B |^0C |^0D |^0E |^0F “
s$ = s$ & “;|0000|0010 |0020 |0030 |0040 |0050 |0060 |0070 |0080 |0090 |00A0 |”
s$ = s$ & “00B0 |00C0 |00D0 |00E0 |00F0 |”
s$ = s$ & “0100 |0110 |0120 |0130 |0140 |0150 |0160 |0170 |0180 |0190 |01A0 |01B0 |01C0 |01D0 |01E0 |01F0 |”
s$ = s$ & “0200 |0210 |0220 |0230 |0240 |0250 |0260 |0270 |0280 |0290 |02A0 |02B0 |02C0 |02D0 |02E0 |02F0 |”
s$ = s$ & “0300 |0310 |0320 |0330 |0340 |0350 |0360 |0370 |0380 |0390 |03A0 |03B0 |03C0 |03D0 |03E0 |03F0 |”
s$ = s$ & “0400 |0410 |0420 |0430 |0440 |0450 |0460 |0470 |0480 |0490 |04A0 |04B0 |04C0 |04D0 |04E0 |04F0 |”
s$ = s$ & “0500 |0510 |0520 |0530 |0540 |0550 |0560 |0570 |0580 |0590 |05A0 |05B0 |05C0 |05D0 |05E0 |05F0 |”
s$ = s$ & “0600 |0610 |0620 |0630 |0640 |0650 |0660 |0670 |0680 |0690 |06A0 |06B0 |06C0 |06D0 |06E0 |06F0 |”
s$ = s$ & “0700 |0710 |0720 |0730 |0740 |0750 |0760 |0770 |0780 |0790 |07A0 |07B0 |07C0 |07D0 |07E0 |07F0 “
grid.FormatString = s$

For I = 0 To txtbytesize – 1
buffer2(I) = &HFF
Next I

End Sub

Private Sub grid_Click()
Dim jumlah, I, J As Integer

If val(txtepromfile) = 1 Then
txtisi = “isi EEPROM”

txtalamatedit = Hex(((grid.ColSel – 1) + (16 * (grid.RowSel – 1))))

tunda 0.5
Call cmdreadbyte_Click

ElseIf val(txtepromfile) = 0 Then
txtisi = “isi file”
txtalamatedit = Hex(((grid.ColSel – 1) + (16 * (grid.RowSel – 1))))
txtdataedit = grid.Text
Else
MsgBox “buka file atau baca eprom”

End If

End Sub

Private Sub grid_EnterCell()
Dim I, J As Integer

If val(txtepromfile) = 1 Then
txtisi = “isi EEprom”
txtalamatedit = Hex(((grid.ColSel – 1) + (16 * (grid.RowSel – 1))))
tunda 0.05
Call cmdreadbyte_Click

txtalamatedit = Hex(((grid.ColSel – 1) + (16 * (grid.RowSel – 1))))

ElseIf val(txtepromfile) = 0 Then
txtisi = “isi file”
txtalamatedit = Hex(((grid.ColSel – 1) + (16 * (grid.RowSel – 1))))
txtdataedit = grid.Text

Else
MsgBox “buka file atau baca eprom”
End If
txtcol.Text = grid.Col
txtrow.Text = grid.Row

End Sub

Private Sub grid_LostFocus()
‘Label3.Caption = “ALAMAT = ” & Hex(((grid.ColSel – 1) + (16 * (grid.RowSel – 1))))

End Sub

Private Sub mn16K_Click()

txtbytesize.Text = 2048
txtnumber.Text = 1
addr.Text = 2
s$ = “|^00 |^01 |^02 |^03 |^04 |^05 |^06 |^07 |^08 |^09 |^0A |”
s$ = s$ & “^0B |^0C |^0D |^0E |^0F “
s$ = s$ & “;|0000|0010 |0020 |0030 |0040 |0050 |0060 |0070 |0080 |0090 |00A0 |”
s$ = s$ & “00B0 |00C0 |00D0 |00E0 |00F0 |”
s$ = s$ & “0100 |0110 |0120 |0130 |0140 |0150 |0160 |0170 |0180 |0190 |01A0 |01B0 |01C0 |01D0 |01E0 |01F0 |”
s$ = s$ & “0200 |0210 |0220 |0230 |0240 |0250 |0260 |0270 |0280 |0290 |02A0 |02B0 |02C0 |02D0 |02E0 |02F0 |”
s$ = s$ & “0300 |0310 |0320 |0330 |0340 |0350 |0360 |0370 |0380 |0390 |03A0 |03B0 |03C0 |03D0 |03E0 |03F0 |”
s$ = s$ & “0400 |0410 |0420 |0430 |0440 |0450 |0460 |0470 |0480 |0490 |04A0 |04B0 |04C0 |04D0 |04E0 |04F0 |”
s$ = s$ & “0500 |0510 |0520 |0530 |0540 |0550 |0560 |0570 |0580 |0590 |05A0 |05B0 |05C0 |05D0 |05E0 |05F0 |”
s$ = s$ & “0600 |0610 |0620 |0630 |0640 |0650 |0660 |0670 |0680 |0690 |06A0 |06B0 |06C0 |06D0 |06E0 |06F0 |”
s$ = s$ & “0700 |0710 |0720 |0730 |0740 |0750 |0760 |0770 |0780 |0790 |07A0 |07B0 |07C0 |07D0 |07E0 |07F0 “
grid.FormatString = s$
End Sub

Private Sub mn1K_Click()
grid.Refresh

Label1.Visible = True
Label2.Visible = True
txtbytesize.Visible = True
txtnumber.Visible = True
txtbytesize.Text = 128
txtnumber.Text = 8
addr.Text = 2
grid.Row = 4
grid.Redraw = True
grid.Refresh

s$ = “|^00 |^01 |^02 |^03 |^04 |^05 |^06 |^07 |^08 |^09 |^0A |”
s$ = s$ & “^0B |^0C |^0D |^0E |^0F “
s$ = s$ & “;|0000|0010 |0020 |0030 |0040 |0050 |0060 |0070 |0080 |0090 |00A0 |”
s$ = s$ & “00B0 |00C0 |00D0 |00E0 |00F0 “
grid.FormatString = s$
grid.Redraw = True
grid.Refresh
End Sub

Private Sub mn2K_Click()
txtbytesize.Text = 256
txtnumber.Text = 8
addr.Text = 2
s$ = “|^00 |^01 |^02 |^03 |^04 |^05 |^06 |^07 |^08 |^09 |^0A |”
s$ = s$ & “^0B |^0C |^0D |^0E |^0F “
s$ = s$ & “;|0000|0010 |0020 |0030 |0040 |0050 |0060 |0070 |0080 |0090 |00A0 |”
s$ = s$ & “00B0 |00C0 |00D0 |00E0 |00F0 “
grid.FormatString = s$
End Sub

Private Sub mn4K_Click()
txtbytesize.Text = 512
txtnumber.Text = 4
addr.Text = 2
s$ = “|^00 |^01 |^02 |^03 |^04 |^05 |^06 |^07 |^08 |^09 |^0A |”
s$ = s$ & “^0B |^0C |^0D |^0E |^0F “
s$ = s$ & “;|0000|0010 |0020 |0030 |0040 |0050 |0060 |0070 |0080 |0090 |00A0 |”
s$ = s$ & “00B0 |00C0 |00D0 |00E0 |00F0 |”
s$ = s$ & “0100 |0110 |0120 |0130 |0140 |0150 |0160 |0170 |0180 |0190 |01A0 |01B0 |01C0 |01D0 |01E0 |01F0 “
grid.FormatString = s$
End Sub

Private Sub mn8K_Click()
txtbytesize.Text = 1024
txtnumber.Text = 2
addr.Text = 2
s$ = “|^00 |^01 |^02 |^03 |^04 |^05 |^06 |^07 |^08 |^09 |^0A |”
s$ = s$ & “^0B |^0C |^0D |^0E |^0F “
s$ = s$ & “;|0000|0010 |0020 |0030 |0040 |0050 |0060 |0070 |0080 |0090 |00A0 |”
s$ = s$ & “00B0 |00C0 |00D0 |00E0 |00F0 |”
s$ = s$ & “0100 |0110 |0120 |0130 |0140 |0150 |0160 |0170 |0180 |0190 |01A0 |01B0 |01C0 |01D0 |01E0 |01F0 |”
s$ = s$ & “0200 |0210 |0220 |0230 |0240 |0250 |0260 |0270 |0280 |0290 |02A0 |02B0 |02C0 |02D0 |02E0 |02F0 |”
s$ = s$ & “0300 |0310 |0320 |0330 |0340 |0350 |0360 |0370 |0380 |0390 |03A0 |03B0 |03C0 |03D0 |03E0 |03F0 “
grid.FormatString = s$
End Sub

Private Sub mnconvert_Click()
Dim I As Integer
Dim J As Integer
Dim text_konfersi As String

Baca_File_Hex ‘ubah source hex file ke data byte dan simpan di buffer1

tunda 1000

For J = 1 To val(txtbytesize.Text) / 16
For I = 1 To 16
y = (buffer1((I – 1) + ((J – 1) * 16))) ‘aslinya buffer1 dirubah oleh dodi
grid.TextMatrix(J, I) = Hex(y) ‘tampilkan di grid dalam bentuk hexa
If y > 15 Then
grid.TextMatrix(J, I) = Hex(y)
Else
grid.TextMatrix(J, I) = “0″ & Hex(y)
End If
Next I
Next J

Label5.Caption = “data from converting ” & CommonDialog1.FileName & ” is :”
grid.ToolTipText = “data hex from ” & CommonDialog1.FileName
End Sub

Private Sub mneraseall_Click()

If txtbytesize.Text = “” Then
MsgBox “pilih ukuran EEPROM nya”
Exit Sub
End If

Dim count, p, reg, n As Integer
Dim writecontrolbyte As Byte
Dim I, J, a As Integer

‘buffer2 = FF
For I = 0 To txtbytesize.Text
buffer2(I) = &HFF ‘aslinya bufer1 | buffer diisi dengan data FFh, sebagai default data EEPROM siap diisi
Next I

tunda 300

writecontrolbyte = &HA0 ‘writecontrolbyte

count = 0
p = 0
reg = 0
‘———————————————

For n = 1 To txtnumber.Text ‘nxIC EEPROM yang akan di tulis

For J = 0 To ((txtbytesize.Text / 8 ) – 1) ‘jumlah page memori IC

Label4.Caption = ” …processing data,please wait…”
‘————————————————–
‘inisialisasi 1 byte device address untuk operasi page write data ke EEPROM hingga alamat 2048
If count > 1791 Then
writecontrolbyte = &HAE
ElseIf count > 1535 Then
writecontrolbyte = &HAC
ElseIf count > 1279 Then
writecontrolbyte = &HAA
ElseIf count > 1023 Then
writecontrolbyte = &HA8
ElseIf count > 767 Then
writecontrolbyte = &HA6
ElseIf count > 511 Then
writecontrolbyte = &HA4
ElseIf count > 255 Then
writecontrolbyte = &HA2
Else
writecontrolbyte = writecontrolbyte

End If

i2cstart ‘START -> perubahan sda dari haigh ke low saat scl high
i2csendbyte (writecontrolbyte) ‘kirim 1 byte device address

‘—————————————–
‘inisialisasi word address
If J > &HFF Then
reg = (J – 256) * 8

ElseIf J > 223 Then
reg = (J – 224) * 8

ElseIf J > 191 Then
reg = (J – 192) * 8

ElseIf J > 159 Then
reg = (J – 160) * 8

ElseIf J > 127 Then
reg = (J – 128 ) * 8

ElseIf J > 95 Then
reg = (J – 96) * 8

ElseIf J > 63 Then
reg = (J – 64) * 8

ElseIf J > 31 Then
reg = (J – 32) * 8

Else
reg = J * 8 ’0,8,16,32
End If

i2csendbyte (reg) ‘kirim word address
For k = 0 To 7
bar1.Max = 7
bar1.value = k
p = k + (8 * J)
i2csendbyte (buffer2(p)) ‘kirim data per byte dari buffer1 dirubah by dodi
count = count + 1
Next k
i2cstop
tunda 0.5001
Next J

‘inisialisasi 1 byte device address untuk pengalamatan eksternal beberapa IC
If txtbytesize.Text = 2048 Then ’1 IC 16 Kb
writecontrolbyte = writecontrolbyte
ElseIf txtbytesize.Text = 1024 Then ‘ IC 8 Kb ke 2
writecontrolbyte = writecontrolbyte + 8
ElseIf txtbytesize.Text = 512 Then ‘IC 4 Kb ke 2,3,4
writecontrolbyte = writecontrolbyte + 4
ElseIf txtbytesize.Text <= 256 Then ‘IC 1K dan 2K ke 2,3,4…8
writecontrolbyte = writecontrolbyte + 2
End If

Next n

bar1.value = 0

Label4.Caption = “…process completed…”
Label5.Caption = ” …for read the EEPROM use READ button…”
End Sub

Private Sub mnopen_Click()
namafile.Visible = True
Dim line_text, Teks As String
Dim Jum_Data, Al_awal, jenis, I, J, k, M As Integer
”On Error GoTo keluar
Command2.Visible = False
Command6.Visible = True
If Form1.txtbytesize.Text = “” Then
MsgBox “pilih ukuran EEPROM nya”
Exit Sub
End If

For J = 0 To txtbytesize – 1
buffer1(J) = &HFF
Next J

Form1.CommonDialog1.CancelError = True
Form1.CommonDialog1.Filter = “text file (*.hex)|*.hex”
Form1.CommonDialog1.ShowOpen

If Form1.CommonDialog1.FileName <> “” Then
namafile.Text = CommonDialog1.FileName
Open Form1.CommonDialog1.FileName For Input As #1
Form1.Refresh
jumlah_data = “ok”
Do Until EOF(1)
Line Input #1, line_text
Jum_Data = HexToByte2(Mid(line_text, 2, 2))
Al_awal = HexToByte4(Mid(line_text, 4, 4))
jenis = HexToByte2(Mid(line_text, 8, 2))
Teks = Teks & line_text & Chr(13) & Chr(10)
If ((Jum_Data <> 0) And (jenis = 0)) Then
For I = 0 To (Jum_Data – 1)
buffer1(Al_awal + I) = HexToByte2(Mid(line_text, 10 + (I * 2), 2)) ‘aslinya buffer1

Next I

End If
Loop
Close #1
txtdata.Text = Teks
End If

For M = 1 To val(txtbytesize) / 16
For k = 1 To 16
y = (buffer1((k – 1) + ((M – 1) * 16)))
grid.TextMatrix(M, k) = Hex(y) ‘tampilkan di grid dalam bentuk hexa
If y > 15 Then
grid.TextMatrix(M, k) = Hex(y)
Else
grid.TextMatrix(M, k) = “0″ & Hex(y)
End If
Next k
Next M

txtepromfile = 0
txtisi = “isi file”
Exit Sub
‘keluar:
‘ MsgBox “ada error/ pilih size “

End Sub

Private Sub mnrangkaian_Click()
Form3.Show
End Sub

Private Sub mnreadall_Click()
cmdreadbyte.Visible = True
namafile.Visible = False
Command2.Visible = True
Command6.Visible = False
If txtbytesize.Text = “” Then
MsgBox “pilih ukuran EEPROM nya”
Exit Sub
End If
bar1.value = 0

Call readrandom(0, val(txtbytesize))

‘—————————————
tunda 2000
Dim text_baca As String
Dim DATA, al_akhir, Al_awal, st, jenis, cs, count, jum_dat As Byte
Dim id, st1, st2 As String

For l = 1 To txtbytesize.Text / 16 ‘setting baris grid
count = 0
n_data = 0
jumlah = 0
DATA = “”
Al_awal = 0

For M = 1 To 16 ‘setting kolom grid

text_baca = (buffer2((M – 1) + ((l – 1) * 16)))

If text_baca > 15 Then ‘tiap sel diisikan 2 digit data hexa
grid.TextMatrix(l, M) = Hex(text_baca)
Else
grid.TextMatrix(l, M) = “0″ & Hex(text_baca)
End If

If grid.TextMatrix(l, M) = “FF” Then
count = count
Else
count = count + 1
End If

If count = 1 And grid.TextMatrix(l, M) <> “FF” Then ‘deteksi data 1 baris yang bukan “FF” yang akan
Al_awal = (M – 1) + ((l – 1) * 16) ‘digunakan sebagai data untuk alamat awal
Else
Al_awal = Al_awal
End If

If count < 1 Then
n_data = n_data
DATA = DATA
jumlah = jumlah
jenis = “”
id = “”
Else ‘jika alamat awal sudah terdeteksi, data setelah alamat awal termasuk data untuk file hex
n_data = n_data + 1 ‘pencacah data
DATA = DATA & (grid.TextMatrix(l, M))
jumlah = jumlah + HexToByte2(grid.TextMatrix(l, M)) ‘jumlah data untuk mempermudah cheksum
jenis = “00″
id = “:”
End If

Next M ‘naikkan kolom

jum_dat = (n_data) ‘jumlah data diubah ke bentuk hexa 2 digit
If jum_dat > 15 Then
y = Hex(jum_dat)
Else
y = “0″ & Hex(jum_dat)
End If

If Al_awal > 4095 Then ‘alamat awal diubah ke bilangan hexa 4 digit
z = Hex(Al_awal)
ElseIf Al_awal > 255 Then
z = “0″ & Hex(Al_awal)
ElseIf Al_awal > 15 Then
z = “00″ & Hex(Al_awal)
Else
z = “000″ & Hex(Al_awal)
End If

If Al_awal > 255 Then ‘untuk mempermudah ceksum yaitu alamat awal di jumlahkan 2 digit 2 digit
x = Al_awal – 255
Else
x = Al_awal
End If

cs = Right(Hex(256 – (jum_dat + x + jumlah)), 2) ‘cheksum = 2 digit terakhir hexa dari (256-(cacah data + al_awal + jumlah data))

st = id & y & z & jenis & DATA & cs & Chr(13) & Chr$(10) ‘format intel hex untuk data tiap baris

If st = 0 Then
st = “”
Else
st = st
End If
temp = temp & st

Next l

txtdata.Text = temp & “:00000001FF” ‘bagian akhir baris dari intel hex file

txtepromfile = 1
txtisi = “isi EEPROM”

MsgBox “ok”, , “selesai”

End Sub

Private Sub mnsaveas_Click()
Dim Teks As String
Teks = Teks & CStr(txtdata.Text)
CommonDialog1.Filter = “*.hex|*.hex”
CommonDialog1.CancelError = True
On Error GoTo kensel:
CommonDialog1.ShowSave
If CommonDialog1.FileName <> “” Then
Open CommonDialog1.FileName For Output As #1
Print #1, Teks
Close #1
End If
kensel:
End Sub

Private Sub mntentang_Click()

For I = 1 To 16
tunda 50
grid.TextMatrix(4, I) = Mid(txtwelcome, I, 1)
grid.TextMatrix(5, I) = Mid(txtwelcome4, I, 1)
Next I
For I = 16 To 1 Step -1
tunda 50
grid.TextMatrix(6, I) = Mid(txtwelcome2, I, 1)
grid.TextMatrix(7, I) = “*”
Next I
For I = 1 To 16
tunda 50
grid.TextMatrix(8, I) = Mid(txtwelcome3, I, 1)
grid.TextMatrix(9, I) = “>”
Next I
tunda 1000
mnuclearbuffer_Click

End Sub

Private Sub mntest_Click()
Form2.Show
End Sub

Private Sub mnuclearbuffer_Click()
Dim I, J, l, M As Integer
‘clear kan isi buffer2
For I = 0 To txtbytesize.Text
buffer1(I) = &HFF ‘aslinya bufer1 | buffer diisi dengan data FFh, sebagai default data EEPROM siap diisi
Next I

For J = 0 To txtbytesize.Text
buffer2(J) = &HFF ‘aslinya bufer1 | buffer diisi dengan data FFh, sebagai default data EEPROM siap diisi
Next J

‘—————————————
txtdata = “”
For l = 1 To txtbytesize.Text / 16 ‘setting baris grid
For M = 1 To 16 ‘setting kolom grid
grid.TextMatrix(l, M) = “”
Next M ‘naikkan kolom
Next l

End Sub

Private Sub mnusave_Click()
Dim Teks As String

”Teks = Teks & CStr(txtdata.Text)
”CommonDialog1.Filter = “*.hex|*.hex”
”CommonDialog1.CancelError = True
”On Error GoTo kensel:
” CommonDialog1.FileName = namafile.Text
” Open CommonDialog1.FileName For Output As #1
” Print #1, Teks
‘Close #1

Dim text_baca As String
Dim DATA, al_akhir, Al_awal, st, jenis, cs, count, jum_dat As Byte
Dim id, st1, st2 As String

For l = 1 To txtbytesize.Text / 16 ‘setting baris grid
count = 0
n_data = 0
jumlah = 0
DATA = “”
Al_awal = 0

For M = 1 To 16 ‘setting kolom grid
text_baca = (buffer1((M – 1) + ((l – 1) * 16)))

If grid.TextMatrix(l, M) = “FF” Then
count = count
Else
count = count + 1
End If

If count = 1 And grid.TextMatrix(l, M) <> “FF” Then ‘deteksi data 1 baris yang bukan “FF” yang akan
Al_awal = (M – 1) + ((l – 1) * 16) ‘digunakan sebagai data untuk alamat awal
Else
Al_awal = Al_awal
End If

If count < 1 Then
n_data = n_data
DATA = DATA
jumlah = jumlah
jenis = “”
id = “”
Else ‘jika alamat awal sudah terdeteksi, data setelah alamat awal termasuk data untuk file hex
n_data = n_data + 1 ‘pencacah data
DATA = DATA & (grid.TextMatrix(l, M))
jumlah = jumlah + HexToByte2(grid.TextMatrix(l, M)) ‘jumlah data untuk mempermidah cheksum
jenis = “00″
id = “:”
End If

Next M ‘naikkan kolom

jum_dat = (n_data) ‘jumlah data diubah ke bentuk hexa 2 digit
If jum_dat > 15 Then
y = Hex(jum_dat)
Else
y = “0″ & Hex(jum_dat)
End If

If Al_awal > 4095 Then ‘alamat awal diubah ke bilangan hexa 4 digit
z = Hex(Al_awal)
ElseIf Al_awal > 255 Then
z = “0″ & Hex(Al_awal)
ElseIf Al_awal > 15 Then
z = “00″ & Hex(Al_awal)
Else
z = “000″ & Hex(Al_awal)
End If

If Al_awal > 255 Then ‘untuk mempermudah ceksum yaitu alamat awal di jumlahkan 2 digit 2 digit
x = Al_awal – 255
Else
x = Al_awal
End If

cs = Right(Hex(256 – (jum_dat + x + jumlah)), 2) ‘cheksum = 2 digit terakhir hexa dari (256-(cacah data+al_awal+ jumlah data))

st = id & y & z & jenis & DATA & cs & Chr(13) & Chr$(10) ‘format intel hex untuk data tiap baris

If st = 0 Then
st = “”
Else
st = st
End If
temp = temp & st

Next l

txtdata.Text = temp & “:00000001FF” ‘bagian akhir baris dari intel hex file

StatusBar1.SimpleText = “membaca selesai”

txtepromfile = 0
txtisi = “isi file”

MsgBox “ok”, , “selesai”
Command18.Enabled = True
End Sub

Private Sub mnuser_Click()
txtdata.Alignment = 0
txtdata.Text = “cara pakai :” & Chr(13) & Chr$(10) _
& Chr(13) & Chr$(10) _
& “1. Pastikan EEPROM telah terpasang dengan benar ” & Chr(13) & Chr$(10) _
& “2. aktifkan I2C BUS dengan klik I2C ON” & Chr(13) & Chr$(10) _
& “3. pilih ukuran EEPROM yang akan digunakan melalui menu EEPROM SIZE” & Chr(13) & Chr$(10) _
& “4. buka file (.hex(intel hex file)) yang akan ada copy-kan ke EEPROM dengan tombol OPEN” & Chr(13) & Chr$(10) _
& “5. untuk mengisi EEPROM gunakan tombol WRITE” & Chr(13) & Chr$(10) _
& “6. untuk membaca EEPROM gunakan tombol READ” & Chr(13) & Chr$(10) _
& “7. bandingkan isi EEPROM dengan file masternya dengan tombol COMPARE” & Chr(13) & Chr$(10) _
& “8. simpan hasil pembacaan EEPROM dengan tombol SAVE (otomatis akan tersimpan dalam hex intel file format) ” & Chr(13) & Chr$(10) _
& Chr(13) & Chr$(10) _
& “10. berdoalah sebelum mencoba” _
& Chr(13) & Chr$(10)
End Sub

Private Sub mnwrite_Click()

End Sub

Private Sub mnverify_Click()
Label4.Visible = False
Dim I, J, l As Integer
Dim compare As String
On Error GoTo keluar
‘Baca_File_Hex ‘ ubah source file ke byte dan simpan di buffer1
Call readrandom(0, val(txtbytesize))

For I = 1 To txtbytesize.Text / 16
For J = 1 To 16
buffer1((J – 1) + ((I – 1) * 16)) = HexToByte2(grid.TextMatrix(I, J)) ‘simpan data grid yang tertampil ke buffer3
Combo3.AddItem buffer3((J – 1) + ((I – 1) * 16))
Next J
Next I

compare = “”

For l = 0 To txtbytesize.Text – 1

If buffer1(l) <> buffer2(l) Then

If Len(Hex(l)) = 1 Then
compare = compare & “000″ & Hex(l) & ” | ” & Hex(Module2.buffer1(l)) & ” | ” & Hex(Module2.buffer2(l)) _
& Chr(13) & Chr$(10)
ElseIf Len(Hex(l)) = 2 Then
compare = compare & “00″ & Hex(l) & ” | ” & Hex(Module2.buffer1(l)) & ” | ” & Hex(Module2.buffer2(l)) _
& Chr(13) & Chr$(10)
ElseIf Len(Hex(l)) = 3 Then
compare = compare & “0″ & Hex(l) & ” | ” & Hex(Module2.buffer1(l)) & ” | ” & Hex(Module2.buffer2(l)) _
& Chr(13) & Chr$(10)
Else
compare = compare & Hex(l) & ” | ” & Hex(Module2.buffer1(l)) & ” | ” & Hex(Module2.buffer2(l)) _
& Chr(13) & Chr$(10)
End If

End If
DoEvents
Next l

If compare = “” Then
txtdata.Text = txtdata.Text
MsgBox “tak ada perbedaan”
Else ‘tampilkan data yang berbeda
txtdata.Text = “Daftar perbedaan data:” & CommonDialog1.FileTitle & Chr(13) & Chr$(10) _
& ” ___________________________ ” & Chr(13) & Chr$(10) _
& “|Alamat |File*.HEX | EEPROM |” & Chr(13) & Chr$(10) _
& ” ——————————- ” & Chr(13) & Chr$(10) _
& compare
End If

MsgBox “SELESAI COMPARE”
Exit Sub
keluar:
MsgBox “hmmm…”
End Sub

Private Sub mnwriteall_Click()
bar1.Visible = True
If txtbytesize.Text = “” Or txtdata = “” Then
Exit Sub
End If

Dim count, p, reg, n As Integer
Dim writecontrolbyte As Byte
Dim I, J, k, a As Integer

‘clear kan isi buffer2
”For i = 0 To txtbytesize.Text
”buffer1(i) = &HFF ‘aslinya bufer1 | buffer diisi dengan data FFh, sebagai default data EEPROM siap diisi
”Next i

For I = 1 To val(txtbytesize.Text) / 16
For J = 1 To 16
buffer2((J – 1) + ((I – 1) * 16)) = HexToByte2(grid.TextMatrix(I, J)) ‘aslinya buffer1 buffer mengambil data dari grid sebagai input
Next J
Next I

For k = 0 To txtbytesize – 1
Combo2.AddItem buffer1(I)
Next k

Label4.Caption = “…wait for a moment…”
Label5.Caption = ” “
tunda 1000

writecontrolbyte = writecontrolbyte

count = 0
p = 0
reg = 0
‘———————————————

For n = 1 To txtnumber.Text ‘nxIC EEPROM yang akan di tulis

For J = 0 To ((txtbytesize.Text / 8 ) – 1) ‘jumlah page memori IC

Label4.Caption = ” …processing data,please wait…”
‘————————————————–
‘inisialisasi 1 byte device address untuk operasi page write data ke EEPROM hingga alamat 2048
If count > 1791 Then
writecontrolbyte = &HAE
ElseIf count > 1535 Then
writecontrolbyte = &HAC
ElseIf count > 1279 Then
writecontrolbyte = &HAA
ElseIf count > 1023 Then
writecontrolbyte = &HA8
ElseIf count > 767 Then
writecontrolbyte = &HA6
ElseIf count > 511 Then
writecontrolbyte = &HA4
ElseIf count > 255 Then
writecontrolbyte = &HA2
Else
writecontrolbyte = &HA0
End If

i2cstart ‘START -> perubahan sda dari haigh ke low saat scl high
i2csendbyte (writecontrolbyte) ‘kirim 1 byte device address

‘—————————————–
‘inisialisasi word address
If J > &HFF Then
reg = (J – 256) * 8

ElseIf J > 223 Then
reg = (J – 224) * 8

ElseIf J > 191 Then
reg = (J – 192) * 8

ElseIf J > 159 Then
reg = (J – 160) * 8

ElseIf J > 127 Then
reg = (J – 128 ) * 8

ElseIf J > 95 Then
reg = (J – 96) * 8

ElseIf J > 63 Then
reg = (J – 64) * 8

ElseIf J > 31 Then
reg = (J – 32) * 8

Else
reg = J * 8 ’0,8,16,32
End If

i2csendbyte (reg) ‘kirim word address
For k = 0 To 7
bar1.Max = 7
bar1.value = k
p = k + (8 * J)
i2csendbyte (buffer2(p)) ‘kirim data per byte dari buffer1 dirubah by dodi
count = count + 1
Next k
i2cstop
tunda 0.5001
Next J

‘inisialisasi 1 byte device address untuk pengalamatan eksternal beberapa IC
If txtbytesize.Text = 2048 Then ’1 IC 16 Kb
writecontrolbyte = writecontrolbyte
ElseIf txtbytesize.Text = 1024 Then ‘ IC 8 Kb ke 2
writecontrolbyte = writecontrolbyte + 8
ElseIf txtbytesize.Text = 512 Then ‘IC 4 Kb ke 2,3,4
writecontrolbyte = writecontrolbyte + 4
ElseIf txtbytesize.Text <= 256 Then ‘IC 1K dan 2K ke 2,3,4…8
writecontrolbyte = writecontrolbyte + 2
End If

Next n

bar1.value = 0

bar1.Visible = True
MsgBox “selesai”
End Sub

Private Sub Opoff_Click()

mneeprom.Enabled = False
End Sub

Private Sub Opon_Click()
Call releaseSDA
Call releaseSCL
txtbytesize.Enabled = True
mneeprom.Enabled = True

End Sub

Private Sub TXTALAMAT_Click()

End Sub

Private Sub txtclear_Click()
txtalamatedit = “”
txtdataedit = “”
End Sub

‘===============================================

tambahkan file modul di project vb6 isinys sbb:

tambahkan 2 module

module1 :

Option Explicit
Public Declare Sub tunda Lib “paralel_IO.dll” (ByVal lama As Long)
Public Declare Function Geser_Kiri Lib “paralel_IO.dll” (ByVal DATA As Integer, ByVal kali As Integer) As Integer
Public Declare Function Geser_Kanan Lib “paralel_IO.dll” (ByVal DATA As Integer, ByVal kali As Integer) As Integer
Public Declare Function Inp Lib “inpout32.dll” _
Alias “Inp32″ (ByVal PortAddress As Integer) As Integer
Public Declare Sub Out Lib “inpout32.dll” _
Alias “Out32″ (ByVal PortAddress As Integer, ByVal value As Integer)
Public control_byte As Byte
‘d0 = sda d1 = scl

Public writecontrolbyte As Byte
Public readcontrolbyte As Byte

‘setup LPT1 status
Public Sub Setup_output() ‘bit 5 alamat control = 0
control_byte = control_byte And &HDF
End Sub
Public Sub Setup_input() ‘bit 5 alamat control = 1
control_byte = control_byte Or &H20
End Sub
Public Sub pulldownSDA()
Out &H378, (Inp(&H378) Or &H1) ‘Out &H378, &H0
End Sub
‘pembangkit pulsa sda
Public Static Function releaseSDA() ‘bit 7 alamat data high
Out &H378, (Inp(&H378) And &HFE) ‘Out &H378, &H80
End Function
Public Static Function pulldownSCL() ‘bit 2 alamat control low
Out &H378, (Inp(&H378) Or &H2) ‘Out &H37A, (control_byte And &HFB)
End Function
‘pembangkit pulsa scl
Public Static Function releaseSCL() ‘bit 2 alamat control High
Out &H378, (Inp(&H378) And &HFD) ‘Out &H37A, (control_byte Or &H4)
End Function
Public Static Function readSDA() As Integer
readSDA = Geser_Kanan((Inp(&H379) And &H8), 3) And &H1
End Function

Public Static Function readSCL()
readSCL = Geser_Kanan((Inp(&H379) And &H10), 4) And &H1
End Function
Public Sub i2cstart()
releaseSCL ‘—-
releaseSDA ‘—-
pulldownSDA ‘—-____
pulldownSCL ‘——–____
End Sub
‘stop
Public Sub i2cstop()
pulldownSDA ‘____
releaseSCL ‘____—-
releaseSDA ‘________—-
‘tunda 0.75
End Sub
‘kirim data
Public Function i2csendbyte(ByVal DATA As Byte) As Byte
Dim I, tmp, tmp2 As Byte
For I = 0 To 7
tmp = Geser_Kiri(DATA, I)
tmp = tmp And &H80
If tmp = 128 Then
releaseSDA
Else
pulldownSDA
End If
releaseSCL
pulldownSCL
Next I
‘baca ack
releaseSDA
releaseSCL
tunda 0.05
tmp2 = readSDA
pulldownSCL
Setup_output

i2csendbyte = tmp2
End Function
Public Function i2creadbyte(ackcontrol As Integer) As Byte
Dim I, val, ack As Byte
releaseSDA
Setup_input ‘ LPT1 mode baca

For I = 7 To 0 Step -1
releaseSCL
val = val Or Geser_Kiri(readSDA, I) ‘data xxxx xxx1
pulldownSCL
Next I

Select Case (ackcontrol)
Case 0 ‘ ack
pulldownSDA
releaseSCL
pulldownSCL
releaseSDA

Case 1 ‘nack
releaseSDA
releaseSCL
pulldownSCL
pulldownSDA
Case 2 ‘ expect_ack
releaseSCL
ack = readSDA()
pulldownSCL

If ack <> 0 Then
MsgBox “ada kesalahan baca”
End
End If

Case Else
i2cstop

End Select

i2creadbyte = val ‘return

Setup_output ‘kembalikan lagi LPT1 ke mode tulis
End Function

Public Function writebyte(ByVal address As Integer, ByVal value As Byte)
Dim I, controlbyte As Integer
i2cstart

controlbyte = writecontrolbyte Or (Geser_Kanan((address And &H700), 7))
If (i2csendbyte(controlbyte)) <> 0 Then
i2cstop
MsgBox “ada kesalahan sewaktu menulis 1 modul”
End
End If

If (i2csendbyte(address And &HFF)) <> 0 Then
i2cstop
MsgBox “ada kesalahan sewaktu menulis2 modul”
End
End If

If (i2csendbyte(value)) <> 0 Then
i2cstop
MsgBox “ada kesalahan sewaktu menulis 3modul”
End
End If

i2cstop
tunda 0.05

‘====tunggu Twr===== jika respon 0 eeprom sedang sibuk (internal operation)
For I = 1 To 100
i2cstart
If (i2csendbyte(controlbyte)) = 0 Then
break
End If
Loop

End Function

Public Static Function readrandom(ByVal address As Integer, ByVal leng As Integer) As Integer
Dim I, l, DATAX, controlbyte As Integer

i2cstart
controlbyte = writecontrolbyte Or (Geser_Kanan((address And &H700), 7))
If (i2csendbyte(controlbyte)) <> 0 Then
i2cstop
MsgBox “ada kesalahan sewaktu menulis 1″
End
End If

If (i2csendbyte(address And &HFF)) <> 0 Then
i2cstop
MsgBox address, , “ada kesalahan sewaktu menulisalamat 2″
End
End If

‘issue read
i2cstart
controlbyte = readcontrolbyte Or (Geser_Kanan((address And &H700), 7))

If (i2csendbyte(controlbyte)) <> 0 Then
i2cstop
MsgBox “ada kesalahan sewaktu menulis 3″
End
End If

For I = address To leng – 1
If I < leng – 1 Then ‘

tunda 0.05
DATAX = i2creadbyte(0) ‘READ BYTE ACK
tunda 0.1
ElseIf I = leng – 1 Then
DATAX = i2creadbyte(1) ‘READ BYTE NACK ALAMAT TERAKHIR
End If

buffer2(I) = DATAX
Next I
i2cstop

Form1.bar1.value = 0
End Function

module2 :

Option Explicit
Public buffer1(5000) As Byte ‘isi file hex
Public buffer2(5000) As Byte ‘isi eprom
Public buffer3(5000) As Byte
Public jumlah_data As String
‘——————————————
Public Function HexToByte(Hex As String) As Byte
Select Case Hex
Case “0″
HexToByte = 0
Case “1″
HexToByte = 1
Case “2″
HexToByte = 2
Case “3″
HexToByte = 3
Case “4″
HexToByte = 4
Case “5″
HexToByte = 5
Case “6″
HexToByte = 6
Case “7″
HexToByte = 7
Case “8″
HexToByte = 8
Case “9″
HexToByte = 9
Case “A”
HexToByte = 10
Case “B”
HexToByte = 11
Case “C”
HexToByte = 12
Case “D”
HexToByte = 13
Case “E”
HexToByte = 14
Case “F”
HexToByte = 15
Case Else
HexToByte = 0
End Select

End Function

Public Function HexToByte2(Hex As String) As Byte
HexToByte2 = (16 * HexToByte(Mid(Hex, 1, 1))) + (HexToByte(Mid(Hex, 2, 1)))
End Function

Public Function HexToByte4(Hex As String) As Integer
HexToByte4 = (256 * (HexToByte2(Mid(Hex, 1, 2)))) + (HexToByte2(Mid(Hex, 3, 2)))
End Function

‘——————————————-
Public Sub Baca_File_Hex()

Dim line_text As String
Dim Jum_Data, Al_awal, jenis, I As Integer
On Error GoTo keluar

If Form1.txtbytesize.Text = “” Then
MsgBox “pilih ukuran EEPROM nya”
Exit Sub
End If

For I = 0 To 2047
buffer1(I) = &HFF ‘ buffer isi file
Next I

Form1.CommonDialog1.CancelError = True
Form1.CommonDialog1.Filter = “text file (*.hex)|*.hex”
Form1.CommonDialog1.ShowOpen
If Form1.CommonDialog1.FileName <> “” Then
Open Form1.CommonDialog1.FileName For Input As #1
Form1.Refresh
jumlah_data = “ok”
Do Until EOF(1)
Line Input #1, line_text
Jum_Data = HexToByte2(Mid(line_text, 2, 2))
Al_awal = HexToByte4(Mid(line_text, 4, 4))
jenis = HexToByte2(Mid(line_text, 8, 2))

If ((Jum_Data <> 0) And (jenis = 0)) Then
For I = 0 To (Jum_Data – 1)
buffer1(Al_awal + I) = HexToByte2(Mid(line_text, 10 + (I * 2), 2)) ‘aslinya buffer1
Next I

End If
Loop
Close #1
End If
Exit Sub
keluar:
MsgBox “ada error jangan tanya kenapa”

End Sub

Advertisements

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis untuk pemula.

Posted on 19/04/2011, in Contoh-contoh Aplikasi. Bookmark the permalink. 5 Comments.

  1. mas, bole minta vb nya?
    saya lagi belajar buat it dan blm berhasil..

  2. tolong minta skema ya yg lebih gampang

  3. Keren banget mas, ada yg mo share soskod, ditunggu yg versi USB,
    Salam & succes slalu

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: