Hi to all
about 1 week i write a bascom code, but after 30 min device is write wrong text on lcd with rooling on lcd,
then device will be reset more and more.
i dont know whats my mistake.
i use external crystal.
i changed my code more to improve, but isnt ok yet.
plz help me
:cry: :x :roll:
[code:1:9d5ea8b578]
$regfile = "m8def.dat"
$crystal = 4000000
'$baud = 9600
$swstack = 60
$hwstack = 60
$framesize = 100
'Declare Sub Getdatetime()
Config Lcdpin = Pin , Db4 = Portd.6 , Db5 = Portd.5 , Db6 = Portd.4 , Db7 = Portd.3 , E = Portd.7 , Rs = Portb.0
Config Lcd = 16 * 2
Config Adc = Single , Prescaler = Auto
Cls
Cursor Off
'--------------------------- ds 1307 -----------------------------------------
$lib "ds1307clock.lib"
Config Clock = User
'configure the scl and sda pins
Config Sda = Portb.5
Config Scl = Portb.4
'address of ds1307
Const Ds1307w = &HD0 ' Addresses of Ds1307 clock
Const Ds1307r = &HD1
'Config I2cdelay = 10
'Config Date = Ymd , Separator = -
'_______________________________________________________________________________
Dim Index As Byte
Dim Key As Byte
Dim I As Byte
Dim I1 As Byte
Dim D As Byte
Dim W As Word
Dim W1 As Word
Dim W2 As Word
Dim W3 As Word
Dim W4 As Word
Dim Temp As Word
Dim Temp_lf As Byte
Dim Buf1 As Word
Dim Fl As Bit
Dim Sil As Bit
Dim Cnt As Byte
Dim Beep As Bit
Dim Pwr As Bit
Dim S As String * 15
Dim A As Byte , B As Byte , Data1 As Byte , C As Byte
Dim Light_bit As Bit
Dim V As Long
Dim Welcome As Bit
Dim Alarm_bit As Bit
Dim Flash As Bit
Dim Sh As Byte
Dim Cnv As Byte
Dim Stk As Byte
Dim Stv(20) As Eram String * 15
Dim Def As Eram Byte
Dim Smoke As Eram Word
Dim Ecnt As Eram Byte
Dim Temp_h As Eram Word
Dim Temp_l As Eram Word
'Dim Comp As Word
Dim Ivi As Long
If Def = 255 Then
Def = 14
Ecnt = 0
Temp_h = 35
Temp_l = 25
Smoke = 900
For I = 1 To 20
Stv(i) = ""
Next I
Alarm_bit = 1
End If
Dim Seco As Byte , Mine As Byte , Hour As Byte
Dim M_day As Byte
Dim M_month As Byte
Dim M_year As Byte
Dim Sm As Bit
Dim Tp As Bit
'-------------------------------------------------------------------------------
Buzzer Alias Portd.2 : Ddrd.2 = 1
Light Alias Portb.1 : Ddrb.1 = 1
Alarm Alias Portb.2 : Ddrb.2 = 1
Fan Alias Portb.3 : Ddrb.3 = 1
Ddrc.0 = 0 : Portc.0 = 1
Ddrd.1 = 1
Buzzer = 0
'----------------------------------------------------------------------------
'Cursor Off
'Cls
Buzzer = 1
Portd.1 = 1
Beep = 1
Deflcdchar 2 , 4 , 14 , 14 , 31 , 31 , 31 , 14 , 32 ' replace ? with number (0-7)
Deflcdchar 3 , 4 , 14 , 14 , 17 , 17 , 14 , 32 , 21 ' replace ? with number (0-7)
Deflcdchar 4 , 32 , 2 , 6 , 30 , 31 , 30 , 6 , 2 ' replace ? with number (0-7)
'Deflcdchar 5 , 32 , 32 , 32 , 17 , 27 , 21 , 17 , 17 ' replace ? with number (0-7)
Deflcdchar 6 , 4 , 10 , 4 , 32 , 32 , 32 , 32 , 32 ' replace ? with number (0-7)
Cls
Home U : Lcd " TIAM NETWORK"
Wait 3
Cls
Lcd " iRACK"
Home L : Lcd " ULTRA+"
Wait 3
Cls
W = Temp_h
Lcd "HIGH TEMP:" ; W ; Chr(6) ; "c"
Home L
W = Temp_l
Lcd "LOW TEMP:" ; W ; Chr(6) ; "c"
Wait 3
Cls
Pwr = 1
'Gosub Power_on
I1 = 20
Gosub Ds1307
'-------------------------------------------------------------------------------
Do
Main:
Gosub Keyb
Ivi = Ivi + 1
If Ivi > 10000000 Then
Cls
Ivi = 0
End If
Select Case Key
Case 1 : Toggle Pwr 'Gosub Power_on
Case 2 : Gosub Light_on
Case 3 :
If Flash = 1 Then Flash = 0 Else Gosub Show_fault
Case 4 : Gosub Mute
Case 5 :
Case 6 :
Toggle Sil
Cls : Locate 1 , 4
If Sil = 1 Then
Lcd " Farinhate"
Else
Lcd "Cantigrade"
End If
Wait 2
Cls
Case 7 : Gosub Menu
End Select
If Pwr = 1 Then
Portd.1 = 1
'Incr I1
'If I1 > 10 Then
W1 = Getadc(1) : W1 = W1 / 2 'out temp
W2 = Getadc(2) : W2 = W2 / 2
' I1 = 0
'End If
Buf1 = W2
If Sil = 1 Then
Buf1 = W2 * 18
Buf1 = Buf1 / 10
Buf1 = Buf1 + 32
End If
Home U : Lcd "IN:" ; Buf1 ; Chr(6)
If Sil = 0 Then Lcd "c" Else Lcd "F"
If Flash = 1 Then
Wait 1
Home U : Lcd " "
Wait 1
End If
Buf1 = W1
If Sil = 1 Then
Buf1 = W1 * 18
Buf1 = Buf1 / 10
Buf1 = Buf1 + 32
End If
Lcd " OUT:" ; Buf1 ; Chr(6)
If Sil = 0 Then Lcd "c " Else Lcd "F "
W3 = Getadc(3) 'smoke
If W2 > Temp_h Then Fan = 1
If W2 < Temp_l Then Fan = 0 'Else Fan = 0
Home L :
If Fan = 0 Then Lcd "FAN OFF " Else Lcd "FAN ON "
If Light_bit = 0 Then Lcd Chr(2) Else Lcd Chr(3) 'lamp off
If Beep = 1 Then Lcd Chr(4) Else Lcd " "
If W3 > Smoke Then
Locate 2 , 11
Lcd "Smoke "
Wait 1
Locate 2 , 11
Lcd " "
If Beep = 1 Then Buzzer = 0
'If Alarm_bit = 1 Then Alarm = 1
Alarm = 1
Sm = 1
Goto Store
Wait 1
Buzzer = 1
Else
Locate 2 , 11
W4 = Getadc(5) : W4 = W4 / 10
Lcd " H:" ; W4 ; "% "
Alarm = 0
End If
Temp = Temp_h + 3
If W2 > Temp Then
If Beep = 1 Then Buzzer = 0
Flash = 1
Tp = 1
Goto Store
Wait 1
Else
Flash = 0
End If
If W2 < Temp_l And W3 < Smoke Then
Fan = 0
Buzzer = 1
Alarm = 0
'Flash = 0
Sm = 0
Tp = 0
End If
Else
Cls
Locate 1 , 4 : Lcd "POWER OFF"
Welcome = 0
Wait 2
Portd.1 = 0
Cls
Alarm = 0
Fan = 0
Light = 0
End If
Goto Main
Goto Main
Store:
Gosub Ds1307
If Stk > Mine Then Goto Main
Cnt = Ecnt
Cnt = Cnt + 1
'Gosub Ds1307
S = Str(hour) + ":" + Str(mine) + " " + Str(m_month) + "/" + Str(m_day) '+ " " + Str(w2)
If Cnt > 25 Then Cnt = 1
Ecnt = Cnt
If Tp = 1 Then S = "T " + S
If Sm = 1 Then S = "S " + S 'Str(hour) + ":" + Str(mine) + " " + Str(m_year) + "/" + Str(m_month) + "/" + Str(m_day) + " " + Str(w3)
Stv(cnt) = S
'Gosub Ds1307
Stk = Mine
Stk = Stk + 5
Loop
End 'end program
'********************************************* STORE
'''''''''''''''''''' keyb
Keyb:
Key = 0
W = Getadc(0)
Waitms 100
W = Getadc(0)
'Home U :
'Print "w:" ; W ; " "
Select Case W
Case 40 To 55 : Key = 1
Case 70 To 90 : Key = 2
Case 100 To 115 : Key = 3
Case 125 To 150 : Key = 4
Case 155 To 170 : Key = 6
Case 175 To 190 : Key = 5
Case 200 To 220 : Key = 7
Case Else:
End Select
If Key = 0 Then Cnv = Cnv + 1
If Cnv > 90 Then
'Cexit = 1
Cnv = 0
Goto Main
End If
If Pwr = 0 And Key > 1 Then
Key = 0
Return
End If
If Key > 0 And Beep = 1 Then
Buzzer = 0
Cnv = 0
End If
: Waitms 200 : Buzzer = 1
Waitms 10
Return
'****************** light_on
Light_on:
'If Pwr = 0 Then Return
Light_bit = Not Light_bit
Light = Light_bit
Wait 1
Return
'****************** Show_fault
Show_fault:
Cls
Cnt = Ecnt
While Key <> 7
Gosub Keyb
If Key = 5 Then Incr Cnt
If Key = 6 Then Decr Cnt
If Key = 7 Then Exit While
If Cnt > 20 Then Cnt = 20
If Cnt < 1 Then Cnt = 1
S = Stv(cnt)
'I = Instr(1 , S , "-" )
'I = I - 1
'S = Mid(s , 1 , I)
Home U : Lcd Cnt ; ":" '; S ; " "
'I = Instr(s , " ")
'S = Mid(s , I , 10)
'Home L
Lcd S ; " "
Wait 1
Wend
'Gosub Rep
Return
'***************************** menu
Menu:
Cls
Key = 0
Wait 1
While Index < 5
Gosub Keyb
Locate 1 , 7
Lcd "MENU"
Locate 2 , 3
If Key = 6 Then Index = Index -1
If Index < 1 Then Index = 1
If Key = 5 Then Index = Index + 1
'If Index > 4 Then Index = 4
Select Case Index
Case 1 :
Lcd " SET TEMP "
If Key = 7 Then Gosub Set_temp
Case 2 :
Lcd " SET GAS "
If Key = 7 Then Gosub Set_gas
'Case 3 : Gosub Show_fault
Case 4 :
Lcd "CLEAN FAULT"
If Key = 7 Then Gosub Erase_fault
Case 3 :
Lcd " SET TIME "
If Key = 7 Then Gosub Settime
End Select
Waitms 400
Wend
Cls
'Gosub Rep
Index = 1
'Goto Main
Return
'**************************** Set temp
Set_temp:
'If Pwr = 0 Then Return
Key = 0
Cls
Lcd "Temp Low:"
'Wait 2
Home L
Temp = Temp_l
Buf1 = Temp_h - 2
While Key <> 7
Gosub Keyb
If Key = 5 Then Incr Temp
If Key = 6 Then Decr Temp
If Temp < 1 Then Temp = 1
If Temp > Buf1 Then Temp = Buf1
Locate 2 , 6 : Lcd Temp ; Chr(6) ; "c "
Waitms 50
Wend
Temp_l = Temp
Cls
Gosub Don 'Lcd "Low Temp ok!"
Wait 2
Cls
Lcd "Temp High:"
'Wait 1
Home L
Key = 0
Temp = Temp_h
Buf1 = Temp_l + 2
While Key <> 7
Gosub Keyb
If Key = 5 Then Incr Temp
If Key = 6 Then Decr Temp
If Temp < Buf1 Then Temp = Buf1
If Temp > 100 Then Temp = 100
Locate 2 , 6 : Lcd Temp ; Chr(6) ; "c "
Waitms 50
Wend
Temp_h = Temp
Cls
Gosub Don 'Lcd "High Temp ok!"
Wait 2
Buf1 = Temp - 1
Key = 0
Cls
Return
'********************** humidity
Set_gas:
'If Pwr = 0 Then Return
Key = 0
Cls
Lcd "Gas Sens:"
Wait 1
Home L
Temp = Smoke
While Key <> 7
Gosub Keyb
If Key = 5 Then Temp = Temp + 100
If Key = 6 Then Temp = Temp - 100
If Temp < 100 Then Temp = 100
If Temp > 1000 Then Temp = 1000
Locate 2 , 6 : Lcd Temp ; "ppm "
Waitms 100
Wend
Smoke = Temp
Cls
Gosub Don 'Lcd "Done!"
Wait 2
Cls
'Goto Main
Return
'******************** mute
Mute:
Toggle Beep
Toggle Alarm_bit
Cls
Locate 1 , 5
Lcd "MUTE "
If Beep = 1 Then
Lcd "ON"
Buzzer = 0 : Waitms 200 : Buzzer = 1
Else
Lcd "OFF"
Alarm = 0
End If
Wait 2
Cls
'if beep=0 then lcd "
Return
'******************** Eareas
Erase_fault:
'If Pwr = 0 Then Return
Cls
Locate 1 , 3
Lcd "CLANE"
For I = 1 To 15
Stv(i) = ""
Next I
Home L
Gosub Don 'Lcd "Done!"
Wait 2
Cls
Return
'-------------------------------------------------------------------------------
Ds1307:
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 0 ' start address in 1307
I2cstart ' Generate start code
I2cwbyte Ds1307r ' send address
I2crbyte Seco , Ack 'sec
I2crbyte Mine , Ack ' MINUTES
I2crbyte Hour , Ack ' Hours
I2crbyte I , Ack
I2crbyte M_day , Ack
I2crbyte M_month , Ack
I2crbyte M_year , Nack
I2cstop
Seco = Makedec(seco) : Mine = Makedec(mine) : Hour = Makedec(hour)
M_day = Makedec(m_day) : M_year = Makedec(m_year) : M_month = Makedec(m_month)
If Seco > 59 Then Return
If Mine > 59 Then Return
If Hour > 23 Then
Hour = 0
'Gosub Seco
End If
Return
'-------------------------------------------------------------------------------
'---------------------------------------------
Mine:
If Key = 5 Then Incr Sh
If Key = 6 Then Decr Sh
If Sh = 255 Then Sh = 0
If Sh > 59 Then Sh = 59
Mine = Makebcd(sh)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 1 ' starting address in 1307
I2cwbyte Mine
I2cstop
Return
'-------------------------------------
Hour:
If Key = 5 Then Incr Sh
If Key = 6 Then Decr Sh
If Sh = 255 Then Sh = 0
If Sh > 23 Then Sh = 23
Hour = Makebcd(sh)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 2 ' starting address in 1307
I2cwbyte Hour
I2cstop
Return
'-------------------------------------
'Setdate:
' 'Seco = Makebcd(seco) : Mine = Makebcd(mine) : Hour = Makebcd(hour)
' M_day = Makebcd(m_day) : M_month = Makebcd(m_month) : M_year = Makebcd(m_year)
' I2cstart ' Generate start code
' I2cwbyte Ds1307w ' send address
' I2cwbyte 5
' 'I2cwbyte 1
' 'I2cwbyte Mine ' starting address in 1307
' 'I2cwbyte Hour
' 'I2cwbyte 1
' I2cwbyte M_day ' Send Data to SECONDS
' I2cwbyte M_month ' MINUTES
' I2cwbyte M_year ' Hours
' I2cstop
'Return
Settime:
Cls
Key = 0
Wait 1
Gosub Ds1307
Sh = Hour
While Key <> 7
Gosub Keyb
Home U : Lcd "Set hour:" ; Sh ; " "
Gosub Hour
Waitms 500
Wend
Sh = 0
Cls
Wait 2
Gosub Ds1307
Sh = Mine
Key = 0
While Key <> 7
Gosub Keyb
Home U : Lcd "Set minute:" ; Sh ; " "
Gosub Mine
Waitms 500
Wend
Cls
Wait 2
Cls
Gosub Ds1307
Sh = M_day
Key = 0
While Key <> 7
Gosub Keyb
Home U : Lcd "Set day:" ; Sh ; " "
If Key = 5 Then Incr Sh
If Key = 6 Then Decr Sh
If Sh > 31 Then Sh = 31
If Sh < 1 Then Sh = 1
M_day = Makebcd(sh)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 4 ' starting address in 1307
I2cwbyte M_day
I2cstop
Waitms 500
Wend
Cls
Wait 1
Gosub Ds1307
Key = 0
Sh = M_month
While Key <> 7
Gosub Keyb
Home U : Lcd "Set month:" ; Sh ; " "
If Key = 5 Then Incr Sh
If Key = 6 Then Decr Sh
If Sh > 12 Then Sh = 12
If Sh < 1 Then Sh = 1
M_month = Makebcd(sh)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 5 ' starting address in 1307
I2cwbyte M_month
I2cstop
Waitms 500
Wend
Cls
Wait 1
Gosub Ds1307
Key = 0
'M_year = 10
Sh = M_year
While Key <> 7
Gosub Keyb
Home U : Lcd "Set year:" ; Sh ; " "
If Key = 5 Then Incr Sh
If Key = 6 Then Decr Sh
If Sh > 99 Then Sh = 0
If Sh < 10 Then Sh = 10
M_year = Makebcd(sh)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 6 ' starting address in 1307
I2cwbyte M_year
I2cstop
Waitms 500
Wend
'Gosub Setdate
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 0 ' starting address in 1307
I2cwbyte 1
I2cstop
Cls
For I1 = 1 To 6
Gosub Ds1307
Locate 1 , 5 : Lcd Hour ; ":" ; Mine ; ":" ; Seco ; " "
Locate 2 , 5 : Lcd M_year ; "/" ; M_month ; "/" ; M_day
Wait 1
Next I1
Cls
Gosub Don 'Lcd "Done!"
Wait 1
Cls
Return
Don:
Lcd "Done!"
Return
[/code:1:9d5ea8b578]
↧