Rangkaian
AC PHASE CONTROL DIMMER LAMP
If Sensor = &B11111111 ThenIf Pv < 0 Then : Pv = -30End IfIf Pv > 0 Then : Pv = 30End IfEnd If
Sp_sensor = 0 ‘setpoint sensorError = Sp_sensor – Pv ‘nilai errorP = Kp * Error ‘proporsional kontrolAplikasi kontrol proporsional pada PWM ialah sebagai berikut:Pwm = Sp_kecepatan + P ‘motor kiriPwm = Sp_kecepatan – P ‘motor kanan
Sp_sensor = 0 ‘setpoint sensorError = Sp_sensor – Pv ‘nilai errorP = Kp * Error ‘proporsional kontrolD1 = Kd * 10 ‘derivatif kontrolD2 = D1 / TsD3 = Error – Last_error ‘rateD = D2 * D3Last_error = Error ‘error lampauPd = P + D ‘proporsional-derivatif kontrolAplikasi kontrol proporsional dan drivatif pada PWM ialah sebagai berikut:Pwm = Sp_kecepatan + Pd ‘motor kiriPwm = Sp_kecepatan – Pd ‘motor kanan
Sp_sensor = 0 ‘setpoint sensorError = Sp_sensor – Pv ‘nilai errorP = Kp * Error ‘proporsional kontrolD1 = Kd * 10 ‘derivatif kontrolD2 = D1 / TsD3 = Error – Last_error ‘rateD = D2 * D3I1 = Ki / 10 ’integratif kontrolI2 = Error + Last_error ‘akumulasi errorI3 = I1 * I2I = I3 * TsLast_error = Error ‘error lampauPd = P + D ‘proporsional-derivatif kontrolPid = Pd+I ‘proporsional-integratif-derivatifAplikasi kontrol proporsional, integratif dan drivatif pada PWM ialah sebagai berikut:Pwm = Sp_kecepatan + Pid ‘motor kiriPwm = Sp_kecepatan – Pid ‘motor kanan
$regfile = “m8535.dat”
$crystal = 12000000
$eeprom‘—————————inisialisasi pwm————————————
Config Timer1 = Pwm , Pwm = 10 , Prescale = 64 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down ‘pwm dengan settingan fast pwm 10 bit‘—————————inisialisasi adc————————————
Config Adc = Single , Prescaler = Auto‘—————————inisialisasi lcd————————————
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7 , E = Portc.2 , Rs = Portc.0
Config Lcd = 16 * 2 : Cursor Off‘——————————————————————————-
‘PENDEFINISIAN PIN MIKRO
‘——————————————————————————-
Config Portd = Output‘alias logika motor
M1a Alias Portd.0
M1b Alias Portd.1
M2a Alias Portd.2
M2b Alias Portd.3Config Portb = Input
Portb = 255
‘alias tombol
Sw_ok Alias Pinb.3
Sw_cancel Alias Pinb.2
Sw_down Alias Pinb.1
Sw_up Alias Pinb.0Config Portc.3 = Output‘——————————————————————————-
‘DEKLARASI VARIABEL
‘——————————————————————————-
Dim B1 As Bit , B2 As Bit , B3 As Bit , B4 As Bit , B5 As Bit , B6 As Bit , B7 As Bit , B8 As Bit,
Dim S1 As Byte , S2 As Byte , S3 As Byte , S4 As Byte , S5 As Byte , S6 As Byte , S7 As Byte , S8 As Byte
Dim Adc1 As Integer , Adc2 As Integer , Adc3 As Integer , Adc4 As Integer , Adc5 As Integer , Adc6 As Integer , Adc7 As Integer , Adc8 As Integer,
Dim S12 As Byte , S123 As Byte , S1234 As Byte , S12345 As Byte , S123456 As Byte , S1234567 As Byte , S12345678 As Byte
Dim C As Byte
Dim B As Byte
Dim Sensor As Byte
Dim Konversi_sensor As Byte
Dim Data1 As Byte
Dim Menu As Byte‘——————————-declarasi variabel——————————
‘variabel eeprom
Dim Kp_eprom As Eram Integer
Dim Ki_eprom As Eram Integer
Dim Kd_eprom As Eram Integer
Dim Ts_eprom As Eram Integer
Dim Sp_kecepatan_eprom As Eram Integer
Dim Up_eprom As Eram Integer
Dim Lp_eprom As Eram Integer
Dim Nos_eprom As Eram Integer
Dim Hold_eprom As Eram IntegerDim Kp As Integer
Dim Ki As Integer
Dim Kd As Integer
Dim Ts As Integer
Dim Sp_sensor As Integer
Dim Sp_kecepatan As Integer
Dim Up As Integer
Dim Lp As Integer
Dim Nos As Integer
Dim Hold As IntegerDim Error As Integer
Dim Last_error As Integer
Dim Pv As Integer
Dim P As Integer
Dim Pd As Integer
Dim Pid As Integer
Dim Pwm As IntegerDim I1 As Integer
Dim I2 As Integer
Dim I3 As Integer
Dim I As IntegerDim D1 As Integer
Dim D2 As Integer
Dim D3 As Integer
Dim D As IntegerDim N1 As Integer
Dim N2 As Integer‘——————————————————————————-
‘DEKLARASI SUBRUTIN
‘——————————————————————————-Declare Sub Baca_sensor()
Declare Sub Kiri_maju()
Declare Sub Kanan_maju()
Declare Sub Kiri_mundur()
Declare Sub Kanan_mundur()
Declare Sub Bantingkanan()
Declare Sub Bantingkiri()Portc.3 = 0 ‘lcd onCls : Home
Lcd “ADC LINEfollower”
Waitms 500
Lowerline
Lcd “Fahmizal_dte2006″
Waitms 1500Portc.3 = 1 ‘lcd offMenu_utama:
Cls
Home : Lcd “‘menu’<<<<^>>>>>”
Lowerline
Lcd “Please choice…”
Do
If Sw_up = 0 Then : Waitms 250 : Menu = 1
End If
If Sw_down = 0 Then : Waitms 250 : Menu = 2
End IfIf Menu = 1 Then : Locate 2 , 1 : Lcd “Setting ROBOT…”
If Sw_ok = 0 Then : Waitms 250 : Gosub Setting_pid : End If
End IfIf Menu = 2 Then : Locate 2 , 1 : Lcd “Go……..Ready?”
If Sw_ok = 0 Then : Waitms 250 : Gosub Go : End If
End If
Loop‘——————————-menu Konstanta PID——————————
Setting_pid:
Cls
Menu_kp:
DoKp = Kp_eprom
If Sw_up = 0 Then : Incr Kp : Waitms 10
End If
If Sw_down = 0 Then : Decr Kp : Waitms 10
End IfIf Sw_ok = 0 Then : Waitms 250 : Gosub Menu_ki
End If
If Sw_cancel = 0 Then : Waitms 250 : Gosub Menu_utama
End IfIf Kp > 50 Then : Kp = 0 : Waitms 10
End If
If Kp < 0 Then : Kp = 50 : Waitms 10
End IfHome : Lcd “Tuning PID ^–^ ” : Locate 2 , 1 : Lcd “KP:” : Locate 2 , 4 : Lcd “ ” : Locate 2 , 4 : Lcd Kp : Waitms 100Kp_eprom = Kp
Loop‘———————————————Menu_ki:
DoKi = Ki_eprom
If Sw_up = 0 Then : Incr Ki : Waitms 10
End If
If Sw_down = 0 Then : Decr Ki : Waitms 10
End IfIf Sw_ok = 0 Then : Waitms 250 : Gosub Menu_kd
End If
If Sw_cancel = 0 Then : Waitms 250 : Gosub Menu_kp
End IfIf Ki > 20 Then : Ki = 0 : Waitms 10
End If
If Ki < 0 Then : Ki = 20 : Waitms 10
End IfHome : Lcd “Tuning PID ^–^ ” : Locate 2 , 1 : Lcd “KI:” : Locate 2 , 4 : Lcd “ ” : Locate 2 , 4 : Lcd Ki : Waitms 100Ki_eprom = Ki
Loop‘———————————————Menu_kd:
DoKd = Kd_eprom
If Sw_up = 0 Then : Kd = Kd + 5 : Waitms 10
End If
If Sw_down = 0 Then : Kd = Kd – 5 : Waitms 10
End IfIf Sw_ok = 0 Then : Waitms 250 : Gosub Menu_ts
End If
If Sw_cancel = 0 Then : Waitms 250 : Gosub Menu_ki
End IfIf Kd > 150 Then : Kd = 0 : Waitms 10
End If
If Kd < 0 Then : Kd = 150 : Waitms 10
End IfHome : Lcd “Tuning PID ^–^ ” : Locate 2 , 1 : Lcd “KD:” : Locate 2 , 4 : Lcd “ ” : Locate 2 , 4 : Lcd Kd : Waitms 100Kd_eprom = KdLoop‘———————————————Menu_ts:
DoTs = Ts_eprom
If Sw_up = 0 Then : Ts = Ts + 5 : Waitms 10
End If
If Sw_down = 0 Then : Ts = Ts – 5 : Waitms 10
End IfIf Sw_ok = 0 Then : Waitms 250 : Gosub Menu_sp_kecepatan
End If
If Sw_cancel = 0 Then : Waitms 250 : Gosub Menu_kd
End IfIf Ts > 20 Then : Ts = 0 : Waitms 10
End If
If Ts < 0 Then : Ts = 20 : Waitms 10
End IfHome : Lcd “TimeSampling(ms)” : Locate 2 , 1 : Lcd “TS:” : Locate 2 , 4 : Lcd “ ” : Locate 2 , 4 : Lcd Ts : Waitms 100Ts_eprom = TsLoop‘———————————————Menu_sp_kecepatan:
DoSp_kecepatan = Sp_kecepatan_eprom
If Sw_up = 0 Then : Sp_kecepatan = Sp_kecepatan + 50 : Waitms 10
End If
If Sw_down = 0 Then : Sp_kecepatan = Sp_kecepatan – 50 : Waitms 10
End IfIf Sw_ok = 0 Then : Waitms 250 : Gosub Menu_up
End If
If Sw_cancel = 0 Then : Waitms 250 : Gosub Menu_ts
End IfIf Sp_kecepatan > 600 Then : Sp_kecepatan = 400 : Waitms 5
End If
If Sp_kecepatan < 400 Then : Sp_kecepatan = 600 : Waitms 5
End IfHome : Lcd “Set Point ^–^ ” : Locate 2 , 1 : Lcd “kecepatan:” : Locate 2 , 11 : Lcd “ ” : Locate 2 , 11 : Lcd Sp_kecepatan : Waitms 100Sp_kecepatan_eprom = Sp_kecepatanLoop‘———————————————
Menu_up:
DoUp = Up_eprom
If Sw_up = 0 Then : Up = Up + 50 : Waitms 10
End If
If Sw_down = 0 Then : Up = Up – 50 : Waitms 10
End IfIf Sw_ok = 0 Then : Waitms 250 : Gosub Menu_nos
End If
If Sw_cancel = 0 Then : Waitms 250 : Gosub Menu_sp_kecepatan
End IfIf Up > 1000 Then : Up = 700 : Waitms 10
End If
If Up < 700 Then : Up = 1000 : Waitms 10
End IfHome : Lcd “TOP speed ROBOT ” : Locate 2 , 1 : Lcd “top:” : Locate 2 , 5 : Lcd “ ” : Locate 2 , 5 : Lcd Up : Waitms 100Up_eprom = UpLoop‘———————————————
Menu_nos:
DoNos = Nos_eprom
If Sw_up = 0 Then : Nos = Nos + 100 : Waitms 10
End If
If Sw_down = 0 Then : Nos = Nos -100 : Waitms 10
End IfIf Sw_ok = 0 Then : Waitms 250 : Gosub Menu_hold:
End If
If Sw_cancel = 0 Then : Waitms 250 : Gosub Menu_up
End IfIf Nos > 500 Then : Nos = 100 : Waitms 10
End If
If Nos < 100 Then : Nos = 500 : Waitms 10
End IfHome : Lcd “POWER ^–^ ROBOT” : Locate 2 , 1 : Lcd “start:” : Locate 2 , 7 : Lcd “ ” : Locate 2 , 7 : Lcd Nos : Waitms 100Nos_eprom = NosLoop
‘——————————————————————————-Menu_hold:
DoHold = Hold_eprom
If Sw_up = 0 Then : Hold = Hold + 5 : Waitms 10
End If
If Sw_down = 0 Then : Hold = Hold – 5 : Waitms 10
End IfIf Sw_ok = 0 Then : Waitms 250 : Gosub Rampung_setting:
End If
If Sw_cancel = 0 Then : Waitms 250 : Gosub Menu_nos
End IfIf Hold > 75 Then : Hold = 25 : Waitms 10
End If
If Hold < 25 Then : Hold = 75 : Waitms 10
End IfHome : Lcd “TressHold^SENSOR” : Locate 2 , 1 : Lcd “hold:” : Locate 2 , 6 : Lcd “ ” : Locate 2 , 6 : Lcd Hold : Waitms 100Hold_eprom = HoldLoop
‘——————————————————————————-‘——————————-tampilansaveepprom——————————
Rampung_setting:Cls
Locate 1 , 1 : Lcd “Write’_to_EEPROM”
Locate 2 , 1 : Lcd “=” : Waitms 200
Locate 2 , 2 : Lcd “=” : Waitms 180
Locate 2 , 3 : Lcd “=” : Waitms 155
Locate 2 , 4 : Lcd “=” : Waitms 125
Locate 2 , 5 : Lcd “=” : Waitms 90
Locate 2 , 6 : Lcd “=” : Waitms 70
Locate 2 , 7 : Lcd “=” : Waitms 50
Locate 2 , 8 : Lcd “=” : Waitms 35
Locate 2 , 9 : Lcd “=” : Waitms 20
Locate 2 , 10 : Lcd “=” : Waitms 15
Locate 2 , 11 : Lcd “=” : Waitms 13
Locate 2 , 12 : Lcd “=” : Waitms 11
Locate 2 , 13 : Lcd “=” : Waitms 9
Locate 2 , 14 : Lcd “=” : Waitms 7
Locate 2 , 15 : Lcd “=” : Waitms 5
Locate 2 , 16 : Lcd “=” : Waitms 300
Locate 2 , 1 : Lcd “writesucsesfully” : Waitms 500 : Gosub Go:
‘——————————————————————————-Go:Kp = Kp_eprom
Ki = Ki_eprom
Kd = Kd_eprom
Ts = Ts_eprom
Sp_kecepatan = Sp_kecepatan_eprom
Up = Up_eprom
Nos = Nos_eprom
Hold = Hold_epromPortc.3 = 0 ‘lcd on
Cls
Locate 2 , 5 : Lcd “Ready..!!”
Locate 1 , 8 : Lcd “-” : Waitms 150
Locate 1 , 8 : Lcd “3″ : Waitms 150
Locate 1 , 8 : Lcd “2″ : Waitms 150
Locate 1 , 8 : Lcd “1″ : Waitms 150
Locate 1 , 8 : Lcd “-” : Waitms 150
Cls : Locate 2 , 7 : Lcd “GO…” : Waitms 200 : ClsDoPortc.3 = 1 ‘lcd offCall Baca_sensor()Sensor = Konversi_sensorIf Sensor = &B00000001 Then : Pv = 20 : End If
If Sensor = &B00000010 Then : Pv = 11 : End If
If Sensor = &B00000100 Then : Pv = 5 : End If
If Sensor = &B00001000 Then : Pv = 0 : End If
If Sensor = &B00010000 Then : Pv = 0 : End If
If Sensor = &B00100000 Then : Pv = -5 : End If
If Sensor = &B01000000 Then : Pv = -11 : End If
If Sensor = &B10000000 Then : Pv = -20 : End IfIf Sensor = &B00000011 Then : Pv = 16 : End If
If Sensor = &B00000110 Then : Pv = 8 : End If
If Sensor = &B00001100 Then : Pv = 2 : End If
If Sensor = &B00011000 Then : Pv = 0 : End If
If Sensor = &B00110000 Then : Pv = -2 : End If
If Sensor = &B01100000 Then : Pv = -8 : End If
If Sensor = &B11000000 Then : Pv = -16 : End IfIf Sensor = &B00000111 Then : Pv = 12 : End If
If Sensor = &B00001110 Then : Pv = 6 : End If
If Sensor = &B00011100 Then : Pv = 3 : End If
If Sensor = &B00111000 Then : Pv = -3 : End If
If Sensor = &B01110000 Then : Pv = -6 : End If
If Sensor = &B11100000 Then : Pv = -12 : End IfIf Sensor = &B00001111 Then : Pv = 8 : End If
If Sensor = &B00011110 Then : Pv = 2 : End If
If Sensor = &B00111100 Then : Pv = 0 : End If
If Sensor = &B01111000 Then : Pv = -2 : End If
If Sensor = &B11110000 Then : Pv = -8 : End IfIf Sensor = &B00000000 Then
Portc.3 = 0 ‘lcd on
If Pv < -10 Then : Pv = -30 : Nos = Up / 2
End If
If Pv > 10 Then : Pv = 30 : Nos = Up / 2
End If
End IfIf Pv < 0 Then
N1 = Up / 2
N2 = Nos / 3
Nos = N1 + N2
End IfIf Pv > 0 Then
N1 = Up / 2
N2 = Nos / 3
Nos = N1 + N2
End If‘setpoint sensor
Sp_sensor = 0
‘nilai error
Error = Sp_sensor – Pv
‘proportional control
P = Kp * Error‘integrativ controlI1 = Error + Last_error
I2 = I1 / 2
I = Ki * I2‘derivative control
D1 = Kd * 10
D2 = Error – Last_error
D = D1 * D2‘error lampau
Last_error = Error‘proportional-derivative control
Pd = P + D‘proportional-integrativ-derivative control
Pid = Pd + I‘===== Hitung Kondisi Pertama=============’sebelah kiri
Pwm = Sp_kecepatan + Pid
If Pwm > 0 Then : Call Kiri_maju() : Locate 2 , 7 : Lcd “F” : End If
If Pwm > Up Then : Pwm = Up : End If
If Pwm < 0 Then : Call Kiri_mundur() : Pwm = 0 – Pwm : Locate 2 , 7 : Lcd “R” : End If
If Pv = 0 Then : Pwm = Nos : If Nos < Up Then : Nos = Nos + 25 : End If : End If
Pwm1a = Pwm‘=====Hitung Kondisi Kedua===============’sebelah kanan
Pwm = Sp_kecepatan – Pid
If Pwm > 0 Then : Call Kanan_maju() : Locate 2 , 16 : Lcd “F” : End If
If Pwm > Up Then : Pwm = Up : End If
If Pwm < 0 Then : Call Kanan_mundur() : Pwm = 0 – Pwm : Locate 2 , 16 : Lcd “R” : End If
If Pv = 0 Then : Pwm = Nos : If Nos < Up Then : Nos = Nos + 25 : End If : End If
Pwm1b = PwmLocate 2 , 1 : Lcd “L:” : Locate 2 , 3 : Lcd “ ” : Locate 2 , 3 : Lcd Pwm1a : Locate 2 , 10 : Lcd “R:” : Locate 2 , 12 : Lcd “ ” : Locate 2 , 12 : Lcd Pwm1b
Locate 1 , 11 : Lcd “PV:” : Locate 1 , 14 : Lcd “ ” : Locate 1 , 14 : Lcd Pv‘selang waktu cuplik
Waitms Ts ‘time sampling
Loop
EndSub Baca_sensor()
Start Adc
Adc1 = Getadc(0) : Adc1 = Adc1 / 5
Adc2 = Getadc(1) : Adc2 = Adc2 / 5
Adc3 = Getadc(2) : Adc3 = Adc3 / 5
Adc4 = Getadc(3) : Adc4 = Adc4 / 5
Adc5 = Getadc(4) : Adc5 = Adc5 / 5
Adc6 = Getadc(5) : Adc6 = Adc6 / 5
Adc7 = Getadc(6) : Adc7 = Adc7 / 5
Adc8 = Getadc(7) : Adc8 = Adc8 / 5
If Adc1 < Hold Then : S1 = &B00000000 : B1 = 0 : End If : If Adc1 > Hold Then : S1 = &B00000001 : B1 = 1 : End If
If Adc2 < Hold Then : S2 = &B00000000 : B2 = 0 : End If : If Adc2 > Hold Then : S2 = &B00000010 : B2 = 1 : End If
If Adc3 < Hold Then : S3 = &B00000000 : B3 = 0 : End If : If Adc3 > Hold Then : S3 = &B00000100 : B3 = 1 : End If
If Adc4 < Hold Then : S4 = &B00000000 : B4 = 0 : End If : If Adc4 > Hold Then : S4 = &B00001000 : B4 = 1 : End If
If Adc5 < Hold Then : S5 = &B00000000 : B5 = 0 : End If : If Adc5 > Hold Then : S5 = &B00010000 : B5 = 1 : End If
If Adc6 < Hold Then : S6 = &B00000000 : B6 = 0 : End If : If Adc6 > Hold Then : S6 = &B00100000 : B6 = 1 : End If
If Adc7 < Hold Then : S7 = &B00000000 : B7 = 0 : End If : If Adc7 > Hold Then : S7 = &B01000000 : B7 = 1 : End If
If Adc8 < 120 Then : S8 = &B00000000 : B8 = 0 : End If : If Adc8 > 120 Then : S8 = &B10000000 : B8 = 1 : End If
S12 = S1 Or S2 : S123 = S12 Or S3 : S1234 = S123 Or S4 : S12345 = S1234 Or S5 : S123456 = S12345 Or S6 : S1234567 = S123456 Or S7 : S12345678 = S1234567 Or S8
Konversi_sensor = S12345678 And &B11111111
Locate 1 , 1 : Lcd “s”
Locate 1 , 2 : Lcd B1
Locate 1 , 3 : Lcd B2
Locate 1 , 4 : Lcd B3
Locate 1 , 5 : Lcd B4
Locate 1 , 6 : Lcd B5
Locate 1 , 7 : Lcd B6
Locate 1 , 8 : Lcd B7
Locate 1 , 9 : Lcd B8
End SubSub Kiri_maju()
Reset M2a
Set M2b
End SubSub Kanan_maju()
Reset M1a
Set M1b
End SubSub Kiri_mundur()
Set M2a
Reset M2b
End SubSub Kanan_mundur()
Set M1a
Reset M1b
End Sub
#include<mega8535.h>
#include <delay.h>
#define sensor PINB
//===pwm motor alias
#define motor_ki OCR1A //sip
#define motor_ka OCR1B#define m1a PORTD.0
#define m1b PORTD.1
#define m2a PORTD.2
#define m2b PORTD.3void maju ()
{
m1a=1;m1b=0;
m2a=0;m2b=1;
}void rem_kanan ()
{
m1a=0;m1b=1;
m2a=0;m2b=1;
}void rem_kiri ()
{
m1a=1;m1b=0;
m2a=1;m2b=0;
}//===Tombol Yang Digunakan======
#define sw_ok PINC.3
#define sw_cancel PINC.2
#define sw_down PINC.1
#define sw_up PINC.0#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>
#include <stdio.h>//====Variabel di eeprom=====
eeprom int Kp = 0;
eeprom int Ki = 0;
eeprom int Kd = 0;
eeprom int Ts= 0;
eeprom int Upper= 0;
eeprom int Lower= 0;
eeprom int Set_point=0;
//====Nilai-nilai eror=======
eeprom int e0= 0;
eeprom int e1 = 0;
eeprom int e2 = 0;
eeprom int e3 = 0;
eeprom int e4 = 0;
eeprom int e5 = 0;
eeprom int e6 = 0;
eeprom int e7 = 0;int error,error1,nil_pid,pwm,kode;
char lcd_buff[33];void seting_awal ()
{
set:
Kp=Kp;
if(sw_up==0){Kp++;delay_ms(10);}
if(sw_down==0){Kp=Kp-1;delay_ms(10);}
if(Kp>100){Kp=0;delay_ms(10);}
if(Kp<0){Kp=100;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“***************”);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”Nilai Kp:%i”,Kp);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0)
{
delay_ms(150);goto set1;
}
else goto set;
//====================================
set1:
Ki=Ki;
if(sw_up==0) {Ki++;delay_ms(10);}
if(sw_down==0) {Ki=Ki-1;delay_ms(10);}
if(Ki>100){Ki=0;delay_ms(10);}
if(Ki<0) {Ki=100;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“***************”);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”Nilai Ki:%i.%i”,Ki/10,Ki%10);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set2;}
if(sw_cancel==0){delay_ms(150);goto set;}
else
goto set1;
//======================================
set2:
Kd=Kd;
if(sw_up==0) {Kd++;delay_ms(10);}
if(sw_down==0) {Kd=Kd-1;delay_ms(10);}
if(Kd>100){Kd=0;delay_ms(10);}
if(Kd<0) {Kd=100;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“***************”);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”Nilai Kd:%i”,Kd);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set3;}
if(sw_cancel==0){delay_ms(150);goto set1;}
else
goto set2;
//========================================
set3:
Ts=Ts;
if(sw_up==0) {Ts++;delay_ms(10);}
if(sw_down==0) {Ts=Ts-1;delay_ms(10);}
if(Ts>100){Ts=0;delay_ms(10);}
if(Ts<0) {Ts=100;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Time Sampling”);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”Delay:%ims”,Ts);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set4;}
if(sw_cancel==0){delay_ms(150);goto set2;}
else
goto set3;
//=============================================
set4:
Ts=Ts;
if(sw_up==0) {Set_point++;delay_ms(10);}
if(sw_down==0) {Set_point=Set_point-1;delay_ms(10);}
if(Set_point>255){Set_point=0;delay_ms(10);}
if(Set_point<0) {Set_point=255;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Set Point”);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”Kec:%i”,Set_point);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set5;}
if(sw_cancel==0){delay_ms(150);goto set3;}
else
goto set4;
//=================================================
set5:
Upper=Upper;
if(sw_up==0) {Upper++;delay_ms(10);}
if(sw_down==0) {Upper=Upper-1;delay_ms(10);}
if(Upper>255){Upper=0;delay_ms(10);}
if(Upper<0) {Upper=255;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Limit Speed”);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”Upper:%i”,Upper);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set6;}
if(sw_cancel==0){delay_ms(150);goto set4;}
else
goto set5;
//====================================================
set6:
Lower=Lower;
if(sw_up==0) {Lower++;delay_ms(10);}
if(sw_down==0) {Lower=Lower-1;delay_ms(10);}
if(Lower>255){Lower=0;delay_ms(10);}
if(Lower<0) {Lower=255;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Limit Speed”);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”Lower:%i”,Lower);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set7;}
if(sw_cancel==0){delay_ms(150);goto set5;}
else
goto set6;//========Menampilkan Eror-Eror===============
set7:
e0=e0;
if(sw_up==0) {e0++;delay_ms(10);}
if(sw_down==0) {e0=e0-1;delay_ms(10);}
if(e0>100){e0=0;delay_ms(10);}
if(e0<0) {e0=100;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Error 0″);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”e0:%i”,e0);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set8;}
if(sw_cancel==0){delay_ms(150);goto set6;}
else
goto set7;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//========Menampilkan Eror-Eror===============
set8:
e1=e1;
if(sw_up==0) {e1++;delay_ms(10);}
if(sw_down==0) {e1=e1-1;delay_ms(10);}
if(e1>100){e1=0;delay_ms(10);}
if(e1<0) {e1=100;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Error 1″);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”e1:%i”,e1);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set9;}
if(sw_cancel==0){delay_ms(150);goto set7;}
else
goto set8;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//========Menampilkan Eror-Eror===============
set9:
e2=e2;
if(sw_up==0) {e2++;delay_ms(10);}
if(sw_down==0) {e2=e2-1;delay_ms(10);}
if(e2>100){e2=0;delay_ms(10);}
if(e2<0) {e2=100;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Error 2″);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”e2:%i”,e2);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set10;}
if(sw_cancel==0){delay_ms(150);goto set8;}
else
goto set9;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//========Menampilkan Eror-Eror===============
set10:
e3=e3;
if(sw_up==0) {e3++;delay_ms(10);}
if(sw_down==0) {e3=e3-1;delay_ms(10);}
if(e3>100){e3=0;delay_ms(10);}
if(e3<0) {e3=100;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Error 3″);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”e3:%i”,e3);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set11;}
if(sw_cancel==0){delay_ms(150);goto set9;}
else
goto set10;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//========Menampilkan Eror-Eror===============
set11:
e4=e4;
if(sw_up==0) {e4++;delay_ms(10);}
if(sw_down==0) {e4=e4-1;delay_ms(10);}
if(e4>100){e4=0;delay_ms(10);}
if(e4<0) {e4=100;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Error 4″);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”e4:%i”,e4);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set12;}
if(sw_cancel==0){delay_ms(150);goto set10;}
else
goto set11;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//========Menampilkan Eror-Eror===============
set12:
e5=e5;
if(sw_up==0) {e5++;delay_ms(10);}
if(sw_down==0) {e5=e5-1;delay_ms(10);}
if(e5>100){e5=0;delay_ms(10);}
if(e5<0) {e5=100;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Error 5″);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”e5:%i”,e5);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set13;}
if(sw_cancel==0){delay_ms(150);goto set11;}
else
goto set12;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//========Menampilkan Eror-Eror===============
set13:
e6=e6;
if(sw_up==0) {e6++;delay_ms(10);}
if(sw_down==0) {e6=e6-1;delay_ms(10);}
if(e6>100){e6=0;delay_ms(10);}
if(e6<0) {e6=100;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Error 6″);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”e6:%i”,e6);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto set14;}
if(sw_cancel==0){delay_ms(150);goto set12;}
else
goto set13;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//========Menampilkan Eror-Eror===============
set14:
e7=e7;
if(sw_up==0) {e7++;delay_ms(10);}
if(sw_down==0) {e7=e7-1;delay_ms(10);}
if(e7>100){e7=0;delay_ms(10);}
if(e7<0) {e7=100;delay_ms(10);}
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Error 7″);
lcd_gotoxy(0,1);
sprintf(lcd_buff,”e7:%i”,e7);
lcd_puts(lcd_buff);
delay_ms(100);
if(sw_ok==0) {delay_ms(150);goto Magelang;}
if(sw_cancel==0){delay_ms(150);goto set13;}
else
goto set14;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Magelang:
lcd_clear();
lcd_gotoxy(1,0);
lcd_putsf(” Save To EEPROM”);
delay_ms(200);
Kp=Kp;Ki=Ki;Kd=Kd;Ts=Ts;Set_point=Set_point;Lower=Lower;Upper=Upper;
e0=e0;e1=e1;e2=e2;e3=e3;e4=e4;e5=e5;e6=e6;e7=e7;}void tampilan_awal()
{
lcd_gotoxy(0,0);
lcd_putsf(” C_Tools__! “);
delay_ms(1000);
lcd_gotoxy(6,1);
lcd_putsf(“Present “);
delay_ms(1000);
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Fahmizal_DTE_UGM”);
delay_ms(500);
lcd_gotoxy(0,1);
lcd_putsf(” Line Follower “);
delay_ms(1000);
lcd_clear();
}//=====Perlihatkan Sensor=========
//#define sensor PINA
#define s0 PINB.0
#define s1 PINB.1
#define s2 PINB.2
#define s3 PINB.3
#define s4 PINB.4
#define s5 PINB.5
#define s6 PINB.6
#define s7 PINB.7void show_sensor()
{
lcd_gotoxy(2,1);
if (s7) lcd_putchar(’1′);
else lcd_putchar(’0′);
if (s6) lcd_putchar(’1′);
else lcd_putchar(’0′);
if (s5) lcd_putchar(’1′);
else lcd_putchar(’0′);
if (s4) lcd_putchar(’1′);
else lcd_putchar(’0′);
if (s3) lcd_putchar(’1′);
else lcd_putchar(’0′);
if (s2) lcd_putchar(’1′);
else lcd_putchar(’0′);
if (s1) lcd_putchar(’1′);
else lcd_putchar(’0′);
if (s0) lcd_putchar(’1′);
else lcd_putchar(’0′);
}
//=====Tutup Perlihatkan Sensor====void PID ()
{
//***hitam=mati/0 & putih=nyala/1//=====sensor mengarah ke Kiri
if (sensor==0b11101110) {error=e0;}
else if (sensor==0b11001110) {error=-e1;}
else if (sensor==0b11011110) {error=-e2;}
else if (sensor==0b10011110) {error=-e3;}
else if (sensor==0b10111110) {error=-e4;}
else if (sensor==0b00111110) {error=-e5;}
else if (sensor==0b01111110) {error=-e6;kode=12;}//=====sensor mengarah ke Kanan
else if (sensor==0b11101110) {error=e0;}
else if (sensor==0b11100110) {error=e1;}
else if (sensor==0b11110110) {error=e2;}
else if (sensor==0b11110010) {error=e3;}
else if (sensor==0b11111010) {error=e4;}
else if (sensor==0b11111000) {error=e5;}
else if (sensor==0b11111100) {error=e6;kode=24;}else if (sensor==0b11111110)//di putih semua
{
if (kode==12)
{
error=-e7;
rem_kiri();
delay_ms(20);
}
else if (kode==24)
{
error=e7;
rem_kanan();
delay_ms(20);
}
}maju();
nil_pid=((Kp*error)+((Ki/10)*(error+error1)*Ts)+((Kd/Ts)*(error-error1)));
error1=error;//===== Hitung Kondisi Pertama=============
pwm=Set_point-(nil_pid);
if(pwm>Upper){pwm=Upper;}
if(pwm<Lower){pwm=Lower;}
motor_ka=pwm;
//=====Hitung Kondisi Kedua===============
pwm=Set_point+(nil_pid);
if(pwm>Upper){pwm=Upper;}
if(pwm<Lower){pwm=Lower;}
motor_ki=pwm;
//kode=0;
delay_ms(Ts);
}void show_detail()
{
lcd_gotoxy(0,0);
sprintf(lcd_buff,”e:%i”,error);
lcd_puts(lcd_buff);
lcd_gotoxy(5,0);
sprintf(lcd_buff,”Ki%i”,motor_ki);
lcd_puts(lcd_buff);
lcd_gotoxy(11,0);
sprintf(lcd_buff,”Ka%i”,motor_ka);
lcd_puts(lcd_buff);
}
void inisialisasi_port()
{
//===Seting untuk sensor============
DDRB=0b00000000;
PORTB=0b11111111;
//===Seting untuk fungsi Tombol======
DDRC=0×00;
PORTC=0xff;
//===Seting motor====================
DDRD=0xff;
PORTD=0×00;
//——-inisialisasi PWM—–//
TCCR1A=0xA3;
TCCR1B=0x0B;
TCNT1=0×0000;//PWM 16 bit//
OCR1A=0x3FF;
OCR1B=0x3FF;
TIFR=0;}
void main(void)
{
inisialisasi_port();
lcd_init(16);
lcd_clear();
tampilan_awal();
seting_awal();
while(1)
{
lcd_clear();
PID();
show_sensor();
kode=0;
show_detail();
delay_ms(Ts);
}
}