Uygulama-1: Dijital Saat Yapımı


Dijital Saat Yapımı

Bu uygulamamızda 7-segment displaylerden oluşan bir dijital saat yapmaya çalışacağız. Uygulamamız tamamen dijital elektronikteki mantığın aslında basit bir simulasyonu gibidir. Mantık sırası itibari ile sistem saatini okuyacağız, bunu String veriye çevireceğiz (tamamen size kalmış) sonrasında her bir dijitini (basamağını) tek tek ele alarak 7-segment displaylerimize (ki burada Sayfa1 deki bazı hücerelere karşılık gelmektedir) gerekli lojik 1 sinyallerini göndererek sayıların görüntülenmesini sağlayacağız. Hepsi bu kadar. Bundan birkaç yıl önce tam hatırlamamakla beraber son sınıf arkadaşlara Paralel portun data pinlerine bağlı bir 7-segment displayi 0-9 arasında aşağı-yukarı sayıcı olarak programlamıştık. (Port erişimi teferruatlı bir konu onu daha sonra bir ara ele alırız. Sonuçta ofis uygulmasından portlara erişmek ne kadar işe yarar? Tartışılır.)

1) Öncelikle bir Excel dokümanı oluşturup kaydedin. Aşağıda verilen hücre biçimlendirmelerini yapın, sonrasında Araçlar>Makro>Güvenlik komutları ile ekrana gelen pencerede Makro güvenliği sekmesinde Düşük seçemeğini işaretleyerek Tamam düğmesi ile onaylayın.Kaydet düğmesine basıp dokümanı kapatıp yeniden açın. Şekil-1 de verildiği üzere varsayılan olarak Sayfa1, üzerinde gerekli hücre birleştirme işlemlerini yapın.
Şekil-1
2.Sonrasında Görünüm > Araç çubukları ile ekrana gelen listeden Visual Basic araç çubuğunu işaretleyin. Bu araç çubuğunda tasarım modu (Şekil-2 / 3 numara) düğmesine basın.
Şekil-2
3) Yine Visual Basic araç çubuğunda yer alan Denetim Araç Kutusu düğmesine (Şekil-2 / 2 numara) basın ve kullanabileceğiz ocx nesnelerini görüntüleyin. Bunların arasından Komut düğmesi nesnesini tıklayın ve sonrasında farenin sol tuşunu basılı tutarak çalışma sayfası üzerinde sürükle bırak yönetimi ile makul büyüklükte 3 adet düğme çizin. Sonrasında bu düğmelerin üzerinde farenin sağ tuşuna basın ekrana gelen pop-up mönüden Özellikler/Properties tıklayın ve sırasıyla bu düğmelerin Caption özelliklerini BAŞLAT, KAYDET ÇIK ve ÇIK yapın. Sonrasında da bu düğmelerin name özelliklerini sırasıyla btnBaslatDurdur, btnKaydetCik ve btnCik yapın.

4) Veri giriş işlemlerini tamamladıktan sonra Klavyeden ALT+F11 tuşlarına basarak VBA kod editörünü açıyoruz. Burada (şayet ekrana gözükmüyorsa) Project Explorer penceresini ekrana getirmek için CTRL+L tuşlarına basıyoruz. Sonrasında Sayfa1 i çift tıklıyoruz ekrana gelen kod penceresine aşağıda verilen kodları yazıyoruz


Dim saat, dakika, saniye, zaman As String
Dim satNum(1 To 7) As Integer, sutNum(1 To 7) As Integer
Private Sub dijitleriGoruntule(deger As Integer, colorNumber As Byte, defColor As Byte, whichDigit As Byte)
'************************************************************************************************
'Coded By               : Bilal SERT
'Creation Time / Date   :
'Aim                    :
'Possible I/O           :
'Calls                  :
'Called By              :
'************************************************************************************************
Select Case whichDigit
    Case 1:
        satNum(1) = 6: sutNum(1) = 2
        satNum(2) = 7: sutNum(2) = 5
        satNum(3) = 12: sutNum(3) = 5
        satNum(4) = 16: sutNum(4) = 2
        satNum(5) = 12: sutNum(5) = 2
        satNum(6) = 7: sutNum(6) = 2
        satNum(7) = 11: sutNum(7) = 2
    Case 2:
        satNum(1) = 6: sutNum(1) = 7
        satNum(2) = 7: sutNum(2) = 10
        satNum(3) = 12: sutNum(3) = 10
        satNum(4) = 16: sutNum(4) = 7
        satNum(5) = 12: sutNum(5) = 7
        satNum(6) = 7: sutNum(6) = 7
        satNum(7) = 11: sutNum(7) = 7
    Case 3:
        satNum(1) = 6: sutNum(1) = 14
        satNum(2) = 7: sutNum(2) = 17
        satNum(3) = 12: sutNum(3) = 17
        satNum(4) = 16: sutNum(4) = 14
        satNum(5) = 12: sutNum(5) = 14
        satNum(6) = 7: sutNum(6) = 14
        satNum(7) = 11: sutNum(7) = 14
    Case 4:
        satNum(1) = 6: sutNum(1) = 19
        satNum(2) = 7: sutNum(2) = 22
        satNum(3) = 12: sutNum(3) = 22
        satNum(4) = 16: sutNum(4) = 19
        satNum(5) = 12: sutNum(5) = 19
        satNum(6) = 7: sutNum(6) = 19
        satNum(7) = 11: sutNum(7) = 19
    
    Case 5:
        satNum(1) = 6: sutNum(1) = 26
        satNum(2) = 7: sutNum(2) = 29
        satNum(3) = 12: sutNum(3) = 29
        satNum(4) = 16: sutNum(4) = 26
        satNum(5) = 12: sutNum(5) = 26
        satNum(6) = 7: sutNum(6) = 26
        satNum(7) = 11: sutNum(7) = 26
    Case 6:
        satNum(1) = 6: sutNum(1) = 31
        satNum(2) = 7: sutNum(2) = 34
        satNum(3) = 12: sutNum(3) = 34
        satNum(4) = 16: sutNum(4) = 31
        satNum(5) = 12: sutNum(5) = 31
        satNum(6) = 7: sutNum(6) = 31
        satNum(7) = 11: sutNum(7) = 31
    Case Else
End Select

With Sayfa1
    Select Case deger
        Case 0:
            .Cells(satNum(1), sutNum(1)).Interior.ColorIndex = colorNumber  'd0-A
            .Cells(satNum(2), sutNum(2)).Interior.ColorIndex = colorNumber 'd1-B
            .Cells(satNum(3), sutNum(3)).Interior.ColorIndex = colorNumber 'd2-C
            .Cells(satNum(4), sutNum(4)).Interior.ColorIndex = colorNumber 'd3-D
            .Cells(satNum(5), sutNum(5)).Interior.ColorIndex = colorNumber 'd4-E
            .Cells(satNum(6), sutNum(6)).Interior.ColorIndex = colorNumber 'd5-F
            .Cells(satNum(7), sutNum(7)).Interior.ColorIndex = defColor    'd6-G
        Case 1:
            .Cells(satNum(1), sutNum(1)).Interior.ColorIndex = defColor  'd0-A
            .Cells(satNum(2), sutNum(2)).Interior.ColorIndex = colorNumber 'd1-B
            .Cells(satNum(3), sutNum(3)).Interior.ColorIndex = colorNumber 'd2-C
            .Cells(satNum(4), sutNum(4)).Interior.ColorIndex = defColor 'd3-D
            .Cells(satNum(5), sutNum(5)).Interior.ColorIndex = defColor 'd4-E
            .Cells(satNum(6), sutNum(6)).Interior.ColorIndex = defColor 'd5-F
            .Cells(satNum(7), sutNum(7)).Interior.ColorIndex = defColor    'd6-G
        Case 2:
            .Cells(satNum(1), sutNum(1)).Interior.ColorIndex = colorNumber  'd0-A
            .Cells(satNum(2), sutNum(2)).Interior.ColorIndex = colorNumber 'd1-B
            .Cells(satNum(3), sutNum(3)).Interior.ColorIndex = defColor 'd2-C
            .Cells(satNum(4), sutNum(4)).Interior.ColorIndex = colorNumber 'd3-D
            .Cells(satNum(5), sutNum(5)).Interior.ColorIndex = colorNumber 'd4-E
            .Cells(satNum(6), sutNum(6)).Interior.ColorIndex = defColor 'd5-F
            .Cells(satNum(7), sutNum(7)).Interior.ColorIndex = colorNumber    'd6-G
        Case 3:
            .Cells(satNum(1), sutNum(1)).Interior.ColorIndex = colorNumber  'd0-A
            .Cells(satNum(2), sutNum(2)).Interior.ColorIndex = colorNumber 'd1-B
            .Cells(satNum(3), sutNum(3)).Interior.ColorIndex = colorNumber 'd2-C
            .Cells(satNum(4), sutNum(4)).Interior.ColorIndex = colorNumber 'd3-D
            .Cells(satNum(5), sutNum(5)).Interior.ColorIndex = defColor 'd4-E
            .Cells(satNum(6), sutNum(6)).Interior.ColorIndex = defColor 'd5-F
            .Cells(satNum(7), sutNum(7)).Interior.ColorIndex = colorNumber    'd6-G
        Case 4:
            .Cells(satNum(1), sutNum(1)).Interior.ColorIndex = defColor  'd0-A
            .Cells(satNum(2), sutNum(2)).Interior.ColorIndex = colorNumber 'd1-B
            .Cells(satNum(3), sutNum(3)).Interior.ColorIndex = colorNumber 'd2-C
            .Cells(satNum(4), sutNum(4)).Interior.ColorIndex = defColor 'd3-D
            .Cells(satNum(5), sutNum(5)).Interior.ColorIndex = defColor 'd4-E
            .Cells(satNum(6), sutNum(6)).Interior.ColorIndex = colorNumber 'd5-F
            .Cells(satNum(7), sutNum(7)).Interior.ColorIndex = colorNumber    'd6-G
        Case 5:
            .Cells(satNum(1), sutNum(1)).Interior.ColorIndex = colorNumber  'd0-A
            .Cells(satNum(2), sutNum(2)).Interior.ColorIndex = defColor 'd1-B
            .Cells(satNum(3), sutNum(3)).Interior.ColorIndex = colorNumber 'd2-C
            .Cells(satNum(4), sutNum(4)).Interior.ColorIndex = colorNumber 'd3-D
            .Cells(satNum(5), sutNum(5)).Interior.ColorIndex = defColor 'd4-E
            .Cells(satNum(6), sutNum(6)).Interior.ColorIndex = colorNumber 'd5-F
            .Cells(satNum(7), sutNum(7)).Interior.ColorIndex = colorNumber    'd6-G

        Case 6:
            .Cells(satNum(1), sutNum(1)).Interior.ColorIndex = colorNumber  'd0-A
            .Cells(satNum(2), sutNum(2)).Interior.ColorIndex = defColor 'd1-B
            .Cells(satNum(3), sutNum(3)).Interior.ColorIndex = colorNumber 'd2-C
            .Cells(satNum(4), sutNum(4)).Interior.ColorIndex = colorNumber 'd3-D
            .Cells(satNum(5), sutNum(5)).Interior.ColorIndex = colorNumber 'd4-E
            .Cells(satNum(6), sutNum(6)).Interior.ColorIndex = colorNumber 'd5-F
            .Cells(satNum(7), sutNum(7)).Interior.ColorIndex = colorNumber    'd6-G
        Case 7:
            .Cells(satNum(1), sutNum(1)).Interior.ColorIndex = colorNumber  'd0-A
            .Cells(satNum(2), sutNum(2)).Interior.ColorIndex = colorNumber 'd1-B
            .Cells(satNum(3), sutNum(3)).Interior.ColorIndex = colorNumber 'd2-C
            .Cells(satNum(4), sutNum(4)).Interior.ColorIndex = defColor 'd3-D
            .Cells(satNum(5), sutNum(5)).Interior.ColorIndex = defColor 'd4-E
            .Cells(satNum(6), sutNum(6)).Interior.ColorIndex = defColor 'd5-F
            .Cells(satNum(7), sutNum(7)).Interior.ColorIndex = defColor    'd6-G
        Case 8:
            .Cells(satNum(1), sutNum(1)).Interior.ColorIndex = colorNumber  'd0-A
            .Cells(satNum(2), sutNum(2)).Interior.ColorIndex = colorNumber 'd1-B
            .Cells(satNum(3), sutNum(3)).Interior.ColorIndex = colorNumber 'd2-C
            .Cells(satNum(4), sutNum(4)).Interior.ColorIndex = colorNumber 'd3-D
            .Cells(satNum(5), sutNum(5)).Interior.ColorIndex = colorNumber 'd4-E
            .Cells(satNum(6), sutNum(6)).Interior.ColorIndex = colorNumber 'd5-F
            .Cells(satNum(7), sutNum(7)).Interior.ColorIndex = colorNumber    'd6-G
        Case 9:
            .Cells(satNum(1), sutNum(1)).Interior.ColorIndex = colorNumber  'd0-A
            .Cells(satNum(2), sutNum(2)).Interior.ColorIndex = colorNumber 'd1-B
            .Cells(satNum(3), sutNum(3)).Interior.ColorIndex = colorNumber 'd2-C
            .Cells(satNum(4), sutNum(4)).Interior.ColorIndex = colorNumber 'd3-D
            .Cells(satNum(5), sutNum(5)).Interior.ColorIndex = defColor 'd4-E
            .Cells(satNum(6), sutNum(6)).Interior.ColorIndex = colorNumber 'd5-F
            .Cells(satNum(7), sutNum(7)).Interior.ColorIndex = colorNumber    'd6-G
    End Select
End With
End Sub
Private Sub btnBaslatDurdur_Click()
    If btnBaslatDurdur.Caption = "BAŞLAT" Then
        btnBaslatDurdur.Caption = "DURDUR"
    Else
        btnBaslatDurdur.Caption = "BAŞLAT"
    End If
    Do
        bekle = Timer
        Do
            DoEvents
        Loop Until Timer > bekle + 1
        zaman = Format(Time, "hh:mm:ss")
        Sayfa1.Cells(2, 2) = zaman
        saat = Left(zaman, 2)
        dakika = Mid(zaman, 4, 2)
        saniye = Right(zaman, 2)
        'saat ayristiriliyor
        Sayfa1.Cells(4, 2) = Left(saat, 1)
        Sayfa1.Cells(4, 7) = Right(saat, 1)
        'dakika ayristiriliyor
        Sayfa1.Cells(4, 14) = Left(dakika, 1)
        Sayfa1.Cells(4, 19) = Right(dakika, 1)
        'saniye ayristiriliyor
        Sayfa1.Cells(4, 26) = Left(saniye, 1)
        Sayfa1.Cells(4, 31) = Right(saniye, 1)
        
        dijitleriGoruntule Val(Right(saniye, 1)), 43, 0, 6
        dijitleriGoruntule Val(Left(saniye, 1)), 43, 0, 5
        
        dijitleriGoruntule Val(Right(dakika, 1)), 43, 0, 4
        dijitleriGoruntule Val(Left(dakika, 1)), 43, 0, 3
        
        dijitleriGoruntule Val(Right(saat, 1)), 43, 0, 2
        dijitleriGoruntule Val(Left(saat, 1)), 43, 0, 1
        
    Loop Until btnBaslatDurdur.Caption = "BAŞLAT"
End Sub
Private Sub btnExit_Click()
    Application.DisplayAlerts = False
    Application.Quit
End Sub
Private Sub btnSaveAndExit_Click()
    ThisWorkbook.Save
    Application.Quit
End Sub

Yukarıda verilen dijitleriGoruntule() alt programında yeşil renkte verilen açıklama satırlarını yazmayı alışkanlık haline getirseniz iyi olur. Çünkü yazdığınız kodların üzerinden belirli bir süre geçtikten sonra yeniden onları düzenlemek istediğinizde işler biraz karışabilir. Ancak burada yazacağınız açıklamalar ile (daha sonra basitçe sizlere anlatırım) kod-izleme haritaları yapabilirsiniz. Bu işlem sizi daha çabuk sonuca götürür. Eğer buraya aktaracak kadar sabredebilirsem göreceksiniz ki ofis altında da binlerce satır kod yazdığımız zamanlar oluyor...Evet işlem tamam artık yazdığınız kodları test edebilirsiniz. Ancak unutmayın yukarıda yaptıklarınızın çalışabilmesi için Makro Güvenlik düzeyinin düşük olması gerekmekte ya da Ofis 2007 ya da 2010 da kodların çalıştırılmasına izin vermeniz gerekmektedir. Bir tavsiye; önce hazırını kullanmayın yapmaya çalışın baktınız olmadı örnek uygulamayı indirip deneyebilirsiniz elbette...Güç sizinle olsun
Yardımcı olması dileğiyle