'****************************************************************
'*  Author  : Coenen Stijn [Stynus]                             *
'*  Notice  : Copyright (c) 2008 ElektronicaStynus.be           *
'*          : All Rights Reserved                               *
'*  Date    : 17/07/2008                                        *
'*  Version : 1.8                                               *
'*  Notes   : Teller                                            *
'****************************************************************
Device  16F628A
Config WDT_OFF, PWRTE_OFF, MCLRE_OFF, INTRC_OSC_NOCLKOUT, LVP_OFF, BODEN_ON 
ALL_DIGITAL         =   true  
'****************************************************************         
Symbol  Cu          =   PORTA.0
Symbol  Cd          =   PORTA.1
Symbol  Cu10        =   PORTA.2
Symbol  Cd10        =   PORTA.3
Symbol  noPower     =   PORTA.4
Symbol  reset       =   PORTA.5
Symbol  digit2      =   PORTA.6
Symbol  digit3      =   PORTA.7
Symbol  digita      =   PORTB.0
Symbol  display     =   PORTB
Symbol  digit1      =   PORTB.7
'****************************************************************
        TRISA       =   %00111111
        TRISB       =   %00000000
'**************************************************************** 
Dim     CuBit       As  Bit
Dim     CdBit       As  Bit
Dim     Cu10Bit     As  Bit
Dim     Cd10Bit     As  Bit
Dim     index       As  Byte
Symbol  aantal      = 2
Dim     waarde      As  Byte
Dim     waarde100   As  Byte
Dim     waarde10    As  Byte
Dim     waarde1     As  Byte
'**************************************************************** 
'Init
Clear
DelayMS 1000
'Oude tellerstand terug inlezen
waarde1   =   ERead 0 
waarde10  =   ERead 1
waarde100 =   ERead 2

DelayMS 1000
'****************************************************************
While 1 = 1
'************************************************************
    'Knoppen inlezen
    'Count up
    If Cu = 0 And CuBit = 0 Then
        Inc waarde1
        If waarde1 = 10 Then
            waarde1 = 0
            Inc waarde10
            If waarde10 = 10 Then
                waarde10 = 0
                Inc waarde100
                If waarde100 = 10 Then
                    waarde100 = 0
                EndIf            
            EndIf            
        EndIf
        CuBit = 1
    EndIf
    If Cu = 1 And CuBit = 1 Then
        CuBit = 0
    EndIf
    GoSub spanningweg
    'Count down
    If Cd = 0 And CdBit = 0 Then
        Dec waarde1
        If waarde1 = 255 Then
            waarde1 = 9
            Dec waarde10
            If waarde10 = 255 Then
                waarde10 = 9
                Dec waarde100
                If waarde100 = 255 Then
                    waarde100 = 9
                EndIf            
            EndIf            
        EndIf
        CdBit = 1
    EndIf
    If Cd = 1 And CdBit = 1 Then
        CdBit = 0
    EndIf
    GoSub spanningweg
    'Count up + 10
    If Cu10 = 0 And Cu10Bit = 0 Then
        Inc waarde10
        If waarde10 = 10 Then
            waarde10 = 0
            Inc waarde100
            If waarde100 = 10 Then
                waarde100 = 0
            EndIf            
        EndIf       
        Cu10Bit = 1
    EndIf
    If Cu10 = 1 And Cu10Bit = 1 Then
        Cu10Bit = 0
    EndIf
    GoSub spanningweg
    'Count down - 10
    If Cd10 = 0 And Cd10Bit = 0 Then
        Dec waarde10
        If waarde10 = 255 Then
            waarde10 = 9
            Dec waarde100
            If waarde100 = 255 Then
                waarde100 = 9
            EndIf            
        EndIf          
        Cd10Bit = 1
    EndIf
    If Cd10 = 1 And Cd10Bit = 1 Then
        Cd10Bit = 0
    EndIf
    GoSub spanningweg
    'Controleren op reset
    If reset = 0 Then
        waarde1   = 0
        waarde10  = 0
        waarde100 = 0
    EndIf    
    '************************************************************ 
    GoSub dispuit   
Wend
'**************************************************************** 
'Subroutines
dispuit:
    For index = 0 To aantal  
        Select index
            Case 0
                waarde  = waarde1
            Case 1
                waarde  = waarde10          
            Case 2
                waarde  = waarde100          
        EndSelect   
        Select Case waarde    
        Case 0          'gfedcba
            display =  %00111111    
        Case 1          'gfedcba
            display =  %00000110              
        Case 2          'gfedcba
            display =  %01011011              
        Case 3          'gfedcba
            display =  %01001111              
        Case 4          'gfedcba
            display =  %01100110              
        Case 5          'gfedcba
            display =  %01101101              
        Case 6          'gfedcba
            display =  %01111101             
        Case 7          'gfedcba
            display =  %00000111              
        Case 8          'gfedcba
            display =  %01111111             
        Case 9          'gfedcba
            display =  %01101111  
        'Bij fout
        Case Else       'gfedcba
            display =  %01111001     
        EndSelect
        Select index
            Case 0
                High    digit1
            Case 1
                High    digit2            
            Case 2
                High    digit3            
        EndSelect 
        DelayMS 1
        GoSub spanningweg
        Low digit1
        Low digit2
        Low digit3
    Next
Return
spanningweg:
    'Controleren op voedingsspanning
    If noPower = 0 Then
        EWrite 0 , [waarde1]
        DelayMS 20
        EWrite 1 , [waarde10]
        DelayMS 20
        EWrite 2 , [waarde100]
        While noPower = 0
            DelayMS 1000
        Wend
    EndIf
Return
'**************************************************************** 
End