12.16.2012

Kendali Motor dengan Simulasi Proteus 7.5 dan GUI Matlab

Salam Robotika Indonesia !!
Kali ini saya mau posting tentang Kendali motor dengan Proteus dan dijalankan dengan GUI dari matlab.
Saya menggunakan program bahasa C dengan mikro Atmega 16/32/8535.

Komponen yang digunakan adalah :
1. IC ATmega 16/32/8535
2. Push Button
3. Motor DC
4. Relay 5v
5. DB-9
6. LCD 16x2
7. X-Tal 12Mhz
8. IC Max 232 (Kalo dibutuhkan)

Ini adalah salah GUI dari Matlab :


Untuk menghubungkan GUI Matlab dibutuhkan sebuah aplikasi serial virtual. Aplikasi yang saya gunakan adalah Virtual Serial Port Emulator (VSPE) atau bisa juga memakai Virtual Serial Port Driver (VSPD).



Pilih pair dan tentukan COM sesuai yang di ingikan. Listing Program untuk mikro :
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : Kendali Motor
Version : 2.01 
Date    : 06/12/2012
Author  : Herwin
Company : FT Untirta
Comments: Dalam proses pengerjaan


Chip type               : ATmega16
Program type            : Application
AVR Core Clock frequency: 12,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega16.h>
#include <delay.h>
#include <delay.h>

// Definisi Tombol
#define s1 PINB.4
#define s2 PINB.5
#define s3 PINB.6

// Definisi Relay
#define relay1 PORTB.0
#define relay2 PORTB.1
#define relay3 PORTB.2

// Alphanumeric LCD Module functions
#include <alcd.h>

// Standard Input/Output functions
#include <stdio.h>

#define ADC_VREF_TYPE 0x00

// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=Out 
// State7=T State6=T State5=T State4=T State3=T State2=0 State1=0 State0=0 
PORTB=0b01110000;
DDRB=0x07;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x4D;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 750,000 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: ADC Stopped
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// Alphanumeric LCD initialization
// Connections specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTC Bit 0
// RD - PORTC Bit 1
// EN - PORTC Bit 2
// D4 - PORTC Bit 4
// D5 - PORTC Bit 5
// D6 - PORTC Bit 6
// D7 - PORTC Bit 7
// Characters/line: 16
lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf("UjiKendaliMotor");
lcd_gotoxy(0,1);
lcd_putsf("Oleh : Herwin");

while (1)
      {
      /*if (s1==0){lcd_clear();relay1=1;delay_ms(10);relay2=0;delay_ms(10);relay3=0;delay_ms(10);lcd_gotoxy(0,0);lcd_putsf("Relay 1");};
      if (s2==0){lcd_clear();relay1=0;delay_ms(10);relay2=1;delay_ms(10);relay3=0;delay_ms(10);lcd_gotoxy(0,0);lcd_putsf("Relay 2");};
      if (s3==0){lcd_clear();relay1=0;delay_ms(10);relay2=0;delay_ms(10);relay3=1;delay_ms(10);lcd_gotoxy(0,0);lcd_putsf("Relay 3");};*/

      if (getchar()=='1'){lcd_clear();relay1=1;delay_ms(10);relay2=0;delay_ms(10);relay3=0;delay_ms(10);lcd_gotoxy(0,0);lcd_putsf("Relay 1");};
      if (getchar()=='2'){lcd_clear();relay1=0;delay_ms(10);relay2=1;delay_ms(10);relay3=0;delay_ms(10);lcd_gotoxy(0,0);lcd_putsf("Relay 2");};
      if (getchar()=='3'){lcd_clear();relay1=0;delay_ms(10);relay2=0;delay_ms(10);relay3=1;delay_ms(10);lcd_gotoxy(0,0);lcd_putsf("Relay 3");};
      }
}

ini adalah program matlabnya :
function varargout = project(varargin)
% PROJECT MATLAB code for project.fig
%      PROJECT, by itself, creates a new PROJECT or raises the existing
%      singleton*.
%
%      H = PROJECT returns the handle to a new PROJECT or the handle to
%      the existing singleton*.
%
%      PROJECT('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PROJECT.M with the given input arguments.
%
%      PROJECT('Property','Value',...) creates a new PROJECT or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before project_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to project_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help project

% Last Modified by GUIDE v2.5 06-Dec-2012 07:12:16

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @project_OpeningFcn, ...
                   'gui_OutputFcn',  @project_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before project is made visible.
function project_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to project (see VARARGIN)

% Choose default command line output for project
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes project wait for user response (see UIRESUME)
% uiwait(handles.figure1);

%Program untuk inisialisasi Port dan Baudrate
global s
s = serial('COM1');
set(s,'BaudRate',9600);
fopen(s);

% --- Outputs from this function are returned to the command line.
function varargout = project_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in relay1.
function relay1_Callback(hObject, eventdata, handles)
% hObject    handle to relay1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global s
data1='1';
fprintf(s,'%s',data1);
fwrite(s,13);


% --- Executes on button press in relay2.
function relay2_Callback(hObject, eventdata, handles)
% hObject    handle to relay2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global s
data2='2';
fprintf(s,'%s',data2);
fwrite(s,13);

% --- Executes on button press in relay3.
function relay3_Callback(hObject, eventdata, handles)
% hObject    handle to relay3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global s
data3='3';
fprintf(s,'%s',data3);
fwrite(s,13);

% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
fclose(s)
delete(s)
clear s
delete(hObject);