' firmware für "3-achs positionsanzeige für digitale messschieber"
' version 29.12.2013
' malte t. ahlers 2013
' infos auf http://www.malteahlers.de
'
$regfile = "m168def.dat"
$crystal = 8000000
$framesize = 64
$swstack = 64
$hwstack = 64
$lib "glcdKS108.lib"
'
Config Graphlcd = 128 * 64sed , Dataport = Portb , Controlport = Portd , Ce = 0 , Ce2 = 1 , Cd = 7 , Rd = 6 , Reset = 4 , Enable = 5
Setfont Font8x8
'
Config Timer0 = Timer , Prescale = 64
Config Timer1 = Timer , Prescale = 64
Config Timer2 = Timer , Prescale = 64
'
'PC0 = dat3
Config Pinc.= Input
'PC1 = dat2
Config Pinc.= Input
'PC2 = dat1
Config Pinc.= Input
'PC3 = clk3
Config Pinc.= Input
'PD2 = int0 = clk1
Config Pind.= Input
'PD3 = int1 = clk2
Config Pind.= Input
'
On Int0 Int0_isr , Nosave
Config Int0 = Rising
On Int1 Int1_isr , Nosave
Config Int1 = Rising
Pcmsk1 = &B0001000
On Pcint1 Pcint11_isr , Nosave
On Timer0 Tmr0_isr
On Timer1 Tmr1_isr
On Timer2 Tmr2_isr
'
Dim Val1 As Long
Dim Val2 As Long
Dim Val3 As Long
'
Dim Valn_out(3) As Long
Dim Val1_out As Long At Valn_out(1) Overlay
Dim Val2_out As Long At Valn_out(2) Overlay
Dim Val3_out As Long At Valn_out(3) Overlay
'
Dim Val1_msk As Long
Dim Val2_msk As Long
Dim Val3_msk As Long
'
Dim Valn_unit(3) As Byte
Dim Val1_unit As Byte At Valn_unit(1) Overlay               ' 0 -> mm; 1 -> inch
Dim Val2_unit As Byte At Valn_unit(2) Overlay
Dim Val3_unit As Byte At Valn_unit(3) Overlay
'
Dim Val1_timeout As Byte
Dim Val2_timeout As Byte
Dim Val3_timeout As Byte
'
Dim Val_nr As Byte
Dim Val_valid As Byte
Dim New_val As Byte
'
Dim V1 As Dword
Dim V2 As Dword
Dim V3 As Dword
'
Dim Vtemp As Dword
'
Dim I As Byte
'
Dim B As Dword
Dim X As Byte
Dim Y As Byte
'
Dim T As Word
'
Declare Sub Showdata(As Byte , Y As Byte , B As Dword)
'
Cls
Lcdat 2 , 1 , "X             mm"
Lcdat 5 , 1 , "Y             mm"
Lcdat 8 , 1 , "Z             mm"
Line(, 19) -(126 , 19) , 255
Line(, 44) -(126 , 44) , 255
Boxfill(74 , 11) -(77 , 14) , 255
Boxfill(74 , 35) -(77 , 38) , 255
Boxfill(74 , 59) -(77 , 62) , 255
'
Enable Timer0
Enable Timer1
Enable Timer2
Enable Int0
Enable Int1
Enable Pcint1
Enable Interrupts
'
Start Timer0
Timer1 = 65280
Start Timer1
Start Timer2
'
Val1_msk = 1
Val2_msk = 1
Val3_msk = 1
'
Do
   For Val_nr = 0 To 2
      If New_val.val_nr = 1 Then
         New_val.val_nr = 0
         Y = Val_nr * 24
         If Val_valid.val_nr = 1 And Valn_unit(val_nr + 1) = 0 Then
            If Valn_out(val_nr + 1).20 = 1 Then
               Valn_out(val_nr + 1).20 = 0
               Showpic 8 , Y , Mns
            Else
               If Valn_out(val_nr + 1) = 0 Then
                  Showpic 8 , Y , Spcs
               Else
                  Showpic 8 , Y , Pls
               End If
            End If
            T = 10000
            X = 23
            Vtemp = Valn_out(val_nr + 1)
            For I = 1 To 5
               B = Vtemp / T
               Call Showdata(, Y , B)
               Vtemp = Vtemp Mod T
               T = T / 10
               If I = 3 Then
                  X = X + 24
               Else
                  X = X + 16
               End If
            Next I
         Else
            Showpic 8 , Y , Mns
            Showpic 24 , Y , Mns
            Showpic 40 , Y , Mns
            Showpic 56 , Y , Mns
            Showpic 80 , Y , Mns
            Showpic 96 , Y , Mns
         End If
      End If
   Next
   Waitms 100
Loop
'
End
'
'
'
Pcint11_isr:
   $asm
      SBIS PINC, 3
      RJMP Pcint11_end
      '
      push R20
      push R21
      PUSH R22
      PUSH R23
      push R24
      push R25
      PUSH R26
      PUSH R27
      '
      IN R24, SREG
      PUSH R24
      '
      clr R24
      Out Tcnt0 , R24
      '
      LDS R20, {Val3}
      LDS R21, {Val3+1}
      LDS R22, {Val3+2}
      '
      LDS R24, {Val3_msk}
      LDS R25, {Val3_msk+1}
      LDS R26, {Val3_msk+2}
      '
      CPI R26, 128
      BREQ Pcint11_Msk_g_23
      '
   Pcint11_msk_k_23:
      '
      SBIC PINC, 0
      RJMP Pcint11_set_bit
   Pcint11_clear_bit:
      '
      mov R27, R24
      COM R27
      AND R20, R27
      '
      mov R27, R25
      COM R27
      AND R21, R27
      '
      mov R27, R26
      COM R27
      AND R22, R27
      '
      RJMP Pcint11_shift
   Pcint11_set_bit:
      '
      !OR R20, R24
      !OR R21, R25
      !OR R22, R26
   Pcint11_shift:
      clc
      rol R24
      ROL R25
      ROL R26
      '
      RJMP Pcint11_End_shiftbit
   Pcint11_msk_g_23:
      '
      CLR R26
      CLR R25
      LDI R24, 1
      '
      sts {val3_unit}, R26
      SBIC PINC, 0
      sts {val3_unit}, R24
      '
      sts {val3_timeout}, R26
      '
      LDS R27, {Val_valid}
      ORI R27, &B00000100
      STS {Val_valid}, R27
      '
      LDS R27, {new_Val}
      ORI R27, &B00000100
      STS {new_Val}, R27
      '
      STS {Val3_out}, R20
      STS {Val3_out+1}, R21
      STS {Val3_out+2}, R22
      '
   Pcint11_end_shiftbit:
      STS {Val3}, R20
      STS {Val3+1}, R21
      STS {Val3+2}, R22
      STS {Val3_msk}, R24
      STS {Val3_msk+1}, R25
      STS {Val3_msk+2}, R26
      '
      POP R24
      !OUT SREG, R24
      POP R27
      pop R26
      pop R25
      POP R24
      POP R23
      pop R22
      pop R21
      POP R20
      '
   Pcint11_end:
   $end Asm
Return
'
Int0_isr:
   $asm
      push R20
      push R21
      PUSH R22
      PUSH R23
      push R24
      push R25
      PUSH R26
      PUSH R27
      '
      IN R24, SREG
      PUSH R24
      '
      clr R24
      Out Tcnt1l , R24
      '
      LDS R20, {Val1}
      LDS R21, {Val1+1}
      LDS R22, {Val1+2}
      '
      LDS R24, {Val1_msk}
      LDS R25, {Val1_msk+1}
      LDS R26, {Val1_msk+2}
      '
      CPI R26, 128
      BREQ int0_Msk_g_23
      '
   Int0_msk_k_23:
      '
      SBIC PINC, 2
      RJMP int0_set_bit
   Int0_clear_bit:
      '
      mov R27, R24
      COM R27
      AND R20, R27
      '
      mov R27, R25
      COM R27
      AND R21, R27
      '
      mov R27, R26
      COM R27
      AND R22, R27
      '
      RJMP int0_shift
   Int0_set_bit:
      '
      !OR R20, R24
      !OR R21, R25
      !OR R22, R26
   Int0_shift:
      clc
      rol R24
      ROL R25
      ROL R26
      '
      RJMP int0_End_shiftbit
   Int0_msk_g_23:
      '
      CLR R26
      CLR R25
      LDI R24, 1
      '
      sts {val1_unit}, R26
      SBIC PINC, 2
      sts {val1_unit}, R24
      '
      sts {val1_timeout}, R26
      '
      LDS R27, {Val_valid}
      ORI R27, &B00000001
      STS {Val_valid}, R27
      '
      LDS R27, {new_Val}
      ORI R27, &B00000001
      STS {new_Val}, R27
      '
      STS {Val1_out}, R20
      STS {Val1_out+1}, R21
      STS {Val1_out+2}, R22
      '
   Int0_end_shiftbit:
      STS {Val1}, R20
      STS {Val1+1}, R21
      STS {Val1+2}, R22
      STS {Val1_msk}, R24
      STS {Val1_msk+1}, R25
      STS {Val1_msk+2}, R26
      '
      POP R24
      !OUT SREG, R24
      POP R27
      pop R26
      pop R25
      POP R24
      POP R23
      pop R22
      pop R21
      POP R20
   $end Asm
Return
'
Int1_isr:
   $asm
      push R20
      push R21
      PUSH R22
      PUSH R23
      push R24
      push R25
      PUSH R26
      PUSH R27
      IN R24, SREG
      PUSH R24
      '
      clr R24
      Out Tcnt2 , R24
      '
      LDS R20, {Val2}
      LDS R21, {Val2+1}
      LDS R22, {Val2+2}
      '
      LDS R24, {Val2_msk}
      LDS R25, {Val2_msk+1}
      LDS R26, {Val2_msk+2}
      '
      CPI R26, 128
      BREQ Int1_Msk_g_23
      '
   Int1_msk_k_23:

      SBIC PINC, 1
      RJMP Int1_set_bit
   Int1_clear_bit:
      '
      mov R27, R24
      COM R27
      AND R20, R27
      '
      mov R27, R25
      COM R27
      AND R21, R27
      '
      mov R27, R26
      COM R27
      AND R22, R27
      '
      RJMP int1_shift
   Int1_set_bit:
      '
      !OR R20, R24
      !OR R21, R25
      !OR R22, R26
   Int1_shift:
      clc
      rol R24
      ROL R25
      ROL R26
      '
      RJMP Int1_End_shiftbit
   Int1_msk_g_23:
      '
      CLR R26
      CLR R25
      LDI R24, 1
      '
      sts {val2_unit}, R26
      SBIC PINC, 1
      sts {val2_unit}, R24
      '
      sts {val2_timeout}, R26
      '
      LDS R27, {Val_valid}
      ORI R27, &B00000010
      STS {Val_valid}, R27
      '
      LDS R27, {new_Val}
      ORI R27, &B00000010
      STS {new_Val}, R27
      '
      STS {Val2_out}, R20
      STS {Val2_out+1}, R21
      STS {Val2_out+2}, R22
      '
   Int1_end_shiftbit:
      STS {Val2}, R20
      STS {Val2+1}, R21
      STS {Val2+2}, R22
      STS {Val2_msk}, R24
      STS {Val2_msk+1}, R25
      STS {Val2_msk+2}, R26
      '
      POP R24
      !OUT SREG, R24
      POP R27
      pop R26
      pop R25
      POP R24
      POP R23
      pop R22
      pop R21
      POP R20
   $end Asm
Return
'
Tmr0_isr:
   Val3_msk = 1
   If Val3_timeout < 60 Then
      Incr Val3_timeout
   Else
      Val_valid.= 0
      New_val.= 1
   End If
Return
'
Tmr1_isr:
   Timer1 = 65280
   Val1_msk = 1
   If Val1_timeout < 60 Then
      Incr Val1_timeout
   Else
      Val_valid.= 0
      New_val.= 1
   End If
Return
'
Tmr2_isr:
   Val2_msk = 1
   If Val2_timeout < 60 Then
      Incr Val2_timeout
   Else
      Val_valid.= 0
      New_val.= 1
   End If
Return
'
Sub Showdata(As Byte , Y As Byte , B As Dword)
   Select Case B
      Case 0 : Showpic X , Y , Gh0
      Case 1 : Showpic X , Y , Gh1
      Case 2 : Showpic X , Y , Gh2
      Case 3 : Showpic X , Y , Gh3
      Case 4 : Showpic X , Y , Gh4
      Case 5 : Showpic X , Y , Gh5
      Case 6 : Showpic X , Y , Gh6
      Case 7 : Showpic X , Y , Gh7
      Case 8 : Showpic X , Y , Gh8
      Case 9 : Showpic X , Y , Gh9
   End Select
End Sub
'
Gh0:
$bgf "pics\gh0.bgf"
Gh1:
$bgf "pics\gh1.bgf"
Gh2:
$bgf "pics\gh2.bgf"
Gh3:
$bgf "pics\gh3.bgf"
Gh4:
$bgf "pics\gh4.bgf"
Gh5:
$bgf "pics\gh5.bgf"
Gh6:
$bgf "pics\gh6.bgf"
Gh7:
$bgf "pics\gh7.bgf"
Gh8:
$bgf "pics\gh8.bgf"
Gh9:
$bgf "pics\gh9.bgf"
Pls:
$bgf "pics\plus.bgf"
Mns:
$bgf "pics\minus.bgf"
Spcs:
$bgf "pics\spc.bgf"
$include "font8x8.font"