mjuneja
Advanced Member level 4
![Full Member level 1](/styles/images/ranks/elevel1.gif)
- Joined
- Aug 28, 2016
- Messages
- 105
- Helped
- 12
- Reputation
- 24
- Reaction score
- 10
- Trophy points
- 18
- Location
- Bangalore, India
- Activity points
- 674
I have written a code for simulating UART receiver in MPLAB for dsPIC33FJ128GP802 as follows.
Oscillator mode as selected in configuration bits and defined in header file as below.
For simulation Fcyc = 230.3125 KHz and RC oscillator freq. = 460.625 KHz in the oscillator options.
The problem is UART receiver interrupt is not generated during simulation, can anybody throw some light what could be the possible causes for that.
Stimulus for the code is attached below.
Thanks
Code:
#include "xc.h"
#include "newxc16_header.h"
int count_pulse = 0;
int count_byte = 0;
int pls_1,pls_2,pls_3,pls_4 = 0;
int timer_cnt1,timer_cnt2 = 0;
int stp_cnt = 0;
int trig = 0;
int main(void) {
//UART configuration
U1MODEbits.UARTEN = 1; // UART1 is enabled.
U1MODEbits.USIDL = 0; // continues operation in idle mode.
U1MODEbits.IREN = 0; //IrDA disabled.
U1MODEbits.RTSMD = 1; // simplex mode
U1MODEbits.UEN = 0b00; // U1Rx and U1Tx are enabled
U1MODEbits.WAKE = 1; // interrupt generate on falling edge.
U1MODEbits.LPBACK = 0; // loopback testing disabled
U1MODEbits.ABAUD = 0; // auto baud rate disabled.
U1MODEbits.URXINV = 0; // no inverse polarity
U1MODEbits.BRGH = 1; // 4x baud clock.
U1MODEbits.PDSEL = 0b00; // no parity
U1MODEbits.STSEL = 0; // 1 stop bit
U1STAbits.UTXISEL1 = 1; // not used (Xmission interrupt mode)
U1STAbits.UTXISEL0 = 1; //
U1STAbits.UTXINV = 0; // xmission idle state is 1
U1STAbits.UTXBRK = 0; // transmit break disabled
U1STAbits.UTXEN = 1; // transmit enabled
U1STAbits.URXISEL = 0b01; // interrupt on receive of 1 data char.
U1STAbits.ADDEN = 0; // addr. detect mode disabled.
U1BRG = 5; // for 9600bps baud rate
// pin configuration
RPINR18bits.U1RXR = 2; // assigning UART receiver to RP2
RPOR1bits.RP3R = 3; // assigning UART transmitter to RP3
TRISBbits.TRISB6 = 0; // to set pin RB6 as output for enable.
TRISBbits.TRISB8 = 0; // to set pin RB8 as output for direction.
TRISBbits.TRISB9 = 0; // to set pin RB9 as output for pulse train.
TRISBbits.TRISB2 = 1; // to set pin RB2 as input.
TRISBbits.TRISB3 = 0; // to set pin RB3 as output.
// Interrupts enabling, clearing interrupt flag and priority assignment
INTCON1bits.NSTDIS = 0; // Interrupt nesting enabled here
IFS0bits.T1IF = 0; // clear timer 1 interrupt.
IFS0bits.U1RXIF = 0; // clear UART 1 receiver interrupt.
IEC0bits.T1IE = 1; // timer 1 interrupt enabled.
IPC0bits.T1IP = 4; // timer 1 interrupt is assigned #3 priority
IEC0bits.U1RXIE = 1; // UART1 receiver interrupt enable bit
IPC2bits.U1RXIP = 5; // UART1 receiver interrupt is assigned #2 priority
IEC4bits.U1EIE = 1; //UART 1 error interrupt enable bit.
IPC16bits.U1EIP = 6; //UART 1 error interrupt is assigned #1 priority.
while(1)
{
//Check for receive errors
if(U1STAbits.FERR == 1)
{continue;}
//Must clear the overrun error to keep UART receiving
if(U1STAbits.OERR == 1)
{
U1STAbits.OERR = 0;
continue;
}
if(IFS0bits.U1RXIF == 1)
{trig = 1;}
}
void __attribute__((interrupt,no_auto_psv)) _U1RXInterrupt(void){
if(count_byte == 8)
{count_byte = 1;}
else
{count_byte = count_byte + 1;}
switch(count_byte){
case 1:
if(U1RXREGbits.URXREG0 == 0)
{PORTBbits.RB6 = 0;} // enable or disable the motor
else
{PORTBbits.RB6 = 1;}
break;
case 2:
if(U1RXREGbits.URXREG0 == 0)
{PORTBbits.RB8 = 0;} // fixing the direction
else
{PORTBbits.RB8 = 1;}
break;
case 3:
timer_cnt1 = U1RXREG; // fixing the pulse rate.
break;
case 4:
timer_cnt2 = U1RXREG;
break;
case 5:
pls_1 = U1RXREG; // LSB first
break;
case 6:
pls_2 = U1RXREG;
break;
case 7:
pls_3 = U1RXREG;
break;
case 8:
pls_4 = U1RXREG;
T1CON = 0XA000; // timer enable
trig = 1;
break;
}
IFS0bits.U1RXIF = 0; // clear UART 1 receiver interrupt.
}
Oscillator mode as selected in configuration bits and defined in header file as below.
Code:
// FOSCSEL
#pragma config FNOSC = FRCDIV16 // Oscillator Mode (Internal Fast RC (FRC) divide by 16)
#pragma config IESO = OFF // Internal External Switch Over Mode (Start-up device with user-selected oscillator source)
For simulation Fcyc = 230.3125 KHz and RC oscillator freq. = 460.625 KHz in the oscillator options.
The problem is UART receiver interrupt is not generated during simulation, can anybody throw some light what could be the possible causes for that.
Stimulus for the code is attached below.
Thanks