'-------------------------------------------------------------- ' 200311 Program to control a 2.5GHz counter ' Charlos Potma, PA3CKR '-------------------------------------------------------------- '$regfile = "8535def.dat" $crystal = 10000000 'timing is depending on this clock frequency Dim Lastbit As Bit Dim Freq As Long At $80 'holds resulting count Dim Freqzero As Byte At $83 Overlay 'always zero Dim Freqmsb As Byte At $82 Overlay 'leftmost count byte Dim Freq2sb As Byte At $81 Overlay 'middle count byte Dim Freqlsb As Byte At $80 Overlay 'rightmost count byte Dim Freqtmp As Long 'used in freq calc Dim Gatetimectr As Long 'gate time counter Dim Freqstr As String * 10 'frequency string Dim Freqstrfmt As String * 10 'frequency format string Dim Loglvlstr As String * 5 'log-level string Dim Loglvlstrfmt As String * 5 'log-level format string Dim Logadc As Word 'adc value for log-level Dim Loglvl As Single 'log-level float value Dim Loglvlint As Integer 'log-level integer value Dim Logbar As Single 'log-bar float value Dim Logbarint As Integer 'log-bar integer value Dim Lognumboxes As Integer 'number of boxes in log-bar Dim Logboxnumber As Byte 'box counter Dim Logbarremainder As Byte 'partial box Dim Levelok As Bit 'log-level ok or too low/high Const Gatetimeconst = 21332 'count to this value to get 128msec gate time Const Logafactor = 0.06 'a and b factor to calc. log-level Const Logbfactor = -62.3 'from logadc value Const Logbarafactor = 1.33 'a and b factor to calc log-bar Const Logbarbfactor = 53.33 'from log-level Const Lowlevel = -40 'low log-level limit to display frequency Const Highlevel = -10 'high log-level limit to display frequency Deflcdchar 0 , 14 , 10 , 14 , 21 , 14 , 10 , 17 , 17 'jumping man-a Deflcdchar 1 , 14 , 10 , 21 , 21 , 14 , 10 , 10 , 17 'jumping man-b Deflcdchar 2 , 32 , 32 , 32 , 32 , 32 , 32 , 32 , 32 'empty box Deflcdchar 3 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 0 '1/5 box Deflcdchar 4 , 24 , 24 , 24 , 24 , 24 , 24 , 24 , 0 '2/5 box Deflcdchar 5 , 28 , 28 , 28 , 28 , 28 , 28 , 28 , 0 '3/5 box Deflcdchar 6 , 30 , 30 , 30 , 30 , 30 , 30 , 30 , 0 '4/5 box Deflcdchar 7 , 31 , 31 , 31 , 31 , 31 , 31 , 31 , 0 'full box Cls Cgate Alias Portc.0 'counter gate pin Cflush Alias Portc.1 'flush gate pin Creset Alias Portc.2 'reset pin Loglevel Alias Porta.0 'log-level adc pin Presclr Alias Portc.3 'prescaler fet switch pin Extclk Alias Pind.4 'high when on external clock Intclk Alias Pind.5 'low when on external clock Config Porta = Input 'porta all input Config Portc = Output 'portc all output Config Portd = Input 'portd all input Config Adc = Single , Prescaler = Auto 'adc config Start Adc Config Timer0 = Counter , Edge = Falling 'timer0 config Reset Creset 'reset reset Reset Cgate 'close counter gate Reset Cflush 'close flush gate Reset Presclr 'prescalers off Levelok = 0 Cursor Off 'no lcd cursor Gosub Splash Main: 'switch on prescaler Set Presclr 'wait for prescaler to stabilise Waitms 100 'use gate order: set cgate / set cflush / reset cflush / reset cgate 'to prevent gate switching to cause extraneous counts in the 4020 Locate 1 , 1 Lcd Chr(0) 'jumping man-a Set Creset 'reset 4020 Reset Creset Timer0 = 0 'clear timer0 value Set Cgate 'open count gate, now counting... Set Cflush 'open flush gate For Gatetimectr = 0 To Gatetimeconst 'wait count time Next Gatetimectr Reset Cflush 'close flush gate Reset Cgate 'close count gate, counting ends Freqmsb = Timer0 Timer0 = 0 'clear timer Freqtmp = 0 'reset temp freq Locate 1 , 1 Lcd Chr(1) 'jumping man-b Reset Presclr 'prescaler off Waitms 100 'wait a while Logadc = Getadc(0) 'get log-level value Flush: Set Cflush 'pulse flush gate Reset Cflush Freqlsb = Timer0 'read timer0 value If Freqlsb = 0 Then 'and as long as it is zero, Incr Freqtmp 'the external counter has not Goto Flush 'yet overflowed, so increment Else 'temp freq Goto Endflush End If Endflush: 'now it has overflowed Freqtmp = 16383 - Freqtmp 'remainder is content of 4020 Freq = Freqmsb 'pack the bytes in freq Shift Freq , Left , 14 Freq = Freq + Freqtmp Upperline Locate 1 , 2 If Extclk = 1 Then 'indicate clock type Lcd "E" Else Lcd "I" End If Locate 1 , 3 If Levelok = 1 Then 'if level is ok,display frequency Freqstr = Str(freq) Freqstrfmt = Format(freqstr , " 0.000000") 'according to this format Lcd Freqstrfmt Else Lcd " -.------" 'else display empty string End If Locate 1 , 13 Lcd " GHz" Lowerline Loglvl = Logadc 'calculate actual log-level Loglvl = Logafactor * Loglvl 'from adc value read Loglvl = Loglvl + Logbfactor 'and corresponding a and b factors Loglvl = Round(loglvl) 'round to nearest whole number Loglvlint = Int(loglvl) 'and get integer value If Loglvlint < Lowlevel Then 'indicate if level too low Levelok = 0 Lcd "Input < " ; Lowlevel ; " dBm" Elseif Loglvlint > Highlevel Then 'or too high Levelok = 0 Lcd "Input > " ; Highlevel ; " dBm" Else Levelok = 1 'if level is ok freq can be displayed Logbar = Loglvlint 'calculate length of log-bar Logbar = Logbarafactor * Logbar 'from log-level and Logbar = Logbar + Logbarbfactor 'corresponding a and b factors Logbarint = Int(logbar) 'and get integer value Lognumboxes = Logbarint / 5 'calc number of boxes in bar Logbarremainder = 5 * Lognumboxes 'and determine remainder Logbarremainder = Logbarint - Logbarremainder For Logboxnumber = 1 To Lognumboxes 'number of boxes to lcd Lcd Chr(7) Next Logboxnumber Logbarremainder = 2 + Logbarremainder 'make remainder point to Lcd Chr(logbarremainder) 'correct lcd char and output char to lcd Logbarremainder = 8 - Lognumboxes 'calc number of spaces to go to lcd For Logboxnumber = 1 To Logbarremainder 'and output them Lcd " " Next Logbarremainder Loglvlstr = Str(loglvlint) 'display log-level Loglvlstrfmt = Format(loglvlstr , "-00") 'according to this format Lcd Loglvlstrfmt Lcd " dBm" End If Goto Main End Splash: Cls Upperline Lcd "PA3CKR 20031110" 'display call/date Lowerline Lcd "rfcounter11c" 'and version Waitms 3000 'for a while Cls Return