• AVR Freaks

Hot!dsPIC33EP256MU810 UART2 Rx Interrupt is not working

Author
luckyspring
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/05/09 23:04:14
  • Location: 0
  • Status: offline
2019/05/12 18:07:25 (permalink)
0

dsPIC33EP256MU810 UART2 Rx Interrupt is not working

Hi All. I need a help. 
I am working in a project with a dsPIC33EP256MU810,
The language is C (XC16) on MPLAB X v5.15.
I use UART2 RX interrupt, but its not working.
I checked RX2 signal come in the RX2 pin.
UART1 is for checking U2RXREG (communicate with PC, baud rate 19200)
UART2 is for sending message(communicate with servovalve baud rate 57600)
The source codes in C attached below.
I would appreciate any help.
Thanks.
 
 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <p33EP256MU810.h>
#include <libpic30.h>
#include "serial.h"
#include <xc.h>
 
 
 

#define FCY 8000000UL
#define Fin 8000000 //Fosc 16M, My_FCY 8M 
#define Fosc Fin*(M/(N1*N2)) //
#define M 8 // PLLFBD +2
#define N1 2 // PLLPRE +2
#define N2 2 // 2*(PLLPOST+1)
#define BaudRate1 19200 //RS232 pc serial
#define BaudRate2 57600 //RS232 servovalve serial
#define My_FCY Fosc/2 
#define U1brg (int)((My_FCY/BaudRate1)/16)-1
#define U2brg (int)((My_FCY/BaudRate2)/16)-1
#define MICROSEC 1 // 1.5 us
#define MILLISEC FCY/80000
 

unsigned char Received_data;
unsigned char Send_data;
unsigned char Command;
unsigned char prtbuf1[100]={0,};;
unsigned char prtbuf[100], prtbuf2[100], prtbuf3[100] = {0};
 

//char ucaRxBuffer[100];
//int ucRxBufferPointer, ucRxDataReady;
//
//void __attribute__((interrupt,no_auto_psv)) _U2RXInterrupt(void) 
//{ 
// 
// 
// if (U2STAbits.OERR == 1) 
// { 
// U2STAbits.OERR = 0; 
// } 
// Received_data = U2RXREG ; 
// ucaRxBuffer[ucRxBufferPointer++] = Received_data ; 
// if(ucRxBufferPointer>29) 
// ucRxBufferPointer = 0 ; 
// if(Received_data == 0x0d) 
// ucRxDataReady = 1 ; 
// IFS1bits.U2RXIF = 0;
//}
 
 
void InitClock(void)
{
PLLFBD = 6; // M = 8
CLKDIVbits.PLLPOST = 0; // N1 = 2
CLKDIVbits.PLLPRE = 0; // N2 = 2

OSCTUN = 0;
RCONbits.SWDTEN = 0;
// Clock switch to incorporate PLL
__builtin_write_OSCCONH( 0x03 ); // Initiate Clock Switch to
// External oscillator with PLL (NOSC=0b011)
__builtin_write_OSCCONL( OSCCON || 0x01 ); // Start clock switching
while( OSCCONbits.COSC != 0b011 );
// Wait for Clock switch to occur
while( OSCCONbits.LOCK != 1 );
}
 
 
int FGS __attribute__((space(prog), address(0xF80004))) = 0xFFCF ;
//_FGS(
// GWRP_OFF & // General Segment Write-Protect bit (General Segment may be written)
// GSS_OFF & // General Segment Code-Protect bit (General Segment Code protect is disabled)
// GSSK_OFF // General Segment Key bits (General Segment Write Protection and Code Protection is Disabled)
//);
int FOSCSEL __attribute__((space(prog), address(0xF80006))) = 0xFF7B ;
//_FOSCSEL(
// FNOSC_PRIPLL & // Initial Oscillator Source Selection bits (Primary Oscillator (XT, HS, EC) with PLL)
// IESO_OFF // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)
//);
int FOSC __attribute__((space(prog), address(0xF80008))) = 0xFFFE ;
//_FOSC(
// POSCMD_HS & // Primary Oscillator Mode Select bits (HS Crystal Oscillator Mode)
// OSCIOFNC_OFF & // OSC2 Pin Function bit (OSC2 is clock output)
// IOL1WAY_ON & // Peripheral pin select configuration (Allow only one reconfiguration)
// FCKSM_CSDCMD // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are disabled)
//);
int FWDT __attribute__((space(prog), address(0xF8000A))) = 0xFFFF ;
//_FWDT(
// WDTPOST_PS32768 & // Watchdog Timer Postscaler bits (1:32,768)
// WDTPRE_PR128 & // Watchdog Timer Prescaler bit (1:128)
// PLLKEN_ON & // PLL Lock Wait Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
// WINDIS_OFF & // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
// FWDTEN_ON // Watchdog Timer Enable bit (Watchdog timer always enabled)
//);
int FPOR __attribute__((space(prog), address(0xF8000C))) = 0xFFF7 ;
//_FPOR(
// FPWRT_PWR128 & // Power-on Reset Timer Value Select bits (128ms)
// BOREN_OFF & // Brown-out Reset (BOR) Detection Enable bit (BOR is disabled)
// ALTI2C1_OFF & // Alternate I2C pins for I2C1 (SDA1/SCK1 pins are selected as the I/O pins for I2C1)
// ALTI2C2_OFF // Alternate I2C pins for I2C2 (SDA2/SCK2 pins are selected as the I/O pins for I2C2)
//);
int FICD __attribute__((space(prog), address(0xF8000E))) = 0xFFDF ;
//_FICD(
// ICS_PGD1 & // ICD Communication Channel Select bits (Communicate on PGEC1 and PGED1)
// RSTPRI_PF & // Reset Target Vector Select bit (Device will obtain reset instruction from Primary flash)
// JTAGEN_OFF // JTAG Enable bit (JTAG is disabled)
//);
int FAS __attribute__((space(prog), address(0xF80010))) = 0xFFCF ;
//_FAS(
// AWRP_OFF & // Auxiliary Segment Write-protect bit (Aux Flash may be written)
// APL_OFF & // Auxiliary Segment Code-protect bit (Aux Flash Code protect is disabled)
// APLK_OFF // Auxiliary Segment Key bits (Aux Flash Write Protection and Code Protection is Disabled)
//);
 

void Delay_ms(unsigned int N) //// Delay_ms(1) == 1.251ms
{
unsigned int cnt;
while(N--)
for(cnt=0;cnt < MILLISEC;cnt++);
}
void Delay_us(unsigned int N) //// Delay_us(1) == 295 us
{
unsigned int cnt;
while(N--)
for(cnt=0;cnt < (unsigned int)MICROSEC;cnt++);
}
 
void Init_uart1(void) // PC serial
{
U1MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func
//U1MODEbits.notimplemented;// Bit14
U1MODEbits.USIDL = 0; // Bit13 Continue in Idle
U1MODEbits.IREN = 0; // Bit12 No IR translation
U1MODEbits.RTSMD = 0; // Bit11 Simplex Mode
//U1MODEbits.notimplemented;// Bit10
U1MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U1MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here)
U1MODEbits.LPBACK = 0; // Bit6 No Loop Back
U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
U1MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
U1MODEbits.STSEL = 0; // Bit0 One Stop Bit

// Load all values in for U1STA SFR
U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
U1STAbits.UTXINV = 0; //Bit14 N/A, IRDA config
U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
//U1STAbits.notimplemented = 0;//Bit12
U1STAbits.UTXBRK = 0; //Bit11 Disabled
U1STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
U1STAbits.UTXBF = 0; //Bit9 *Read Only Bit*
U1STAbits.TRMT = 0; //Bit8 *Read Only bit*
U1STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved
U1STAbits.ADDEN = 0; //Bit5 Address Detect Disabled
U1STAbits.RIDLE = 0; //Bit4 *Read Only Bit*
U1STAbits.PERR = 0; //Bit3 *Read Only Bit*
U1STAbits.FERR = 0; //Bit2 *Read Only Bit*
U1STAbits.OERR = 0; //Bit1 *Read Only Bit*
U1STAbits.URXDA = 0; //Bit0 *Read Only Bit*
// IPC3 = 0x4400; // Mid Range Interrupt Priority level, no urgent reason

IPC3bits.U1TXIP = 0;
IPC2bits.U1RXIP = 0;

// IFS0bits.U1TXIF = 0; // Clear the Transmit Interrupt Flag
// IEC0bits.U1TXIE = 1; // Enable Transmit Interrupts
IFS0bits.U1RXIF = 0; // Clear the Recieve Interrupt Flag
IEC0bits.U1RXIE = 1; // Enable Recieve Interrupts 

RPOR6bits.RP87R = 1; //RP37/RB5 as U1TX
RPINR18bits.U1RXR = 86; //RP38/RB6 as U1RX

U1MODEbits.UARTEN = 1; // And turn the peripheral on
U1STAbits.UTXEN = 1;
U1BRG = 25; // baud rate 19200 25

}
 

void Init_uart2(void) // servovalve serial
{
 
//U2MODEbits.notimplemented;// Bit14
U2MODEbits.USIDL = 0; // Bit13 Continue in Idle
U2MODEbits.IREN = 0; // Bit12 No IR translation
U2MODEbits.RTSMD = 0; // Bit11 Simplex Mode
//U2MODEbits.notimplemented;// Bit10
U2MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U2MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here)
U2MODEbits.LPBACK = 0; // Bit6 No Loop Back
U2MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U2MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
U2MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
U2MODEbits.STSEL = 0; // Bit0 One Stop Bit


// Load all values in for U1STA SFR

U2STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
U2STAbits.UTXISEL0 = 0;
U2STAbits.UTXINV = 0; //Bit14 N/A, IRDA config
//U2STAbits.notimplemented = 0;//Bit12
U2STAbits.UTXBRK = 0; //Bit11 Disabled
U2STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
U2STAbits.UTXBF = 0; //Bit9 *Read Only Bit*
U2STAbits.TRMT = 0; //Bit8 *Read Only bit*

U2STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved

U2STAbits.ADDEN = 0; //Bit5 Address Detect Disabled
U2STAbits.RIDLE = 0; //Bit4 *Read Only Bit*
U2STAbits.PERR = 0; //Bit3 *Read Only Bit*
U2STAbits.FERR = 0; //Bit2 *Read Only Bit*
U2STAbits.OERR = 0; //Bit1 *Read Only Bit*
U2STAbits.URXDA = 0; //Bit0 *Read Only Bit*


IPC7bits.U2TXIP = 0;
IPC7bits.U2RXIP = 4; //Set Uart RX Interrupt Priority

// IFS1bits.U2TXIF = 0; // Clear the Transmit Interrupt Flag
// IEC1bits.U2TXIE = 1; // Enable Transmit Interrupts

IEC4bits.U2EIE =0; // *******Disables error interrupt.*********
IFS4bits.U2EIF =0; // *******Clears error interrupt flag.*******

IFS1bits.U2RXIF = 0; // Clear Recieve Interrupt Flag ********
IEC1bits.U2RXIE = 1; // Enable Recieve Interrupts ********

RPOR14bits.RP120R = 3; // U2TX PIN12 RP118
RPINR19bits.U2RXR = 119; // U2RX PIN11 RPI119

U2MODEbits.UARTEN = 1; // And turn the peripheral on
U2STAbits.UTXEN = 1;

// INTCON2bits.GIE = 1;
// U2BRG = 8; // baud rate 57600 7 or8 
U2BRG = 8;
}








void Init_Port(void)
{
PORTE=0;
// ANSELGbits.ANSG7=0;
ANSELG=0;
ANSELE=0;
TRISGbits.TRISG7 = 1;
TRISD=0; //output = 0 
PORTD=1;
}
 
int main(void)
{
int str = 0;


Init_uart1();
Init_uart2();

Init_Port();
InitClock();
 

sprintf(prtbuf, "Servovalve check\n\r");
printf(prtbuf);




while(1)
{

if(str>5)
{
UART2_Tx_String("\x05\x30\x30\x31\x52\x49\x50\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x39\x35\x36\x03\n\r"); 

sprintf(prtbuf2, "%s!!\n\r", Received_data);
UART1_Tx_String(prtbuf2);


// sprintf(prtbuf2, "%s!!\n\r", Received_data);
// printf(prtbuf2);
str = 0;


}
Delay_ms(2000); 



str++;
}


return 0;
}

/// board to PC
//void __attribute__((interrupt, auto_psv)) _U1RXInterrupt(void)
//{
// IFS0bits.U1RXIF = 0;
// IEC0bits.U1RXIE = 0;
// Received_data = U1RXREG;
// Uart1RxAddBufChar(getcUART1());
// IEC0bits.U1RXIE = 1;
//}

//void __attribute__ ( (interrupt, no_auto_psv) ) _U1TXInterrupt( void )
//{
// 
// IFS0bits.U1TXIF = 0; 
//
//}
 

// board to servovalve
//void __attribute__((interrupt, auto_psv)) _U2TXInterrupt(void)
//{
// 
// IFS1bits.U2TXIF = 0; // Clear TX Interrupt flag
// 
// 
//}
 
void __attribute__((interrupt, auto_psv)) _U2RXInterrupt(void)
{

// IEC1bits.U2RXIE = 0; 
Received_data = U2RXREG; 


// Uart2RxAddBufChar(getcUART2());
// IEC1bits.U2RXIE = 1;
IFS1bits.U2RXIF = 0;
}

void UART1_Tx_String(char *uart_string)
{
while(*uart_string != 0 )
{
U1TXREG = *uart_string++;
while(!U1STAbits.TRMT); 


Delay_ms(200);
}
void UART2_Tx_String(char *uart_string1)
{
while(*uart_string1 != 0 )
{
U2TXREG = *uart_string1++;
while(!U2STAbits.TRMT); 


Delay_ms(200);
}
 
 

post edited by luckyspring - 2019/05/14 04:53:02
#1
du00000001
Just Some Member
  • Total Posts : 2679
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/13 12:14:43 (permalink)
0
It doesn't help to post n times :(
 
The original thread:  https://www.microchip.com/forums/m1097852.aspx

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#2
Aussie Susan
Super Member
  • Total Posts : 3559
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/13 19:35:32 (permalink)
0
Please use code tags.
Please remove all of the unnecessary code (i.e. all of the commented-out functions).
It is always best to show a small but complete app that exhibits the problem - in this case use using UART2 with only the Rx ISR.
Also, how do you know it is not working? What checks have you done?
As 'Received_data' is updated in the ISR, it should be declared volatile.
Your 'Delay_ms' function may not be working correctly for you - any degree of optimisation applied by the compiler and the whole function effectively becomes a 'NOP'. Ditto the 'Delay_us' function.
You don't need the processor-specific #include - just use the 'xc.h' one.
I won't comment on your coding style other than to say I find it very hard to read and understand what you are doing.
Susan
#3
luckyspring
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/05/09 23:04:14
  • Location: 0
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/14 00:24:10 (permalink)
0
Hello du.
it was my mistake, i confused tag , and post 2 times :(
#4
luckyspring
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/05/09 23:04:14
  • Location: 0
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/14 00:33:41 (permalink)
0
Hi Susan.
Thank you for advise to me ( despite my code was very hard to understand.. sorry)
I remove all of the unnecessary code.
and i checked ISR is working properly, but can't store data in U2RXREG.
I m going to check your suggestion and reply again !
 
 
#5
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/14 00:44:08 (permalink)
0
Did you understand the point about "code tags" ?
That means when you paste your code into your message, put [CODE] on a line before it, and [/CODE] after it, but change it to lower case, i.e. CODE -> code
So
 
[CODE]
your code goes here
second line of your code
[/CODE]
will look like this when posted using lower case tags:

your code goes here
second line of your code


Nearly there...
#6
luckyspring
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/05/09 23:04:14
  • Location: 0
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/14 00:49:29 (permalink)
0
Thank you qhb
i didnt know about it 
cuz this post is my first post
I will use code tag next time 
Thanks!
#7
MBedder
Circuit breaker
  • Total Posts : 6749
  • Reward points : 0
  • Joined: 2008/05/30 11:24:01
  • Location: Zelenograd, Russia
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/14 02:34:21 (permalink)
0
luckyspringI will use code tag next time
No need to wait that long. Edit your first post and insert the tags as shown.


#8
luckyspring
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/05/09 23:04:14
  • Location: 0
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/14 04:48:03 (permalink)
0
I changed it successfully :)
thank you MBedder 
#9
luckyspring
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/05/09 23:04:14
  • Location: 0
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/14 04:56:41 (permalink)
0
i arranged my code but still can't receive RX2 data .
 

#define FCY 8000000UL
 
 
 
#include <xc.h>
 

#define Fin 8000000 //Fosc 16M, My_FCY 8M
#define Fosc Fin*(M/(N1*N2)) //
 
#define M 8 // PLLFBD +2
#define N1 2 // PLLPRE +2
#define N2 2 // 2*(PLLPOST+1)
#define BaudRate1 19200 //RS232 pc serial
#define BaudRate2 57600 //RS232 servovalve serial
#define My_FCY Fosc/2
#define U1brg (int)((My_FCY/BaudRate1)/16)-1
#define U2brg (int)((My_FCY/BaudRate2)/16)-1
 
#define MICROSEC 1 // 1.5 us
#define MILLISEC FCY/80000
 
 
 

unsigned char Received_data;
unsigned char Send_data;
unsigned char Command;
unsigned char prtbuf1[100]={0};
unsigned char prtbuf[100], prtbuf2[100], prtbuf3[100] = {0};
int a = 0;
 
 
 
 
 
void InitClock(void)
{
PLLFBD = 6; // M = 8
CLKDIVbits.PLLPOST = 0; // N1 = 2
CLKDIVbits.PLLPRE = 0; // N2 = 2

OSCTUN = 0;
RCONbits.SWDTEN = 0;
 
// Clock switch to incorporate PLL
__builtin_write_OSCCONH( 0x03 ); // Initiate Clock Switch to
 
// External oscillator with PLL (NOSC=0b011)
__builtin_write_OSCCONL( OSCCON || 0x01 ); // Start clock switching
while( OSCCONbits.COSC != 0b011 );
 
// Wait for Clock switch to occur
while( OSCCONbits.LOCK != 1 );
}
 

#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
#pragma config GSS = OFF // General Segment Code-Protect bit (General Segment Code protect is disabled)
#pragma config GSSK = OFF // General Segment Key bits (General Segment Write Protection and Code Protection is Disabled)
 
// FOSCSEL
#pragma config FNOSC = PRIPLL // Initial Oscillator Source Selection bits (Primary Oscillator (XT, HS, EC) with PLL)
#pragma config IESO = OFF // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)
 
// FOSC
#pragma config POSCMD = HS // Primary Oscillator Mode Select bits (XT Crystal Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = ON // Peripheral pin select configuration (Allow only one reconfiguration)
#pragma config FCKSM = CSDCMD // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are disabled)
 
// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler bits (1:32,768)
#pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = ON // PLL Lock Wait Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)
 
// FPOR
#pragma config FPWRT = PWR128 // Power-on Reset Timer Value Select bits (128ms)
#pragma config BOREN = OFF // Brown-out Reset (BOR) Detection Enable bit (BOR is enabled)
#pragma config ALTI2C1 = OFF // Alternate I2C pins for I2C1 (SDA1/SCK1 pins are selected as the I/O pins for I2C1)
#pragma config ALTI2C2 = OFF // Alternate I2C pins for I2C2 (SDA2/SCK2 pins are selected as the I/O pins for I2C2)
 
// FICD
#pragma config ICS = PGD1 // ICD Communication Channel Select bits (Communicate on PGEC1 and PGED1)
#pragma config RSTPRI = PF // Reset Target Vector Select bit (Device will obtain reset instruction from Primary flash)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)
 
// FAS
#pragma config AWRP = OFF // Auxiliary Segment Write-protect bit (Aux Flash may be written)
#pragma config APL = OFF // Auxiliary Segment Code-protect bit (Aux Flash Code protect is disabled)
#pragma config APLK = OFF // Auxiliary Segment Key bits (Aux Flash Write Protection and Code Protection is Disabled)
 

void Init_uart1(void) // PC serial
{
 
U1MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func
 
//U1MODEbits.notimplemented;// Bit14
U1MODEbits.USIDL = 0; // Bit13 Continue in Idle
U1MODEbits.IREN = 0; // Bit12 No IR translation
U1MODEbits.RTSMD = 0; // Bit11 Simplex Mode
 
//U1MODEbits.notimplemented;// Bit10
U1MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U1MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here)
U1MODEbits.LPBACK = 0; // Bit6 No Loop Back
U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
U1MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
U1MODEbits.STSEL = 0; // Bit0 One Stop Bit
 

// Load all values in for U1STA SFR
U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
U1STAbits.UTXINV = 0; //Bit14 N/A, IRDA config
U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
 
//U1STAbits.notimplemented = 0;//Bit12
U1STAbits.UTXBRK = 0; //Bit11 Disabled
U1STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
U1STAbits.UTXBF = 0; //Bit9 *Read Only Bit*
U1STAbits.TRMT = 0; //Bit8 *Read Only bit*
U1STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved
U1STAbits.ADDEN = 0; //Bit5 Address Detect Disabled
U1STAbits.RIDLE = 0; //Bit4 *Read Only Bit*
U1STAbits.PERR = 0; //Bit3 *Read Only Bit*
U1STAbits.FERR = 0; //Bit2 *Read Only Bit*
U1STAbits.OERR = 0; //Bit1 *Read Only Bit*
U1STAbits.URXDA = 0; //Bit0 *Read Only Bit*
 

IPC3bits.U1TXIP = 0;
IPC2bits.U1RXIP = 0;
 
IFS0bits.U1RXIF = 0; // Clear the Recieve Interrupt Flag
IEC0bits.U1RXIE = 1; // Enable Recieve Interrupts

RPOR6bits.RP87R = 1; //RP37/RB5 as U1TX
RPINR18bits.U1RXR = 86; //RP38/RB6 as U1RX
 

U1MODEbits.UARTEN = 1; // And turn the peripheral on
U1STAbits.UTXEN = 1;
 
U1BRG = 25; // baud rate 19200 25

}
 
 
 

void Init_uart2(void) // servovalve serial
{
 
 
 
//U2MODEbits.notimplemented;// Bit14
U2MODEbits.USIDL = 0; // Bit13 Continue in Idle
U2MODEbits.IREN = 0; // Bit12 No IR translation
U2MODEbits.RTSMD = 0; // Bit11 Simplex Mode

//U2MODEbits.notimplemented;// Bit10
U2MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U2MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here)
U2MODEbits.LPBACK = 0; // Bit6 No Loop Back
U2MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U2MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
U2MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
U2MODEbits.STSEL = 0; // Bit0 One Stop Bit
 


// Load all values in for U1STA SFR

U2STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
U2STAbits.UTXISEL0 = 0;
U2STAbits.UTXINV = 0; //Bit14 N/A, IRDA config
 
//U2STAbits.notimplemented = 0;//Bit12
U2STAbits.UTXBRK = 0; //Bit11 Disabled
U2STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
U2STAbits.UTXBF = 0; //Bit9 *Read Only Bit*
U2STAbits.TRMT = 0; //Bit8 *Read Only bit*

U2STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved

U2STAbits.ADDEN = 0; //Bit5 Address Detect Disabled
U2STAbits.RIDLE = 0; //Bit4 *Read Only Bit*
U2STAbits.PERR = 0; //Bit3 *Read Only Bit*
U2STAbits.FERR = 0; //Bit2 *Read Only Bit*
U2STAbits.OERR = 0; //Bit1 *Read Only Bit*
U2STAbits.URXDA = 0; //Bit0 *Read Only Bit*

IPC7bits.U2RXIP = 4; //Set Uart RX Interrupt Priority (middle 4, high7)
 

IEC4bits.U2EIE =0; // *******Disables error interrupt.*********
IFS4bits.U2EIF =0; // *******Clears error interrupt flag.*******

IEC1bits.U2RXIE = 1; // Enable Recieve Interrupts ********
IFS1bits.U2RXIF = 0; // Clear Recieve Interrupt Flag ********


RPOR14bits.RP120R = 3; // U2TX PIN12
RPINR19bits.U2RXR = 119; // U2RX PIN11

U2MODEbits.UARTEN = 1; // And turn the peripheral on
U2STAbits.UTXEN = 1;

PMD1bits.C1MD = 1;
PMD1bits.AD1MD = 1;
 
U2BRG = 8;
}
 
 
 
void Init_Port(void)
{
PORTE=0;
TRISGbits.TRISG7 = 1;
ANSELGbits.ANSG7=0;
ANSELE=0;

TRISD=0; //output = 0
PORTD=1; // LED

}
 


int main(void)
{
int str = 0;

Init_Port();
Init_uart1();
Init_uart2();
InitClock();
 

Received_data=0x00;

sprintf(prtbuf, "Servovalve check\n\r");
printf(prtbuf);


while(1)
{
 

if(str>50)
{
 
UART2_Tx_String("\x05\x30\x30\x31\x52\x49\x50\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x39\x35\x36\x03\n\r");
 

printf("%d\n\r", a);  // a is for checking RX2 interruput
 
sprintf(prtbuf2, "%s!!\n\r", Received_data);
printf(prtbuf2);
str = 0;
 

}
 
__delay_ms(200);

str++;
}


return 0;
 
}
 
 
 
 
 

// board to servovalve
 
void __attribute__((interrupt, auto_psv)) _U2RXInterrupt(void)
{

Received_data = U2RXREG;

a++;   // checking for interrupt
IFS1bits.U2RXIF = 0;
}
 
 

#10
MBedder
Circuit breaker
  • Total Posts : 6749
  • Reward points : 0
  • Joined: 2008/05/30 11:24:01
  • Location: Zelenograd, Russia
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/14 05:04:49 (permalink)
5 (1)
You enabled the U1RX interrupt but haven't provided the  _U1RXInterrupt code at all.
#11
Aussie Susan
Super Member
  • Total Posts : 3559
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/14 19:36:15 (permalink)
4 (1)
luckyspring
and i checked ISR is working properly, but can't store data in U2RXREG.

How did you check that the ISR is working and how did you check that the value in U2RXREG is wrong?
For example, it is quite reasonable to use the debugger to set a breakpoint inside the ISR to make sure that it is being called.
However you MUST NOT use the debugger to try to read the U2RXREG value. The hardware cannot tell if a register is being accessed by your code or by the debugger. Once the U2RXREG register has been read, it is undefined as to what it will return if it is read again. Therefore you needs to set a breakpoint on a later instruction in the ISR and check the value of 'Received_data'.
Also as you are just checking inside the main loop that 'Received_data' is updated then you must declare the variable as volatile (as I mentioned above).
In the sprintf call when you try to display 'Received_data', you are using the '%s' formatting code which is meant for a pointer to a character string. However you are passing the *value* of 'Received_data'. Use '%c' instead. (Even if you had tried to pass a pointer to 'Received_data' it would still not have been right as '%s' will continue to process characters until it comes to a null character - there is no guarantee that there is a null character after 'Received_data' which is declares as a 'unsigned char'.)
While it is not exactly wrong, you are wasting a lot of coding time, run  time and FLASH space trying to set read-only bits. (And I know you know they are read only - your comments say so!)
Susan
#12
Javier Lopez
Pulsotron designer
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2019/02/27 09:59:32
  • Location: Spain
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/22 06:44:02 (permalink)
0
I had problems with RX of the can1 and solved making following changes:
1. Soldering a 9 pin subd female connector instead inserting "arduino wires" in the connectors to make the connections
2. By delete all CAN flags after the interruption
 
post edited by Javier Lopez - 2019/05/23 03:50:51
#13
Aussie Susan
Super Member
  • Total Posts : 3559
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/22 20:14:01 (permalink)
4 (1)
@Javier - can you please explain how your code that uses the ECAN module will help the OP use the (quite different) UART module?
Or are you hi-jacking this thread with a separate topic?
Susan
#14
Javier Lopez
Pulsotron designer
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2019/02/27 09:59:32
  • Location: Spain
  • Status: offline
Re: dsPIC33EP256MU810 UART2 Rx Interrupt is not working 2019/05/23 03:51:38 (permalink)
0
I am very sorry, I though it was the same (I  have read your code to help me in mine), I have corrected the post to make suitable to your micro
#15
Jump to:
© 2019 APG vNext Commercial Version 4.5