Gambar Rangkaian :
Kode Bascom AVR
1 $regfile = "m8def.dat" 2 $crystal = 1000000 3 '------------------------ lcd ----------------------------------------------- 4 Config Lcd = 16 * 2 5 Config Lcdpin = Pin , Rs = Pind.0 , E = Pind.1 , Db4 = Pind.2 , Db5 = Pind.3 , Db6 = Pind.4 , Db7 = Pind.5 6 Cls 7 8 '--------------------------- ds 1307 ----------------------------------------- 9 $lib "ds1307clock.lib" 10 'configure the scl and sda pins 11 Config Sda = Portd.7 12 Config Scl = Portd.6 13 'address of ds1307 14 Const Ds1307w = &HD0 ' Addresses of Ds1307 clock 15 Const Ds1307r = &HD1 16 '------------------------------ key -------------------------------------------- 17 Config Pinc.1 = Input 18 Config Pinc.2 = Input 19 Config Pinc.3 = Input 20 Config Pinc.4 = Input 21 '------------------------------------------------------------------------------ 22 Config Debounce = 30 23 24 Dim A As Byte , B As Byte , Data1 As Byte , C As Byte 25 26 Dim Seco As Byte , Mine As Byte , Hour As Byte 27 28 '---------------------------------------------------------------------------- 29 Cursor Off 30 '----------------------------- 31 Cls 32 Locate 1 , 1 33 Lcd "Jam Digital LCD" 34 Locate 2 , 1 35 Lcd "Bina Project" 36 Wait 2 37 38 For A = 1 To 15 39 Shiftlcd Left 40 Waitms 30 41 Next 42 43 Cls 44 '------------------------------------------------------------------------------- 45 Main: 46 Do 47 Gosub Ds1307 48 Gosub 24_12 49 Gosub Chekkey 50 51 Loop 52 '------------------------------------------------------------------------------- 53 Ds1307: 54 I2cstart ' Generate start code 55 I2cwbyte Ds1307w ' send address 56 I2cwbyte 0 ' start address in 1307 57 I2cstart ' Generate start code 58 I2cwbyte Ds1307r ' send address 59 I2crbyte Seco , Ack 'sec 60 I2crbyte Mine , Ack ' MINUTES 61 I2crbyte Hour , Nack ' Hours 62 I2cstop 63 64 Seco = Makedec(seco) : Mine = Makedec(mine) : Hour = Makedec(hour) 65 66 If Seco > 59 Then Seco = 0 67 If Mine > 59 Then Mine = 0 68 If Hour > 23 Then 69 Hour = 0 70 Gosub Seco 71 End If 72 73 Return 74 '------------------------------------------------------------------------------- 75 24_12: 76 If Pinc.4 = 1 Then Gosub Disply_24 77 If Pinc.4 = 0 Then Gosub Disply_12 78 Return 79 '------------------------------------------------------------------------------- 80 Disply_24: 81 82 Locate 1 , 1 83 Lcd "Jam = " ; Hour ; ":" ; 84 If Mine < 10 Then 85 Lcd "0" ; Mine ; ":" ; 86 Else 87 Lcd Mine ; ":" ; 88 End If 89 90 If Seco < 10 Then 91 Lcd "0" ; Seco 92 Else 93 Lcd Seco 94 End If 95 96 Locate 2 , 6 97 Lcd "(24 Jam)" 98 99 Return 100 101 '------------------------------------------------------------------------------- 102 Disply_12: 103 104 If Hour = 0 Then Hour = 12 105 If Hour > 12 Then Hour = Hour - 12 106 107 Locate 1 , 1 108 Lcd "Jam = " ; Hour ; ":" ; 109 If Mine < 10 Then 110 Lcd "0" ; Mine ; ":" ; 111 Else 112 Lcd Mine ; ":" ; 113 End If 114 115 If Seco < 10 Then 116 Lcd "0" ; Seco 117 Else 118 Lcd Seco 119 End If 120 121 Locate 2 , 6 122 Lcd "(12 Jam)" 123 Return 124 '------------------------------------------------------------------------------- 125 Chekkey: 126 127 Debounce Pinc.1 , 0 , Seco , Sub 128 Debounce Pinc.2 , 0 , Mine , Sub 129 Debounce Pinc.3 , 0 , Hour , Sub 130 131 132 Return 133 '--------------------------------------------- 134 Seco: 135 Incr Seco 136 If Seco > 59 Then Seco = 0 137 Seco = Makebcd(seco) 138 I2cstart ' Generate start code 139 I2cwbyte Ds1307w ' send address 140 I2cwbyte 0 ' starting address in 1307 141 I2cwbyte Seco 142 I2cstop 143 Return 144 145 '------------------------------------- 146 Mine: 147 Incr Mine 148 If Mine > 59 Then Mine = 0 149 Mine = Makebcd(mine) 150 I2cstart ' Generate start code 151 I2cwbyte Ds1307w ' send address 152 I2cwbyte 1 ' starting address in 1307 153 I2cwbyte Mine 154 I2cstop 155 Return