Hot!2 Problems: 1: Creating an Rx usart interrupt. 2: 1st Rx character wrong after start-up.

Author
Mitch
New Member
  • Total Posts : 22
  • Reward points : 0
  • Joined: 2008/11/19 05:58:25
  • Location: 0
  • Status: offline
2017/09/13 08:16:38 (permalink)
0

2 Problems: 1: Creating an Rx usart interrupt. 2: 1st Rx character wrong after start-up.

Programming PIC186723 Chip, XC8 compiler, ICD3 programmer.
Hi,
1.   Can anyone help here please. I have an Rx usart routine (shown below), which is polled for each character, that always receives a corrupt 1st character after start-up. Thereafter characters are received correctly. Any ideas?
 
2.   I can't see how to create the Rx usart routine as an interrupt routine. 
 
Any help gratefully received.
Thanks
Mitch 
 
Part of Init Routine
// ********** set up USART1 TX/RX ints on, 8bit, high speed baud rate mode**********
Close1USART(); // clear any previous settings
TRISC7 = 1; // PortC bit7 input for USART1 RX
TRISC6 = 0; // PortC bit6 output for USART1 TX
RCSTA1bits.SPEN = 1; // Enable USART1
RCSTA1bits.RX9 = 0; // 8 bits RX data
RCSTA1bits.CREN = 1; // Enable USART1 RX
RCSTA1bits.FERR = 0; // No framing error on USART1
RCSTA1bits.OERR = 0; // No OverRun error on USART1
RCSTA1bits.ADDEN = 0; // disables addr det
TXSTA1bits.TX9 = 0; // 8 bits TX data
TXSTA1bits.TXEN = 1; // Enable USART1 TX
TXSTA1bits.SYNC = 0; // USART1 Async Mode
TXSTA1bits.SENDB = 0; // USART1 send break character bit
TXSTA1bits.BRGH = 1; // USART1 High Speed
BAUDCON1bits.BRG16 = 1; // 16-bit Baud Rate Generator
BAUDCON1bits.WUE = 1; // EUSART will continue to sample the Rx pin
BAUDCON1bits.ABDEN = 0; // 0 = Baud rate measurement is disabled or completed
SPBRG1 = 206; //SPBRG1 = 207;
SPBRGH1 = 0;
//**********************************************
 
 
Routine I'd like to have as an interrupt:
if (PIR1bits.RC1IF == 1)               //is it an rx uart int?
{ *rxUartBufInPtr++ =RCREG1;    //put char into rxBuf & inc its ptr (this clears rcreg1)
if (rxUartBufInPtr > (rxUartBuf + 511)) rxUartBufInPtr = rxUartBuf;       //wrap if reqd
generalFlags.rxInt=TRUE;            //indicate to main loop chr received
DIAG_LEDCtr=30; //debug
PIR1bits.RC1IF = FALSE;             //reset rx interrupt flag
}
 
 
#1

7 Replies Related Threads

    CinziaG
    rome burns :D
    • Total Posts : 3082
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: 2 Problems: 1: Creating an Rx usart interrupt. 2: 1st Rx character wrong after start- 2017/09/13 08:22:42 (permalink)
    0
    The "first char corrupted" is usually related to power-up timing and some pins being (even for just a moment) pulled low or such: you should always code so that you ignore the first char after boot (and anyway, some kind of "robust protocol" is in any case a must Smile )
     
    Placing some 100-200mS delay after boot, before using any peripherals, also help ( I think at EEprom, for instance).
     
    As for interrupt code, well, you should be able to find some on this forum: Search for "circular buffer, interrupt-driven)
    #2
    Mitch
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2008/11/19 05:58:25
    • Location: 0
    • Status: offline
    Re: 2 Problems: 1: Creating an Rx usart interrupt. 2: 1st Rx character wrong after start- 2017/09/13 08:30:04 (permalink)
    0
    Hi
    I've seen interrupt routines ok, but it's never clear how to place it such that it's called when an interrupt occurs using XC8.
     
    #3
    CinziaG
    rome burns :D
    • Total Posts : 3082
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: 2 Problems: 1: Creating an Rx usart interrupt. 2: 1st Rx character wrong after start- 2017/09/13 08:34:45 (permalink)
    0
    Well, you put that code inside the standard Interrupt handler - only one if a PIC16 or one of the two possible if on a PIC18
    #4
    mbrowning
    Super Member
    • Total Posts : 476
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: 2 Problems: 1: Creating an Rx usart interrupt. 2: 1st Rx character wrong after start- 2017/09/13 09:42:33 (permalink)
    +1 (1)
    The XC8 User Guide is pretty clear on this. For ver 1.43 section 5.9.1 is "Writing an Interrupt Service Routine"

    Mark
    #5
    Mitch
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2008/11/19 05:58:25
    • Location: 0
    • Status: offline
    Re: 2 Problems: 1: Creating an Rx usart interrupt. 2: 1st Rx character wrong after start- 2017/09/14 05:43:17 (permalink)
    0
    Ok thanks Mark.
     
    Any ideas about the corrupt 1st Rx character? I didn't think adding a start-up delay would help as I get the problem after loading new code with the power supply already connected so no power start-up issues.
    Mitch
    #6
    mbrowning
    Super Member
    • Total Posts : 476
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: 2 Problems: 1: Creating an Rx usart interrupt. 2: 1st Rx character wrong after start- 2017/09/14 06:41:59 (permalink)
    +2 (2)
    Maybe this is your problem
    BAUDCON1bits.WUE = 1; // EUSART will continue to sample the Rx pin

    From the datasheet
    20.2.4.2 Special Considerations Using the WUE Bit
    . . .  setting the WUE bit
    places the EUSART in an inactive state. The wake-up
    event causes a receive interrupt by setting the RCxIF
    bit. The WUE bit is cleared after this when a rising
    edge is seen on RXx/DTx. The interrupt condition is
    then cleared by reading the RCREGx register.
    Ordinarily, the data in RCREGx will be dummy data
    and should be discarded.

    I think WUE should only be set right before the sleep instruction and only when RXD is quiet. Otherwise it will cause an interrupt on the first RXD rising edge and provide "dummy data".
    also
    PIR1bits.RC1IF = FALSE;             //reset rx interrupt flag
    is useless. RC1IF is a read only bit that can only be cleared by reading RCREG
     

    Mark
    #7
    Mitch
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2008/11/19 05:58:25
    • Location: 0
    • Status: offline
    Re: 2 Problems: 1: Creating an Rx usart interrupt. 2: 1st Rx character wrong after start- 2017/09/19 03:33:51 (permalink)
    0
    Thanks Mark, that did the trick.
    Mitch
    #8
    Jump to:
    © 2017 APG vNext Commercial Version 4.5