#include <P33FJ32MC204.h>
#define __33FJ32MC20_H
#include <libpic30.h>
#include <stdio.h>
_FOSCSEL(1); //Oscilador FRC interno con PLL
_FOSC (195); //Osc primario desabilitado, Cambio reloj desabilitado
#define FCY 40000000 //Instruction Cycle Frequency
#define BAUDRATE 9600 //Baudios
#define BRGVAL ((FCY/BAUDRATE)/16)-1
/***************************FuncionTx******************************/
void putUART1( char c){
while ( U1STAbits.UTXBF); // wait while Tx buffer full
U1TXREG = c;
}
/**********************Interrupcion_Rx*****************************/
void __attribute__ ((interrupt, no_auto_psv)) _U1RXInterrupt(void){
TRISA = 0x0000;
PORTA = 0xfff;
putUART1('K');
}
/***************Inicializar_UART********************************/
void InitUART1(){
TRISBbits.TRISB5=1;
TRISBbits.TRISB0=0;
RPOR0=3;
RPINR18bits.U1RXR0 = 0x05;
U1MODEbits.STSEL = 0; // 1-stop bit
U1MODEbits.PDSEL = 0; // No Parity, 8-data bits
U1MODEbits.ABAUD = 0; // Auto-Baud Disabled
U1MODEbits.BRGH = 0; // Low Speed mode
U1BRG = BRGVAL; // BAUD Rate Setting
U1STAbits.UTXISEL0 = 0; // Interrupt after one Tx character is transmitted
U1STAbits.UTXISEL1 = 0;
IEC0bits.U1TXIE = 0; // Disenable UART Tx interrupt
IFS0bits.U1RXIF = 0; //-Clear the Recieve Interrupt Flag
IEC0bits.U1RXIE = 1; //-Enable Recieve Interrupts
U1MODEbits.UARTEN = 1; // Enable UART
U1STAbits.UTXEN = 1; // Enable UART Tx
U1STAbits.URXISEL1 = 0; //-Enable UART Rx
IPC2bits.U1RXIP=7; //Interrupt Rx Mayor prioridad
}
int main (void){
CLKDIVbits.PLLPRE=4; //Divisor PLL_1 por 6
CLKDIVbits.PLLPOST=0; //Divisor PLL_2 por 2
PLLFBD=128; //Multiplicador PLL por 130
OSCTUN=0; //Frecuencia del FRC 7.37 MHz
TRISA = 0x0000;
PORTA = 0;
InitUART1();
while(1){
printf("hola");
}
}