$regfile = "m32adef.dat"
$crystal = 1600000
$hwstack = 32
$swstack=8
$framesize = 64
$projecttime = 25
Config Submode = New
Config Lcd = 16x4
Config Lcdpin = Pin , Rs = Portb.5 , E = Portb.4 , Db4 = Portb.0 , Db5 = Portb.1 , Db6 = Portb.2 , Db7 = Portb.3
Data_out Alias Pina.0 : Config Data_out = Input : Set Porta.0 'DO + pullup
Cs Alias Porta.1 : Config Cs = Output : Set Cs 'chip select Hi=Inactive
Clk Alias Porta.2 : Config Clk = Output : Set Clk 'clock output readed when clk is low
Pin_reset Alias Pinc.7 : Portc.7 = 1 'pullup
Declare Sub Poloha(byref P As Integer)
Declare Sub Determinuj_polohu(p_old As Integer , P_new As Integer , Sm As Integer , Pp As Integer)
Declare Sub Cti_data(byref Pozice As Integer)
Declare Sub Ssi_init
Declare Sub Ssi_close
Dim Pozice As Integer
'Dim Poloha As Long
Dim Vzd_pom As Long
Dim Vzdalenost As Long
Dim Mm2 As Integer
Dim Draha As String * 16
Dim Pom_str As String * 16
Dim Pom_s As Single
Dim Pozice_old As Integer
Dim Pozice_new As Integer
Dim Pozice_start As Integer
Dim Smer As Integer
Dim Priznak As Integer
Dim Fix_prechodu As Bit
Dim Pocitadlo As Integer
Dim Ocf As Byte
Dim Cof As Byte
Dim Lin As Byte
Dim Mag_inc As Byte
Dim Mag_dec As Byte
Dim Parita As Byte
Dim Poms As String * 16
Call Poloha(pozice_start)
Pozice_old = Pozice_start
Do
If Pin_reset = 0 Then
'określ pozycję wyjściową'
Mm2 = 0
Call Poloha(pozice_start) 'Poloha--> Pozycja
Pozice_old = Pozice_start
End If
Call Poloha(pozice_new)
Call Determinuj_polohu(pozice_old , Pozice_new , Smer , Priznak)
If Smer = 0 Then 'Smer--> Kierunek
If Priznak = 0 Then
'Odległość rośnie w ciągu 2 m
Vzd_pom = Mm2 * 4096 : Vzd_pom = Vzd_pom - Pozice_start
Vzdalenost = Vzd_pom + Pozice_new
Else
'Odległość rośnie ponad 2mm
Incr Mm2
Vzd_pom = Mm2 * 4096 : Vzd_pom = Vzd_pom - Pozice_start
Vzdalenost = Vzd_pom + Pozice_new
End If
Else
If Priznak = 0 Then
'Odległość spada w granicach 2mm
Vzd_pom = Mm2 * 4096 : Vzd_pom = Vzd_pom - Pozice_start
Vzdalenost = Vzd_pom + Pozice_new
Else
'Odległość spada ponad 2mm
Decr Mm2
Vzd_pom = Mm2 * 4096 : Vzd_pom = Vzd_pom - Pozice_start
Vzdalenost = Vzd_pom + Pozice_new
End If
End If
Waitus 20
'wartość pojawia się na wyświetlaczu co 20 cykli
Incr Pocitadlo
If Pocitadlo > 50 Then
Pocitadlo = 0
Cls : Lcd Pozice_new
Lowerline
Pom_s = Vzdalenost / 2048
Lcd Fusing(pom_s , "#.###")
End If
Pozice_old = Pozice_new
Loop
End
' ****************************
'určí směr pohybu a překrocění hranice 0 /-/ 4095-->określa kierunek ruchu i przekroczenie granicy 0 / - / 4095
' ****************************
Sub Determinuj_polohu(p_old As Integer , P_new As Integer , Sm As Integer , Pp As Integer)
'Sm ... směr
' 0 = vzdaluji se od reference-->Odchodzę od referencji
' 1 = přibližuji se-->zbliżam się
'Pp ... příznak přechodu přes 4095-->Pp ... przekraczanie flagi ponad 4095
' 0 = nepřekročil jsem 4095-->nie przekroczyłem 4095
' 1 = překročil jsem přes 4095-->przekroczyłem 4095
Dim Delta As Single
Dim Sgn_delta As Single
Dim Abs_delta As Single
Delta = P_new - P_old
Abs_delta = Abs(delta)
Sgn_delta = Sgn(delta)
Select Case Delta
'delta je kladné nebo nula-->delta jest dodatnia lub zero
Case Is >= 0
If Abs_delta < 1000 Then
'vzdálenost roste a jsem v rozsahu 2 mm-->odległość rośnie i jestem w zakresie 2 mm
Sm = 0 : Pp = 0
Else
'vzdálenost klesá a překročil jsem 2 mm-->odległość maleje i przekroczyłem 2 mm
Sm = 1 : Pp = 1
End If
'delta je záporné-->delta jest ujemna
Case Else
If Abs_delta < 1000 Then
'vzdálenost klesá a jsem v rozsahu 2 mm-->odległość maleje i jestem w zakresie 2 mm
Sm = 1 : Pp = 0
Else
'vzdálenost klesá a překročil jsem 2 mm-->odległość maleje i przekroczyłem 2 mm
Sm = 0 : Pp = 1
End If
End Select
End Sub
' ****************************
'vyčte polohu z čipu-->odczytaj pozycję z chipa
' ****************************
Sub Poloha(byref P As Integer)
'inicializace čipu-->inicjalizacja chipa
Ssi_init
'čtení hodnoty-->wartość odczytu
Call Cti_data(p)
'ukončení komunikace s čipem-->zakończenie komunikacji z chipem
Ssi_close
End Sub
' ****************************
'Přečte data z měřiče, bit po bitu-->Odczytuje dane z licznika, bit po bicie
' ****************************
Sub Cti_data(byref Pozice As Integer)
Dim Pom As Byte
'Pin 2 portu A jsou hodiny-->'Pin 2 portu A to zegar
'Proměnná Pozice vrací hodnotu-->Zmienna Pozice zwraca wartość
Pozice = 0
'D11
Reset Porta.2
If Pinc.0 = 1 Then
Pozice = Pozice + 1
End If
Set Porta.2
'D10
Pozice = Pozice * 2
Reset Porta.2
If Pinc.0 = 1 Then
Pozice = Pozice + 1
End If
Set Porta.2
'D9
Pozice = Pozice * 2
Reset Porta.2
If Pinc.0 = 1 Then
Pozice = Pozice + 1
End If
Set Porta.2
'D8
Pozice = Pozice * 2
Reset Porta.2
If Pinc.0 = 1 Then
Pozice = Pozice + 1
End If
Set Porta.2
'D7
Pozice = Pozice * 2
Reset Porta.2
If Pinc.0 = 1 Then
Pozice = Pozice + 1
End If
Set Porta.2
'D6
Pozice = Pozice * 2
Reset Porta.2
If Pinc.0 = 1 Then
Pozice = Pozice + 1
End If
Set Porta.2
'D5
Pozice = Pozice * 2
Reset Porta.2
If Pinc.0 = 1 Then
Pozice = Pozice + 1
End If
Set Porta.2
'D4
Pozice = Pozice * 2
Reset Porta.2
If Pinc.0 = 1 Then
Pozice = Pozice + 1
End If
Set Porta.2
'D3
Pozice = Pozice * 2
Reset Porta.2
If Pinc.0 = 1 Then
Pozice = Pozice + 1
End If
Set Porta.2
'D2
Pozice = Pozice * 2
Reset Porta.2
If Pinc.0 = 1 Then
Pozice = Pozice + 1
End If
Set Porta.2
'D1
Pozice = Pozice * 2
Reset Porta.2
If Pinc.0 = 1 Then
Pozice = Pozice + 1
End If
Set Porta.2
'D0
Pozice = Pozice * 2
Reset Porta.2
If Pinc.0 = 1 Then
Pozice = Pozice + 1
End If
Set Porta.2
'čtení stavových bitů-->odczytaj bity statusu
'OCF
'Waitus 10
Reset Porta.2
Ocf = Pinc.0
Set Porta.2
'COF
'Waitus 10
Reset Porta.2
Cof = Pinc.0
Set Porta.2
'LIN
'Waitus 10
Reset Porta.2
Lin = Pinc.0
Set Porta.2
'Mag_INC
'Waitus 10
Reset Porta.2
Mag_inc = Pinc.0
Set Porta.2
'Mag_DEC
'Waitus 10
Reset Porta.2
Mag_dec = Pinc.0
End Sub
' ****************************
' ****************************
Sub Ssi_init
'A.1 CS ¨¨¨¨¨______
'A.2 CLK ¨¨¨¨¨¨¨__¨¨
' us 2 1 1 1
'PortA.2 CLK ... clock
Set Porta.2
'PortA.1 CS ... chip select
Set Porta.1
Waitus 2
Reset Porta.1
Waitus 1
Reset Porta.2
Waitus 1
Set Porta.2
Waitus 1
End Sub
' ****************************
' ****************************
Sub Ssi_close
'A.1 CS ___¨¨¨¨
'A.2 CLK _¨¨¨¨¨¨
' us 1 2
'čtu ^
'PortA.2 CLK ... clock
'PortA.1 CS ... chip select
'Parita
Set Porta.2
Parita = Pinc.0
Waitus 1
Set Porta.1
Waitus 2
End Sub
' ****************************