• AVR Freaks

AnsweredHot!Unexpected UART data in PIC18F26K83

Page: 12 > Showing page 1 of 2
Author
M.Ali
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2018/11/12 00:36:07
  • Location: 0
  • Status: offline
2019/06/13 08:56:43 (permalink)
0

Unexpected UART data in PIC18F26K83

Hi,
I am getting "00" as a byte when I transmit using UART. I am transmitting the data of 10 bytes as "FF FF FF AA AA AA AA FF FF FF" through UART but when I observe the data on a hyper terminal (Real Term) I can see the data as "FF FF AA AA AA AA FF FF FF 00". 
Here is my 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
 
#include <xc.h>
 

#define _XTAL_FREQ 64000000
 
void PPS_Config();
void UART1_TX_Init();
void UART1_RX_Init();
void WriteData(char data);
void Relay_Connect();
void OSCILLATOR_Initialize(void);
 
static int Byte = 0;
char Recvd_Data[2];
 
void main(void)
{
OSCILLATOR_Initialize();

INTCON0bits.IPEN = 0;
 
INTCON0bits.GIE = 1;

__delay_ms(500);


while(1)
{
__delay_ms(10);

UART1_RX_Init();


if ((Recvd_Data[0] == 0x00) && (Recvd_Data[1] == 0x01))
{
Relay_Connect();
Response();
}

{
;
}

}

return;
}
 
 
 
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 UART1_RX_Init()
{

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 PPS_Config()
{

INTCON0bits.GIE = 0;
PPSLOCK = 0x55;
PPSLOCK = 0xAA;
PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS


/* PPS Configuration code*/


U1RXPPS = 0x17; //RC7->UART1:RX1
RC5PPS = 0x13; //RC5->UART1:TX

PPSLOCK = 0x55;
PPSLOCK = 0xAA;
PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS

INTCON0bits.GIE = 1;
 
}
 

void Relay_Connect()
{
TRISAbits.TRISA5 = 0;
LATAbits.LATA5 = 0;
}
 

void Response()
{
Send[0] = 0xFF;
Send[1] = 0xFF;
Send[2] = 0xFF;

Send[3] = 0xAA;
Send[4] = 0xAA;
Send[5] = 0xAA;
Send[6] = 0xAA;

Send[7] = 0xFF;
Send[8] = 0xFF;
Send[9] = 0xFF;

UART1_TX_Init();
for (Byte = 0 ; Byte < 10; Byte++)
{
UART1_Write(Send[Byte]);
}
}
 
void UART1_TX_Init()
{
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();

}
 
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
{
;
}
if (Byte_Received == 2)
{
Byte_Received = 0;
}
}
 
 
 

 
Any configuration changes I should make ?
 
Regards
Ali
post edited by M.Ali - 2019/06/13 08:58:15
#1
1and0
Access is Denied
  • Total Posts : 9252
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/13 09:03:58 (permalink)
+1 (1)
Why you keep initializing the UART in your main loop?  It should be done only once during initialization.
 
#2
M.Ali
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2018/11/12 00:36:07
  • Location: 0
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/13 09:08:08 (permalink)
0
1and0
Why you keep initializing the UART in your main loop?  It should be done only once during initialization.



But that is just for receive part. Does it have any effect on UART transmit ?
 
Regards
Ali
#3
1and0
Access is Denied
  • Total Posts : 9252
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/13 09:17:57 (permalink)
+1 (1)
M.Ali
1and0
Why you keep initializing the UART in your main loop?  It should be done only once during initialization.

But that is just for receive part. Does it have any effect on UART transmit ?

I don't know as I have not spent the time to study your code in details. Why don't you just move it and see for yourself?
 
Edit:  Recvd_Data[] should be declared as "volatile" as it is being modified in the ISR.  Also, the TX should be initialized only once during initialization, too.
post edited by 1and0 - 2019/06/13 09:30:16
#4
M.Ali
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2018/11/12 00:36:07
  • Location: 0
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/13 10:02:41 (permalink)
0
1and0
M.Ali
1and0
Why you keep initializing the UART in your main loop?  It should be done only once during initialization.

But that is just for receive part. Does it have any effect on UART transmit ?

I don't know as I have not spent the time to study your code in details. Why don't you just move it and see for yourself?
 
Edit:  Recvd_Data[] should be declared as "volatile" as it is being modified in the ISR.  Also, the TX should be initialized only once during initialization, too.




Thank you 1and0. 
I have initialized both RX and Tx only once . And also declared Recvd_Data[] as volatile. 
But also I am getting "FF FF AA AA AA AA FF FF FF 00" as the data on hyper terminal.
 
Regards
Ali
 
#5
1and0
Access is Denied
  • Total Posts : 9252
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/13 10:15:10 (permalink)
0
...
post edited by 1and0 - 2019/06/13 10:19:02
#6
1and0
Access is Denied
  • Total Posts : 9252
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/13 10:18:31 (permalink)
0
M.Ali
Here is my code :

void Response()
{
Send[0] = 0xFF;
Send[1] = 0xFF;
Send[2] = 0xFF;

Send[3] = 0xAA;
Send[4] = 0xAA;
Send[5] = 0xAA;
Send[6] = 0xAA;

Send[7] = 0xFF;
Send[8] = 0xFF;
Send[9] = 0xFF;

UART1_TX_Init();
for (Byte = 0 ; Byte < 10; Byte++)
{
UART1_Write(Send[Byte]);
}
}

 

Where is the definition of Send[]?
#7
mbrowning
Just a Member
  • Total Posts : 1365
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: online
Re: Unexpected UART data in PIC18F26K83 2019/06/13 10:23:36 (permalink)
0
1and0
Where is the definition of Send[]?

and the definition of UART1_Write() ?

Oh well - there's always next year
#8
M.Ali
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2018/11/12 00:36:07
  • Location: 0
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/13 10:31:34 (permalink)
0
1and0
M.Ali
Here is my code :

 
void Response()
{
Send[0] = 0xFF;
Send[1] = 0xFF;
Send[2] = 0xFF;

Send[3] = 0xAA;
Send[4] = 0xAA;
Send[5] = 0xAA;
Send[6] = 0xAA;

Send[7] = 0xFF;
Send[8] = 0xFF;
Send[9] = 0xFF;

UART1_TX_Init();
for (Byte = 0 ; Byte < 10; Byte++)
{
UART1_Write(Send[Byte]);
}
}
 

 

Where is the definition of Send[]?




Hi 1and0,
Sorry. I forgot to mention here. Actually I have written the code in different files. So forgot to include here. 
I am Sorry.
 
Here is the updated 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
#include <xc.h>

#define _XTAL_FREQ 64000000
void PPS_Config();
void UART1_TX_Init();
void UART1_RX_Init();
void WriteData(char data);
void Relay_Connect();
void OSCILLATOR_Initialize(void);
void UART1_Write(char txData)
static int Byte = 0;
char Recvd_Data[2];
char Send[40];
void main(void)
{
OSCILLATOR_Initialize();

INTCON0bits.IPEN = 0;
INTCON0bits.GIE = 1;

__delay_ms(500);


while(1)
{
__delay_ms(10);

UART1_RX_Init();


if ((Recvd_Data[0] == 0x00) && (Recvd_Data[1] == 0x01))
{
Relay_Connect();
Response();
}

{
;
}

}

return;
}
 
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 UART1_RX_Init()
{

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 PPS_Config()
{

INTCON0bits.GIE = 0;
PPSLOCK = 0x55;
PPSLOCK = 0xAA;
PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS


/* PPS Configuration code*/


U1RXPPS = 0x17; //RC7->UART1:RX1
RC5PPS = 0x13; //RC5->UART1:TX

PPSLOCK = 0x55;
PPSLOCK = 0xAA;
PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS

INTCON0bits.GIE = 1;
}

void Relay_Connect()
{
TRISAbits.TRISA5 = 0;
LATAbits.LATA5 = 0;
}

void Response()
{
Send[0] = 0xFF;
Send[1] = 0xFF;
Send[2] = 0xFF;

Send[3] = 0xAA;
Send[4] = 0xAA;
Send[5] = 0xAA;
Send[6] = 0xAA;

Send[7] = 0xFF;
Send[8] = 0xFF;
Send[9] = 0xFF;

UART1_TX_Init();
for (Byte = 0 ; Byte < 10; Byte++)
{
UART1_Write(Send[Byte]);
}
}
void UART1_TX_Init()
{
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();

}
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
{
;
}
if (Byte_Received == 2)
{
Byte_Received = 0;
}
}
void UART1_Write(char txData)
{
while(0 == PIR3bits.U1TXIF)
{
}
U1TXB = txData; // Write the data byte to the USART.
}
 
 

 
Regards
Ali
#9
M.Ali
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2018/11/12 00:36:07
  • Location: 0
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/13 10:33:16 (permalink)
0
mbrowning
1and0
Where is the definition of Send[]?

and the definition of UART1_Write() ?




Hi mbrowning,
 
Here it is :
 

void UART1_Write(char txData)
{
while(0 == PIR3bits.U1TXIF)
{
}
U1TXB = txData; // Write the data byte to the USART.
}
 

 
Regards
Ali
#10
dan1138
Super Member
  • Total Posts : 3105
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/13 12:19:30 (permalink)
0
This is the Original Posters code after a bit of formatting clean up and fixes to get it to compile:
/*
 * File: main.c
 * Target: PIC18F26K83
 * Compiler: XC8 v2.05
 * IDE: MPLABX v5.15
 */
// 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                 //
/*
 *
 */
#include <xc.h>
/*
 *
 */
#define _XTAL_FREQ 64000000
/*
 *
 */
void PPS_Config(void);
void UART1_TX_Init(void);
void UART1_RX_Init(void);
void UART1_Write(char txData);
void WriteData(char data);
void Relay_Connect(void);
void OSCILLATOR_Initialize(void);
void Response(void);
static int Byte = 0;
char Recvd_Data[2];
/*
 *
 */
void main(void)
{
    OSCILLATOR_Initialize();
    UART1_RX_Init();
    UART1_TX_Init();
    INTCON0bits.IPEN = 0;
    INTCON0bits.GIE = 1;
    __delay_ms(500);
    while(1)
    {
        __delay_ms(10);
        if ((Recvd_Data[0] == 0x00) && (Recvd_Data[1] == 0x01))
        {
            Relay_Connect();
            Response();
        }
    }
    return;
}
/*
 *
 */
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 UART1_RX_Init()
{
    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 PPS_Config()
{
    INTCON0bits.GIE = 0;
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS
    /* PPS Configuration code*/
    U1RXPPS = 0x17; //RC7->UART1:RX1
    RC5PPS = 0x13; //RC5->UART1:TX
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS
    INTCON0bits.GIE = 1;
}
/*
 *
 */
void Relay_Connect()
{
    TRISAbits.TRISA5 = 0;
    LATAbits.LATA5 = 0;
}
/*
 *
 */
void Response()
{
    char Send[10];
    Send[0] = 0xFF;
    Send[1] = 0xFF;
    Send[2] = 0xFF;
    Send[3] = 0xAA;
    Send[4] = 0xAA;
    Send[5] = 0xAA;
    Send[6] = 0xAA;
    Send[7] = 0xFF;
    Send[8] = 0xFF;
    Send[9] = 0xFF;
    for (Byte = 0 ; Byte < 10; Byte++)
    {
        UART1_Write(Send[Byte]);
    }
}
/*
 *
 */
void UART1_TX_Init()
{
    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();
}
/*
 *
 */
void __interrupt(irq(U1RX)) INTERRUPT_InterruptManager2 (void)
{
    char Rcvd_Char;
    
    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++;
    }
    if (Byte_Received == 2)
    {
        Byte_Received = 0;
    }
}
/*
 *
 */
void UART1_Write(char txData)
{
    while(0 == PIR3bits.U1TXIF)
    {
    }
    U1TXB = txData; // Write the data byte to the USART.
}

The MPLABX simulator seems to output the bytes to UART1 in the correct order and no extra 0x00 bytes.

Seems like the OP has a hardware problem. In many cases that the MPLABX simulator seems so flaky this may be another case where it does not match reality. But what do I know?
#11
M.Ali
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2018/11/12 00:36:07
  • Location: 0
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/13 22:56:25 (permalink)
0
dan1138
This is the Original Posters code after a bit of formatting clean up and fixes to get it to compile:
/*
 * File: main.c
 * Target: PIC18F26K83
 * Compiler: XC8 v2.05
 * IDE: MPLABX v5.15
 */
// 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                 //
/*
 *
 */
#include <xc.h>
/*
 *
 */
#define _XTAL_FREQ 64000000
/*
 *
 */
void PPS_Config(void);
void UART1_TX_Init(void);
void UART1_RX_Init(void);
void UART1_Write(char txData);
void WriteData(char data);
void Relay_Connect(void);
void OSCILLATOR_Initialize(void);
void Response(void);
static int Byte = 0;
char Recvd_Data[2];
/*
 *
 */
void main(void)
{
    OSCILLATOR_Initialize();
    UART1_RX_Init();
    UART1_TX_Init();
    INTCON0bits.IPEN = 0;
    INTCON0bits.GIE = 1;
    __delay_ms(500);
    while(1)
    {
        __delay_ms(10);
        if ((Recvd_Data[0] == 0x00) && (Recvd_Data[1] == 0x01))
        {
            Relay_Connect();
            Response();
        }
    }
    return;
}
/*
 *
 */
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 UART1_RX_Init()
{
    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 PPS_Config()
{
    INTCON0bits.GIE = 0;
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS
    /* PPS Configuration code*/
    U1RXPPS = 0x17; //RC7->UART1:RX1
    RC5PPS = 0x13; //RC5->UART1:TX
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS
    INTCON0bits.GIE = 1;
}
/*
 *
 */
void Relay_Connect()
{
    TRISAbits.TRISA5 = 0;
    LATAbits.LATA5 = 0;
}
/*
 *
 */
void Response()
{
    char Send[10];
    Send[0] = 0xFF;
    Send[1] = 0xFF;
    Send[2] = 0xFF;
    Send[3] = 0xAA;
    Send[4] = 0xAA;
    Send[5] = 0xAA;
    Send[6] = 0xAA;
    Send[7] = 0xFF;
    Send[8] = 0xFF;
    Send[9] = 0xFF;
    for (Byte = 0 ; Byte < 10; Byte++)
    {
        UART1_Write(Send[Byte]);
    }
}
/*
 *
 */
void UART1_TX_Init()
{
    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();
}
/*
 *
 */
void __interrupt(irq(U1RX)) INTERRUPT_InterruptManager2 (void)
{
    char Rcvd_Char;
    
    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++;
    }
    if (Byte_Received == 2)
    {
        Byte_Received = 0;
    }
}
/*
 *
 */
void UART1_Write(char txData)
{
    while(0 == PIR3bits.U1TXIF)
    {
    }
    U1TXB = txData; // Write the data byte to the USART.
}

The MPLABX simulator seems to output the bytes to UART1 in the correct order and no extra 0x00 bytes.

Seems like the OP has a hardware problem. In many cases that the MPLABX simulator seems so flaky this may be another case where it does not match reality. But what do I know?



Hi dan1138,
What might be the possible hardware issues?
 
Regards
Ali
 
#12
pcbbc
Super Member
  • Total Posts : 1011
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: Unexpected UART data in PIC18F26K83 2019/06/13 23:37:49 (permalink)
+1 (1)
The fact that you are still calling UART1_TX_Init(); in Response?

Note that you said you removed and corrected that in post #5, except that the exact same error appears in your (partial) “corrected” code you post in posts #7 and #9.

So either you are not posting your actual code, or that error still exists.

Note that dan1138 has corrected this for you in the code he posted.

I wouldn’t be at all surprised (I admit I haven’t personally tested it) if the UART hardware glitches when enabled if you write to the BRG or PPS registers, even if you re-write the exact same values that you wrote previously. That really isn’t something the hardware designers would have expected people to be doing. Also I wouldn’t be at all surprised if the emulator does not emulate the actual hardware to that level.

A tip: When you make changes to your code please re-post the entire code; don’t just state what you changed. Otherwise it makes it very difficult for us to keep track of what your new code looks like, and impossible to verify you made the changes correctly.
#13
M.Ali
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2018/11/12 00:36:07
  • Location: 0
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/14 00:27:28 (permalink)
0
pcbbc
The fact that you are still calling UART1_TX_Init(); in Response?

Note that you said you removed and corrected that in post #5, except that the exact same error appears in your (partial) “corrected” code you post in posts #7 and #9.

So either you are not posting your actual code, or that error still exists.

Note that dan1138 has corrected this for you in the code he posted.

I wouldn’t be at all surprised (I admit I haven’t personally tested it) if the UART hardware glitches when enabled if you write to the BRG or PPS registers, even if you re-write the exact same values that you wrote previously. That really isn’t something the hardware designers would have expected people to be doing. Also I wouldn’t be at all surprised if the emulator does not emulate the actual hardware to that level.

A tip: When you make changes to your code please re-post the entire code; don’t just state what you changed. Otherwise it makes it very difficult for us to keep track of what your new code looks like, and impossible to verify you made the changes correctly.



 
Hi pcbbc,
 
I am using the code posted by dan1138 and modified it to receive just once . But also I am getting the same error. 
 
Here is the code :

/*
* File: main.c
* Target: PIC18F26K83
* Compiler: XC8 v2.05
* IDE: MPLABX v5.15
*/
// 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 //
/*
*
*/
#include <xc.h>
/*
*
*/
#define _XTAL_FREQ 64000000
/*
*
*/
void PPS_Config(void);
void UART1_TX_Init(void);
void UART1_RX_Init(void);
void UART1_Write(char txData);
void WriteData(char data);
void Relay_Connect(void);
void OSCILLATOR_Initialize(void);
void Response(void);
static int Byte = 0;
char Recvd_Data[2];
static char Rcvd_Flag;
/*
*
*/
void main(void)
{
OSCILLATOR_Initialize();
UART1_RX_Init();
UART1_TX_Init();
INTCON0bits.IPEN = 0;
INTCON0bits.GIE = 1;
__delay_ms(500);
while(1)
{
__delay_ms(10);
if(Rcvd_Flag == 1)
{
if ((Recvd_Data[0] == 0x00) && (Recvd_Data[1] == 0x01))
{
Relay_Connect();
Response();
}
}
}
return;
}
/*
*
*/
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 UART1_RX_Init()
{
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 PPS_Config()
{
INTCON0bits.GIE = 0;
PPSLOCK = 0x55;
PPSLOCK = 0xAA;
PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS
/* PPS Configuration code*/
U1RXPPS = 0x17; //RC7->UART1:RX1
RC5PPS = 0x13; //RC5->UART1:TX
PPSLOCK = 0x55;
PPSLOCK = 0xAA;
PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS
INTCON0bits.GIE = 1;
}
/*
*
*/
void Relay_Connect()
{
TRISAbits.TRISA5 = 0;
LATAbits.LATA5 = 0;
}
/*
*
*/
void Response()
{
char Send[10];
Send[0] = 0xFF;
Send[1] = 0xFF;
Send[2] = 0xFF;
Send[3] = 0xAA;
Send[4] = 0xAA;
Send[5] = 0xAA;
Send[6] = 0xAA;
Send[7] = 0xFF;
Send[8] = 0xFF;
Send[9] = 0xFF;
for (Byte = 0 ; Byte < 10; Byte++)
{
UART1_Write(Send[Byte]);
}

Rcvd_Flag = 0;
}
/*
*
*/
void UART1_TX_Init()
{
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();
}
/*
*
*/
void __interrupt(irq(U1RX)) INTERRUPT_InterruptManager2 (void)
{
char Rcvd_Char;

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++;
Rcvd_Flag = 1;
}
if (Byte_Received == 2)
{
Byte_Received = 0;
}
}
/*
*
*/
void UART1_Write(char txData)
{
while(0 == PIR3bits.U1TXIF)
{
}
U1TXB = txData; // Write the data byte to the USART.
}

 
And also if I modify the code to transmit only first byte I get 00 instead of FF.
Here is the code :

for (Byte = 0 ; Byte < 1; Byte++)
{
UART1_Write(Send[Byte]);
}

 
Regards
Ali
#14
pcbbc
Super Member
  • Total Posts : 1011
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: Unexpected UART data in PIC18F26K83 2019/06/14 01:48:25 (permalink)
0
Sorry, don't know then.  Is Relay_Connect actually engaging a physical relay?  How is the relay connected/powered?
Try not energising the relay.  Perhaps it is causing a transient on your power supply and glitching the PIC or the serial coms?
#15
M.Ali
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2018/11/12 00:36:07
  • Location: 0
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/14 03:07:25 (permalink)
0
pcbbc
Sorry, don't know then.  Is Relay_Connect actually engaging a physical relay?  How is the relay connected/powered?
Try not energising the relay.  Perhaps it is causing a transient on your power supply and glitching the PIC or the serial coms?


Hi pcbbc,
 
I just commented the Relay_Connect(). But also I am getting 00.
 
Regards
Ali
#16
ric
Super Member
  • Total Posts : 22361
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/14 03:26:03 (permalink)
+2 (2)
That's not very good data for testing purposes.
Just as a test, could you try making each byte different?
e.g. change
FF FF FF AA AA AA AA FF FF FF
to
11 22 33 44 55 66 77 88 99 AA
and report what you see in Hyperterminal.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#17
M.Ali
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2018/11/12 00:36:07
  • Location: 0
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/14 03:30:38 (permalink)
0
ric
That's not very good data for testing purposes.
Just as a test, could you try making each byte different?
e.g. change
FF FF FF AA AA AA AA FF FF FF
to
11 22 33 44 55 66 77 88 99 AA
and report what you see in Hyperterminal.




Hi ric,
I can see 
44 A4 26 54 95 D6 17 2C A6 FD 00
 
Regards
Ali
post edited by M.Ali - 2019/06/14 03:32:30
#18
pcbbc
Super Member
  • Total Posts : 1011
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: Unexpected UART data in PIC18F26K83 2019/06/14 03:37:05 (permalink)
+1 (1)
How soon after PIC power on are you sending/receiving data?
 
You are still fiddling with the UART configuration while it is enabled.  Because both routines...
void UART1_RX_Init()
{
 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;
}

...and...
void UART1_TX_Init()
{
 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();
}

...set  U1CON1bits.ON = 1
 
Don't do that.
1. Configure the PPS.
2. Configure the UART BAUD and MODE once for both TX and RX.
3. Then enable ON, followed by TXEN, RXEN and RXIE etc as the last thing you do.
 
DatasheetNote 1:Changing the UART MODE while ON = 1 may cause unexpected results

I agree you aren't "changing" mode, but you are writing to it.  Who know what effect that may have.
 
Also, why the write to MODE0, MODE1 and MODE2?  These are individual bits of MODE that you have already written, which is pointless.
 
Otherwise I'm out of ideas.  Sorry.
 
Edit: Just seen your received data: 44 A4 26 54 95 D6 17 2C A6 FD 00
Most likely your clock rate (and/or other serial parameters) are wrong at either sender or receiver.
post edited by pcbbc - 2019/06/14 03:39:06
#19
1and0
Access is Denied
  • Total Posts : 9252
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Unexpected UART data in PIC18F26K83 2019/06/14 03:39:10 (permalink) ☼ Best Answerby M.Ali 2019/06/14 03:58:08
+1 (1)
I see you have RX1 on RC7 pin and TX1 to RC5 pin. Your code clears ANSELC7 and TRISC7 most likely default to input, but there are no settings for ANSELC5 and TRISC5 which should be output for TX1.
 
Edit: From the datasheet, "When the TRIS control for the pin corresponding to the TX output is cleared, then the UART will maintain control and the logic level on the TX pin."
post edited by 1and0 - 2019/06/14 03:43:05
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2019 APG vNext Commercial Version 4.5