'-------------------------------------------------------------------------------
Option Explicit

' Hardware interrupt demonstration.
'
' This program has 2 tasks. One task outputs a pulse count to the Com1
' serial port. The output occurs once per second.
'
' The other task is blocked on system call WaitForInterrupt. Whenever an
' interrupt occurs pin on INT1, the pulse count is incremented, and an LED
' blinks for visual feedback. The interrupt occurs on a rising edge.
'
' INT1 pin number:
'
'    BX-01 => 13
'    BX-24 => 11
'    BX-35 => 17

Private Const StackSize As Integer = 25
Private PulseCountStack(1 To StackSize) As Byte
Private PulseCount As Integer
'-------------------------------------------------------------------------------
Public Sub Main()

    Call InitializeBX24

    PulseCount = 0

    Debug.Print
    Debug.Print "Pulse counting demonstration"
    Debug.Print

    CallTask "PulseCountTask", PulseCountStack

    Do
        Debug.Print CStr(PulseCount)
        Call Delay(1.0)
    Loop

End Sub
'-------------------------------------------------------------------------------
Private Sub PulseCountTask()

    '>>Const LEDpin As Byte = 17   ' BX-01
    '>>Const LEDon  As Byte = 1    ' BX-01
    '>>Const LEDoff As Byte = 0    ' BX-01

    Const LEDpin As Byte = 25   ' BX-24 (red LED)
    Const LEDon  As Byte = 0    ' BX-24
    Const LEDoff As Byte = 1    ' BX-24

    '>>Const LEDpin As Byte = 20   ' BX-35
    '>>Const LEDon  As Byte = 1    ' BX-35
    '>>Const LEDoff As Byte = 0    ' BX-35

    Do
        Call WaitForInterrupt(bxPinRisingEdge)
        PulseCount = PulseCount + 1

        Call PutPin(LEDpin, LEDon)

        ' Debounce.
        Call Sleep(0.1)

        Call PutPin(LEDpin, LEDoff)
    Loop        

End Sub
'-------------------------------------------------------------------------------
Private Sub InitializeBX24()

' BX-24 only -- configure INT0 as input-pullup in order to prevent it from
' causing an unwanted interrupt. INT0 is (internal) pin 11 on the 8535 chip,
' and is not connected to an external pin.

    Register.DDRD  = Register.DDRD And bx1111_1011
    Register.PORTD = Register.PORTD Or bx0000_0100
     
End Sub
'-------------------------------------------------------------------------------
