• AVR Freaks

Hot!Missing byte in UART transmission in PIC18F26K83

Author
M.Ali
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2018/11/12 00:36:07
  • Location: 0
  • Status: offline
2019/05/19 22:00:04 (permalink)
0

Missing byte in UART transmission in PIC18F26K83

Hi,
I am transmitting 10 bytes through UART1 in PIC18F26K83 and trying to see it on a hyperterminal(Tera term or Real term). I have enabled the Tx interrupt for this. But on the terminal I can see only 9 bytes + 1 byte as "0". The last byte is always zero. First byte received on the terminal is the second byte of the data which I am transmitting through UART1. Here is the code snippet :
 

void main(void)
{
    OSCILLATOR_Initialize();

   INTCON0bits.IPEN = 0;
   PMD5bits.U1MD = 0;
   PMD5bits.U2MD = 0;

   TRISCbits.TRISC0 = 0;
   TRISAbits.TRISA5 = 0;
   TRISAbits.TRISA7 = 0;
   TRISAbits.TRISA1 = 0;
   TRISAbits.TRISA2 = 0;
   TRISAbits.TRISA3 = 0;
   TRISAbits.TRISA4 = 0;

   TRISCbits.TRISC1 = 0;
   TRISCbits.TRISC3 = 0;
   TRISCbits.TRISC4 = 0;
   TRISCbits.TRISC6 = 0;

   TRISBbits.TRISB0 = 0;
   TRISBbits.TRISB1 = 0;
   TRISBbits.TRISB2 = 0;
   TRISBbits.TRISB5 = 0;
   TRISBbits.TRISB6 = 0;
   TRISBbits.TRISB7 = 0;

   while(1)
   {
        UART1_TX_Initialize();
   }
}

void OSCILLATOR_Initialize(void)
{
// NOSC EXTOSC; NDIV 1;
OSCCON1 = 0x60;
// CSWHOLD may proceed; SOSCPWR Low power;
OSCCON3 = 0x00;
// MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
OSCEN = 0x40;
// HFFRQ 4_MHz;
OSCFRQ = 0x08;
// TUN 0;
OSCTUNE = 0x40;
}
void PPS_Config()
{
INTCON0bits.GIE = 0;
PPSLOCK = 0x55;
PPSLOCK = 0xAA;
PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS
/* PPS Configuration code*/

U1RXPPS = 0x17; //RC7->UART1:RX;
RC5PPS = 0x13; //RC5->UART1:TX
PPSLOCK = 0x55;
PPSLOCK = 0xAA;
PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS
INTCON0bits.GIE = 1;
}
void Peripheral_Off()
{
CLKRCON = 0x00;
CLKRCLK = 0x00;
DMA1CON0 = 0x00;
DMA2CON0 = 0x00;
}

void UART1_TX_Initialize()
{
Tx_Data[0] = 0xFF;
Tx_Data[1] = 0xFF;
Tx_Data[2] = 0xFF;
Tx_Data[3] = 0x41;
Tx_Data[4] = 0x42;
Tx_Data[5] = 0x43;
Tx_Data[6] = 0x44;
Tx_Data[7] = 0xFF;
Tx_Data[8] = 0xFF;
Tx_Data[9] = 0xFF;

U1CON0bits.BRGS = 0; /* Normal rate*/

/* For 9600 Baud Rate */


U1BRGL = 0xA0; /* Baud Rate */
U1BRGH = 0x01; /* Baud Rate */

U1CON0bits.MODE = 0;
U1CON0bits.MODE0 = 0;
U1CON0bits.MODE1 = 0;
U1CON0bits.MODE2 = 0;

U1CON1bits.ON = 1;

U1CON0bits.TXEN = 1;

PPS_Config();

PIE3bits.U1IE = 1;
PIE3bits.U1TXIE = 1;

}
 
void __interrupt(irq(U1TX)) INTERRUPT_InterruptManager1 (void)
{
   if(U1TXIF == 1)
  {
     if(Byte <= 9)
    {
        U1TXB = Tx_Data[Byte];
        Byte++;
    }
    else
    {
        ;
     }
  }
}
 
 
 
Is there anything wrong in UART configuration ? or any other error ?
 
Regards
Ali
 
 
 
#1

17 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/19 22:12:58 (permalink)
    +1 (1)
    Not your problem, but this line in your ISR is probably redundant
       if(U1TXIF == 1)
    as with vectored interrupts, the only way you can get there is if U1TXIF is set.
     
    As the first three bytes you send are all 0xFF, how do you know that you are missing the FIRST byte?
    (You're probably right, but it's not a safe assumption)
     
    I would recommend adding a delay of one byte time (about 1ms for 9600 baud) between enabling the USART, and enabling TX interrupts.
    Without that, the first character will be seen as part of the glitch when the Tx pin is first driven high.
     
     

    Nearly there...
    #2
    kseg
    Starting Member
    • Total Posts : 65
    • Reward points : 0
    • Joined: 2016/05/26 07:30:47
    • Location: 0
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/19 22:17:23 (permalink)
    0
    Seems the compiler version is XC8 2.x. I don't have experience on XC8 2.x.
    try to clear U1TXIF in the ISR.
    #3
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/19 22:20:16 (permalink)
    0
    Thank you qhb.
     
    qhb
    As the first three bytes you send are all 0xFF, how do you know that you are missing the FIRST byte?
    (You're probably right, but it's not a safe assumption)

    I am receiving the data on the terminal as FF FF FF 41 42 43 44 FF FF 00
    Sorry . Its actually the last byte I am missing.
     
    qhb
    I would recommend adding a delay of one byte time (about 1ms for 9600 baud) between enabling the USART, and enabling TX interrupts.
    Without that, the first character will be seen as part of the glitch when the Tx pin is first driven high.
     

    I will try with adding a delay of 1ms.
     
    Regards
    Ali
    #4
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/19 22:24:41 (permalink)
    0
    kseg
    Seems the compiler version is XC8 2.x. I don't have experience on XC8 2.x.
    try to clear U1TXIF in the ISR.



    Thank you Kseg.
    In the datasheet its mentioned that U1TXIF is read only.
     
    Regards
    Ali
    #5
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/19 22:29:49 (permalink)
    +1 (1)
    Your code is rapidly calling        
    UART1_TX_Initialize();
    over and over, which is sure to be confusing the issue!
     
    Maybe get your ISR to clear TXIE once the count has reached 9, and get your main code to wait until it sees TXIE cleared before it loops back.
     
     

    Nearly there...
    #6
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/19 22:32:47 (permalink)
    +1 (1)
    M.Ali
    In the datasheet its mentioned that U1TXIF is read only.

    Indeed. The only way to clear U1TXIF is to write to U1TXB.
    In a real application it is normal to clear TXIE until you actually have data ready to send.
    It's rare to actually require interrupt driven transmit at all. Do you need to be doing other things in parallel with transmission?
    It's much simpler to use polling for transmit, and just use interrupt driven receive to implement a receive buffer.
     
     

    Nearly there...
    #7
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/19 22:39:02 (permalink)
    0
    qhb
    Maybe get your ISR to clear TXIE once the count has reached 9, and get your main code to wait until it sees TXIE cleared before it loops back.

    I tried it but still the issue persists. 
    Here is the updated code :

     
    void __interrupt(irq(U1TX)) INTERRUPT_InterruptManager1 (void)
    {
     if(U1TXIF == 1)
     {
        if(Byte <= 9)
       {
          U1TXB = Tx_Data[Byte];
          Byte++;
       }
       else
       {
          PIE3bits.U1TXIE = 0;
        }
     
      }
    }
     

    post edited by M.Ali - 2019/05/19 22:54:27
    #8
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/19 22:41:31 (permalink)
    0
    qhb
    M.Ali
    In the datasheet its mentioned that U1TXIF is read only.

    Indeed. The only way to clear U1TXIF is to write to U1TXB.
    In a real application it is normal to clear TXIE until you actually have data ready to send.
    It's rare to actually require interrupt driven transmit at all. Do you need to be doing other things in parallel with transmission?
    It's much simpler to use polling for transmit, and just use interrupt driven receive to implement a receive buffer.



    Yes.
    I have kept it as last option.
     
     
    Regards
    Ali
    #9
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/19 23:00:22 (permalink)
    +1 (1)
    M.Ali
    I tried it but still the issue persists. 

    Have you tried NOT rapidly calling the init function?

    Nearly there...
    #10
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/19 23:06:25 (permalink)
    0
    qhb
    M.Ali
    I tried it but still the issue persists. 

    Have you tried NOT rapidly calling the init function?

    Thank you qhb.
     
    Not yet.
    I will check it now.
     
    Regards
    Ali
    #11
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/20 02:49:30 (permalink)
    0
    qhb
    M.Ali
    I tried it but still the issue persists. 

    Have you tried NOT rapidly calling the init function?



    I tried by not rapidly calling the init function. But also the issue persists. I called the init function just once before while loop.
     
    Regards
    Ali
    #12
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/20 03:04:41 (permalink)
    +1 (1)
    Please post what your entire C file looks like now.
     

    Nearly there...
    #13
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/20 03:11:18 (permalink)
    0
    qhb
    Please post what your entire C file looks like now.



    Hi qhb.
    Here is the entire file :


    void main(void) 
    {
        OSCILLATOR_Initialize();

       INTCON0bits.IPEN = 0;
       PMD5bits.U1MD = 0;
       PMD5bits.U2MD = 0;

       TRISCbits.TRISC0 = 0;
       TRISAbits.TRISA5 = 0;
       TRISAbits.TRISA7 = 0;
       TRISAbits.TRISA1 = 0;
       TRISAbits.TRISA2 = 0;
       TRISAbits.TRISA3 = 0;
       TRISAbits.TRISA4 = 0;

       TRISCbits.TRISC1 = 0;
       TRISCbits.TRISC3 = 0;
       TRISCbits.TRISC4 = 0;
       TRISCbits.TRISC6 = 0;

       TRISBbits.TRISB0 = 0;
       TRISBbits.TRISB1 = 0;
       TRISBbits.TRISB2 = 0;
       TRISBbits.TRISB5 = 0;
       TRISBbits.TRISB6 = 0;
       TRISBbits.TRISB7 = 0;

    UART1_TX_Initialize();
       while(1)
       {
            ;
       }
    }

    void OSCILLATOR_Initialize(void)
    {
    // NOSC EXTOSC; NDIV 1;
    OSCCON1 = 0x60;
    // CSWHOLD may proceed; SOSCPWR Low power;
    OSCCON3 = 0x00;
    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = 0x40;
    // HFFRQ 4_MHz;
    OSCFRQ = 0x08;
    // TUN 0;
    OSCTUNE = 0x40;
    }
    void PPS_Config()
    {
    INTCON0bits.GIE = 0;
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS
    /* PPS Configuration code*/

    U1RXPPS = 0x17; //RC7->UART1:RX;
    RC5PPS = 0x13; //RC5->UART1:TX
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS
    INTCON0bits.GIE = 1;
    }
    void Peripheral_Off()
    {
    CLKRCON = 0x00;
    CLKRCLK = 0x00;
    DMA1CON0 = 0x00;
    DMA2CON0 = 0x00;
    }

    void UART1_TX_Initialize()
    {
    Tx_Data[0] = 0xFF;
    Tx_Data[1] = 0xFF;
    Tx_Data[2] = 0xFF;
    Tx_Data[3] = 0x41;
    Tx_Data[4] = 0x42;
    Tx_Data[5] = 0x43;
    Tx_Data[6] = 0x44;
    Tx_Data[7] = 0xFF;
    Tx_Data[8] = 0xFF;
    Tx_Data[9] = 0xFF;

    U1CON0bits.BRGS = 0; /* Normal rate*/

    /* For 9600 Baud Rate */


    U1BRGL = 0xA0; /* Baud Rate */
    U1BRGH = 0x01; /* Baud Rate */

    U1CON0bits.MODE = 0;
    U1CON0bits.MODE0 = 0;
    U1CON0bits.MODE1 = 0;
    U1CON0bits.MODE2 = 0;

    U1CON1bits.ON = 1;

    U1CON0bits.TXEN = 1;

    PPS_Config();

    PIE3bits.U1IE = 1;
    PIE3bits.U1TXIE = 1;

    }
     
    void __interrupt(irq(U1TX)) INTERRUPT_InterruptManager1 (void)
    {
       if(U1TXIF == 1)
      {
         if(Byte <= 9)
        {
            U1TXB = Tx_Data[Byte];
            Byte++;
        }
        else
        {
           PIE3bits.U1TXIE = 0;
         }
      }
    }



     
     
    Regards
    Ali
    post edited by M.Ali - 2019/05/20 03:12:52
    #14
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/20 03:15:28 (permalink)
    +1 (1)
    Entire?
    Where are the CONFIG settings, and where is the "Byte" variable defined and initialised?
     

    Nearly there...
    #15
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/20 03:25:18 (permalink)
    0
    qhb
    Entire?
    Where are the CONFIG settings, and where is the "Byte" variable defined and initialised?

    Sorry.
     
    Here are the CONFIG settings :
     

    // CONFIG1L
    #pragma config FEXTOSC = OFF // External Oscillator Selection (Oscillator not enabled)
    #pragma config RSTOSC = HFINTOSC_64MHZ// Reset Oscillator Selection (HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1)
    // CONFIG1H
    #pragma config CLKOUTEN = ON // Clock out Enable bit (CLKOUT function is enabled)
    #pragma config PR1WAY = OFF // PRLOCKED One-Way Set Enable bit (PRLOCK bit can be set and cleared repeatedly)
    #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
    #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
    // CONFIG2L
    #pragma config MCLRE = INTMCLR // MCLR Enable bit (If LVP = 0, MCLR pin function is port defined function; If LVP =1, RE3 pin fuction is MCLR)
    #pragma config PWRTS = PWRT_64 // Power-up timer selection bits (PWRT set at 64ms)
    #pragma config MVECEN = ON // Multi-vector enable bit (Multi-vector enabled, Vector table used for interrupts)
    #pragma config IVT1WAY = ON // IVTLOCK bit One-way set enable bit (IVTLOCK bit can be cleared and set only once)
    #pragma config LPBOREN = ON // Low Power BOR Enable bit (ULPBOR enabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled according to SBOREN)
    // CONFIG2H
    #pragma config BORV = VBOR_2P85 // Brown-out Reset Voltage Selection bits (Brown-out Reset Voltage (VBOR) set to 2.8V)
    #pragma config ZCD = OFF // ZCD Disable bit (ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
    #pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
    #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
    #pragma config DEBUG = OFF // Debugger Enable bit (Background debugger disabled)
    #pragma config XINST = OFF // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)
    // CONFIG3L
    #pragma config WDTCPS = WDTCPS_7// WDT Period selection bits (Divider ratio 1:4096)
    #pragma config WDTE = SWDTEN // WDT operating mode (WDT enabled/disabled by SWDTEN bit)
    // CONFIG3H
    #pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
    #pragma config WDTCCS = LFINTOSC// WDT input clock selector (WDT reference clock is the 31.0 kHz LFINTOSC)
    // CONFIG4L
    #pragma config BBSIZE = BBSIZE_512// Boot Block Size selection bits (Boot Block size is 512 words)
    #pragma config BBEN = OFF // Boot Block enable bit (Boot block disabled)
    #pragma config SAFEN = OFF // Storage Area Flash enable bit (SAF disabled)
    #pragma config WRTAPP = OFF // Application Block write protection bit (Application Block not write protected)
    // CONFIG4H
    #pragma config WRTB = OFF // Configuration Register Write Protection bit (Configuration registers (300000-30000Bh) not write-protected)
    #pragma config WRTC = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
    #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
    #pragma config WRTSAF = OFF // SAF Write protection bit (SAF not Write Protected)
    #pragma config LVP = OFF // Low Voltage Programming Enable bit (HV on MCLR/VPP must be used for programming)
    // CONFIG5L
    #pragma config CP = OFF // PFM and Data EEPROM Code Protection bit (PFM and Data EEPROM code protection disabled)

     
     
    And the Byte is defined in the ISR 

    void __interrupt(irq(U1TX)) INTERRUPT_InterruptManager1 (void)
    {
    static int Byte = 0;
       if(U1TXIF == 1)
      {
         if(Byte <= 9)
        {
            U1TXB = Tx_Data[Byte];
            Byte++;
        }
        else
        {
           PIE3bits.U1TXIE = 0;
         }
      }
    }


     
    Regards
    Ali
    post edited by M.Ali - 2019/05/20 03:26:28
    #16
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/20 03:44:12 (permalink)
    +2 (2)
    M.Ali
    Here are the CONFIG settings :

    I'm not sure if that defaults to WDT on, or off.
    I'd recommend setting it totally OFF in the config settings, until such time as you see a need for it.
     

    And the Byte is defined in the ISR 

    When did you add that? It doesn't appear in any of the versions you posted earlier.
    Please, do NOT create edited versions of your code to post here. Post what you are really using.
    It wastes everyone's time when you pick and choose what to post, as most people asking questions are looking in the wrong part of their code for the fault.

    Nearly there...
    #17
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Missing byte in UART transmission in PIC18F26K83 2019/05/20 20:35:23 (permalink)
    0
    qhb
    M.Ali
    Here are the CONFIG settings :

    I'm not sure if that defaults to WDT on, or off.
    I'd recommend setting it totally OFF in the config settings, until such time as you see a need for it.
     

    And the Byte is defined in the ISR 

    When did you add that? It doesn't appear in any of the versions you posted earlier.
    Please, do NOT create edited versions of your code to post here. Post what you are really using.
    It wastes everyone's time when you pick and choose what to post, as most people asking questions are looking in the wrong part of their code for the fault.

    Hi qhb,
    Actually I am facing issues in both UART transmit and receive, so I tried to ask two separately on  the forum. While modifying I missed some parts.
     Here is the entire code :
     

    // CONFIG1L
    #pragma config FEXTOSC = OFF // External Oscillator Selection (Oscillator not enabled)
    #pragma config RSTOSC = HFINTOSC_64MHZ// Reset Oscillator Selection (HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1)
    // CONFIG1H
    #pragma config CLKOUTEN = ON // Clock out Enable bit (CLKOUT function is enabled)
    #pragma config PR1WAY = OFF // PRLOCKED One-Way Set Enable bit (PRLOCK bit can be set and cleared repeatedly)
    #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
    #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
    // CONFIG2L
    #pragma config MCLRE = INTMCLR // MCLR Enable bit (If LVP = 0, MCLR pin function is port defined function; If LVP =1, RE3 pin fuction is MCLR)
    #pragma config PWRTS = PWRT_64 // Power-up timer selection bits (PWRT set at 64ms)
    #pragma config MVECEN = ON // Multi-vector enable bit (Multi-vector enabled, Vector table used for interrupts)
    #pragma config IVT1WAY = ON // IVTLOCK bit One-way set enable bit (IVTLOCK bit can be cleared and set only once)
    #pragma config LPBOREN = ON // Low Power BOR Enable bit (ULPBOR enabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled according to SBOREN)
    // CONFIG2H
    #pragma config BORV = VBOR_2P85 // Brown-out Reset Voltage Selection bits (Brown-out Reset Voltage (VBOR) set to 2.8V)
    #pragma config ZCD = OFF // ZCD Disable bit (ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
    #pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
    #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
    #pragma config DEBUG = OFF // Debugger Enable bit (Background debugger disabled)
    #pragma config XINST = OFF // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)
    // CONFIG3L
    #pragma config WDTCPS = WDTCPS_7// WDT Period selection bits (Divider ratio 1:4096)
    #pragma config WDTE = OFF// WDT operating mode (WDT enabled/disabled by SWDTEN bit)
    // CONFIG3H
    #pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
    #pragma config WDTCCS = LFINTOSC// WDT input clock selector (WDT reference clock is the 31.0 kHz LFINTOSC)
    // CONFIG4L
    #pragma config BBSIZE = BBSIZE_512// Boot Block Size selection bits (Boot Block size is 512 words)
    #pragma config BBEN = OFF // Boot Block enable bit (Boot block disabled)
    #pragma config SAFEN = OFF // Storage Area Flash enable bit (SAF disabled)
    #pragma config WRTAPP = OFF // Application Block write protection bit (Application Block not write protected)
    // CONFIG4H
    #pragma config WRTB = OFF // Configuration Register Write Protection bit (Configuration registers (300000-30000Bh) not write-protected)
    #pragma config WRTC = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
    #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
    #pragma config WRTSAF = OFF // SAF Write protection bit (SAF not Write Protected)
    #pragma config LVP = OFF // Low Voltage Programming Enable bit (HV on MCLR/VPP must be used for programming)
    // CONFIG5L
    #pragma config CP = OFF // PFM and Data EEPROM Code Protection bit (PFM and Data EEPROM code protection disabled)

    void main(void)
    {
    OSCILLATOR_Initialize();

    INTCON0bits.IPEN = 0;
    PMD5bits.U1MD = 0;
    PMD5bits.U2MD = 0;

    PPS_Config();

    TRISCbits.TRISC0 = 0;
    TRISAbits.TRISA5 = 0;
    TRISAbits.TRISA7 = 0;
    TRISAbits.TRISA1 = 0;
    TRISAbits.TRISA2 = 0;
    TRISAbits.TRISA3 = 0;
    TRISAbits.TRISA4 = 0;

    TRISCbits.TRISC1 = 0;
    TRISCbits.TRISC3 = 0;
    TRISCbits.TRISC4 = 0;
    TRISCbits.TRISC6 = 0;

    TRISBbits.TRISB0 = 0;
    TRISBbits.TRISB1 = 0;
    TRISBbits.TRISB2 = 0;
    TRISBbits.TRISB5 = 0;
    TRISBbits.TRISB6 = 0;
    TRISBbits.TRISB7 = 0;

    while(1)
    {
    __delay_ms(1000);
    UART1_RX_Initialize();
    if ((Recvd_Byte[0] == 0x00) && (Recvd_Byte[1] == 0x00))
    {
    ADC();
    }
    else if((Recvd_Byte[0] == 0x00) && (Recvd_Byte[1] == 0x01))
    {
    LED();
    }
    else
    {
    ;
    }
    }
    }
    void OSCILLATOR_Initialize(void)
    {
    // NOSC EXTOSC; NDIV 1;
    OSCCON1 = 0x60;
    // CSWHOLD may proceed; SOSCPWR Low power;
    OSCCON3 = 0x00;
    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = 0x40;
    // HFFRQ 4_MHz;
    OSCFRQ = 0x08;
    // TUN 0;
    OSCTUNE = 0x40;
    }
    void PPS_Config()
    {
    INTCON0bits.GIE = 0;
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS
    /* PPS Configuration code*/

    U1RXPPS = 0x17; //RC7->UART1:RX;
    RC5PPS = 0x13; //RC5->UART1:TX
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS
    INTCON0bits.GIE = 1;
    }
    void Peripheral_Off()
    {
    CLKRCON = 0x00;
    CLKRCLK = 0x00;
    DMA1CON0 = 0x00;
    DMA2CON0 = 0x00;
    }
    void UART1_TX_Initialize()
    {
    Tx_Data[0] = 0xFF;
    Tx_Data[1] = 0xFF;
    Tx_Data[2] = 0xFF;
    Tx_Data[7] = 0xFF;
    Tx_Data[8] = 0xFF;
    Tx_Data[9] = 0xFF;
    U1CON0bits.BRGS = 0; /* Normal rate*/

    /* For 9600 Baud Rate */


    U1BRGL = 0xA0; /* Baud Rate */
    U1BRGH = 0x01; /* Baud Rate */

    U1CON0bits.MODE = 0;
    U1CON0bits.MODE0 = 0;
    U1CON0bits.MODE1 = 0;
    U1CON0bits.MODE2 = 0;

    U1CON1bits.ON = 1;

    U1CON0bits.TXEN = 1;


    PIE3bits.U1IE = 1;
    PIE3bits.U1TXIE = 1;

    }

    void __interrupt(irq(U1TX)) INTERRUPT_InterruptManager1 (void)
    {
    static int Byte = 0;
    if(U1TXIF == 1)
    {
    if(Byte <= 9)
    {
    U1TXB = Tx_Data[Byte];
    Byte++;
    }
    else
    {
    PIE3bits.U1TXIE = 0;
    }
    }
    }

    void UART1_RX_Initialize()
    {
    U1CON0bits.BRGS = 0; /* Normal rate*/

    /* For 9600 Baud Rate */

    U1BRGL = 0xA0; /* Baud Rate */
    U1BRGH = 0x01; /* Baud Rate */



    ANSELCbits.ANSELC7 = 0x00;

    U1CON0bits.MODE = 0;
    U1CON0bits.MODE0 = 0;
    U1CON0bits.MODE1 = 0;
    U1CON0bits.MODE2 = 0;

    U1CON1bits.ON = 1;

    PIE3bits.U1IE = 1;
    PIE3bits.U1RXIE = 1;

    U1CON0bits.RXEN = 1;
    }
     

    void __interrupt(irq(U1RX)) INTERRUPT_InterruptManager2 (void)
    {
    Rcvd_Char = U1RXB;
    WriteData(Rcvd_Char);
    }


    void WriteData(char data)
    {
    static int Byte_Received = 0 ;
    if(Byte_Received < 2)
    {
    Recvd_Data[Byte_Received] = data;
    Byte_Received++;
    }
    else
    {
    Byte_Received = 0;
    }
    }

    void ADC()
    {

    Tx_Data[3] = 0x41;
    Tx_Data[4] = 0x42;
    Tx_Data[5] = 0x43;
    Tx_Data[6] = 0x44;
    UART1_TX_Initialize();
    }

    void LED()
    {
    //ON the LED
    Tx_Data[3] = 0x61;
    Tx_Data[4] = 0x62;
    Tx_Data[5] = 0x63;
    Tx_Data[6] = 0x64;
    UART1_TX_Initialize();
    }

     
    The above code consists of all the changes you had mentioned like, Calling the PPS_Config() before UART initialization, disabling the WDT, clearing Byte_Received variable back to zero, clearing the TXIE and not calling the UART init function repeatedly.
     
    The issue still persists. 
     
     
    Regards
    Ali
    #18
    Jump to:
    © 2019 APG vNext Commercial Version 4.5