define external user library $lib “bcd.lbx” ‘library function must be defined for proper use’ Declare Function Makebcd(binval As Byte) As Byte ‘multiplex display routine’ Declare Sub Multiplex Declare Sub Sethour Declare Sub Setmin Dim A As Byte Dim Temp As Byte Dim Ticks As Byte Dim Seconds As Byte Dim Minutes As Byte Dim Hours As Byte ‘twelve or twenty-four hour clock’ Dim Twelve As Bit ‘is it odd or even’ Dim Module As Byte ‘BCD value of seconds, minutes and seconds’ Dim Secs As Byte Dim Mins As Byte Dim Hrs As Byte ‘set first three bits of port D as output, all others as input’ Ddrd = &B0000_0111 ‘set all bits of port B as output’ Ddrb = &B1111_1111 ‘Set bit 4 and bit 5 of port D high’ Set Pind.4 Set Pind.5 ‘set time and bcd values’ A = 0 Ticks = 0 Seconds = 55 Secs = Makebcd(seconds) Minutes = 59 Mins = Makebcd(minutes) Hours = 11 Hrs = Makebcd(hours) ‘Twelve hour clock’ Twelve = 1 ‘Set up Timer0 as a timer and set when the interrupt is generated’ ‘The timer0 is prescaled to 256, this means that an interrupt is generated every 256 * 256’ ‘cycles. Since the crystal runs at 4.096 Mhz that means that in one second there are’ ‘62.5 interrupts (4096000/65536). This number is used as the basis for the clock but must be’ ‘massaged over a slightly longer timescale (125 interrupts or 2*62.5)to get an accurate clock.’ Config Timer0 = Timer , Prescale = 256 ‘Enable the interrupts’ Enable Interrupts Enable Ovf0 ‘when the interrupt is generated go to Count subroutine ‘ On Ovf0 Count Do ‘if button, on pin 4 of portd, is pressed then wait 5 milliseconds ( button debounce )’ If Pind.4 = 0 Then Waitms 5 ‘if button still pressed call sethour subroutine’ If Pind.4 = 0 Then Call Sethour End If End If if button, on pin 5 of portd, is pressed then wait 5 milliseconds ( button debounce )’ If Pind.5 = 0 Then Waitms 5 ‘if button still pressed call setminute subroutine’ If Pind.5 = 0 Then Call Setmin End If End If Loop ‘Main timing subroutine’ Count: ‘This is where we average the number to count to exactly 125 interrupts over two seconds’ ‘We do this by finding if the second is odd or even’ ‘If the second is even then there are 62 ticks to a second’ ‘If the second is odd then there are 63 ticks to the second’ ‘Module is equal to 0 if even and 1 if odd’ Module = Seconds And 1 ‘ticks incremented’ Incr Ticks ‘Call multiplex display routine, display routine is called every time an interrupt is generated’ Call Multiplex ‘it must be odd if it got to 63, no need to check’ If Ticks = 63 Then Ticks = 0 End If ‘If module = 0 then this is an even second, so it has 62 ticks’ If Module = 0 Then If Ticks = 62 Then Ticks = 0 End If End If ‘update seconds If Ticks = 0 Then Incr Seconds If Seconds = 60 Then Seconds = 0 Incr Minutes If Minutes = 60 Then Minutes = 0 Incr Hours ‘If twelve =1 then this is a 12 hour clock, doesn’t do much now ‘ ‘but is here in case someone wants a 24 clock’ If Twelve = 1 Then If Hours = 13 Then Hours = 1 End If Else If Hours = 24 Then Hours = 0 End If End If End If End If End If Return End Sub Multiplex Incr A If A = 2 Then A = 1 If A = 1 Then Temp = Seconds ‘Secs is the BCD equivalent of seconds, but we leave seconds alone’ Secs = Makebcd(temp) ‘Secs is sent to portb’ Portb = Secs ‘turn on the seconds nixies by setting bit 0 of port d high’ Set Portd.0 ‘Keep the display on for a bit, this is only about 5 milliseconds’ ‘as the implementation of this timing loop is inexact’ Waitms 5 Temp = Minutes ‘Mins is the BCD value of minutes’ Mins = Makebcd (temp) ‘Turn off the seconds nixies by resetting bit 0 of portd’ Reset Portd.0 ‘Mins is sent to port b’ Portb = Mins ‘turn on the minutes nixies by setting bit 1 of portd high’ Set Portd.1 ‘Keep the display on for a bit, this is only about 5 milliseconds’ ‘as the implementation of this timing loop is inexact’ Waitms 5 Temp = Hours ‘Hrs is the BCD value of hours’ Hrs = Makebcd(temp) ‘Turn off the minutes nixies by resetting bit 1 of portd’ Reset Portd.1 ‘Hrs is sent to portb’ Portb = Hrs ‘turn on the hours nixies by setting bit 2 of portd high’ Set Portd.2 ‘Keep the display on for a bit, this is only about 5 milliseconds’ ‘as the implementation of this timing loop is inexact’ Waitms 5 ‘turn off hours nixies’ Reset Portd.2 End If End Sub Sub Sethour ‘Hours increased by one’ Incr Hours ‘twelve hour clock’ If Hours = 13 Then Hours = 1 ‘put hours on portb’ Portb = Hours ‘return and display after next tick’ End If End Sub Sub Setmin ‘minutes increased by one’ Incr Minutes ‘make sure minutes stays within bounds’ If Minutes = 60 Then Minutes = 0 ‘put minutes on portb’ Portb = Minutes ‘return and display after next tick’ End If End Sub