اطلاعیه

Collapse
No announcement yet.

تبدیل کد از مگا16 به مگا8 (بیسیک)

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    تبدیل کد از مگا16 به مگا8 (بیسیک)

    با سلام
    من یک تیکه کد دارم برای سنسور dht22 که برای ای سی مگا16 برنامه ریزی شده است و الان میخوام پین هاش را طوری تغییر بدهم که روی ای سی مگا8 کار کنه یک سری تغییرات در مورد پورت های ورودی و خروجی دادم ولی تاثیری نداشت! لطفا راهنمایی کنید!

    کد اصلی برنامه:
    کد:
    $regfile = "m16def.dat"
    $crystal = 8000000                     'Const Min_time = 48
    '$crystal = 16000000                     'Const Min_time = 95
    '$crystal = 11059200                     'Const Min_time = 66
    '-------------------------------------------------------------------------------
    
    Config Pind.0 = Input                    'DHT22 Data
    Config Timer0 = Timer , Prescale = 8
    
    '.-------------------------------------------------------------------------------
    'Const Min_time = 95
    Const Min_time = 48
    'Const Min_time = 66
    '-------------------------------------------------------------------------------
    
    Dim Count As Byte
    Dim Signaltime(43) As Byte
    Dim Humidityw As Word
    Dim Temperaturew As Word
    Dim Humsens_chksum As Byte
    
    Dim Humiditys As String * 16
    Dim Temperatures As String * 16
    
    
    Dim Strstring As String * 14
    Dim Ib As Byte
    
    '-------------------------------------------------------------------------------
    Config Lcd = 16 * 2
    Config Lcdpin = Pin , Db4 = Pina.2 , Db5 = Pina.3 , Db6 = Pina.4 , Db7 = Pina.5 , E = Pina.1 , Rs = Pina.0
    Cursor Off
    Cls
    Cls
    Deflcdchar 0 , 14 , 10 , 14 , 32 , 32 , 32 , 32 , 32    ' replace ? with number (0-7)
    '-------------------------------------------------------------------------------
    Declare Sub Read_timings
    Declare Sub Humtemp_values
    
    Declare Function Compare_chksum(byval Hsens_humidity As Word , _
    Byval Hsens_temperature As Word , Byval Hsens_chksum As Byte) As Byte
    
    '-------------------------------------------------------------------------------
    Do
    
     Call Humtemp_values
    
      Humiditys = Str(humidityw)
      Temperatures = Str(temperaturew)
     Locate 1 , 1
     Lcd "Humidity:" ; Format(humiditys , "0.0") ; " %   "
     Lowerline
     Lcd "Temp:" ; Format(temperatures , "0.0") : Lcd " " : Lcd Chr(0) : Lcd "C"
     Waitms 100
    
    Loop
    
    End
    '-------------------------------------------------------------------------------
    
    Sub Read_timings
    
      Wait 3
    
      Count = 1
    
      Config Pind.0 = Output : Portd.0 = 0           ' request data
      Waitms 20                        ' wait 20 ms
      Config Pind.0 = Input                  ' wait for data, receive data
    
      While Count < 43                     'collect 42 timings / signals
    
       Bitwait Pind.0 , Set                 'signal goes high > start timer
       Start Timer0
       Bitwait Pind.0 , Reset                'signal goes low > stop timer
       Stop Timer0
       Signaltime(count) = Tcnt0               'store number of ticks per signal in Signaltime byte
    
       Tcnt0 = 0
       Incr Count
    
     Wend
    
    
    End Sub
    
    '############################################## Humidity and temperature values
    
    Sub Humtemp_values
    
      Local X As Byte
    
      Humidityw = 0
      Temperaturew = 0
      Humsens_chksum = 0
    
      Call Read_timings
    
      For Count = 3 To 42                   ' skip first two
    
       Select Case Count
    
         Case 3 To 18
          X = 18 - Count
          If Signaltime(count) > Min_time Then Toggle Humidityw.x
    
         Case 19 To 34
          X = 34 - Count
          If Signaltime(count) > Min_time Then Toggle Temperaturew.x
    
         Case 34 To 42
          X = 42 - Count
          If Signaltime(count) > Min_time Then Toggle Humsens_chksum.x
    
       End Select
    
     Next
    
    End Sub
    
    '######################### Calculate Checksum and compare with trasnmitted value
    
    
    Function Compare_chksum(byval Hsens_humidity As Word , Byval Hsens_temperature As Word , Byval Hsens_chksum As Byte) As Byte
    
      Local Chksum As Byte
    
      Chksum = Low(hsens_humidity ) + High(hsens_humidity )
      Chksum = Chksum + Low(hsens_temperature)
      Chksum = Chksum + High(hsens_temperature)
    
      If Chksum = Hsens_chksum Then
       Compare_chksum = 1
      Else
       Compare_chksum = 0
      End If
    
    End Function
    کد تغییر داده شده: طبق شماتیک مدار (مگا8)

    کد:
     $regfile = "m8def.dat"
    $crystal = 8000000                     'Const Min_time = 48
    '$crystal = 16000000                     'Const Min_time = 95
    '$crystal = 11059200                     'Const Min_time = 66
    '-------------------------------------------------------------------------------
    
    Config Pind.6 = Input                    'DHT22 Data
    Config Timer0 = Timer , Prescale = 8
    
    '.-------------------------------------------------------------------------------
    'Const Min_time = 95
    Const Min_time = 48
    'Const Min_time = 66
    '-------------------------------------------------------------------------------
    
    Dim Count As Byte
    Dim Signaltime(43) As Byte
    Dim Humidityw As Word
    Dim Temperaturew As Word
    Dim Humsens_chksum As Byte
    
    Dim Humiditys As String * 16
    Dim Temperatures As String * 16
    
    
    Dim Strstring As String * 14
    Dim Ib As Byte
    
    '-------------------------------------------------------------------------------
    Config Lcd = 16 * 2
    Config Lcdpin = Pin , Db4 = Pinb.4 , Db5 = Pinb.5 , Db6 = Pinb.6 , Db7 = Pinb.7 , E = Pinb.2 , Rs = Pinb.0
    Cursor Off
    Cls
    Cls
    Deflcdchar 0 , 14 , 10 , 14 , 32 , 32 , 32 , 32 , 32    ' replace ? with number (0-7)
    '-------------------------------------------------------------------------------
    Declare Sub Read_timings
    Declare Sub Humtemp_values
    
    Declare Function Compare_chksum(byval Hsens_humidity As Word , _
    Byval Hsens_temperature As Word , Byval Hsens_chksum As Byte) As Byte
    
    '-------------------------------------------------------------------------------
    Do
    
     Call Humtemp_values
    
      Humiditys = Str(humidityw)
      Temperatures = Str(temperaturew)
     Locate 1 , 1
     Lcd "Humidity:" ; Format(humiditys , "0.0") ; " %   "
     Lowerline
     Lcd "Temp:" ; Format(temperatures , "0.0") : Lcd " " : Lcd Chr(0) : Lcd "C"
     Waitms 100
    
    Loop
    
    End
    '-------------------------------------------------------------------------------
    
    Sub Read_timings
    
      Wait 3
    
      Count = 1
    
      Config Pind.6 = Output : Portd.6 = 0           ' request data
      Waitms 20                        ' wait 20 ms
      Config Pind.6 = Input                  ' wait for data, receive data
    
      While Count < 43                     'collect 42 timings / signals
    
       Bitwait Pind.6 , Set                 'signal goes high > start timer
       Start Timer0
       Bitwait Pind.6 , Reset                'signal goes low > stop timer
       Stop Timer0
       Signaltime(count) = Tcnt0               'store number of ticks per signal in Signaltime byte
    
       Tcnt0 = 0
       Incr Count
    
     Wend
    
    
    End Sub
    
    '############################################## Humidity and temperature values
    
    Sub Humtemp_values
    
      Local X As Byte
    
      Humidityw = 0
      Temperaturew = 0
      Humsens_chksum = 0
    
      Call Read_timings
    
      For Count = 3 To 42                   ' skip first two
    
       Select Case Count
    
         Case 3 To 18
          X = 18 - Count
          If Signaltime(count) > Min_time Then Toggle Humidityw.x
    
         Case 19 To 34
          X = 34 - Count
          If Signaltime(count) > Min_time Then Toggle Temperaturew.x
    
         Case 34 To 42
          X = 42 - Count
          If Signaltime(count) > Min_time Then Toggle Humsens_chksum.x
    
       End Select
    
     Next
    
    End Sub
    
    '######################### Calculate Checksum and compare with trasnmitted value
    
    
    Function Compare_chksum(byval Hsens_humidity As Word , Byval Hsens_temperature As Word , Byval Hsens_chksum As Byte) As Byte
    
      Local Chksum As Byte
    
      Chksum = Low(hsens_humidity ) + High(hsens_humidity )
      Chksum = Chksum + Low(hsens_temperature)
      Chksum = Chksum + High(hsens_temperature)
    
      If Chksum = Hsens_chksum Then
       Compare_chksum = 1
      Else
       Compare_chksum = 0
      End If
    
    End Function
    مداری مورد نظر برای تغییر پورت ها:




    سپاس



    #2
    پاسخ : تبدیل کد از مگا16 به مگا8 (بیسیک)

    مطمئنی با مگا 16 کار میکرده؟
    الهم صل علی محمد و آل محمد و عجل فرجهم

    دیدگاه


      #3
      پاسخ : تبدیل کد از مگا16 به مگا8 (بیسیک)

      سلام: بله: کار می کرد؟!ایا تغییراتی که برای مگا8 دادم درست هست(پورت ها را طبق شماتیک تعیین کردم)؟
      سپاس

      دیدگاه

      لطفا صبر کنید...
      X