//***********************************************************************************
//
// PROGRAMA:
// Obtener datos enviados por computador a traves de comunicacion RS232,
// los datos se estan enviando atraves de una aplicacion software llamada terminal
// similar al hyperterminal de windows, pero con mejores caracteristicas
//
// Entorno de programacion: MPLAB v8.20
// Compilador: C30
// Dispositivo: dsPIC30F4011
// Placa desarrollo: dsPICDEM2
//
//***********************************************************************************
#include <p30f4011.h>
#include <uart.h>
#include <stdlib.h>
//Macros for Configuration Fuse Registers:
//Invoke macros to set up device configuration fuse registers.
//The fuses will select the oscillator source, power-up timers, watch-dog
//timers, BOR characteristics etc. The macros are defined within the device
//header files. The configuration fuse registers reside in Flash memory.
_FOSC(CSW_FSCM_OFF & XT_PLL8); //Run this project using an external crystal
//routed via the PLL in 8x multiplier mode
//For the 7.3728 MHz crystal we will derive a
//throughput of 7.3728e+6*8/4 = 14.74 MIPS(Fcy)
//,~67nanoseconds instruction cycle time(Tcy).
_FWDT(WDT_OFF); //Turn off the Watch-Dog Timer.
_FBORPOR(MCLR_EN & PWRT_OFF); //Enable MCLR reset pin and turn off the
//power-up timers.
_FGS(CODE_PROT_OFF); //Disable Code Protection
//Defines for System Clock Timing -
//For oscillator configuration XT x PLL8 mode,
//Device Throughput in MIPS = Fcy = 7372800*8/4 = ~14.74 MIPS
//Instruction Cycle time = Tcy = 1/(Fcy) = ~68 nanoseconds
#define XTFREQ 7372800 //On-board Crystal frequency
#define PLLMODE 8 //On-chip PLL setting
#define FCY XTFREQ*PLLMODE/4 //Instruction Cycle Frequency
#define BAUDRATE 9600 //Designamos rango de baudios
#define BRGVAL (((FCY/BAUDRATE)/16)-1) //Formula para registro U1BRG, obtenida del manual
//dsPIC30F Family Reference Manual
/*****************
** PROTOTIPOS **
*****************/
void UART_Init (void);
void __attribute__((__interrupt__)) _U1RXInterrupt(void);
int main (void);
/****************
** GLOBALES **
****************/
unsigned char bufferRX[4]; //buffer donde voy almacenar los elementos de la pila
unsigned char *Ptro_bufferRX=NULL; //inicializacion del buffer
unsigned char index=0; //contador que me indica las veces que se ha leido un dato
/*****************
** FUNCIONES **
*****************/
void UART_Init (void)
{
unsigned int U1MODEvalue;
unsigned int U1STAvalue;
CloseUART1(); //Turn off UART1module
//Configuramos interrupciones de UART1, recepcion
ConfigIntUART1(UART_TX_INT_DIS & UART_RX_INT_EN & UART_RX_INT_PR7);
//configuramos los registros UMODE y USTA
U1MODEvalue= UART_EN & UART_IDLE_CON & UART_DIS_WAKE & UART_DIS_ABAUD & UART_ALTRX_ALTTX &
UART_DIS_LOOPBACK & UART_ODD_PAR_8BIT & UART_1STOPBIT;
U1STAvalue= UART_INT_TX & UART_INT_TX_BUF_EMPTY & UART_TX_PIN_NORMAL & UART_TX_DISABLE &
UART_INT_RX_CHAR & UART_ADR_DETECT_DIS & UART_RX_OVERRUN_CLEAR;
OpenUART1(U1MODEvalue, U1STAvalue, BRGVAL); //activamnos el uart con la configuracion seleccionada
Ptro_bufferRX= &bufferRX[0]; //el puntero apunta al primer elemento del buffer
}
/*********************
** Interrupciones **
*********************/
// This is UART1 receive ISR
void __attribute__((__interrupt__,no_auto_psv)) _U1RXInterrupt(void)
{
IFS0bits.U1RXIF = 0; //borramos flag de interrupcion
if((U1STAbits.PERR==0) && (U1STAbits.FERR==0)) //compruebo errores
{
(*(Ptro_bufferRX)++)=U1RXREG;
}
if(U1STAbits.OERR) U1STAbits.OERR=0; //si hay error de overflow es borrado
}
/***********
** main **
***********/
int main (void)
{
UART_Init(); //iniciamos las configuracion de UART
while(1) //bucle infinito, loop main
{
if(index==4) //se acede cuando se han leido cuatro bytes
{
Ptro_bufferRX= &bufferRX[0]; //vuelvo apuntar al primer elemento del buffer
index=0; //inicializo contador
}
}
CloseUART1(); //si por alguan circunstacia se abandona loop main se cierra el dispostivo UART
return 0;
}
//main
//***********************************************************************************
//
// PROGRAMA:
// Obtener datos enviados por computador a traves de comunicacion RS232,
// los datos se estan enviando atraves de una aplicacion software llamada terminal
// similar al hyperterminal de windows, pero con mejores caracteristicas
//
// Entorno de programacion: MPLAB v8.20
// Compilador: C30
// Dispositivo: dsPIC30F4011
// Placa desarrollo: dsPICDEM2
//
//***********************************************************************************
#include <p30f4011.h>
#include <stdlib.h>
//Macros for Configuration Fuse Registers:
//Invoke macros to set up device configuration fuse registers.
//The fuses will select the oscillator source, power-up timers, watch-dog
//timers, BOR characteristics etc. The macros are defined within the device
//header files. The configuration fuse registers reside in Flash memory.
_FOSC(CSW_FSCM_OFF & XT_PLL8); //Run this project using an external crystal
//routed via the PLL in 8x multiplier mode
//For the 7.3728 MHz crystal we will derive a
//throughput of 7.3728e+6*8/4 = 14.74 MIPS(Fcy)
//,~67nanoseconds instruction cycle time(Tcy).
_FWDT(WDT_OFF); //Turn off the Watch-Dog Timer.
_FBORPOR(MCLR_EN & PWRT_OFF); //Enable MCLR reset pin and turn off the
//power-up timers.
_FGS(CODE_PROT_OFF); //Disable Code Protection
//Defines for System Clock Timing -
//For oscillator configuration XT x PLL8 mode,
//Device Throughput in MIPS = Fcy = 7372800*8/4 = ~14.74 MIPS
//Instruction Cycle time = Tcy = 1/(Fcy) = ~68 nanoseconds
#define XTFREQ 7372800 //On-board Crystal frequency
#define PLLMODE 8 //On-chip PLL setting
#define FCY XTFREQ*PLLMODE/4 //Instruction Cycle Frequency
#define BAUDRATE 9600 //Designamos rango de baudios
#define BRGVAL (((FCY/BAUDRATE)/16)-1) //Formula para registro U1BRG, obtenida del manual
//dsPIC30F Family Reference Manual
/*****************
** PROTOTIPOS **
*****************/
void UART_Init (void);
void __attribute__((__interrupt__)) _U1RXInterrupt(void);
int main (void);
/****************
** GLOBALES **
****************/
unsigned char bufferRX[4]; //buffer donde voy almacenar los elementos de la pila
unsigned char *Ptro_bufferRX=NULL; //inicializacion del buffer
unsigned char indice=0; //contador que me indica las veces que se ha leido un dato
/*****************
** FUNCIONES **
*****************/
void UART_Init (void)
{
U1MODE=0;
U1STA=0;
TRISCbits.TRISC14=1; //configguramos el puerto poniendo como entrada la linea RX->RC14
//cargamos el valor BRGVAL en el registro U!BRG, sera el que nos indica la valocidad de comunicacion
U1BRG=BRGVAL; //los valores serian: 95->9600 7->115200 47->19200 15->57600 PLLx8
IPC2bits.U1RXIP = 7; // establecmos prioridad de la interrupcion
//U1MODE
U1MODEbits.UARTEN=1; //Enable UART (implies reception)
U1MODEbits.USIDL=0;
U1MODEbits.ALTIO=1;
U1MODEbits.WAKE = 0;
U1MODEbits.LPBACK=0;
U1MODEbits.ABAUD = 0; // Autobaud desactivado
U1MODEbits.PDSEL = 0b10; // odd parity
U1MODEbits.STSEL = 0; // 1-stop bit
//U1STA
U1STAbits.URXISEL = 0b00; //indicamos tipo de interrupcion
U1STAbits.ADDEN=0; //address detect mode desactivado
U1STAbits.OERR=0; //borramos el overrun
IFS0bits.U1RXIF = 0; // borramos flag interrupcion
IEC0bits.U1RXIE = 1; //interrupcion recepcion activada
Ptro_bufferRX= &bufferRX[0]; //inicializamos el puntero al primer elemento de la tabla
}
/*********************
** Interrupciones **
*********************/
// This is UART1 receive ISR
void __attribute__((__interrupt__,no_auto_psv)) _U1RXInterrupt(void)
{
IFS0bits.U1RXIF = 0; //borramos flag de interrupcion
if((U1STAbits.PERR==0) && (U1STAbits.FERR==0)) //compruebo errores
{
(*(Ptro_bufferRX)++)=U1RXREG;
indice++; //incremento el valor del indice cada vez que llega un dato
}
if(U1STAbits.OERR) U1STAbits.OERR=0; //si hay error de overflow es borrado
}
/***********
** main **
***********/
int main (void)
{
UART_Init(); //iniciamos las configuracion de UART
while(1) //bucle infinito, loop main
{
if(indice==4) //se acede cuando se han leido cuatro bytes
{
Ptro_bufferRX= &bufferRX[0]; //vuelvo apuntar al primer elemento del buffer
indice=0; //inicializo contador
}
}
CloseUART1(); //si por alguan circunstacia se abandona loop main se cierra el dispostivo UART
return 0;
}
//main
//***********************************************************************************
//
// PROGRAMA:
// Obtener datos enviados por computador a traves de comunicacion RS232,
// los datos se estan enviando atraves de una aplicacion software llamada terminal
// similar al hyperterminal de windows, pero con mejores caracteristicas
//
// Entorno de programacion: MPLAB v8.20
// Compilador: C30
// Dispositivo: dsPIC30F4011
// Placa desarrollo: dsPICDEM2
//
//***********************************************************************************
#include <p30f4011.h>
#include <uart.h>
#include <stdlib.h>
//Macros for Configuration Fuse Registers:
//Invoke macros to set up device configuration fuse registers.
//The fuses will select the oscillator source, power-up timers, watch-dog
//timers, BOR characteristics etc. The macros are defined within the device
//header files. The configuration fuse registers reside in Flash memory.
_FOSC(CSW_FSCM_OFF & XT_PLL8); //Run this project using an external crystal
//routed via the PLL in 8x multiplier mode
//For the 7.3728 MHz crystal we will derive a
//throughput of 7.3728e+6*8/4 = 14.74 MIPS(Fcy)
//,~67nanoseconds instruction cycle time(Tcy).
_FWDT(WDT_OFF); //Turn off the Watch-Dog Timer.
_FBORPOR(MCLR_EN & PWRT_OFF); //Enable MCLR reset pin and turn off the
//power-up timers.
_FGS(CODE_PROT_OFF); //Disable Code Protection
//Defines for System Clock Timing -
//For oscillator configuration XT x PLL8 mode,
//Device Throughput in MIPS = Fcy = 7372800*8/4 = ~14.74 MIPS
//Instruction Cycle time = Tcy = 1/(Fcy) = ~68 nanoseconds
#define XTFREQ 7372800 //On-board Crystal frequency
#define PLLMODE 8 //On-chip PLL setting
#define FCY XTFREQ*PLLMODE/4 //Instruction Cycle Frequency
#define BAUDRATE 9600 //Designamos rango de baudios
#define BRGVAL (((FCY/BAUDRATE)/16)-1) //Formula para registro U1BRG, obtenida del manual
//dsPIC30F Family Reference Manual
/*****************
** PROTOTIPOS **
*****************/
void UART_Init (void);
void __attribute__((__interrupt__,no_auto_psv)) _U1RXInterrupt(void);
int main (void);
/****************
** GLOBALES **
****************/
unsigned char bufferRX[4]; //buffer donde voy almacenar los elementos de la pila
unsigned char *Ptro_bufferRX=NULL; //inicializacion del buffer
unsigned char index_byte=0; //contador que me indica las veces que se ha leido un dato
/*****************
** FUNCIONES **
*****************/
/*==========================
== INICIALIZACION UART ==
==========================*/
void UART_Init (void)
{
unsigned int U1MODEvalue;
unsigned int U1STAvalue;
CloseUART1(); //Turn off UART1module
//Configuramos interrupciones de UART1, recepcion
ConfigIntUART1(UART_TX_INT_DIS & UART_RX_INT_EN & UART_RX_INT_PR7);
//configuramos los registros UMODE y USTA
U1MODEvalue= UART_EN & UART_IDLE_CON & UART_DIS_WAKE & UART_DIS_ABAUD & UART_ALTRX_ALTTX &
UART_DIS_LOOPBACK & UART_ODD_PAR_8BIT & UART_1STOPBIT;
U1STAvalue= UART_INT_TX & UART_INT_TX_BUF_EMPTY & UART_TX_PIN_NORMAL & UART_TX_DISABLE &
UART_INT_RX_BUF_FUL & UART_ADR_DETECT_DIS & UART_RX_OVERRUN_CLEAR;
OpenUART1(U1MODEvalue, U1STAvalue, BRGVAL); //activamnos el uart con la configuracion seleccionada
Ptro_bufferRX= &bufferRX[0]; //el puntero apunta al primer elemento del buffer
}
/*********************
** INTERRUPCIONES **
*********************/
// This is UART1 receive ISR
void __attribute__((__interrupt__,no_auto_psv)) _U1RXInterrupt(void)
{
IFS0bits.U1RXIF = 0; //borramos flag de interrupcion
if((U1STAbits.PERR==0) && (U1STAbits.FERR==0)) //compruebo errores
{
while(U1STAbits.URXDA==1) //lee mientras haya datos en la pila este bit indica que hay datos y pueden ser leidos
{
(*(Ptro_bufferRX)++)= U1RXREG;
index_byte++; //se incrementa
}
}
if(U1STAbits.OERR) U1STAbits.OERR=0; //si hay error de overflow es borrado
}
/************
** MAIN **
************/
int main (void)
{
UART_Init(); //iniciamos las configuracion de UART
while(1) //bucle infinito, loop main
{
if(index_byte==4) //se acede cuando se han leido cuatro bytes
{
Ptro_bufferRX= &bufferRX[0]; //vuelvo apuntar al primer elemento del buffer
index_byte=0; //inicializo contador
}
}
CloseUART1(); //si por alguan circunstacia se abandona loop main se cierra el dispostivo UART
return 0;
}
//main