1. C:UsersCARLOSDesktoppractica dspicUART_LEDS.c
#include "p30fxxxx.h" #include "LCD.h" #include "UART.h"
#include "libpic30.h"
//Macros para la configuración de los fusibles
_FOSC(CSW_FSCM_OFF & XT_PLL4); _FWDT(WDT_OFF); _FBORPOR(PBOR_OFF & PWRT_16 & MCLR_EN);
_FGS(CODE_PROT_OFF);
#define BORRAR_HYPERTERMINAL TX_UART1_CHAR('f')
unsigned char rx; void main() {
ADPCFG = 0xFFFF;
__delay_ms(15);
//LCD_CONFIG();
INICIO_UART1(_9600);
//BORRAR_HYPERTERMINAL; //TX_UART1_MENSAJE("Hola alumnos UTPnr",18); //TX_UART1_MENSAJE("ttCuanto va el Peru
Panama?nr",29);
ADPCFG = 0XFFFF;
TRISB = 0xFF00; //Los cuatro pines menos significativos son salida
while(1);
}
void __attribute__((__interrupt__))_U1RXInterrupt(void)
{
rx = U1RXREG;
// if(rx=='s' || rx=='S')
if(rx=='A' ) { PORTBbits.RB0 = 1; __delay_ms(200);PORTBbits.RB0 = 0; __delay_ms(200);}
if(rx=='B' ) { PORTBbits.RB1 = 1; __delay_ms(200);PORTBbits.RB1 = 0; __delay_ms(200); }
// TX_UART1_MENSAJE("Va ganando Peru 2 a 1n",23);
IFS0bits.U1RXIF = 0;
}
2. // ***********************CONFIGURACION DEL REGISTRO UART***************************
void INICIO_UART1(unsigned char baudios)
{
U1BRG = baudios; //9600 bps, U1BRG es un registro de 16 bits U1MODE = 0; //8 bits de datos, sin paridad(STSEL=0) y 1 bit de
stop.
/*PDSEL<1:0> - Parity and data selection bits 00 – 8-bit data, no parity 01 – 8-bit data, even parity 10 – 8-bit data, odd parity 11 – 9-bit
data, no parity*/
// STSEL – STOP selection bit (STSEL=0 one STOP bit, STSEL=1 two STOP bits)
U1MODEbits.UARTEN = 1; // habilito UART dentro del registro U1MODE
U1STAbits.UTXEN = 1; // habilito la transmision dentro del registro U1STA
IPC2 = IPC2 | 0x0030; // Prioridad nivel 3 para U1RXIP
/* Los registros IECx permite habilitar las respectivas interrupciones del CPU. Una vez puesto a '1' logico el respectivo bit, la
interrupcion queda activa y es posible que un evento la dispare. Estos bits,normalmente se habilitan una sola vez en el programa, a
menos que el programador desee desactivar la interrupcion a raiz de otro evento */
IEC0bits.U1RXIE = 1; // Para habilitar la interrupcion
/* Los registros IFSx contienen las banderas de las diversas interrupciones del DSPIC30F3014. Estas banderas automaticamente se
ponen a '1' logico para indicar que una interrupcion ha ocurrido. Una vez atendida la interrupcion esta bandera debe ser puesta a '0'
logico por firmware, ya que de no hacerlo la rutina de interrupcion sera considerada no atendida por el CPU y esta se volvera a
ejecutar */
IFS0bits.U1RXIF = 0; // Para borrar la bandera o flag de interrupcion por el U1RXREG
}
void TX_UART1_CHAR(unsigned char dato)
{
U1TXREG = dato;
while(U1STAbits.TRMT == 0);
/* TRMT - Transmitir registro de desplazamiento es poco vacío (TRMT = 0 transmiten registro de desplazamiento no está vacío,
transmisión en curso, TRMT = 1 registro de desplazamiento de transmisión está vacía, la transmisión completa) */
}
4. EN LA FIGURA SE MUESTRA LA MANERA DE AJUNTAR ESTOS ARCHIVOS “xxx.h” y “xxx.c”
5. INTERFAZ GUI MATLAB PARA PROBAR LA COMUNICACIÓN SERIAL DSPIC30F3014
function varargout = LEDS(varargin)
% LEDS M-file for LEDS.fig
%
LEDS, by itself, creates a new LEDS or raises the existing
%
singleton*.
%
%
H = LEDS returns the handle to a new LEDS or the handle to
%
the existing singleton*.
%
%
LEDS('CALLBACK',hObject,eventData,handles,...) calls the local
%
function named CALLBACK in LEDS.M with the given input arguments.
%
%
LEDS('Property','Value',...) creates a new LEDS or raises the
%
existing singleton*. Starting from the left, property value pairs are
%
applied to the GUI before LEDS_OpeningFcn gets called. An
%
unrecognized property name or invalid value makes property application
%
stop. All inputs are passed to LEDS_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
6. % Edit the above text to modify the response to help LEDS
% Last Modified by GUIDE v2.5 15-Feb-2014 11:26:49
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @LEDS_OpeningFcn, ...
'gui_OutputFcn', @LEDS_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 LEDS is made visible.
function LEDS_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 LEDS (see VARARGIN)
a=imread('ALF.jpg');
imshow(a);
% Choose default command line output for LEDS
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes LEDS wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
7. function varargout = LEDS_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 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('COM3');
set(s,'Baudrate',9600);
fopen(s);
fprintf(s,'%s','A');
fclose(s);
delete(s);
clear s
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject
handle to pushbutton3 (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('COM3');
set(s,'Baudrate',9600);
fopen(s);
fprintf(s,'%s','B');
fclose(s);
delete(s);
clear s