Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Interrupt based UART receiver simulation in dsPIC33FJ128GP802 using MPLAB X IDE v4.0

Status
Not open for further replies.

mjuneja

Advanced Member level 4
Full Member level 1
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.

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
 

Attachments

  • Stimulus.PNG
    Stimulus.PNG
    11.6 KB · Views: 140

Hi,

For a baud rate of 9600..
* one bit length is about 104us
* a "8N1" byte length is about 1.04ms.

I can't find these values in your stimulus chart.
What is the unit in X-axis?

Klaus
 

Hi,

For a baud rate of 9600..
* one bit length is about 104us
* a "8N1" byte length is about 1.04ms.

I can't find these values in your stimulus chart.
What is the unit in X-axis?

Klaus

Here is the code written in SCL file.
Code:
// 
// F:\Celeostat\Celeostat_pic.X\UART.scl
// Generated by Stimulus MPLAB X
// Fri Aug 25 23:33:27 IST 2017
// 


configuration for "dspic33fj128gp802" is
end configuration;

testbench for "dspic33fj128gp802" is
begin
	process is
	begin
        RP2 <= '1';
        wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '0';
		wait for 105 us;
		RP2 <= '0';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
                wait for 105 us;
                RP2 <= '1';
        wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
		wait for 105 us;
		RP2 <= '1';
        wait for 105 us;
	end process;
end testbench;
 
Last edited by a moderator:

I've not used it myself but I don't think you drive the UART in the simulator that way. The 'Help' for the simulator (in the section on device specific 24-bit peripherals) has the following:
The following limitations apply to UART operations:

The receiver register must be stimulated using a
message-based stimulus file. The characters are clocked into the receiver register at the default or current baud rate, starting at the time defined within the message-based stimulus file. If the receiver is not enabled when the data starts arriving, the data is lost. If the receiver is enabled the characters are read into the FIFO buffer until the FIFO is full. If the characters in the FIFO are not read out in time the remaining characters in the stimulus file will be lost and the OV status bit will be set.

The transmitter register must be captured using an on-demand stimulus file. When the transmitter is enabled, characters written to the transmitter register are clocked into the transmitter FIFO at the current baud rate. When a character becomes available in the FIFO, it is written to the on-demand response file.
Susan
 

I tried simulating UART using loop back mode and in that mode I was able to simulate by transmitting certain data and receiving it back in the main code itself.

And for that I have to enable the transmitter in UART status and control register as well.

However using SCL I couldn't simulate the same thing.
 

I suggest that you raise a support ticket with Microchip about this.
I know that there is a very helpful person on the Microchip simulator forum (i.e. the support engineer for the simulator) and he would probably get the ticket.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top