'---
' (c) 2009
'---
'AS5311
$regfile = "m32def.dat"
$crystal = 1000000
$baud = 19200
'$sim
' ****************************
'* LCD nastavení *
' ****************************
'$lib "LCD4.lbx" ' use the alternative library
'this is like always, define the kind of LCD
Config LCD = 16 * 2
Config Lcdpin = Pin , Rs = Portb.5 , E = Portb.4 , Db4 = Portb.0 , Db5 = Portb.1 , Db6 = Portb.2 , Db7 = Portb.3
Config Porta = Output
Config Portc = Input
Config Pina.0 = Input 'DO ... data out
Config Porta.1 = Output 'CS ... chip Select
Config Porta.2 = Output 'CLK ... clock
'data out
'Data_out Alias Pina.0
'Chip select
'Cs Alias Pina.1
'hodiny
'Clk Alias Pina.2
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
'určení výchozí polohy při startu čipu
Pocitadlo = 0
Mm2 = 0
Call Poloha(pozice_start)
Pozice_old = Pozice_start
'main
'PINC.7 reset. tlačítko
Do
'stisk reset tlačítka
Incr Pocitadlo
If Pinc.7 = 1 Then
'určení výchozí pozice
Mm2 = 0
Call Poloha(pozice_start)
Pozice_old = Pozice_start
End If
'Waitus 10
Call Poloha(pozice_new)
Call Determinuj_polohu(pozice_old , Pozice_new , Smer , Priznak)
If Smer = 0 Then
If Priznak = 0 Then
'vzdálenost roste v rámci 2mm
Vzd_pom = Mm2 * 4096 : Vzd_pom = Vzd_pom - Pozice_start
Vzdalenost = Vzd_pom + Pozice_new
Else
'vzdálenost roste přes 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
'vzdálenost klesá v rámci 2mm
Vzd_pom = Mm2 * 4096 : Vzd_pom = Vzd_pom - Pozice_start
Vzdalenost = Vzd_pom + Pozice_new
Else
'vzdálenost klesá přes 2mm
Decr Mm2
Vzd_pom = Mm2 * 4096 : Vzd_pom = Vzd_pom - Pozice_start
Vzdalenost = Vzd_pom + Pozice_new
End If
End If
'Waitus 10
'hodnota na display se zobrazí každý 20 cyklus
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
' ****************************
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
' 1 = přibližuji se
'Pp ... příznak přechodu přes 4095
' 0 = nepřekročil jsem 4095
' 1 = překročil jsem přes 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
Case Is >= 0
If Abs_delta < 1000 Then
'vzdálenost roste a jsem v rozsahu 2 mm
Sm = 0 : Pp = 0
Else
'vzdálenost klesá a překročil jsem 2 mm
Sm = 1 : Pp = 1
End If
'delta je záporné
Case Else
If Abs_delta < 1000 Then
'vzdálenost klesá a jsem v rozsahu 2 mm
Sm = 1 : Pp = 0
Else
'vzdálenost klesá a překročil jsem 2 mm
Sm = 0 : Pp = 1
End If
End Select
End Sub
' ****************************
'vyčte polohu z čipu
' ****************************
Sub Poloha(p As Integer)
'inicializace čipu
Ssi_init
'čtení hodnoty
Call Cti_data(p)
'ukončení komunikace s čipem
Ssi_close
End Sub
' ****************************
'Přečte data z měřiče, bit po bitu
' ****************************
Sub Cti_data(byref Pozice As Integer)
Dim Pom As Byte
'Pin 2 portu A jsou hodiny
'Proměnná Pozice vrací hodnotu
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
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ů
'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
' ****************************