Sei sulla pagina 1di 13

MATLAB Serial Communication,

for quire contact: engnr.shakeel@gamil.com

LED control by MATLAB and


MASSAGE display on LCD, RS232
SERIAL COMMUNICATION, and PIC
16F877A
PIC16F877A is an easy micro controller for the beginners
Here in project:
1)
2)
3)
4)

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

BEST OF LUCK.
Engr Rana Muhammad Shakeel
Engnr.shakeel@gmail.com
+92-333-4962507
https://www.facebook.com/EngnrShakeel

FOR MY MORE SHORT PROJECTS:


https://uettaxila.academia.edu/EngrRanaMShakeel

plz like my page:


1

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

MATLAB Serial Communication,

for quire contact: engnr.shakeel@gamil.com

https://www.facebook.com/Electrical4Electronics

Circuit Diagram

SETTING FOR COMPIM:

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

MATLAB Serial Communication,

for quire contact: engnr.shakeel@gamil.com

Virtual Port Driver 6.9v PORT


settings

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

MATLAB Serial Communication,

for quire contact: engnr.shakeel@gamil.com

You can also see these virtual ports from Control Panel-Device manger-Ports

Code for MATLAB LED GUI:

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

MATLAB Serial Communication,

for quire contact: engnr.shakeel@gamil.com

CODE:
function varargout = LED(varargin)
% LED M-file for LED.fig
%
LED, by itself, creates a new LED or raises the existing
%
singleton*.
%
%
H = LED returns the handle to a new LED or the handle to
%
the existing singleton*.
%
%
LED('CALLBACK',hObject,eventData,handles,...) calls the local
%
function named CALLBACK in LED.M with the given input
arguments.
%
5

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

MATLAB Serial Communication,

for quire contact: engnr.shakeel@gamil.com

%
LED('Property','Value',...) creates a new LED or raises the
%
existing singleton*. Starting from the left, property value
pairs are
%
applied to the GUI before LED_OpeningFcn gets called. An
%
unrecognized property name or invalid value makes property
application
%
stop. All inputs are passed to LED_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 LED
% Last Modified by GUIDE v2.5 26-Nov-2013 19:21:22
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @LED_OpeningFcn, ...
'gui_OutputFcn', @LED_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 LED is made visible.
function LED_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 LED (see VARARGIN)
% Choose default command line output for LED
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
6

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

MATLAB Serial Communication,

for quire contact: engnr.shakeel@gamil.com

% UIWAIT makes LED wait for user response (see UIRESUME)


% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = LED_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject
handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
clear all;
s=serial('COM1');
set(s,'BaudRate',9600);
fopen(s);
fprintf(s,'%s','g');
fclose(s)
delete (s)
clear s

% --- Executes on button press in pushbutton2.


function pushbutton2_Callback(hObject, eventdata, handles)
% hObject
handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
clear all;
s=serial('COM1');
set(s,'BaudRate',9600);
fopen(s);
fprintf(s,'%s','b');
fclose(s)
delete (s)
clear s

Code for PIC 16F877A:


7

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

MATLAB Serial Communication,

for quire contact: engnr.shakeel@gamil.com

// LCD module connections


sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

unsigned int adc_rd;

// Declare variables

char *text;

//

char *error;

//

char *g;

//

char *b;

//

char uart_rd;

//

void introduction (void)


8

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

MATLAB Serial Communication,

for quire contact: engnr.shakeel@gamil.com

Lcd_Cmd(_LCD_CLEAR);
text = " Serial COM ";
Lcd_Out(1,1,text);
text = "by PIC16F877A";
Lcd_Out(2,1,text);

// 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);
Lcd_Cmd(_LCD_CLEAR);
text = "Engr Rana M";
Lcd_Out(1,1,text);

// Define the first message


// Write the first message in the first line

Delay_ms(100);
text = " Shakeel ";
Lcd_Out(2,1,text);

// Define the second message


// Define the first message

Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
text = "Enter : g or b";

// LCD command (clear LCD)


// Define the first message

Lcd_Out(1,1,text);

void main ()
{
TRISD=0;
9

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

MATLAB Serial Communication,

for quire contact: engnr.shakeel@gamil.com

PORTD=0;
UART1_Init(9600); // Initialize hardware UART1 and establish communication at
9600 bps
// Initializes desired hardware UART module with the desired baud rate
Delay_ms (1000);

trisB=0x00;
portb=1;
adcon1=0x00;

Lcd_Init();

// LCD display initialization

Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR);

// LCD command (cursor off)


// LCD command (clear LCD)

Delay_ms(100);
introduction (void);
g = "g= Green LED ";
b = "b= Blue LED ";

// Define the GREEN LED message


// Define the BLUE LED message

do {
if(UART1_Data_Ready()) // data is ready read it
{ uart_rd=UART1_Read(); // READ DATA AND SAVE TO VARIABLE
if (uart_rd == 'g') // if i send g to MC it ON the LED D1
{
portd=0b00000001; Delay_ms(500);;
Lcd_Cmd(_LCD_CLEAR);

// LCD command (clear LCD)

Lcd_Out(1,2,text);
10

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

MATLAB Serial Communication,

for quire contact: engnr.shakeel@gamil.com

Lcd_Out(2,2,g);
Delay_ms(500);
}
if (uart_rd == 'b') // if i send b to MC it ON the LED D2
{
portd=0b00100000;Delay_ms(500);;
Lcd_Cmd(_LCD_CLEAR);

// LCD command (clear LCD)

Lcd_Out(1,2,text);
Lcd_Out(2,2,b);
Delay_ms(500);
}
}
}
while(1);
}

11

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

MATLAB Serial Communication,

for quire contact: engnr.shakeel@gamil.com

OUT PUT:

12

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

MATLAB Serial Communication,

13

for quire contact: engnr.shakeel@gamil.com

Engr Rana M Shakeel, +92-333-4962507 | engnr.shakeel@gmail.com

Potrebbero piacerti anche