• AVR Freaks

Hot!Unable to receive continuously in PIC18F26K83(UART1)

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

Unable to receive continuously in PIC18F26K83(UART1)

Hi,
I am trying to send two bytes of data from hyper-terminal (real term) to the PIC18F26K83. I am facing two issues :
 
a- I am able to receive the data only when I provide some delay before calling the UART receive init function in the while loop.
b- I am unable to receive the data continuously i.e after receiving the first set of two bytes and processing some task based on these bytes I am unable to receive the data i.e unable to receive the next two bytes.
When I try to debug I can see that the execution stops or doesn't move further (in the second iteration)  from the delay statement written before the  UART receive init function.
 
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)
{
    __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_RX_Initialize()
{
U1CON0bits.BRGS = 0; /* Normal rate*/

/* For 9600 Baud Rate */

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

PPS_Config();

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
  {
     ;
  }
}

 
Is there any configuration part missing ? or any logical mistake ?
 
 
Regards
Ali
#1

8 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: Unable to receive continuously in PIC18F26K83(UART1) 2019/05/20 03:22:47 (permalink)
    +1 (1)
    As in your other topic, you are not showing the ENTIRE C file.
    Have you disabled the Watchdog Timer in the config settings?
     

    Nearly there...
    #2
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Unable to receive continuously in PIC18F26K83(UART1) 2019/05/20 03:30:54 (permalink)
    0
    qhb
    As in your other topic, you are not showing the ENTIRE C file.
    Have you disabled the Watchdog Timer in the config settings?



    Hi qhb.
     
    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)
     
     
    Regards
    Ali
    #3
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Unable to receive continuously in PIC18F26K83(UART1) 2019/05/20 03:53:26 (permalink)
    +1 (1)
    Try doing the PPSconfig immediately before any delay in main(), BEFORE you initialise the USART
     

    b- I am unable to receive the data continuously i.e after receiving the first set of two bytes and processing some task based on these bytes I am unable to receive the data i.e unable to receive the next two bytes.

    Yes, that is exactly what the code you wrote will do.
    Once your "Byte_Received" variable reaches 2, you stop receiving characters.
    Nothing ever clears that variable back to zero.
     
     

    Nearly there...
    #4
    rolandoaguilera90
    Starting Member
    • Total Posts : 61
    • Reward points : 0
    • Joined: 2017/07/04 01:02:21
    • Location: 0
    • Status: offline
    Re: Unable to receive continuously in PIC18F26K83(UART1) 2019/05/20 03:53:32 (permalink)
    +2 (2)
    UART initialize function shouldnt go on the while(1) loop, it should go before the infinite loop. Maybe that is the causing the issue.
    #5
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Unable to receive continuously in PIC18F26K83(UART1) 2019/05/20 20:46:15 (permalink)
    0
    qhb
    Try doing the PPSconfig immediately before any delay in main(), BEFORE you initialise the USART
     

    b- I am unable to receive the data continuously i.e after receiving the first set of two bytes and processing some task based on these bytes I am unable to receive the data i.e unable to receive the next two bytes.

    Yes, that is exactly what the code you wrote will do.
    Once your "Byte_Received" variable reaches 2, you stop receiving characters.
    Nothing ever clears that variable back to zero.
     



    Thank you qhb,
     
    I tried that , but also unable to receive the data continuously.
     
    Regards
    Ali
    #6
    pcbbc
    Super Member
    • Total Posts : 1013
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: Unable to receive continuously in PIC18F26K83(UART1) 2019/05/20 22:24:45 (permalink)
    +1 (1)
    M.Ali
    qhbTry doing the PPSconfig immediately before any delay in main(), BEFORE you initialise the USART 
    b- I am unable to receive the data continuously i.e after receiving the first set of two bytes and processing some task based on these bytes I am unable to receive the data i.e unable to receive the next two bytes.
    Yes, that is exactly what the code you wrote will do.Once your "Byte_Received" variable reaches 2, you stop receiving characters.Nothing ever clears that variable back to zero. 


    Thank you qhb, I tried that , but also unable to receive the data continuously. RegardsAli

    You tried what?

    qhb pointed out a bug in your code: It receives some characters and then stops. There was no solution posted, only an observation, so therefore nothing “to try”.

    Pro tip: Every time you change something post your entire modified code so we can see exactly what you have changed.
    #7
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Unable to receive continuously in PIC18F26K83(UART1) 2019/05/20 22:32:16 (permalink)
    0
    pcbbc
    You tried what?

    qhb pointed out a bug in your code: It receives some characters and then stops. There was no solution posted, only an observation, so therefore nothing “to try”.

    Pro tip: Every time you change something post your entire modified code so we can see exactly what you have changed.



    Hi pcbbc,
     
    I have cleared the Byte_Receive variable back to zero in the WriteData function.
     
    Here is the 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 = 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)

    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)
    {
    __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_RX_Initialize()
    {
    U1CON0bits.BRGS = 0; /* Normal rate*/

    /* For 9600 Baud Rate */

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

    PPS_Config();

    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;
    }
    }

     
    Regards
    Ali
    #8
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Unable to receive continuously in PIC18F26K83(UART1) 2019/05/20 22:47:27 (permalink)
    +1 (1)
    Your new code is going to throw away every third byte received.
    You really don't seem to be very good at thinking through what your code is going to do...

    Nearly there...
    #9
    Jump to:
    © 2019 APG vNext Commercial Version 4.5