'$sim
$regfile = "m2560def.dat"
$crystal = 16000000
$hwstack = 255
$swstack = 128
$framesize = 128
$baud = 9600
$eeprom
'****************************************************************
Debug Off
'Debug On
'-------------------------------------------------------------------------------------------
'****************************************************************
$projecttime = 40
$version 0 , 0 , 861
Config Submode = New
Enable Interrupts
'****************************************************************
'* CONFIG BUFFERED USART FOR DEBUG via USB *
'****************************************************************
Config Com1 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Serialin0 = Buffered , Size = 100 'buforowoanie pierwszy uart
Config Serialout0 = Buffered , Size = 100 'buforowoanie pierwszy uart
Echo Off
Open "COM1:" For Binary As #1
Const Debug_ = 1
'****************************************************************
'* CONFIG BUFFERED USART FOR RS485_1 FULL DUPLEX *
'****************************************************************
Config Com2 = 38400 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Serialin1 = Buffered , Size = 100 'buforowoanie drugi uart
Config Serialout1 = Buffered , Size = 100 'buforowoanie drugi uart
Open "COM2:" For Binary As #2
Const RS485_1 = 2
'****************************************************************
'* CONFIG BUFFERED USART FOR LCD *
'****************************************************************
Config Com3 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Serialin2 = Buffered , Size = 100 'buforowoanie trzeci uart
Config Serialout2 = Buffered , Size = 100 'buforowoanie trzeci uart
Open "COM3:" For Binary As #3
Const LCD_ = 3
'****************************************************************
'* CONFIG BUFFERED USART FOR LCD *
'****************************************************************
Config Com4 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Serialin3 = Buffered , Size = 100 'buforowoanie czwarty uart
Config Serialout3 = Buffered , Size = 100 'buforowoanie czwarty uart
Open "COM4:" For Binary As #4
Const Usb = 4
'****************************************************************
$include "subs\mruganie.inc"
$include "subs\Uartrx0.inc"
$include "subs\Uartrx1.inc"
$include "subs\Uartrx2.inc"
$include "subs\Uartrx3.inc"
$include "subs\Hold_.inc"
$include "subs\Left_.inc"
$include "subs\Right_.inc"
$include "subs\BMP180declarations.inc"
$include "subs\BMP180functions.inc"
'-------------------------------------------------------------------------------------------
'KONFIGURACJA TIMERA
Config Timer1 = Timer , Prescale = 8
On Timer1 Przertimer1
Enable Timer1
Config Timer2 = Pwm , Compare_a_pwm = Clear_up , Prescale = 1024
Enable timer2
Enable Interrupts
'-------------------------------------------------------------------------------------------
'##########################################################
'KONFIGURACJA PORTÓW
Config Porta.0 = Output : Reset porta.0 : In_a Alias porta.0 'IN_A
Config porta.1 = Output : Reset porta.1 : In_b Alias porta.1
Config porta.2 = Output : Set porta.2 : Cs_dis Alias porta.2
Config porta.3 = Output : Set porta.3 : Ena_diaga Alias porta.3
Config porta.4 = Output : Set porta.4 : Ena_diagb Alias porta.4
Config porta.5 = Output : Reset porta.5 : Led_hold Alias Porta.5
Config portb.4 = Output : Set portb.4
Config portB.7 = Output : Set portb.7 : Led_Live Alias Portb.7
'##########################################################
' ZMIENNE #
'##########################################################
' ZMIENNE CZASU #
'##########################################################
Dim Timer_tikx10 As Byte 'zmienna liczaca przepełnienie timera
Dim Timertikx100 As Byte 'zmienna liczaca przepełnienie timera x10
Dim Timertikx200 As Byte 'zmienna liczaca przepełnienie timera x 20
Dim Timertikx500 As Byte 'zmienna liczaca przepełnienie timera x 50
Dim Timer_tik_flag As Byte
Flaga10ms Alias Timer_tik_flag.0
Flaga100ms Alias Timer_tik_flag.1
Flaga500ms Alias Timer_tik_flag.2
Flaga1s Alias Timer_tik_flag.3
Flaga2s Alias Timer_tik_flag.4
Flaga2_5s Alias Timer_tik_flag.5
'zostaly jeszce trzy wolne flagi
' a uzylem tylko jednego bajtu
'Pawer SP9DR
'##########################################################
' ZMIENNE ADRESU #
'##########################################################
Dim Rs485_dane(11) As Byte
'Dim Rs485_adres_h As Byte
Dim Rs485_komenda As String * 12
Dim Rs485_wartosc As Integer
Dim Rs485_adres_ee As Eram Byte At &H10 'Rs485 address
Dim Rs485_adres_temp As Byte ' potrzebne przy porownywani uadresu odebranego z wlasciwym
'****************************************************************
'**********************zmienne dla uart0***************************
Dim Usb_arr_rx(5) As Byte
Dim Usb_string As string * 5 At Usb_arr_rx(2) overlay
Dim enkoder_1(11) As Byte
'****************************************************************
'**********************zmienne dla uart1*************************** '
Dim Lcount_1 As Byte
Dim Uart_command_1 As String * 20 'musi byc wystarczjaco dlugie
Dim Getcommands_1(4) As String * 5 'tablica na pociete komendy
Dim Rs485_adres_1 As Byte
Dim komenda_1 As String * 10
Dim wartosc_1 As Word
'****************************************************************
'**********************zmienne dla uart2*************************** '
Dim Lcount_2 As Byte
Dim Uart_command_2 As String * 20 'musi byc wystarczjaco dlugie
Dim Getcommands_2(10) As String * 10 'tablica na pociete komendy
'****************************************************************
'**********************zmienne dla uart3*************************** '
Dim Lcount_3 As Byte
Dim Uart_command_3 As String * 20 'musi byc wystarczjaco dlugie
Dim Getcommands_3(10) As String * 10 'tablica na pociete komendy
'****************************************************************
' ZMIENNE STEROWANIA *
'****************************************************************
'ZMIENNE STEROWNIKA NAPĘDU
Dim Azymut_set As Word
Dim Azymut_old As Word
Dim Azymut_read As Word
Dim Azymut_rs485 As Word
Dim Adres_rs485 As Byte
Dim Pwm_h As Byte
'****************************************************************
'ZMIENNE SPOWALNIANIA PRĘDKOŚCI NAPĘDU
Dim Roznica As Word
'Function Calc_diff(byval A As Word , byval B As Word) As Word
'****************************************************************
' STAŁE
Const On = 1
Const Off = 0
'****************************************************************
'-------------------------------------------------------------------------------
Const G1_c_1 = "Adres_rs485" 'odczytany adres urzadzenia
Const G1_c_2 = "komenda_1" '
Const G1_c_3 = "wartosc_1" '
'------------------------------------------------------------------------------
Const G2_c_1 = "Kat" '
Const Info_Board = "i"
Const Reboot = "reboot"
Const Help = "?"
Const Notincommands = "nie rozpoznano "
Const Ok = "ack"
Const Prompt = "command>>"
'-------------------------------------------------------------------------------------------
'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
' DANE POCZĄTKOWE
Ena_diaga = 1
Ena_diagb = 1
'****************************************************************
'* ODCZYT Z PAMIĘCI EPROM ADRESU URZĄDZENIA *
'Rs485_adres = Rs485_adres_ee
'****************************************************************
Enable Interrupts
Wait 2
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
' DANE DO TESTOWANIA USUNĄĆ PO TESTACH
Pwm_h = 50
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Print #Debug_ , "Rotor board ver: " ; Version(2)
'-------------------------------------------------------------------------------------------
Do
'=====================================================
If Timer_tikx10 >= 10 Then 'jeśli uplyneło 100ms to
Flaga100ms = 1 'ustaw flage upłyneło 100ms
Incr Timertikx100 'zwiększaj Timertikx100
Timer_tikx10 = 0 'zeruj liczenie
End If 'koniec warunku
If Timertikx100 >= 10 Then 'jeśli uplynela sekunda
Flaga1s = 1 'ustaw flage upłyneła sekunda
Incr Timertikx200 'zwiększaj Timertikx200
Incr Timertikx500
Timertikx100 = 0 'zeruj liczenie
End If 'koniec warunku
If Timertikx500 >= 5 Then
Flaga500ms = 1
Timertikx500 = 0
End If
If Timertikx200 >= 2 Then 'jeśli upłyneły dwie sekundy
Flaga2s = 1 'ustaw flagę upłyneły dwie sekundy
Timertikx200 = 0 'zeruj liczenie
End If 'koniec warunku
'=====================================================
If Flaga10ms = 1 Then 'jeśli Flaga10ms jest równa 1
Flaga10ms = 0 'wyzeruj Flaga10ms
End If 'koniec warunku
'-------------------------------------------------------------------------------------------
If Flaga100ms = 1 Then 'jeśli Flaga100ms jest równa 1
Flaga100ms = 0 'wyzeruj Flaga100ms
End If 'koniec warunku
'-------------------------------------------------------------------------------------------
If Flaga500ms = 1 Then 'jeśli Flaga500ms jest równa 1
Flaga500ms = 0 'wyzeruj Flaga500ms
End If 'koniec warunku
'-------------------------------------------------------------------------------------------
If Flaga1s = 1 Then 'jeśli Flaga1s jest równa 1
Flaga1s = 0 'wyzeruj Flaga1s
Call Mruganie
Print #RS485_1 , "200 a"
'Print #RS485_1 , "200 t"
Print #Debug_ , "AZ=" ; Azymut_read
End If 'koniec warunku
'-------------------------------------------------------------------------------------------
If Flaga2s = 1 Then 'jeśli Flaga2s jest równa 1
Flaga2s = 0 'wyzeruj Flaga
'Print #RS485_1 , "200 a"
End If 'koniec warunku
'-------------------------------------------------------------------------------------------
Azymut_read = wartosc_1
If Azymut_read <= 0 Then
Azymut_read = 0
End If
If Azymut_read => 360 Then
Azymut_read = 360
End If
'-------------------------------------------------------------------------------------------
'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
'&&&&&&&&&&&&&&ODBIÓR DANYCH&&&&&&&&&&&&&&&
'-------------------------------------------------------------------------------------------
'ODBIÓR DANYCH UART3
While Ischarwaiting(#Usb) <> 0 'While data waiting in the buffer
Inputbin #Usb , Usb_arr_rx(1) , 1 'input them in to the array
Debug #Debug_ , "received_ID= " ; Usb_arr_rx(1)
Print #Usb , "AZ=" ; Azymut_read
Select Case Usb_arr_rx(1) 'command &h00 to &h08
Case &H43:
Print #Usb , "AZ=" ; Azymut_read
'Print #Debug_ , "AZ=" ; Azymut_read
Case &H04D:
Inputbin #Usb , Usb_arr_rx(2) , 3
Print #Debug_ , "usb_string: " ; Usb_string
Print #Debug_ , "AZ=" ; Azymut_read
End select
Wend
'-------------------------------------------------------------------------------------------
'ODBIÓR DANYCH UART1
If Ischarwaiting(#RS485_1) > 0 Then 'calls _gotchar and use URXC
Lcount_1 = Inkey(#RS485_1) 'pobierz znak z bufora
If Lcount_1 = 13 Or Len(uart_command_1) > 20 Then '20 Then ' jeslii ENTER lub wiecej niz 20 znaków to call uartrx
Call Uartrx1() 'wywołaj podprogram Uartrx1
'Debug "czyszczenie bufora po odebranje komendzie" 'Using a constant will let us change the prompt
'clear serialin 'nie sprawdzone
Uart_command_1 = "" ' koniecznie potrzenbe do czysczenia bufora
Else 'inaczej
Uart_command_1 = Uart_command_1 + Chr(Lcount_1) 'kluczowe budowanie bufora
End If 'koniec warunku
End If 'koniec warunku
'-------------------------------------------------------------------------------------------
'ODBIÓR DANYCH UART2
If Ischarwaiting(#LCD_) > 0 Then 'calls _gotchar and use URXC
Lcount_2 = Inkey(#LCD_) 'pobierz znak z bufora
If Lcount_1 = 13 Or Len(uart_command_2) > 20 Then '20 Then ' jeslii ENTER lub wiecej niz 20 znaków to call uartrx
Call Uartrx2() 'wywołaj podprogram Uartrx1
'Debug "czyszczenie bufora po odebranje komendzie" 'Using a constant will let us change the prompt
'clear serialin 'nie sprawdzone
Uart_command_2 = "" ' koniecznie potrzenbe do czysczenia bufora
Else 'inaczej
Uart_command_2 = Uart_command_2 + Chr(Lcount_2) 'kluczowe budowanie bufora
End If 'koniec warunku
End If 'koniec warunku
'-------------------------------------------------------------------------------------------
'ODBIÓR DANYCH UART0
If Ischarwaiting(#DEBUG_) > 0 Then 'calls _gotchar and use URXC
Lcount_3 = Inkey(#DEBUG_) 'pobierz znak z bufora
If Lcount_3 = 13 Or Len(uart_command_3) > 20 Then '20 Then ' jeslii ENTER lub wiecej niz 20 znaków to call uartrx
Call Uartrx3()
Print #Debug_ , Prompt 'wywołaj podprogram Uartrx1
'Debug "czyszczenie bufora po odebranje komendzie" 'Using a constant will let us change the prompt
'clear serialin 'nie sprawdzone
Uart_command_3 = "" ' koniecznie potrzenbe do czysczenia bufora
Else 'inaczej
Uart_command_3 = Uart_command_3 + Chr(Lcount_3) 'kluczowe budowanie bufora
End If 'koniec warunku
End If 'koniec warunku
'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
'& STEROWANIE ROTOREM &
'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
If Rs485_adres_1 = 200 And komenda_1 = "kat" Then
Azymut_read = wartosc_1
End If
'-------------------------------------------------------------------------------------------
Azymut_old = Val(Usb_string)
If Azymut_set <> Azymut_old Then
Azymut_set = Azymut_old
Print #Debug_ , "Azymut_set: " ; Azymut_set
End If
'****************************************************************
If Azymut_set > Azymut_read Then
Call Left_
End If
If Azymut_set < Azymut_read Then
Call Right_
End If
If Azymut_set = Azymut_read Then
Call Hold_
End If
'****************************************************************
Function Calc_diff(byval A As Word , byval B As Word) As Byte
A = Azymut_set
B = Azymut_read
Select Case A
Case Is > B : Calc_diff = A - B : Exit Function
Case B : Calc_diff = 0 : Exit Function
Case Is < B : Calc_diff = B - A : Exit Function
End Select
End Function
Roznica = Call Calc_diff(8 , 5)
Select Case Roznica
Case 3 :
If Roznica = 10 Then
Pwm_h = 5 'zrob to i tamto
End If
Case Else
End Select
'****************************************************************
Loop
End
'-------------------------------------------------------------------------------------------
' podprogram przerwań Timer1
Przertimer1:
Timer1 = 45536 'wstawienie do Timer1 wartości 45536
Incr Timer_tikx10
If Timer_tikx10 = 1 Then
Set Flaga10ms
End If
Return 'powrót
'-------------------------------------------------------------------------------------------