'////////////////////////////////////////////////////////// '/// FastAVR Basic Compiler for AVR by MICRODESIGN /// '/// Mooving LED for STK-200, 500 /// '////////////////////////////////////////////////////////// $Device = 2313 ' used device $Stack = 60 ' stack depth $Clock = 10 ' used crystal $Timer1 = Timer, Prescale=1, CompareA=DisConnect, Clear $Lcd = PORTB.0, Rs=PORTB.5, En=PORTB.4, 20, 4 $LeadChar = " ", Format(4.1) $Def Trg555=PORTD.3 $Def MegaP =DDRD.4 $Def KiloP =DDRD.5 $Def Mega =PORTD.4 $Def Kilo =PORTD.5 $Def Res555=PORTD.6 Declare Interrupt Oc1() Declare Interrupt Int0() Declare Sub Init() Declare Sub WaitForC() Declare Sub ShowResult() Declare Sub SetMode0() Dim Range As Byte, StartTimer1 As Byte, Prescal As Byte Dim Time As Word, Average As Word, Cap As Word Dim Units As String*2 Dim OK As Bit, TimeOut As Bit Dim tmp As Long, tmp1 As Long Const Prescale_1 = 9 Const Prescale_8 =10 Const Prescale_64=11 Init() Do Enable Oc1 Reset Trg555 ' Trg555 555 Timer1=0 Set Trg555 TCCR1B=StartTimer1 ' start Timer1 Reset Int0: Enable Int0 While Not OK And Not TimeOut: Wend Disable Int0: Disable Oc1 If OK Then ' end of pulse Time=Timer1 ' read timer Select Case Range Case 0 ' pF If Time<1250 Then Average=Time ' parasite Cap Prescal=1 ' because prescale=1 Case 1 If Time<156 Then SetMode0() Prescal=8 ' because prescale=8 Case 2 If Time<20 Then SetMode0() Prescal=64 Case 3 If Time<1250 Then SetMode0() Prescal=1 Case 4 If Time<156 Then SetMode0() Prescal=8 Case 5 If Time<20 Then SetMode0() Prescal=64 Case Else SetMode0() End Select Time=Time-Average/Prescal ' adjust for parasite Cap ShowResult() Reset OK End If If TimeOut Then ' pulse is too long Reset Res555 Incr Range ' change Range (prescale Set Res555 Select Case Range ' or R) Case 0 SetMode0() ' set Range=0 again Case 1 StartTimer1=Prescale_8 Set Mega: Set MegaP ' mega is active Reset Kilo: Reset KiloP ' kilo is inactive Compare1A=&h61a8 Case 2 StartTimer1=Prescale_64 Set Mega: Set MegaP ' mega is active Reset Kilo: Reset KiloP ' kilo is inactive Compare1A=&hac00 ' Compare1A=&h9a12 Case 3 StartTimer1=Prescale_1 Reset Mega: Reset MegaP ' mega is inactive Set Kilo: Set KiloP ' kilo is active Compare1A=&h4e20 Case 4 StartTimer1=Prescale_8 Reset Mega: Reset MegaP ' mega is inactive Set Kilo: Set KiloP ' kilo is active Compare1A=&h61a8 Case 5 StartTimer1=Prescale_64 Reset Mega: Reset MegaP ' mega is inactive Set Kilo: Set KiloP ' kilo is active Compare1A=&h7a12 Case Else SetMode0() End Select ShowResult() Reset TimeOut End If Loop '////////////////////////////////////////////////////////// Sub SetMode0() StartTimer1=Prescale_1: Range=0 Set Mega: Set MegaP ' mega is active Reset Kilo: Reset KiloP ' mega is active Compare1A=&h4e20 End Sub '////////////////////////////////////////////////////////// Interrupt Int0() ', Save 1 Stop Timer1 Set OK End Interrupt '////////////////////////////////////////////////////////// Interrupt Oc1() ', Save 1 Stop Timer1 Set TimeOut End Interrupt '////////////////////////////////////////////////////////// Sub ShowResult() Format(3.2) tmp=Time*4000 Select Case Range Case 0 Format(4.1) tmp1=4400 Units="pF" Case 1 tmp1=55000 Units="nF" Case 2 tmp1=6875 Units="nF" Case 3 tmp1=440000 Units="uF" Case 4 tmp1=55000 Units="uF" Case 5 tmp1=6875 Units="uF" End Select Locate 2, 1 If Time Then If Range<6 Then tmp=tmp/tmp1 Cap=tmp Lcd Cap; Units Else Lcd"Too big!" End If Else Lcd"Insert C" End If End Sub '////////////////////////////////////////////////////////// Sub Init() Set Trg555: Set DDRD.3 ' Trg555 is output Set PORTD.4: Set Mega ' mega is active Reset OK: Reset TimeOut Set Res555: Set DDRD.6 SetMode0() Int0 Falling Enable Int0 Enable Interrupts Start Timer1 Lcd"Cx meter" End Sub