You are on page 1of 13

MATLAB Serial Communication


for quire contact:

LED control by MATLAB and
MASSAGE display on LCD, RS232
PIC16F877A is an easy micro controller for the beginners
Here in project:

How PIC switches to TWO LEDS from MATLAB Graphic User Interface
HOW we use PIC in Serial communication
How LCD is used with PIC
How PIC is connected with RS232 PORT of PC

Than the micro controller read input from the virtual terminal an than display on the
LCD and also Switch on the LED
a. MickroC PRO
b. Proteous 8 professional
c. MATLAB 2009
commands for CKT
d. Virtual Port Driver 6.9v
your system

// for programming of MICRO CONTROLLER
//For simulation of CKT and Design
// for controlling and sending Control
// for generation of Pair of virtual ports on

Engr Rana Muhammad Shakeel


plz like my page:

Engr Rana M Shakeel, +92-333-4962507 |

com/Electrical4Electronics Circuit Diagram SETTING FOR COMPIM: 2 Engr Rana M . +92-333-4962507 | engnr.MATLAB Serial Communication.shakeel@gmail.facebook. for quire contact: https://www.

com Virtual Port Driver 6. for quire contact: engnr.9v PORT settings 3 Engr Rana M Shakeel.shakeel@gmail.shakeel@gamil.MATLAB Serial Communication. +92-333-4962507 | .

shakeel@gamil. for quire contact: engnr.shakeel@gmail. +92-333-4962507 | You can also see these virtual ports from Control Panel-Device manger-Ports Code for MATLAB LED GUI: 4 Engr Rana M Shakeel.MATLAB Serial .

. by itself.hObject. for quire contact: engnr.M with the given input arguments.fig % LED.shakeel@gamil. % % LED('CALLBACK'.com .eventData. +92-333-4962507 | engnr. creates a new LED or raises the existing % singleton*.handles.. % % H = LED returns the handle to a new LED or the handle to % the existing singleton*.shakeel@gmail.. % 5 Engr Rana M Shakeel.) calls the local % function named CALLBACK in LED.MATLAB Serial CODE: function varargout = LED(varargin) % LED M-file for LED.

% % *See GUI Options on GUIDE's Tools menu. handles). Starting from the left.MATLAB Serial Communication. GUIHANDLES % Edit the above text to modify the response to help LED % Last Modified by GUIDE v2. 'gui_LayoutFcn'.DO NOT EDIT gui_Singleton = 1. .'Value'. .. varargin) % This function has no output args. property value pairs are % applied to the GUI before LED_OpeningFcn gets called... mfilename.shakeel@gamil. % hObject handle to figure % eventdata reserved . varargin{:}). .Executes just before LED is made visible.shakeel@gmail. +92-333-4962507 | engnr. @LED_OpeningFcn. for quire contact: engnr..DO NOT EDIT % --. if nargin && ischar(varargin{1}) gui_State. else gui_mainfcn(gui_State. end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State. % % See also: GUIDE..... 6 Engr Rana M Shakeel. []).com .to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to LED (see VARARGIN) % Choose default command line output for LED handles. % Update handles structure guidata(hObject. eventdata.. 'gui_Callback'. All inputs are passed to LED_OpeningFcn via varargin.output = hObject. gui_Singleton.. [] .) creates a new LED or raises the % existing singleton*.5 26-Nov-2013 19:21:22 % Begin initialization code . end % End initialization code . function LED_OpeningFcn(hObject. Choose "GUI allows only one % instance to run (singleton)" % LED('Property'. 'gui_Singleton'. varargin{:}). GUIDATA. 'gui_OpeningFcn'. .gui_Callback = str2func(varargin{1}). 'gui_OutputFcn'. An % unrecognized property name or invalid value makes property application % stop. gui_State = struct('gui_Name'. . see OutputFcn.. @LED_OutputFcn. handles..

to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) clear all. function varargout = LED_OutputFcn(hObject.Outputs from this function are returned to the command line. handles) % varargout cell array for returning output args (see VARARGOUT). handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved . % --. function pushbutton2_Callback(hObject. eventdata.shakeel@gamil. % hObject handle to figure % eventdata reserved .'%s'. fclose(s) delete (s) clear s % be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) clear all. eventdata. fprintf(s.MATLAB Serial Communication.shakeel@gmail. % --.'BaudRate'. s=serial('COM1').com % UIWAIT makes LED wait for user response (see UIRESUME) % uiwait(handles.'b'). handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved . fopen(s). eventdata. fprintf(s. function pushbutton1_Callback(hObject.9600).com .'g'). fclose(s) delete (s) clear s Code for PIC 16F877A: 7 Engr Rana M Shakeel.'%s'. set(s. fopen(s).'BaudRate'.Executes on button press in pushbutton2. +92-333-4962507 | engnr. set(s.figure1). s=serial('COM1').Executes on button press in 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.9600). for quire contact: engnr.output.

sbit LCD_EN_Direction at TRISB5_bit. // char *b. // char *g.shakeel@gmail. sbit LCD_D5 at RB1_bit. sbit LCD_D7 at RB3_bit. sbit LCD_D5_Direction at TRISB1_bit. sbit LCD_EN at // LCD module connections sbit LCD_RS at RB4_bit. +92-333-4962507 | engnr. sbit LCD_D7_Direction at TRISB3_bit. sbit LCD_D4_Direction at TRISB0_bit. // Declare variables char *text. // char uart_rd. // End LCD module connections unsigned int adc_rd. sbit LCD_D6_Direction at TRISB2_bit.MATLAB Serial Communication.shakeel@gamil. for quire contact: engnr. sbit LCD_D6 at RB2_bit. // char *error. sbit LCD_RS_Direction at . sbit LCD_D4 at RB0_bit. // void introduction (void) 8 Engr Rana M Shakeel.

Lcd_Cmd(_LCD_CLEAR).1. Lcd_Out(2. // LCD command (clear LCD) // Define the first message // Write the first message in the first line // Define the second message // Define the first message Delay_ms(1000).text).1.1.text). text = " Shakeel ".com { Lcd_Cmd(_LCD_CLEAR). text = "by PIC16F877A". } void main () { TRISD=0. Lcd_Out(1. // LCD command (clear LCD) // Define the first message Lcd_Out(1. // Define the second message // Define the first message Delay_ms(1000). +92-333-4962507 | engnr.text). // Define the first message // Write the first message in the first line Delay_ms(100).shakeel@gmail.1.1. text = " Serial COM ". Lcd_Out(2. text = "Enter : g or b".com . Lcd_Cmd(_LCD_CLEAR). for quire contact: engnr.text). text = "Engr Rana M". 9 Engr Rana M Shakeel.MATLAB Serial Communication.text). Lcd_Out(1.shakeel@gamil.

g = "g= Green LED ". adcon1=0x00. // LCD command (cursor off) // LCD command (clear LCD) Delay_ms(100). // READ DATA AND SAVE TO VARIABLE if (uart_rd == 'g') // if i send g to MC it ON the LED D1 { portd=0b00000001. // Initialize hardware UART1 and establish communication at 9600 bps // Initializes desired hardware UART module with the desired baud rate Delay_ms (1000). portb=1. b = "b= Blue LED ". 10 Engr Rana M Shakeel.shakeel@gamil. // LCD command (clear LCD) Lcd_Out(1. trisB=0x00.. Lcd_Cmd(_LCD_CLEAR). +92-333-4962507 | engnr. for quire contact: engnr. Lcd_Init(). introduction (void). // Define the GREEN LED message // Define the BLUE LED message do { if(UART1_Data_Ready()) // data is ready read it { uart_rd=UART1_Read().com PORTD=0. // LCD display initialization Lcd_Cmd(_LCD_CURSOR_OFF). Lcd_Cmd(_LCD_CLEAR).com . Delay_ms(500).text).shakeel@gmail.2. UART1_Init(9600).MATLAB Serial Communication.

shakeel@gamil. Delay_ms(500).2.g). Lcd_Out(2.. Delay_ms(500). Lcd_Cmd(_LCD_CLEAR).com Lcd_Out(2. for quire contact: engnr.b) .Delay_ms(500).2. } if (uart_rd == 'b') // if i send b to MC it ON the LED D2 { portd=0b00100000. +92-333-4962507 | engnr. // LCD command (clear LCD) Lcd_Out(1. } } } while(1).text). } 11 Engr Rana M Shakeel.MATLAB Serial Communication.

com . +92-333-4962507 | engnr.MATLAB Serial OUT PUT: 12 Engr Rana M Shakeel.shakeel@gmail. for quire contact: engnr.shakeel@gamil.

shakeel@gamil.MATLAB Serial Engr Rana M Shakeel. +92-333-4962507 | . 13 for quire contact: engnr.