$REGFILE = "M328PDEF.DAT"
$CRYSTAL = 16000000
$BAUD = 9600
$HWSTACK = 100
$SWSTACK = 100
$FRAMESIZE = 100
ENABLE INTERRUPTS
'------------------------------------------------------------------------------------------------------------------
'--------------------------------------------- UART ---------------------------------------------------------------
'------------------------------------------------------------------------------------------------------------------
CONFIG COM1 = 9600 , SYNCHRONE = 0 , PARITY=NONE , STOPBITS=1 , DATABITS=8 , CLOCKPOL=0
OPEN "COM1:" FOR BINARY AS #1
CONFIG SERIALIN0 = BUFFERED , SIZE = 30 , BYTEMATCH = 13
DIM COM1 AS STRING*30
DIM RS232 AS LONG
ENABLE URXC
'------------------------------------------------------------------------------------------------------------------
'--------------------------------------------- INT1-SILNIK --------------------------------------------------------
'------------------------------------------------------------------------------------------------------------------
CONFIG INT1 = CHANGE
ON INT1 SILNIK
ENABLE INT1
'-------------------------------------------------------------
CONFIG PORTD.3 = INPUT 'ARDUINO NANO - D2
CONFIG PORTC.3 = INPUT 'ARDUINO NANO - A2
DIM POZYCJA_SILNIK AS LONG
'------------------------------------------------------------------------------------------------------------------
'--------------------------------------------- INT0-ENKODER -------------------------------------------------------
'------------------------------------------------------------------------------------------------------------------
CONFIG INT0 = CHANGE
ON INT0 ENKODER
ENABLE INT0
'-------------------------------------------------------------
CONFIG PORTD.2 = INPUT 'ARDUINO NANO - D3
CONFIG PORTC.2 = INPUT 'ARDUINO NANO - A3
DIM POZYCJA_ENKODER AS LONG
'------------------------------------------------------------------------------------------------------------------
'------------------------------------------- TIMER1 / DT ----------------------------------------------------------
'------------------------------------------------------------------------------------------------------------------
CONFIG TIMER1 = TIMER , PRESCALE = 1
ENABLE TIMER1
STOP TIMER1
'------------------------------------------------------------------------------------------------------------------
'------------------------------------------- TIMER0 / PWM ---------------------------------------------------------
'------------------------------------------------------------------------------------------------------------------
' PD3 - OC2B; PB3 - OC2A;
' PD5 - OC0B; PB2 - OC1B;
' PD6 - OC0A; PB1 - OC1A;
CONFIG TIMER0 = PWM ,PWM=ON , COMPARE_A_PWM = CLEAR_UP , COMPARE_B_PWM = CLEAR_UP ,PRESCALE = 1
ENABLE TIMER0
START TIMER0
CONFIG PORTD.5=OUTPUT 'ARDUINO NANO - D5
CONFIG PORTD.6=OUTPUT 'ARDUINO NANO - D6
'------------------------------------------------------------------------------------------------------------------
'--------------------------------------------ZMIENNE---------------------------------------------------------------
'------------------------------------------------------------------------------------------------------------------
DIM CEL AS LONG 'CEL GDZIE MA DOJSC
DIM AKTUALNE AS LONG 'POLOZENIE AKTUALNE
DIM ERROR AS SINGLE 'ROZNICA MIEDZY POLOZENIEM AKTUALNYM A CELEM
DIM DT AS SINGLE 'OKRES POMIARU
DIM ERROR_STARY AS SINGLE 'WARTOSC POPRZEDNIEGO ERROR , CYKL WCZESNIEJ
DIM ZMIANA AS SINGLE 'ROZNICA MIEDZY ERROR_OLD A ERROR
DIM ZBOCZE AS SINGLE
DIM STREFA AS SINGLE
DIM STREFA_1 AS SINGLE
DIM STREFA_STARA AS SINGLE
DIM P AS SINGLE , I AS SINGLE,, D AS SINGLE
DIM K1 AS SINGLE , K2 AS SINGLE , K3 AS SINGLE
DIM PID AS SINGLE
DIM PID_1 AS SINGLE
DIM PID_STARY AS SINGLE
DIM PID_WYJ AS LONG
DIM PID_WYJ_A AS LONG
DIM PID_WYJ_B AS LONG
K1= 22 '11 22 'USTAWIENIA DLA PID DOBRAC POD SILNIK
K2= 0.95 '0.8 0.8
K3=0.01 '0.01 0.001
DT=2000 'DOMYSLNE DT PRZY PRZEJSCIU PIERWSZEJ PETLI
CONFIG PORTC.5=OUTPUT 'ARDUINO NANO - A5 , LED
DO
'----------------------------------------------- ' WYBRAC I ODHACZYC
CEL=RS232 ' ZMIENNA NA UART , ILOSC IMPULSOW DO PRZEBYCIA -/+
'CEL=POZYCJA_ENKODER ' ENKODER STERUJACY , ILOSC IMPULSOW DO PRZEBYCIA -/+
'-----------------------------------------------
ERROR_STARY=ERROR
ERROR=CEL-AKTUALNE
ZMIANA=ERROR-ERROR_STARY
ZBOCZE=ZMIANA * DT
STREFA_1=ERROR / DT
STREFA=STREFA_STARA+STREFA_1
STREFA_STARA=STREFA
P=K1*ERROR ' PID
I=K2*ZBOCZE
D=K3*STREFA
PID_1=P+I
PID_1=PID_1+D
PID=PID_1
PID_STARY=PID
AKTUALNE=POZYCJA_SILNIK
PID_WYJ=PID
'-----------------------------------------------
IF PID_WYJ > -1 THEN
PID_WYJ_A=PID_WYJ
IF PID_WYJ_A>255 THEN PID_WYJ_A=255
COMPARE0B = PID_WYJ_A
COMPARE0A = 0 'OCR0B
END IF 'OCR0A
IF PID_WYJ < 1 THEN
PID_WYJ_B = PID_WYJ*-1
IF PID_WYJ_B>255 THEN PID_WYJ_B=255
COMPARE0A = PID_WYJ_B 'OCR0A
COMPARE0B = 0 'OCR0B
END IF
IF POZYCJA_ENKODER=0 THEN 'KONTROLA LED POZYCJI "0" TYLKO DLA ENKODERA STERUJACEGO
PORTC.5=0 'ARDUINO NANO - A5
ELSE
PORTC.5=1
END IF
STOP TIMER1 'ODLICZANIE DT W PETLI
DT=TIMER1
TIMER1=0
START TIMER1
LOOP
END
'-----------------------------------------------
SERIAL0CHARMATCH:
PUSHALL
INPUT #1,COM1 NOECHO
RS232=VAL(COM1)
POPALL
RETURN
'-----------------------------------------------
SILNIK:
If PIND.3 = 0 AND PINC.3 = 1 THEN DECR POZYCJA_SILNIK
If PIND.3 = 0 AND PINC.3 = 0 THEN INCR POZYCJA_SILNIK
If PIND.3 = 1 AND PINC.3 = 0 THEN DECR POZYCJA_SILNIK
If PIND.3 = 1 AND PINC.3 = 1 THEN INCR POZYCJA_SILNIK
RETURN
ENKODER:
If PIND.2 = 0 AND PINC.2 = 1 THEN DECR POZYCJA_ENKODER
If PIND.2 = 0 AND PINC.2 = 0 THEN INCR POZYCJA_ENKODER
If PIND.2 = 1 AND PINC.2 = 0 THEN DECR POZYCJA_ENKODER
If PIND.2 = 1 AND PINC.2 = 1 THEN INCR POZYCJA_ENKODER
RETURN