'/////////////////////////////////////////////////// '//// IDPA Timer //// '/////////////////////////////////////////////////// $Device = mega8 $Stack = 32 $Clock = 3.6864 $Timer1 = Timer, Prescale=1, Compare=DisConnect, Clear $Key Rows = PORTB &h38 '$WatchDog = 2048 'About two seconds $Lcd = PORTC.0, RS=PORTD.5, EN=PORTB.2, 8, 2 $LeadChar = " ", Format(1,1) ' format for voltage $Sound = PORTB.1 $Def Power= PORTC.5 $Def Sense= DDRB.0 Const NofShots=100 Const BuffLen=NofShots*2 Const EndOfBuf=BuffLen+96 Const StartOfBuf=&h60 Dim buf(BuffLen) As Byte Dim Time As Word, ShootTime As Word, TimeOut As Word Dim n As Byte, m As Byte, tmp As Word Dim PressedKey As Byte, Adr As Word Dim Leap As Word, OldTime As Word, ParTime As Byte Dim ss As String*8 Dim Shoot As Bit, IsParTime As Bit, IPT As Bit Declare Interrupt Oc1A() Declare Interrupt Aci() Declare Sub Init() Declare Sub Show(k As Byte, kaj As Word) Declare Sub Up() Declare Sub Down() Declare Sub ReadData() Declare Sub IsShoot() Declare Sub SetSense() Declare Sub SetParTime() Declare Sub StartShooting() Declare Sub ShowSense() Declare Sub SaveData() Declare Sub LoadData() Declare Sub CheckForPowerOFF() Const T1 = 8, T2 = 16, T3 = 32 Const SetDelay=300 Const PowerOFF=20000 ' 3 min Const s=" Replace Battery " DefLcdChar 0, &h18, &h1C, &h1E, &h1F, &h1F, &h1E, &h1C, &h18 'DefLcdChar 0, &h0A, &h04, &h0E, &h10, &h10, &h11, &h0E, &h00 'DefLcdChar 1, &h0A, &h04, &h0E, &h10, &h0E, &h01, &h1E, &h00 'DefLcdChar 2, &h0A, &h04, &h1F, &h02, &h04, &h08, &h1F, &h00 'DefLcdChar 3, &h0A, &h0E, &h11, &h10, &h10, &h11, &h0E, &h00 'DefLcdChar 4, &h0A, &h0F, &h10, &h0E, &h01, &h11, &h0E, &h00 'DefLcdChar 5, &h0A, &h1F, &h02, &h04, &h08, &h10, &h1F, &h00 'Cls Init() Do PressedKey=Key() If PressedKey Then Select Case PressedKey Case T3 ' up Up() Case T2 ' down Down() Case T1 ' start StartShooting() End Select NoKey() PressedKey=0 End If If Shoot Then IsShoot() If IPT And Time>100*ParTime Then Reset IPT Sound 21,200 End If CheckForPowerOFF() 'Reset WatchDog Loop '/////////////////////////////////////////////////////// Sub CheckForPowerOFF() If TimeOut>PowerOFF Then ' PowerOFF SaveData() 'Reset WatchDog Reset Power End If End Sub '/////////////////////////////////////////////////////// Sub StartShooting() Disable Aci Cls: Lcd "StandBy:" NoKey(): Locate 2,1 m=0 While m<8 Incr m If Key() Then ' any key breaks start Cls: Lcd"OK" NoKey() PressedKey=0: m=0 Exit Sub End If Lcd Chr(0) WaitMs 200: WaitMs 200 ' 8 x .4s = 3.2s start Wend m=0 Format(3,2): Cls If IsParTime Then Set IPT ' this enables ParTime beep Lcd "P:" Else Lcd "N:" End If Time=0: OldTime=0: n=0 TimeOut=0 Enable Aci Sound 21, 150 Adr=StartOfBuf-2 ' in IsShoot is Incr first ' WaitMs 100 ' Enable Aci End Sub '/////////////////////////////////////////////////////// Sub IsShoot() Incr n: m=n Adr=Adr+2 ' prepare for write time Poke(adr, Time) ' write time Locate 1,3: Lcd Time Leap=Time-OldTime ' leap OldTime=Time Locate 2,1: Show(n, Leap) 'If Adr>EndOfBuf Then Adr=Adr-2 WaitMs 50 Reset Shoot TimeOut=0: tmp=Adr End Sub '/////////////////////////////////////////////////////// Sub Up() ' browse data in SRAM Disable Aci TimeOut=0 ' reset TimeOut timer Time=0 ' reset timer If nm Then Decr n tmp=tmp-2 End If If m Then ReadData() While PressedKey=T3 ' enter in SetSense after 3sec PressedKey=Key() If Time>SetDelay Then SetSense() Wend End Sub '/////////////////////////////////////////////////////// Sub Down() ' browse data in SRAM Disable Aci TimeOut=0 ' reset TimeOut timer Time=0 ' reset timer tmp=tmp-2 ' decrement adr to pre word Decr n If Not n Then Incr n tmp=StartOfBuf End If If n>1 Then OldTime=Peek(tmp-2) Else OldTime=0 End If If m Then ReadData() While PressedKey=T2 PressedKey=Key() If Time>SetDelay Then SetParTime() Wend End Sub '/////////////////////////////////////////////////////// Sub ReadData() Format(3,2) ShootTime=Peek(tmp) Leap=ShootTime-OldTime Locate 1, 3: Lcd ShootTime; Locate 2, 1: Show(n, Leap) End Sub '/////////////////////////////////////////////////////// Sub Show(k As Byte, kaj As Word) Format(2,0): Lcd k Format(3,2): Lcd kaj End Sub '/////////////////////////////////////////////////////// Interrupt Aci() ' IsShoot Set Shoot End Interrupt '/////////////////////////////////////////////////////// Interrupt Oc1A(), Save 1 ' update time Incr Time Incr TimeOut ' and TimeOut for PowerDown End Interrupt '/////////////////////////////////////////////////////// Sub SetParTime() Disable Aci Format (3,0): Cls If IsParTime Then Lcd"Normal :" Reset IsParTime Else Lcd"ParTime:" Locate 2,1: Lcd ParTime; " sec" Set IsParTime End If NoKey() PressedKey=0: m=0 Do PressedKey=Key() If PressedKey Then Select Case PressedKey Case T3 ' up If IsParTime Then Incr ParTime Locate 2,1: Lcd ParTime End If Case T2 ' down If IsParTime Then Decr ParTime If ParTime=0 Then ParTime=1 Locate 2,1: Lcd ParTime End If Case T1 ' start PressedKey=0 Cls: Lcd "OK" WriteEE(3, ParTime) WriteEE(4, IsParTime) Exit Sub End Select NoKey() PressedKey=0 CheckForPowerOFF() End If Loop End Sub '/////////////////////////////////////////////////////// Sub SetSense() Disable Aci Cls: Lcd"ShotSens" ShowSense() NoKey() PressedKey=0: m=0 Do PressedKey=Key() If PressedKey Then Select Case PressedKey Case T3 ' up If Sense Then Reset Sense Else Set Sense End If ShowSense() Case T2 ' down If Sense Then Reset Sense Else Set Sense End If ShowSense() Case T1 ' start PressedKey=0 Cls: Lcd "OK" Exit Sub End Select NoKey() PressedKey=0 CheckForPowerOFF() End If 'Reset WatchDog Loop End Sub '/////////////////////////////////////////////////////// Sub ShowSense() Locate 2,1 If Sense Then Lcd "High" WriteEE(1,1) Else Lcd "Low " WriteEE(1,0) End If End Sub '/////////////////////////////////////////////////////// Sub SaveData() Local i As Word, j As Word Disable Interrupts j=10 WriteEE(0, 0) WriteEE(5,Adr) WriteEE(7, m) For i=&h60 To EndOfBuf WriteEE(j, Peek(i)) Incr j Next End Sub '/////////////////////////////////////////////////////// Sub LoadData() ' loads last string and Local i As Word, j As Word ' settings from EE j=10 ParTime=ReadEE(3) If ParTime=&hff Then ParTime=10 WriteEE(3, 10) WriteEE(4, 0) WaitMs 10 End If IsParTime=ReadEE(4) adr=ReadEE(5): tmp=adr m=ReadEE(7): n=m For i=&h60 To EndOfBuf Poke(i, ReadEE(j)) Incr j Next OldTime=Peek(adr-2) If n=1 Then OldTime=0 Cls If m And m<255 Then If IsParTime Then Lcd "P:" Else Lcd "N:" End If ReadData() Else Lcd "OK" End If End Sub '/////////////////////////////////////////////////////// Sub Init() 'Set DDRB.1 Set Power: Set DDRC.5: Set PORTD.3: Set DDRD.3 '////********//// Reset Shoot: Reset IPT Start Adc Compare1A=&h9000 ' 10ms on 3...MHz Aci Rising Set PORTB.3: Set PORTB.4: Set PORTB.5 WaitMs 250 Sound 60,4 Cls Lcd "Bojan I." Time=0 For n=0 To 99 ' averaging voltage Time=Time+Adc8(4) Next Stop Adc Time=Time/249-66 m=Time If m>8 Then m=8 If m=0 Or m>100 Then Wait 1 Locate 2,1 Lcd "Change " Wait 1 Locate 2,1 Lcd "Batery " Wait 1 Locate 2,1 Lcd "Change " Wait 1 Locate 2,1 Lcd "Batery " Wait 2 Else Locate 2,1 For n=1 To m Lcd Chr(&hff) Next End If 'Lcd "Mr.Timer" 'Locate 2,1: Lcd "Bat:"; Time; "V" Time=0: m=0: TimeOut=0: PressedKey=0 If ReadEE(1) Then Set Sense Else Reset Sense End If Sound 50,4 Wait 3 LoadData() Start Timer1 Enable Oc1A Enable Interrupts Start Ac End Sub