• AVR Freaks

AnsweredHot!Serial Communication Example / UART Meaningless Data

Page: 123 > Showing page 1 of 3
Author
sevincom
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2020/05/12 16:13:18
  • Location: 0
  • Status: offline
2020/05/19 14:34:09 (permalink)
0

Serial Communication Example / UART Meaningless Data

Hello,
 
I am trying to communicate PIC16F877A with 3rd party device using serial communication. I use MPLAB IDE v5.35 and XC8 compiler. I have found uart library on internet.
 
For the simplicity of test, I choose hercules serial port software. Hercules on the PC is reading the data written by PIC on com port.
 
The problem is, read data at hercules is not the same I am sending from PIC. For example, PIC is sending strings like OK, 0, 52, NUL etc. while Hercules is reading these like %, ae, -, etc. I thought this is some kind of ASCII/HEX format difference and I found ASCII table internet but it does not still corresponding to the characters on the ASCII table.
 
What is more, the character is not keeping constant. For example I always send NUL string from PIC, while hercules is reading it VE, TE, UE .. So I think the problem is not only about data conversion, because same command is sending different characters from time to time.
 
Below the code. And since I am not able to give links you can easily find ascii conversion table and hercules serial software on google.
Could you help me understand, what mistake in my code blocks me to have a good communication? I want to understand the basic code requirements.
Thanks.
 
 
#include <pic16f877a.h>
#include "seri_config.h"
#include <stdio.h>
#include <stdlib.h>
#include "lcd_config.h"
#include <string.h>
#include "uart.h"

void main(void) {
    TRISA=0XFF;
    TRISB=0X00;
    PORTB=0X00;
    TRISD=0b00000011;

    UART_Init(9600);
    while(1){
        if(PORTDbits.RD0==1){
        UART_Write_Text("NUL");
        PORTB=0b00000001;
        __delay_ms(500);
        PORTB=0b00000000;
        __delay_ms(500);
        }
    }
}

 
char UART_Init(const long int baudrate)
{
  unsigned int x;
  x = (_XTAL_FREQ - baudrate*64)/(baudrate*64); //SPBRG for Low Baud Rate
  if(x>255) //If High Baud Rage Required
  {
    x = (_XTAL_FREQ - baudrate*16)/(baudrate*16); //SPBRG for High Baud Rate
    BRGH = 1; //Setting High Baud Rate
  }
  if(x<256)
  {
    SPBRG = x; //Writing SPBRG Register
    SYNC = 0; //Setting Asynchronous Mode, ie UART
    SPEN = 1; //Enables Serial Port
    TRISC7 = 1; //As Prescribed in Datasheet
    TRISC6 = 1; //As Prescribed in Datasheet
    CREN = 1; //Enables Continuous Reception
    TXEN = 1; //Enables Transmission
    return 1; //Returns 1 to indicate Successful Completion
  }
  return 0; //Returns 0 to indicate UART initialization failed
}
void UART_Write(char data)
{
  while(!TRMT);
  TXREG = data;
}
char UART_TX_Empty()
{
  return TRMT;
}
void UART_Write_Text(char *text)
{
  int i;
  for(i=0;text[i]!='\0';i++)
    UART_Write(text[i]);
}
char UART_Data_Ready()
{
  return RCIF;
}
char UART_Read()
{
  while(!RCIF);
  return RCREG;
}
void UART_Read_Text(char *Output, unsigned int length)
{
  unsigned int i;
  for(int i=0;i<length;i++)
  Output[i] = UART_Read();
}

#1
Jim Nickerson
User 452
  • Total Posts : 6655
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 07:01:28 (permalink)
0
What version of "hercules" ?
Maybe it does not support Windows 10.
 
#2
du00000001
Just Some Member
  • Total Posts : 3675
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 07:28:25 (permalink) ☼ Best Answerby sevincom 2020/05/20 08:31:34
+2 (2)
sevincom
... I thought this is some kind of ASCII/HEX format difference ...

 
I thought this is some kind of baud rate difference - the most common error.
(Are you sure this should be called "baud rage" ??)
But might as well be some 7-Bit vs. 8-Bit, parity or whatever else error.
Might as well be worth to check the value of X (that you're computing), as these calculations are more than ominous.
 
Suggest to
uint_16_t x = ((((uint32_t)_XTAL_FREQ) / 64) / baudrate) - 1;    //  I really love brackets !

Will require to #include <stdint.h> - resulting in more clarity about the number of bits available for calculations.
 
So I'd suggest to take a scope and check the output of your PIC in terms of bit timing, frame timing etc.
And cross-check with the serial settings of hercules (what a name - implying what it might not be able to fulfill).
post edited by du00000001 - 2020/05/20 07:36:15

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#3
sevincom
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2020/05/12 16:13:18
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 08:34:13 (permalink)
0
Hello Jim, hercules version is 3.2.8, supporting Windows 10. And I have tested it on Windows 7 too.
 
du00000001
I thought this is some kind of baud rate difference - the most common error.

 
Yes, you are definitely right! Thanks.
After checking the datasheet I figured out that it is better to use High BRGH parameter for healthier communication with my freq. 4MHz.
 
For future reference who wants an example of UART serial communication, below is the change that I made. I have just deleted the low BRGH portion, because it is not needed for my case.
 
 

char UART_Init(const long int baudrate)
{
unsigned int x;
x = (_XTAL_FREQ - baudrate*16)/(baudrate*16); //SPBRG for High Baud Rate
BRGH = 1; //Setting High Baud Rate
if(x<256)
{
SPBRG = x;
...

 
#4
crosland
Super Member
  • Total Posts : 1939
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Warks, UK
  • Status: online
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 08:59:37 (permalink)
+1 (1)
And where is _XTAL_FREQ defined?
 
ALWAYS show ALL of your code.
#5
dan1138
Super Member
  • Total Posts : 3496
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 09:35:51 (permalink)
+4 (4)
For years I have seen posts from new members that are fragmented, badly formatted with obvious bugs.
 
I have yet to see a first time post that contains all that is required to build the example the OP wants help with.
 
It would seem that people seeking help just assume that their problem must be located in the code they are looking at right now.
 
It would be like looking for your lost keys in the kitchen because the lighting is better, rather than the couch cushions where you were sitting last night. It just proves things are in the last place you would think to look.
#6
ric
Super Member
  • Total Posts : 27022
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 13:45:45 (permalink)
+1 (1)
and just a side note. If you're using XC8, this

#include <pic16f877a.h>
should be
#include <xc.h>
 
There's a good chance you don't need stdlib.h or stdio.h either.
 
 

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!
#7
sevincom
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2020/05/12 16:13:18
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 15:34:32 (permalink)
0
Hi ric,
 
Actually I have this xc.h library inside my seri_config.h as below.
I did not really understand about this libraries, they were confusing. MPLAB was creating some problems on recognizing the functions so I add some extra stuff just in case, although I am not sure if this can create different problems.
 
 

// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ 4000000

 
#8
ric
Super Member
  • Total Posts : 27022
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 15:44:34 (permalink)
+1 (1)
What are your config settings doing in seri_config.h ?
They should be in a C file (not an H file)
All your C files should include xc.h, not rely on some other header file to do it for them.
You should NOT include the device specific header file (pic16f877a.h), as xc.h will be including it also.
 
 

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!
#9
Nacikocan
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2020/04/09 04:10:32
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 15:50:29 (permalink)
0
Hi everyone; I have a same problem which is a receiver. I am doing a  uart serial communication among two microcontrollers in mplab x ide,xc8 compiler. The ide is not a problem in transmitter mcu code but when receiver code was compiling, it results fault. By the way the mcu is a pic16f628. The code is a below.
 

#include <xc.h>
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = ON // Low-Voltage Programming Enable bit (RB4/PGM pin has PGM function, low-voltage programming enabled)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

void char alici(char i[]){
while(!PIR1bits.RCIF);
PIR1bits.RCIF=0;
return RCREG;
}
void main() {
TRISA=0B11110000;
TXSTA=0B00100000; // TXEN 1, GER?S? 0
RCSTA=0B10010000; //SPEN 1, CREN 1 GER?S? 0
SPBRG=51;
INTCON=0B11000000; //KESMELER AKT?F GIE VE PEIE 1 ,
PIE1=0B00100000; //RCIE 1 GER?S? 0
PIR1bits.RCIF=0;
while(1){
PORTA=alici();
}
}
 
 
 
#10
ric
Super Member
  • Total Posts : 27022
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 16:07:12 (permalink)
+1 (1)
Nacikocan
Hi everyone; I have a same problem which is a receiver. I am doing a  uart serial communication among two microcontrollers in mplab x ide,xc8 compiler. The ide is not a problem in transmitter mcu code but when receiver code was compiling, it results fault. INTCON=0B11000000; //KESMELER AKT?F GIE VE PEIE 1 ,By the way the mcu is a pic16f628. The code is a below.
 

You have LVP turned on. That means you MUST have a pulldown resistor on the RB4/PGM pin.
Unless you really know you need it, you should turn LVP off.
 
This line is pointless
PIR1bits.RCIF=0;

The ONLY way you can clear RCIF is by reading RCREG, which you are already doing.
 
Change
void main() {

to
void main(void) {

 
Get rid of this line
INTCON=0B11000000; //KESMELER AKT?F GIE VE PEIE 1 ,

You do NOT want interrupts enabled, as you do not have an interrupt service routine.
 
What exactly do you mean by:

when receiver code was compiling, it results fault.

Is there a compiler error? What does it say?
 
 

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!
#11
Nacikocan
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2020/04/09 04:10:32
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 16:13:49 (permalink)
0
I have tried in proteus. I mean the compiler was compiling my code, it gives a fault. AS you say ı did but result is not change
#12
Nacikocan
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2020/04/09 04:10:32
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 16:14:58 (permalink)
0
 
By the way char alici function is not an interrupt its a polling method. 
#13
ric
Super Member
  • Total Posts : 27022
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 16:36:36 (permalink)
+1 (1)
Nacikocan
I have tried in proteus. I mean the compiler was compiling my code, it gives a fault. AS you say ı did but result is not change

You still have not made clear, what does "gives a fault" actually mean?
 

By the way char alici function is not an interrupt its a polling method.

I know.
It does NOT require the GIE or PEIE bits to be set, and you should NOT be setting them.
As I already stated, do not set GIE if you don't have an interrupt service routine.
You don't have one and you don't need one.
 

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!
#14
Nacikocan
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2020/04/09 04:10:32
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 16:45:59 (permalink)
0
Yes sorry, you're right this fault is below. If you wish, You copy the code in your mplabx ide, you are ganna see that fault.
make[2]: *** [build/default/production/alici.p1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUİLD FAILED (exit value 2, total time: 11s) 
#15
du00000001
Just Some Member
  • Total Posts : 3675
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 16:55:13 (permalink)
+1 (1)
@Nacikocan
I'm quite sure you only gave the last of the messages.
Set the cursor to the otput window, press CTRL+a, CTRL+c to copy everything in this window and provide it.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#16
ric
Super Member
  • Total Posts : 27022
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 17:03:43 (permalink)
+1 (1)
Nacikocan
Yes sorry, you're right this fault is below. If you wish, You copy the code in your mplabx ide, you are ganna see that fault.
...

Did you not see the real error reported just above that?

 
newmain.c:13:6: error: (269) inconsistent type
newmain.c:27:13: error: (187) too few function arguments
(908) exit status = 1

 
That is because of this line
void char alici(char i[]){

which looking at how you call it, should actually be
char alici(void){



 
Get rid of these lines. With them, your code will crash as soon as you receive a character. None of them are required.
    INTCON=0B11000000; //KESMELER AKT?F GIE VE PEIE 1 ,
    PIE1=0B00100000; //RCIE 1 GER?S? 0
    PIR1bits.RCIF=0;

post edited by ric - 2020/05/20 18:33:10

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
dan1138
Super Member
  • Total Posts : 3496
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 17:24:33 (permalink)
+1 (3)
Nacikocan
I have a same problem which is ...


NOT POSTING A COMPLETE PROGRAM THAT BUILDS WITHOUT COMPILER ERRORS OR WARNINGS!
#18
nigelwright7557
Super Member
  • Total Posts : 424
  • Reward points : 0
  • Joined: 2006/11/06 08:15:51
  • Location: 0
  • Status: online
Re: Serial Communication Example / UART Meaningless Data 2020/05/20 19:02:03 (permalink)
0
Simplest way to check baud rate is to output continuous string 1's and 0's and look on a scope at bit times.
 
#19
Nacikocan
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2020/04/09 04:10:32
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/21 05:43:36 (permalink)
0
I deleted your says lines. But result is same error is :
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/Naci Kocan/Desktop/yaz�l�m,proje/Embedded/uart(mplab mcu communication)/uart alici/uartrx.X'
make -f nbproject/Makefile-default.mk dist/default/production/uartrx.X.production.hex
make[2]: Entering directory 'C:/Users/Naci Kocan/Desktop/yaz�l�m,proje/Embedded/uart(mplab mcu communication)/uart alici/uartrx.X'
"C:\Program Files\Microchip\xc8\v2.20\bin\xc8-cc.exe" -mcpu=16F628A -c -mdfp="C:/Program Files (x86)/Microchip/MPLABX/v5.40/packs/Microchip/PIC16Fxxx_DFP/1.2.33/xc8" -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -std=c99 -gdwarf-3 -mstack=compiled:auto:auto -o build/default/production/alici.p1 alici.c
::: advisory: (2049) C99 compliant libraries are currently not available for baseline or mid-range devices, or for enhanced mid-range devices using a reentrant stack; using C90 libraries
alici.c:24:6: error: cannot combine with previous 'void' declaration specifier
void char alici(char i[]){
^
alici.c:37:16: error: too few arguments to function call, single argument 'i' was not specified
PORTA=alici();
~~~~~ ^
alici.c:24:1: note: 'alici' declared here
void char alici(char i[]){
^
2 errors generated.
(908) exit status = 1
nbproject/Makefile-default.mk:107: recipe for target 'build/default/production/alici.p1' failed
make[2]: Leaving directory 'C:/Users/Naci Kocan/Desktop/yaz�l�m,proje/Embedded/uart(mplab mcu communication)/uart alici/uartrx.X'
nbproject/Makefile-default.mk:91: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/Users/Naci Kocan/Desktop/yaz�l�m,proje/Embedded/uart(mplab mcu communication)/uart alici/uartrx.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make[2]: *** [build/default/production/alici.p1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUİLD FAILED (exit value 2, total time: 7s)
 
#20
Page: 123 > Showing page 1 of 3
Jump to:
© 2020 APG vNext Commercial Version 4.5