Hola! Soy nuevo en la programación de microcontroladores de 16 bits y estoy teniendo problemas para comunicar mi PIC con el PC via UART.
Uso XC16, el microcontrolador PIC24FJ64BGB002 y un oscilador de 24 MHz.
He creado un código sencillo, que haga parpadear un LED cada vez que se detecten las interrupciones de recepción o transmisión de datos.
Sin embargo, la de recepción no salta nunca (es decir, no recibe nada).
¿Hay alguien con este mismo problema? ¿Sabe alguien que puedo estar haciendo mal?
Este es mi código:
#include <p24FJ64GB002.h>
#include <xc.h>
#include <uart.h>
#include "PPS.h"
///////////////////////////////////////////////////////////////////////////////
///////////////////// CONFIGURATION BITS //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#pragma config DSWDTPS = DSWDTPSF // DSWDT Postscale Select (1:2,147,483,648 (25.7 days))
#pragma config DSWDTOSC = LPRC // Deep Sleep Watchdog Timer Oscillator Select (DSWDT uses Low Power RC Oscillator (LPRC))
#pragma config RTCOSC = SOSC // RTCC Reference Oscillator Select (RTCC uses Secondary Oscillator (SOSC))
#pragma config DSBOREN = ON // Deep Sleep BOR Enable bit (BOR enabled in Deep Sleep)
#pragma config DSWDTEN = ON // Deep Sleep Watchdog Timer (DSWDT enabled)
// CONFIG3
#pragma config WPFP = WPFP63 // Write Protection Flash Page Segment Boundary (Highest Page (same as page 42))
#pragma config SOSCSEL = SOSC // Secondary Oscillator Pin Mode Select (SOSC pins in Default (high drive-strength) Oscillator Mode)
#pragma config WUTSEL = LEG // Voltage Regulator Wake-up Time Select (Default regulator start-up time used)
#pragma config WPDIS = WPDIS // Segment Write Protection Disable (Segmented code protection disabled)
#pragma config WPCFG = WPCFGDIS // Write Protect Configuration Page Select (Last page and Flash Configuration words are unprotected)
#pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select (Write Protect from WPFP to the last page of memory)
// CONFIG2
#pragma config POSCMOD = HS // Primary Oscillator Select (HS Oscillator mode selected)
#pragma config I2C1SEL = PRI // I2C1 Pin Select bit (Use default SCL1/SDA1 pins for I2C1 )
#pragma config IOL1WAY = ON // IOLOCK One-Way Set Enable (Once set, the IOLOCK bit cannot be cleared)
#pragma config OSCIOFNC = OFF // OSCO Pin Configuration (OSCO pin functions as clock output (CLKO))
#pragma config FCKSM = CSDCMD // Clock Switching and Fail-Safe Clock Monitor (Sw Disabled, Mon Disabled)
#pragma config FNOSC = PRI // Initial Oscillator Select (Primary Oscillator (XT, HS, EC))
#pragma config PLL96MHZ = OFF // 96MHz PLL Startup Select (96 MHz PLL Startup is enabled by user in software( controlled with the PLLEN bit))
#pragma config PLLDIV = NODIV // USB 96 MHz PLL Prescaler Select (Oscillator input used directly (4 MHz input))
#pragma config IESO = ON // Internal External Switchover (IESO mode (Two-Speed Start-up) enabled)
// CONFIG1
#pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128)
#pragma config WINDIS = OFF // Windowed WDT (Standard Watchdog Timer enabled,(Windowed-mode is disabled))
#pragma config FWDTEN = OFF // Watchdog Timer (Watchdog Timer is disabled)
#pragma config ICS = PGx1 // Emulator Pin Placement Select bits (Emulator functions are shared with PGEC1/PGED1)
#pragma config GWRP = OFF // General Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF // General Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = ON // JTAG Port Enable (JTAG port is enabled)
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Definciones para calculo de Baud Rate.-
#define FCY 120000000 // MHz
#define BAUDRATE 9600
#define BRGHigh 0
#define BRGVAL 77
const char Texto[]= "HOLA!!! \n";
void inicializar_UART(){
TRISBbits.TRISB13 = 1;
//Asignamos Tx y Rx de la UART a los pines RP13 y 14
__builtin_write_OSCCONL(OSCCON & 0xbf);
iPPSInput(IN_FN_PPS_U1RX,IN_PIN_PPS_RP13); //Assing U1RX to pin RP13
iPPSOutput(OUT_PIN_PPS_RP14,OUT_FN_PPS_U1TX); //Assing U1TX to pin RP14
__builtin_write_OSCCONL(OSCCON | 0x40);
//Abrimos y configuramos la UART1
CloseUART1();
//UART initialized to 9600 baudrate @BRGH=0, 8bit,no parity and 1 stopbit
OpenUART1(UART_EN & UART_IDLE_STOP & UART_IrDA_DISABLE & UART_MODE_SIMPLEX &
UART_UEN_00 & UART_DIS_WAKE & UART_DIS_LOOPBACK & UART_UXRX_IDLE_ONE &
UART_DIS_ABAUD & UART_NO_PAR_8BIT & UART_BRGH_FOUR & UART_1STOPBIT,
UART_INT_TX & UART_IrDA_POL_INV_ZERO & UART_SYNC_BREAK_DISABLED &
UART_TX_ENABLE & UART_INT_RX_CHAR & UART_ADR_DETECT_DIS & UART_RX_OVERRUN_CLEAR,
BRGVAL);
//Enable UART Interrupts
ConfigIntUART1(UART_RX_INT_EN |UART_RX_INT_PR6 | UART_TX_INT_EN | UART_TX_INT_PR6);
}//Fin del méotodo que inicializa la UART
void __attribute__ ((interrupt,no_auto_psv)) _U2TXInterrupt(void){
PORTBbits.RB2 = 1; // High pin RB1
__delay32(12000000); // Demora
PORTBbits.RB2 = 0; // Low pin RB1
__delay32 (12000000); // Demora
U1TX_Clear_Intr_Status_Bit;
}
void __attribute__((__interrupt__, __shadow__)) _U1RXInterrupt(void){
PORTBbits.RB2 = 1; // High pin RB1
__delay32(12000000); // Demora
PORTBbits.RB2 = 0; // Low pin RB1
__delay32 (12000000); // Demora
U1RX_Clear_Intr_Status_Bit;
//_U1RXIF=0; // Borramos flag.
}//Fin de la la ISR que controla la llegada de datos
void main(void){
TRISA=0x0000;
TRISB=0x0000;
//AD1PCFGbits.PCFG = 0xFFFF;
//Inicializamos la UART
inicializar_UART();
while(1){
PORTAbits.RA1 = 1; // High pin RB1
__delay32(12000000); // Demora
PORTAbits.RA2 = 0; // Low pin RB1
__delay32 (12000000); // Demora
//WriteUART1(Texto);
}//Fin del while 1
}//Fin del método main