Code:
	'   Pic Definition
    '   ==============
        ' Using PIC 16F874 @ 4MHZ and bootloader
        '
        DEFINE LOADER_USED 1
        DEFINE OSC 4        
        DEFINE LCD_DREG PORTB   ' Set LCD Data port        
        DEFINE LCD_DBIT 0       ' Set starting Data bit (0 or 4) if 4-bit bus        
        DEFINE LCD_RSREG PORTB  ' Set LCD Register Select port        
        DEFINE LCD_RSBIT 7      ' Set LCD Register Select bit         
        DEFINE LCD_RWREG PORTB  'LCD read/write port           
        DEFINE LCD_RWBIT 6      'LCD read/write bit          
        DEFINE LCD_EREG PORTB   ' Set LCD Enable port        
        DEFINE LCD_EBIT 5       ' Set LCD Enable bit        
        DEFINE LCD_BITS 4       ' Set LCD bus size (4 or 8 bits)        
        DEFINE LCD_LINES 2      ' Set number of lines on LCD        
        DEFINE LCD_COMMANDUS 2000' Set command delay time in us
        DEFINE LCD_DATAUS 50    ' Set data delay time in us
                
    '   Hardware configuration
    '   ======================
        '
        '
        TRISC  = %10000000    ' PORTC.7 is the RX input
                              ' PORTC.6 is the TX output
        On Interrupt Goto myint ' Define interrupt handler
        INTCON      =   %11000000            ' Enable INTE interrupt
        PIE1.5      =   1
        
        T2CON       =   %01111111
        PIE1.1      =   1
        
        ' LCD background
        LED             var     PORTD.3
        ' LCD
        LcdCls          CON     $01     ' clear the LCD
        LcdLine1        CON     $80     ' DDRAM address of line 1
        LcdLine2        CON     $C0     ' DDRAM address of line 2              
        ' Shaft Encoder
        clock           var     PORTD.0
        stat            var     PORTD.1
        ' ExecuteButton
        exec            var     PORTD.2
        
    '   Serial communication definition
    '   ===============================
        ' Using internal USART and MAX232 to interface to PC
        '
        'RCSTA = $90 
        DEFINE HSER_RCSTA 90h ' enable serial port, enable continuous receive
        'TXSTA = $20 
        define HSER_TXSTA 20h ' enable transmit, BRGH=1
        'SPBRG = 25                  
        define HSER_SPBRG 25 ' set baudrate to 2400   
        DEFINE HSER_CLOERR  1 ' automatic clear overrun error  
        
        T2400           CON     396
        Inverted        CON     $4000
        Baud            CON     T2400 '+ Inverted
        
        txPin           var     PORTC.6
        rxPin           Var     PORTC.7
    '   Alias definition
    '   ================
        '
        '
        RCIF VAR PIR1.5     ' Receive  interrupt flag (1=full , 0=empty)
        TXIF VAR PIR1.4     ' Transmit interrupt flag (1=empty, 0=full)
        OERR var RCSTA.1    ' Overrun error
        CREN var RCSTA.4    ' Continuous receive
        
        dump            var     byte 
        countPulses     VAR     Byte
        print           VAR     Bit
        a               VAR     Byte
        b               VAR     Byte
        c               var     byte
        idx             var     byte
        turnOff         VAR     Bit
        failSafe        VAR     Byte
        timer           var     word
              
    '   Hardware initialisation
    '   =======================
        '
        '
        pause 10 ' safe start-up delay
        countpulses = 1
        turnOff = 0
        print = 0
        high    led
        
Main:
        GOSUB   readpuls
        GOSUB   writeDisp
        GOSUB   readExec
        pause 10
    goto main
    
    '        '**************************
            '**************************
    writeDisp:
            IF (print) THEN
              SELECT case countPulses
    
              CASE 1
                 lcdout $FE,  LcdCls, "Start ALL Syst. "
              CASE 2
                 lcdout $FE,  LcdCls, "Main Sound Syst."
              CASE 3
                 lcdout $FE,  LcdCls, "Sub Sound Syst. "
              CASE 4
                 lcdout $FE,  LcdCls, "Main Disp System"
              CASE 5
                 lcdout $FE,  LcdCls, "Sub Disp System "
    
              CASE ELSE
                 lcdout $FE,  LcdCls, " Illigal value  "
              END SELECT
             
              Print = 0
            ENDIF
            RETURN
    
            '**************************
    
            '**************************
    readExec:
            IF (exec = 0) THEN
                  lcdout $FE,  LcdLine2, "  Starting Up   "
                    gosub set
                  GOSUB logOff
                  lcdout $FE,  LcdLine2, "  Shuting Down  "
                    gosub clr
                  lcdout $FE,  LcdLine2, "                "
        
            ENDIF
    
            RETURN
    
    '        '**************************
    
    '        '**************************
    readPuls:
            IF(clock = 0) THEN
              IF (stat) THEN
                countpulses = ((countpulses - 1)//5)+1'(countpulses - 1) MIN 1'((countpulses - 1)//5)
              ELSE
                countpulses = ((countpulses + 1)//5)+1'(countpulses + 1) MAX 5'((countpulses + 1)//5)
              ENDIF
              print = 1
              high led
              PIE1.0 = 1
              again:  IF clock = 0 THEN GOTO again
              
              high led
            ENDIF
            RETURN
        
    '        '**************************
    
    '        '**************************
    set:
                read countpulses*2, a
                read countpulses*2+1, b
                
                for idx = a to b
                  read idx,c
                  'SEROUT2 txPin, baud,[13,(c dig 1),"S",(c dig 0)+48,((13 + (c dig 1) + "S" + 48+(c dig 0))^$FF)+1]
                  hserout [13,(c dig 1),"S",(c dig 0)+48,((13 + (c dig 1) + "S" + 48+(c dig 0))^$FF)+1]
                  pause 100 
                  'SEROUT2 txPin, baud,[13,(c dig 1),"S",(c dig 0)+48,((13 + (c dig 1) + "S" + 48+(c dig 0))^$FF)+1]
                  hserout [13,(c dig 1),"S",(c dig 0)+48,((13 + (c dig 1) + "S" + 48+(c dig 0))^$FF)+1]
                  pause 1000
                next
             RETURN
             
    '        '**************************
    
    '        '**************************
    clr:
                read countpulses*2, a
                read countpulses*2+1, b
                
                for idx = b to a step -1
                  read idx,c
                  
                  if ((c dig 2)= 1) then
                    pause 5000
                  endif
                    
                  'SEROUT2 txPin, baud,[13,(c dig 1),"C",(c dig 0) + 48,((13 + (c dig 1) + "C" + 48 +(c dig 0))^$FF)+1]
                  hserout [13,(c dig 1),"C",(c dig 0) + 48,((13 + (c dig 1) + "C" + 48 +(c dig 0))^$FF)+1] 
                  pause 100
                  hserout [13,(c dig 1),"C",(c dig 0) + 48,((13 + (c dig 1) + "C" + 48 +(c dig 0))^$FF)+1]
                  'SEROUT2 txPin, baud,[13,(c dig 1),"C",(c dig 0) + 48,((13 + (c dig 1) + "C" + 48 +(c dig 0))^$FF)+1] 
                  pause 1000
                next
             RETURN
            
            '**************************
    
            '**************************
    
    logOff:
            lcdout $FE,  LcdLine2, " All systems GO "
            failsafe = 0
            turnOff = 0
    logOffloop:
            IF (exec = 0) THEN
               PAUSE 10
       loop1:  IF (exec = 0) THEN loop1
               IF turnOff THEN
                 RETURN
               ELSE
                 lcdout $FE,  LcdLine2, "OK, to turn OFF?"
                 PAUSE 200
                 failsafe = 0
                 turnOff = 1
               ENDIF
            ELSE
               failSafe = failSafe + 1
               IF failSafe > 10 THEN
                 turnOff = 0
                 lcdout $FE,  LcdLine2, " All systems GO "
                 PAUSE 10
                 failsafe = 0
               ENDIF
    
            ENDIF
            PAUSE 100
            GOTO logOffLoop
        
    
    ' Interrupt handler
        Disable                 ' No interrupts past this point
myint:  if RCIF = 1 then
            hserin[WAIT($01),a]
            b = 0
            while a = $02
                hserin[a]
                write b, a
                b = b + 1
            wend
            
            While RCif         ' Code until I write the right rutine
                Dump=RCREG
            WEND
            
            lcdout $FE, LcdLine2, " Connected "
        else
            timer = timer+1
            if timer > $FFFE then
                low led
                PIE1.1 = 0
            endif
            
        endif
        Resume                  ' Return to main program
        Enable
 
				
Bookmarks