$regfile = "m328pdef.dat" $crystal = 1000000 $hwstack = 256 $swstack = 256 $framesize = 256 Declare Sub Trig_on Declare Sub Trig_of declare sub EnterTime Declare Sub On_lcd Declare Sub Getvbat Declare Sub Checkstate Dim Hr As Byte Dim Mn As Byte Dim X As Byte Dim Y As Byte Declare Sub Display_time Dim I As Byte Dim Second As Byte Dim Minute As Byte Dim Hour As Byte Dim Systrig As Byte Dim Adcw As Word Dim Vbat As Single Dim Keyin As Byte Dim Currlocate As Byte Dim Blockstate As Byte Dim Trigger As Byte , Keypress As Byte , Keytestpress As Byte Const True = 1 Const False = 0 Const Block = 0 Const Triggertime = 10 Dim Leftflage As Byte 'Config New PCB Config Lcd = 16 * 2 Config Lcdpin = Pin , Db4 = Portc.0 , Db5 = Portc.1 , Db6 = Portc.2 , Db7 = Portc.3 , E = Portd.0 , Rs = Portd.1 '------------- Port 2 Config -------------------- Lcd_enable Alias Portc.4 Ddrd = &B11110000 Portd = &B00001100 Ddrc = &B00010000 Portc = &B00000000 Assr = &H20 Tccr2a = &H00 Tccr2b = &H05 Tcnt2 = &H00 Ocr2a = &H00 Ocr2b = &H00 On Timer2 T2asyn Enable Timer2 Config Pind.2 = Input Set Portd.2 Set Pind.2 Keyup Alias Pind.2 Config Pind.3 = Input Set Portd.3 Set Pind.3 Keyleft Alias Pind.3 On Int0 Int0serv Config Int0 = Low Level Enable Int0 On Int1 Int1serv Config Int1 = Low Level Enable Int1 Enable Interrupts Dim Menulevel As Byte Dim Thour As Byte , Tmin As Byte Dim Hr_on(10) As Byte , Mn_on(10) As Byte Dim Hr_of(10) As Byte , Mn_of(10) As Byte dim TimerIndex as byte Dim Activestate As Byte Dim Shutdownstate As Byte Dim Invert As Byte Dim Ku As Byte Dim Kt As Byte Dim Ktmp As Byte Const Keytime = 10 dim HourSet as Byte dim MinuteSet as byte 'init timer data for TimerIndex =0 to 10 Hr_on(TimerIndex) = 0 Mn_on(TimerIndex) = 0 Hr_of(TimerIndex) = 0 Mn_of(TimerIndex) = 0 next TimerIndex Activestate = False Shutdownstate = False Blockstate = False Call On_lcd Cls Lcd "WATERWORK MODEL1" Wait 1 cls Lcd "EnterRTC" Wait 1 lowerline Hr=Hour Mn=Minute Y=2 X=1 call Display_time 'ENTER TIME HourSet= Hour MinuteSet= Minute call EnterTime Hour= HourSet Minute = MinuteSet for TimerIndex =1 to 4 HourSet= Hr_on(TimerIndex) MinuteSet= Mn_on(TimerIndex) cls lcd "SET TIMER" lcd TimerIndex lcd " ON" lowerline Hr=HourSet Mn=MinuteSet Y=2 X=1 call Display_time call EnterTime Hr_on(TimerIndex)= HourSet Mn_on(TimerIndex) = MinuteSet HourSet= Hr_of(TimerIndex) MinuteSet= Mn_of(TimerIndex) cls lcd "SET TIMER" lcd TimerIndex lcd " OF" lowerline Hr=HourSet Mn=MinuteSet Y=2 X=1 call Display_time call EnterTime Hr_of(TimerIndex)= HourSet Mn_of(TimerIndex) = MinuteSet next TimerIndex do If Keypress = True Then '*** Key press wakeup Disable Int0 Disable Int1 Call On_lcd Bitwait Keyup , Set Bitwait Keyleft , Set Trigger = 0 Keypress = False Systrig = True i = 0 do If Systrig = True Then incr i Call Checkstate Locate 1 , 1 Lcd "STATUS " If Activestate = True Then Lcd "ON " Elseif Activestate = False Then Lcd "OF " End If Locate 2 , 1 Hr=Hour Mn=Minute Y=2 X=1 call Display_time If Invert = True Then Locate 2 , 3 Lcd ":" Invert = False Else Locate 2 , 3 Lcd " " Invert = True End If Cursor Off Systrig = False End If loop until i> 10 end if Ddrc = &B00010000 Ddrb = &B00000000 Set Eifr.0 Set Eifr.1 Enable Int0 Enable Int1 Enable Interrupts Keypress = False Call Checkstate Smcr = &B00000111 sleep nop nop If Keyup = 0 Or Keyleft = 0 Then Keypress = True loop do Loop sub EnterTime 'Return HourSet/MinuteSet Do Cursor Blink Set_h_t: Kt = HourSet \ 10 Ktmp = Kt * 10 Ku = HourSet - Ktmp Locate 2 , 1 Lcd Kt ; Ku Locate 2 , 1 Bitwait Keyleft , Set Waitms Keytime Do 'If Key press then change value If Keyup = 0 Then Trigger = 0 Incr Kt If Kt > 2 Then Kt = 0 Locate 2 , 1 Lcd Kt Locate 2 , 1 Bitwait Keyup , Set Waitms Keytime End If 'If Key press then go to next set value If Keyleft = 0 Then Trigger = 0 If Kt = 2 And Ku > 3 Then HourSet = 23 Else HourSet = Kt * 10 HourSet = HourSet + Ku End If Locate 2 , 1 If HourSet < 10 Then Lcd "0" Lcd HourSet Locate 2 , 1 Goto Set_h_u End If 'If Trigger > Triggertime Then Goto Sleepnow Loop Set_h_u: Kt = HourSet \ 10 Ktmp = Kt * 10 Ku = HourSet - Ktmp Locate 2 , 1 Lcd Kt ; Ku Locate 2 , 2 Bitwait Keyleft , Set Waitms Keytime Do If Keyup = 0 Then Trigger = 0 Incr Ku If Kt = 2 Then If Ku > 3 Then Ku = 0 Else If Ku > 9 Then Ku = 0 End If Locate 2 , 2 Lcd Ku Locate 2 , 2 Bitwait Keyup , Set Waitms Keytime End If If Keyleft = 0 Then Trigger = 0 HourSet = Kt * 10 HourSet = HourSet + Ku Locate 2 , 1 If HourSet < 10 Then Lcd "0" Lcd HourSet Goto Set_m_t End If 'If Trigger > Triggertime Then Goto Sleepnow Loop Set_m_t: Kt = MinuteSet \ 10 Ktmp = Kt * 10 Ku = MinuteSet - Ktmp Locate 2 , 4 Lcd Kt ; Ku Locate 2 , 4 Bitwait Keyleft , Set Waitms Keytime Do If Keyup = 0 Then Trigger = 0 Incr Kt If Kt > 5 Then Kt = 0 Locate 2 , 4 Lcd Kt Locate 2 , 4 Bitwait Keyup , Set Waitms Keytime End If If Keyleft = 0 Then Trigger = 0 MinuteSet = Kt * 10 MinuteSet = MinuteSet + Ku Locate 2 , 4 If MinuteSet < 10 Then Lcd "0" Lcd MinuteSet Locate 2 , 4 Goto Set_m_u End If 'If Trigger > Triggertime Then Goto Sleepnow Loop Set_m_u: Kt = MinuteSet \ 10 Ktmp = Kt * 10 Ku = MinuteSet - Ktmp Locate 2 , 4 Lcd Kt ; Ku Locate 2 , 5 Bitwait Keyleft , Set Waitms Keytime Do If Keyup = 0 Then Trigger = 0 Incr Ku If Ku > 9 Then Ku = 0 Locate 2 , 5 Lcd Ku Locate 2 , 5 Bitwait Keyup , Set Waitms Keytime End If If Keyleft = 0 Then Trigger = 0 MinuteSet = Kt * 10 MinuteSet = MinuteSet + Ku Locate 2 , 4 If MinuteSet < 10 Then Lcd "0" Lcd MinuteSet Cursor Off Bitwait Keyleft , Set goto End_Set_time End If 'If Trigger > Triggertime Then Goto Sleepnow Loop '''' End If '//End keyleft If Keyup = 0 Then Menulevel = 4 '//End keyleft Systrig = True Bitwait Keyup , Set Waitms Keytime goto End_Set_time End If Loop '// Loop End_Set_time: end sub Sub Checkstate '///////////////////////// check for timer ////////////////////////////// for TimerIndex= 1 to 4 If Hr_on(TimerIndex) = Hr_of(TimerIndex) And Mn_on(TimerIndex) = Mn_of(TimerIndex) Then Goto Exitsetstate 'Same time do nothing If Activestate = False Then 'Current State is OF If Hr_on(TimerIndex) = Hr_of(TimerIndex) Then If Mn_on(TimerIndex) = Mn_of(TimerIndex) Then Goto Exitsetstate End If End If If Hour = Hr_on(TimerIndex) And Minute = Mn_on(TimerIndex) Then Call Trig_on Activestate = True End If Else 'Current State is ON If Hour = Hr_of(TimerIndex) And Minute = Mn_of(TimerIndex) Then Call Trig_of Activestate = False End If End If next TimerIndex Exitsetstate: End Sub Sub On_lcd Set Lcd_enable Waitms 10 'Delay Initlcd 'Display On Cursor Noblink Cursor Off Cls End Sub Sub Display_time Locate Y , X If Hr < 10 Then Lcd "0" ; Hr Else Lcd Hr X = X + 2 Locate Y , X If Mn < 10 Then Lcd ":0" ; Mn Else Lcd ":" ; Mn X = X + 2 End Sub '------------------- Motor Reutine ----------------- Sub Trig_on Ddrd = Ddrd Or &HF0 Portd = Portd And &H0F Waitms 1 Set Portd.4 Reset Portd.5 Delay Reset Portd.6 Set Portd.7 Waitms 150 Ddrd = Ddrd Or &HF0 Portd = Portd And &H0F End Sub Sub Trig_of Ddrd = Ddrd Or &HF0 Portd = Portd And &H0F Waitms 1 Reset Portd.4 Set Portd.5 Delay Set Portd.6 Reset Portd.7 Waitms 150 Ddrd = Ddrd Or &HF0 Portd = Portd And &H0F End Sub '---------------------------------------------------------------------- T2asyn: Incr Second If Second > 59 Then '59 Then Second = 0 Incr Minute If Minute > 59 Then Minute = 0 Incr Hour If Hour = 24 Then Hour = 0 End If End If Incr Trigger Systrig = True Return Int0serv: Keypress = True Return Int1serv: Keypress = True Return