USART PPS Mapping help for 16F18345

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
dslocum
Posts: 102
Joined: 31 Aug 2009 12:21

USART PPS Mapping help for 16F18345

#1 Post by dslocum » 30 Mar 2023 19:19

I've been at this for hours, trying everything I can think of. I need to get an interrupt from the USART and read the RX input. Should be simple enough and I "have" done some interrupt stuff successfully in the past. I think I don't understand the PPS stuff, as the docs are pretty confusing.

I'm reading MIDI inputs. I have the requisite OPTO coupler installed and the output looks fine. My 8MHz crystal is working and I've verified the frequency. I've verified the LED is working.

To test, I'm trying to simply light an LED within the interrupt routine to prove that I'm getting there - unfortunately I'm NOT :-( Here are some snippets:

Code: Select all

sbit LED_MIDI at LATC1_bit;

void Init_Main(void) {

  UART1_Remappable_Init(31250);    // set MIDI speed
  
  // MIDI LED
  ANSC1_bit = 0;        // turn off analog
  TRISC1_bit = 0;       // set as output
  
  TRISB.TRISB5 = 1;     // set USART RX as input
  
  // set PPS pins
  Unlock_IOLOCK();
    PPS_Mapping(_RB5, _INPUT, _RXPPS );     // Not really sure WHAT to put here?
  Lock_IOLOCK();
  
  LED_MIDI = 0;

  PIE1.RCIE = 1;            //enable USART receiver interrupt bit
  RCSTA.SPEN = 1;           // enable USART
  INTCON.PEIE = 1;          //enable peripheral interrupt bit
  INTCON.GIE = 1;           //enable global interrupt bit
}

Code: Select all

void interrupt(void) {

    LED_MIDI = 1;

}  // interrupt()

Code: Select all

void main(void) {
  
  Init_Main();

  while(1) {   
  
  }
}
Thanks for any help.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: USART PPS Mapping help for 16F18345

#2 Post by filip » 06 Apr 2023 07:45

Hi,

Have you consulted Help and datasheet on how to set PPS parameters ?

Regards,
Filip.

dslocum
Posts: 102
Joined: 31 Aug 2009 12:21

Re: USART PPS Mapping help for 16F18345

#3 Post by dslocum » 12 Apr 2023 17:24

Thanks Filip,

I got things sorted. Like many things in life, it's easy - once you understand! I would also state that it is NOT straight-forward. Many bits must be set to get this to work, particularly when using an interrupt.

If it helps anyone else, here's what I had to do:

Code: Select all

void interrupt(void) {
  BYTE rcv_byte;
  
  if (PIR1.RCIF) {          // test the interrupt for uart rx
  
    if (RC1STA.FERR == 1) {    // look out for framing errors
      //LED_MIDI = LED_ON;   // for testing only
    }

    if (OERR_bit == 1) {        // over run?
      rcv_byte = RC1REG;        // flush
      rcv_byte = RC1REG;
      rcv_byte = RC1REG;
      rcv_byte = RC1REG;
      OERR_bit = 0;
    }
    else {
      //LED_MIDI = LED_ON;    // for testing only

      rx_fifo[rx_inptr] = Uart1_Remappable_Read();  //
      // wrap pointer if necessary
      if (rx_inptr < RX_FIFOSIZE) {
        rx_inptr++;                    // increment the pointer
      }
      else {
        rx_inptr = 0;
      }
    }
    //PIR1.RCIF = 0;       // clear RCIF
  } // if (PIR1.RCIF)
  
}  // interrupt()

Code: Select all

// UART Init sequence
  TX1STA.TXEN = 0;
  RC1STA.CREN = 1;
  RC1STA.SPEN = 1;
  RC1STA.SYNC = 0;
  RC1STA.RX9 = 0;
  PIE1.RCIE = 1;
  INTCON.PEIE = 1;

  // set USART RX as input
  TRISB.TRISB5 = 1;
  ANSB5_bit = 0;

  UART1_Remappable_Init(31250);    // set MIDI speed
  Unlock_IOLOCK();      // set PPS pins   - Must be done AFTER Init_MIDI
    PPS_Mapping(_RB5, _INPUT, _RXPPS );
  Lock_IOLOCK();

Post Reply

Return to “mikroC PRO for PIC General”