'****************************************************************
'*  Name    : UV Curing Device                                  *
'*  Author  : Stijn Coenen (Stynus)                             *
'*  Notice  : Copyright (c) 2021                                *
'*          : All Rights Reserved                               *
'*  Date    : 26/04/2021                                        *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
Device = 16F877A
Config FOSC_HS, WDTE_OFF, PWRTE_OFF, BOREN_OFF, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, CP_OFF
Xtal    4
All_Digital = true
Declare PORTB_Pullups = 1

Dim teller          As Byte
Dim tempdigit[6]    As Byte  
Dim IntCounter      As Byte   
Dim Buttons         As Byte
Dim LEDS            As Byte
Dim MicroSeconds    As Word
Dim Seconds         As Byte
Dim SetValue        As Word
Dim TimerValue      As Word
Dim SetTime[3]      As Byte
'Dim CountTime[3]    As Byte
Dim CountDownOn     As Bit

Dim RotaryState As Byte
Clear   

EData $3B, $00
     
TRISA = $00
TRISB = %11110001
TRISC = %11111010
TRISD = $00
TRISE = %00000110

'7-segment display 
    Symbol Zero     = 0x3F
    Symbol One      = 0x06
    Symbol Twoo     = 0x5B
    Symbol Three    = 0x4F
    Symbol Fore     = 0x66
    Symbol Five     = 0x6D
    Symbol Six      = 0x7D
    Symbol Seven    = 0x07
    Symbol Eight    = 0x7F
    Symbol Nine     = 0x6F
    Symbol Digit1   = 0x01 
    Symbol Digit2   = 0x02
    Symbol Digit3   = 0x04
    Symbol Digit4   = 0x08
    Symbol DigitDP  = 0x10
                             
    Symbol DL9      = LEDS.3
    Symbol DL10     = LEDS.4
    Symbol ColonTop = LEDS.0
    Symbol ColonBot = LEDS.1 
    Symbol Deg      = LEDS.2
    
    Symbol Buzzer   = PORTA.5
    Symbol Relay    = PORTE.0
    
    'symbol Fan      = portb.1    'Rotating pin broken -> use fan pin
    Symbol TopLight = PORTB.2
   ' Symbol Rotating = PORTB.3    'Rotating pin broken -> use fan pin
    Symbol Rotating = PORTB.1     'Rotating pin broken -> use fan pin
    
    Symbol RotaryA  = PORTB.4
    Symbol RotaryB  = PORTB.5
    
    Symbol BtnStart = Buttons.4 
    Symbol BtnStop  = Buttons.0 
    Symbol BtnProgr = Buttons.1
    Symbol BtnLock  = Buttons.2
    Symbol BtnDisp  = Buttons.3
    
    Symbol TMR0IF   = INTCON.2
    Symbol PEIE     = INTCON.6      'Peripheral Interrupt Enable bit
    Symbol RBIE     = INTCON.3      'RB Port Change Interrupt Enable bit
    Symbol RBIF     = INTCON.0      'RB Port Change Interrupt Flag bit

'****************************************************************    

    On_Interrupt     GoTo TmrInt 
    GoTo Init

'****************************************************************    
TmrInt:
    Context Save
        If TMR0IF = 1 Then
            TMR0IF = 0     
            TMR0 = 5    'Normally 6, but because of oscilator error 5 
            Select IntCounter
                Case 0      
                    PORTA = Digit4     
                    PORTD = tempdigit[3]
                Case 1           
                    PORTA = Digit3      
                    PORTD = tempdigit[2]
                Case 2     
                    PORTA = Digit2     
                    PORTD = tempdigit[1]
                Case 3                                                                                         
                    PORTA = Digit1   
                    PORTD = tempdigit[0]   
                Case 4    
                    PORTC.0 = 1
                    PORTC.2 = 1                  
                    PORTA = DigitDP 
                    PORTD = LEDS       
                Case 5  
                    PORTC.0 = 0
                    PORTC.2 = 0            
                    PORTA = 0
                    PORTD = 0     
                    'Knoppen inlezen
                    TRISD = 0xF0
                    PORTD = 0b00000101
                    Nop
                    Buttons = Buttons & $F0
                    Buttons = Buttons | (PORTD >> 4)  
                    TRISD = 0                         
                    PORTD = 0                            
                Case 6                              
                    PORTA = 0
                    PORTD = 0       
                    'Knoppen inlezen
                    TRISD = 0xF0
                    PORTD = 0b00001010
                    Nop            
                    Buttons = Buttons & $0F
                    Buttons = Buttons | (PORTD & $F0) 
                    TRISD = 0
                    PORTD = 0
                'Case 7
                    'Rotary encoder
                    If CountDownOn = 0 Then
                        If RotaryState.7 = 0 And RotaryA = 1 Then
                            RotaryState.7 = 1 
                            If RotaryA = 1 And RotaryB = 0 Then           
                             If SetValue < 60*99 Then 
                                    Inc SetValue 
                                End If 
                             End If
                             If RotaryA = 1 And RotaryB = 1 Then
                                If SetValue > 1 Then 
                                    Dec SetValue      
                                End If
                             End If
                        EndIf
                    EndIf
                    If PORTB.4 = 0 Then
                        RotaryState.7 = 0     
                    EndIf
            EndSelect
            
            If IntCounter >= 6 Then
                IntCounter = 0
            Else
                Inc IntCounter 
            EndIf
         
            Inc MicroSeconds
            If MicroSeconds > 499 Then
                MicroSeconds = 0
                '1 Second
                If CountDownOn = 1 Then
                    If Seconds < 58 Then
                        Inc Seconds
                    Else
                        Seconds = 0
                        If TimerValue > 0 Then
                            Dec TimerValue 
                        EndIf
                    EndIf
                EndIf                
            EndIf

        EndIf
    Context Restore  
    
'****************************************************************  
Init:
    OPTION_REG.5 = 0    'T0CS
    OPTION_REG.4 = 0    'T0SE
    OPTION_REG.3 = 0    'PSA
    OPTION_REG.2 = 0    'PS2
    OPTION_REG.1 = 1    'PS1
    OPTION_REG.0 = 0    'PS0

    INTCON = %10100000 			      'Interrupt opzetten
    
    PIE1    = 0
    PIR1    = 0
    PIE2    = 0
    PIR2    = 0
    Relay   = 0
    PORTB   = $0F 'Inverted
    
    
    '****************************************************************      
    For teller = 0 To 9
    
        tempdigit[5] = LRead Text_Table + teller
        Select tempdigit[5]
            Case " " 
                tempdigit[3] = $00 
            Case "-" 
                tempdigit[3] = $40    
            Case > 58  
                tempdigit[3] = LRead Font_Table - 65 + tempdigit[5]  
            Case Else   
                If tempdigit[5] > 47 And tempdigit[5] < 58 Then 
                    tempdigit[4] = tempdigit[5] - "0"
                    GoSub DigitOpzoeken
                    tempdigit[3] = tempdigit[4]
                EndIf 
        EndSelect
                 
        tempdigit[5] = LRead Text_Table + teller + 1
        Select tempdigit[5]
            Case " " 
                tempdigit[2] = $00 
            Case "-" 
                tempdigit[2] = $40    
            Case > 58  
                tempdigit[2] = LRead Font_Table - 65 + tempdigit[5]  
            Case Else   
                If tempdigit[5] > 47 And tempdigit[5] < 58 Then 
                    tempdigit[4] = tempdigit[5] - "0"
                    GoSub DigitOpzoeken
                    tempdigit[2] = tempdigit[4]
                EndIf 
        EndSelect
                
        tempdigit[5] = LRead Text_Table + teller + 2
        Select tempdigit[5]
            Case " " 
                tempdigit[1] = $00 
            Case "-" 
                tempdigit[1] = $40    
            Case > 58  
                tempdigit[1] = LRead Font_Table - 65 + tempdigit[5]  
            Case Else   
                If tempdigit[5] > 47 And tempdigit[5] < 58 Then 
                    tempdigit[4] = tempdigit[5] - "0"
                    GoSub DigitOpzoeken
                    tempdigit[1] = tempdigit[4]
                EndIf 
        EndSelect
                
        tempdigit[5] = LRead Text_Table + teller + 3
        Select tempdigit[5]
            Case " " 
                tempdigit[0] = $00 
            Case "-" 
                tempdigit[0] = $40    
            Case > 58  
                tempdigit[0] = LRead Font_Table - 65 + tempdigit[5]  
            Case Else   
                If tempdigit[5] > 47 And tempdigit[5] < 58 Then 
                    tempdigit[4] = tempdigit[5] - "0"
                    GoSub DigitOpzoeken
                    tempdigit[0] = tempdigit[4]
                EndIf 
        EndSelect   
                   
        DelayMS 400                      
    Next    
    DelayMS 750 

'****************************************************************         
main:    
SetValue = ERead 0  
While 1 = 1
            
    If CountDownOn = 1 Then  
        Toggle          ColonTop
        Toggle          ColonBot  
        SetTime[2] = TimerValue /  60    
        SetTime[1] = TimerValue // 60 
    Else       
        ColonTop = 1
        ColonBot = 1  
        SetTime[2] = SetValue /  60    
        SetTime[1] = SetValue // 60 
    EndIf
      
    tempdigit[4] = Dig SetTime[1], 0 
    GoSub DigitOpzoeken
    tempdigit[0] = tempdigit[4]
    tempdigit[4] = Dig SetTime[1], 1  
    GoSub DigitOpzoeken
    tempdigit[1] = tempdigit[4]
    tempdigit[4] = Dig SetTime[2], 0  
    GoSub DigitOpzoeken
    tempdigit[2] = tempdigit[4]
    tempdigit[4] = Dig SetTime[2], 1  
    GoSub DigitOpzoeken 
    tempdigit[3] = tempdigit[4]
    
      
    If BtnProgr = 1 Then
        DelayMS 200
        While BtnProgr = 1
        Wend                  
        EWrite 0, [SetValue]
    EndIf

     If BtnStart= 1 Then
        DelayMS 200
        While BtnStart = 1
        Wend
        Relay = 1
        TopLight = 0
        Rotating = 0
        TimerValue = SetValue 
        CountDownOn = 1        
    EndIf
             
    If BtnStop = 1 Then    
        DelayMS 200
        While BtnStop = 1
        Wend
        CountDownOn = 0 
        Relay = 0    
        TopLight = 1  
        Rotating = 1  
    EndIf
    
    If CountDownOn = 1 And TimerValue = 0 Then
        CountDownOn = 0
        Relay = 0      
        TopLight = 1         
        Rotating = 1            
        FreqOut Buzzer, 200, 500
        DelayMS 500                         
        FreqOut Buzzer, 200, 500   
    EndIf
    
    
  
    DelayMS 100

Wend
DigitOpzoeken:
    tempdigit[4] = LookUpL tempdigit[4], [Zero, One, Twoo, Three, Fore, Five, Six, Seven, Eight, Nine] 
Return
'****************************************************************
Font_Table:- CData As Byte $77, $7C, $39, $5E, $79, $71, $3D, $74, $30, $1E, $75, $38, $15, $37, $3F, $73, $67, $33, $6D, $78, $3E, $2E, $2A, $76, $6E, $4B, $00, $00, $00, $00, $08

                  '0123456789012345678901 
Text_Table: LData "E-STYNUS 2021"
End