ds1307 ile saat garip değerler alıyor (atmega16)

Başlatan ilhan_mkp, 12 Şubat 2012, 09:49:37

ilhan_mkp

abiler atmega16 ile ds1307 entegresi ile bir uygulama yaptım.
saat tarih vs güzel çalışıyor.
sabah işe giderken saat 07:15 iken geldiğimde 64:10 gibi bişiler oluyor.
aynı durum tarih içinde geçerli.
bu saat olayı ne için sabah set ettiğim saatte lambayı yavaş yavaş yakıp akşamda aynı mantıkta söndürmek için. lamba 5w 24v brt13h(optotriak) ile sürüyorum.


$regfile = "m16def.dat"
$crystal = 8000000
$hwstack = 200
$swstack = 200
$framesize = 20

'kütüphaneler*******************************************************************

$include "font8x8.font"

$lib "ds1307clock.lib"

$lib "glcdKS108.lbx"

'glcd tanımlamaları*************************************************************


Config Graphlcd = 128 * 64sed , Dataport = Portb , Controlport = Porta , Ce = _
 3 , Ce2 = 4 , Cd = 2 , Rd = 1 , Reset = 5 , Enable = 0

'**int0 kesme ayarları**********************************************************

Config Int0 = Low Level                                     

On Int0 Int0_int

'**timer ayarları***************************************************************

Config Timer0 = Timer , Prescale = 256                     

On Timer0 Timer_0_int

Enable Interrupts

'I2C AYARLARI*******************************************************************

Config Sda = Portc.1
Config Scl = Portc.0

Const Ds1307w = &HD0
Const Ds1307r = &HD1

Config Clock = User

'I/O AYARLARI*******************************************************************

Config Portd = Input
Config Portc.6 = Output
Config Portc.7 = Output
Portd = 255

'semboller**********************************************************************

S Alias Pind.3
Eksi Alias Pind.4
Arti Alias Pind.5
Enter Alias Pind.6

Lamba Alias Portc.7
Fan Alias Portc.6

'DEĞİŞKENLER********************************************************************

Dim Tmr0_flag As Bit
Dim Sabahf As Bit
Dim Aksamf As Bit
Dim Ayarf As Bit
Dim Dateayarf As Bit
Dim Saatayarf As Bit
Dim Tarihayarf As Bit

Dim Idx As Byte
Dim Idx1 As Byte
Dim Idx2 As Byte
Dim X As Byte
Dim A As Byte
Dim Sabah As Byte
Dim Aksam As Byte
Dim Weekday As Byte
Dim Fanf As Byte

Dim Esabah As Eram Byte
Dim Eaksam As Eram Byte
Dim Efanf As Eram Byte

'EPROM OKUMA********************************************************************

Sabah = Esabah
Aksam = Eaksam
Fanf = Efanf

'*******************************************************************************

Setfont Font8x8

Cls

Lcdat 4 , 10 , "iLHAN_MKP"
Wait 3





Gosub Getdatetime

Cls

Wait 1

'ana döngü**********************************************************************

Basla:

Do

If A > 250 And Sabahf = 1 Then
   Disable Int0
   Disable Timer0
   A = 255
   Sabahf = 0
   Lamba = 1
End If



If A < 5 And Aksamf = 1 Then
   Disable Int0
   Disable Timer0
   A = 0
   Aksamf = 0
   Lamba = 0
End If



If Sabah = _hour And A = 0 Then
   Sabahf = 1
End If



If Aksam = _hour And A = 255 Then
   Aksamf = 1
End If



'*******************************************************************************


If Sabahf = 1 And A < 255 Then

   Enable Int0

   If _sec = 0 Then
      Incr A
      Wait 2
   End If

   If _sec = 15 Then
      Incr A
      Wait 2
   End If

   If _sec = 30 Then
      Incr A
      Wait 2
   End If

   If _sec = 45 Then
      Incr A
      Wait 2
   End If


End If

'-------------------------------------------------------------------------------

If Aksamf = 1 And A > 0 Then

   Enable Int0

   If _sec = 0 Then
      Decr A
      Wait 2
   End If

   If _sec = 15 Then
      Decr A
      Wait 2
   End If

   If _sec = 30 Then
      Decr A
      Wait 2
   End If

   If _sec = 45 Then
      Decr A
      Wait 2
   End If

End If

'tarih saat ayarı***************************************************************

Z:

If Saatayarf = 1 Then
   Lcdat 1 , 20 , "SAAT AYAR"
   Lcdat 3 , 20 , "SAAT  " ; ": " ; _hour
   Lcdat 5 , 20 , "DAKiKA" ; ": " ; _min
   Lcdat 7 , 20 , "SANiYE" ; ": " ; _sec

   If S = 0 Then
      Incr Idx1
      If Idx1 > 2 Then Idx1 = 0
      Waitms 300
      Cls
   End If



   If Idx1 = 0 Then
      Lcdat 3 , 10 , ">"

      If Arti = 0 Then
         Incr _hour
         If _hour > 23 Then _hour = 0
         Waitms 300
         Cls
      End If

      If Eksi = 0 Then
         Decr _hour
         If _hour > 23 Then _hour = 23
         Waitms 300
         Cls
      End If
   End If



   If Idx1 = 1 Then
      Lcdat 5 , 10 , ">"

      If Arti = 0 Then
         Incr _min
         If _min > 59 Then _min = 0
         Waitms 300
         Cls
      End If

      If Eksi = 0 Then
         Decr _min
         If _min > 59 Then _min = 59
         Waitms 300
         Cls
      End If
    End If




   If Idx1 = 2 Then
      Lcdat 7 , 10 , ">"

      If Arti = 0 Then
         Incr _sec
         If _sec > 59 Then _sec = 0
         Waitms 300
         Cls
      End If

      If Eksi = 0 Then
         Decr _sec
         If _sec > 59 Then _sec = 59
         Waitms 300
         Cls
      End If
    End If

    If Enter = 0 Then
       Saatayarf = 0
       Gosub Settime
    Else
       Goto Z
    End If

End If

'-------------------------------------------------------------------------------

W:

If Tarihayarf = 1 Then
   Lcdat 1 , 20 , "TARiH AYAR"
   Lcdat 3 , 20 , "GUN  " ; ": " ; _day
   Lcdat 5 , 20 , "AY   " ; ": " ; _month
   Lcdat 7 , 20 , "YIL  " ; ": " ; _year

   If S = 0 Then
      Incr Idx2
      If Idx2 > 2 Then Idx2 = 0
         Waitms 300
         Cls
   End If



   If Idx2 = 0 Then
      Lcdat 3 , 10 , ">"

      If Arti = 0 Then
         Incr _day
         If _day > 31 Then _day = 1
         Waitms 300
         Cls
      End If

     If Eksi = 0 Then
        Decr _day
        If _day > 31 Then _day = 31
        If _day < 1 Then _day = 31
        Waitms 300
        Cls
     End If
   End If



   If Idx2 = 1 Then
      Lcdat 5 , 10 , ">"

      If Arti = 0 Then
         Incr _month
         If _month > 12 Then _month = 1
         Waitms 300
         Cls
      End If

      If Eksi = 0 Then
         Decr _month
         If _month > 12 Then _month = 12
         If _month < 1 Then _month = 12
         Waitms 300
         Cls
      End If
    End If



    If Idx2 = 2 Then
       Lcdat 7 , 10 , ">"

       If Arti = 0 Then
          Incr _year
          If _year > 99 Then _year = 0
          Waitms 300
          Cls
       End If

       If Eksi = 0 Then
          Decr _year
          If _year > 99 Then _year = 99
          Waitms 300
          Cls
       End If
     End If


     If Enter = 0 Then
        Tarihayarf = 0
        Gosub Setdate
     Else
        Goto W
     End If

End If

'sabah-akşam ayarı**************************************************************

If Ayarf = 1 Then

    Lcdat 1 , 20 , "AYAR"
    Lcdat 3 , 20 , "SABAH" ; ": " ; Sabah
    Lcdat 5 , 20 , "AKSAM" ; ": " ; Aksam
    Lcdat 7 , 20 , "FAN  " ; ": " ; Fanf



    If S = 0 Then                                           
       Incr Idx                                             
       If Idx > 2 Then Idx = 0                             
       Waitms 300                                           
       Cls                                                 
    End If                                                 



    If Idx = 0 Then                                        
       Lcdat 3 , 10 , ">"                                  

       If Arti = 0 Then                                     
          Incr Sabah                                        
          If Sabah > 23 Then Sabah = 0                     
          Waitms 300                                        
          Cls                                              
       End If                                               

       If Eksi = 0 Then                                     
          Decr Sabah                                        
          If Sabah > 23 Then Sabah = 23                     
          Waitms 300                                       
          Cls                                              
       End If                                              
     End If



     If Idx = 1 Then
        Lcdat 5 , 10 , ">"

        If Arti = 0 Then
           Incr Aksam
           If Aksam > 23 Then Aksam = 0
           Waitms 300
           Cls
        End If

        If Eksi = 0 Then
           Decr Aksam
           If Aksam > 23 Then Aksam = 23
           Waitms 300
           Cls
        End If
      End If



      If Idx = 2 Then
         Lcdat 7 , 10 , ">"

         If Arti = 0 Then
            Incr Fanf
            If Fanf > 1 Then Fanf = 0
            Waitms 300
            Cls
         End If

         If Eksi = 0 Then
            Decr Fanf
            If Fanf > 1 Then Fanf = 1
            Waitms 300
            Cls
         End If
       End If




       If Enter = 0 Then
          Ayarf = 0
          Cls
          Esabah = Sabah
          Eaksam = Aksam
          Efanf = Fanf
          Wait 1
       End If
Else


    Incr X



    If X <= 10 Then
       Gosub Saat_goster
    End If



    If X >= 17 Then X = 0


    If Enter = 0 And Arti = 0 Then                         
       Saatayarf = 1
       Cls
       Wait 1
    End If

    If Enter = 0 And Eksi = 0 Then                          
       Tarihayarf = 1
       Cls
       Wait 1
    End If

    If Enter = 0 Then                                       
       Ayarf = 1
       Waitms 300
       Cls
    End If

End If



Loop

'ı2c haberleşme*****************************************************************

Getdatetime:
  I2cstart                                                 
  I2cwbyte Ds1307w                                          
  I2cwbyte 0                                                

  I2cstart                                                  
  I2cwbyte Ds1307r                                         
  I2crbyte _sec , Ack
  I2crbyte _min , Ack                                      
  I2crbyte _hour , Ack                                     
  I2crbyte Weekday , Ack                                   
  I2crbyte _day , Ack                                      
  I2crbyte _month , Ack                                    
  I2crbyte _year , Nack                                    
  I2cstop

  _sec = Makedec(_sec) : _min = Makedec(_min) : _hour = Makedec(_hour)
  _day = Makedec(_day) : _month = Makedec(_month) : _year = Makedec(_year)

Return

'-------------------------------------------------------------------------------
Setdate:

_day = Makebcd(_day) : _month = Makebcd(_month) : _year = Makebcd(_year)

  I2cstart                                                  
  I2cwbyte Ds1307w                                         
  I2cwbyte 4                                                
  I2cwbyte _day                                             
  I2cwbyte _month                                          
  I2cwbyte _year                                           
  I2cstop

Return

'-------------------------------------------------------------------------------

Settime:

_sec = Makebcd(_sec) : _min = Makebcd(_min) : _hour = Makebcd(_hour)

  I2cstart                                                 
  I2cwbyte Ds1307w                                          
  I2cwbyte 0                                               
  I2cwbyte _sec                                           
  I2cwbyte _min                                           
  I2cwbyte _hour                                           
  I2cstop

Return

'*******************************************************************************

Saat_goster:

  Lcdat 3 , 1 , "TARiH: " ; Date$
  Lcdat 6 , 1 , "SAAT : " ; Time$
  Lcdat 1 , 1 , A ; " " ; Sabahf ; " " ; Aksamf ; " "

  Waitms 5

Return

'*******************************************************************************

Int0_int:

    If Tmr0_flag = 0 Then
       Enable Timer0
    Else
       Lamba = 0
       Tmr0_flag = 0
       Disable Timer0
       Tcnt0 = A
    End If

Return

'*******************************************************************************

Timer_0_int:

     Tmr0_flag = 1

     Lamba = 1

Return

'*******************************************************************************