• AVR Freaks

AnsweredHot!Serial Communication Example / UART Meaningless Data

Page: < 123 > Showing page 2 of 3
Author
du00000001
Just Some Member
  • Total Posts : 3985
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/21 06:20:13 (permalink)
+2 (2)
Oh  sad: sad
 
Now search for the magic word "error" within all these messages and get rid of these.
Start with "void char alici(char i[])" which you were already told should be "char alici(void)".
 
For the second time today:
Consider re-taking the C(+/+) 1o1 course - this time really attending!

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#21
Nacikocan
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2020/04/09 04:10:32
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/21 06:53:36 (permalink)
0
Normally. When the function  generated in C, As far as  i concerned, functiondatatype  function name( variable data type, variable name so for example int main(void) and  void receiver(char i) which is a example for c function.
#22
Nacikocan
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2020/04/09 04:10:32
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/21 07:00:24 (permalink)
0
OHH SORRY I be a conffuse my mind. In fact we say same thing void  char is a fault but firstly When i was writing a  char alici (char i)  which gives a fault in ide.
#23
du00000001
Just Some Member
  • Total Posts : 3985
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/21 07:06:45 (permalink)
+1 (1)
Nacikocan
functiondatatype functionname (variabledatatype variablename)

 
Better modify to
returndatatype functionname (parameterdatatype parametername)
 
Do you get the difference?
 
Time to start changing what you were told to change in previous posts.
That is: unless you can prove that you're able to get your misinterpretation compiled/linked.
 
P.S.: for embedded software, you'd better use "void main(void)". The other one is for executables running within a "non-embedded" OS like Win, Unix etc.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#24
du00000001
Just Some Member
  • Total Posts : 3985
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/21 07:09:41 (permalink)
+1 (1)
Nacikocan
OHH SORRY I be a conffuse my mind. In fact we say same thing void  char is a fault but firstly When i was writing a  char alici (char i)  which gives a fault in ide.



Oh yeah - this one gave a fault. But the "fix" added one more fault instead of curing the previous one.
Should have been "char alici(void)".

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#25
dan1138
Super Member
  • Total Posts : 3933
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/21 09:54:23 (permalink)
+1 (1)
@Nacikocan,
 
This is your code as posted:
#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();
    }
}

These are the problems I can see:
/*
    1   Function: void char alici(char i[]) is wrong.
        Should be: char alici( void )

    2   Low-voltage programming is enabled.
        This requires that RB4 is pulled down when not programming.
        Also RB4 cannot be used for a GPIO pin.

    3   UART interrupt enabled but the Interrupt Vector
        is not assigned an Interrupt Service Routine.
        Disable the interrupt system, use statement: INTCON=0;

    4   Comparator module is enabled by default in the PIC16F628A.
        For PORTA to have digital GPIO pins use statement: CMCON=0x07;
*/

This is what I think your code should be:
/*
 * File:   main.c
 * Target: PIC16F628A
 * Author: Nacikocan
 *
 * Created on May 21, 2020, 9:09 AM
 *
 * Description:
 *
 *                    PIC16F628A
 *             +----------:_:----------+
 *        <> 1 : RA2               RA1 : 18 <>
 *        <> 2 : RA3               RA0 : 17 <>
 *        <> 3 : RA4          OSC1/RA7 : 16 <>
 *    VPP -> 4 : RA5/VPP      OSC2/RA6 : 15 <>
 *    GND -> 5 : VSS               VDD : 14 <- 5v0
 *        <> 6 : RB0/INT       PGD/RB7 : 13 <> PGD
 * DBG_RX <> 7 : RB1/RX/DT     PGC/RB6 : 12 <> PGC
 * DBG_TX <> 8 : RB2/RX/CK         RB5 : 11 <>
 *        <> 9 : RB3/CCP       PGM/RB4 : 10 <>
 *             +-----------------------:
 *                      DIP-18
 */

// CONFIG
#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 = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#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)

#include <xc.h>

char alici( void ){
    while(!PIR1bits.RCIF);
    return RCREG;
}
void main() {
    CMCON  = 0x07;
    TRISA  = 0B11110000;
    TXSTA  = 0B00100000;    // TXEN 1, GER?S? 0
    RCSTA  = 0B10010000;    // SPEN 1, CREN 1 GER?S? 0
    SPBRG  = 51;
    INTCON = 0;
    RCREG;
    RCREG;
    while(1){
        PORTA = alici();
    }
}

post edited by dan1138 - 2020/05/21 15:26:46
#26
sevincom
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2020/05/12 16:13:18
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/21 14:17:08 (permalink)
0
ric
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 do not know, almost all example videos on YT was using the config settings this. But I followed your suggestion and created a more clean code.
I am still not quite confusing about library usage since mplab is responding late and it is difficult for me to figure out if it is a bug or do I still missing some libraries.
Anyway, maybe I can ask it on another topic since this one already started to be crowded.
 
Thanks everyone again for your helps!
post edited by sevincom - 2020/05/21 14:21:32
#27
ric
Super Member
  • Total Posts : 28736
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Serial Communication Example / UART Meaningless Data 2020/05/21 15:13:32 (permalink)
+3 (3)
sevincom
I do not know, almost all example videos on YT was using the config settings this. But I followed your suggestion and created a more clean code.

There are  al ot of poor programmers making Youtube video.
Just becuase something is online doesn't mean it is good/correct practice.
 

I am still not quite confusing about library usage since mplab is responding late and it is difficult for me to figure out if it is a bug or do I still missing some libraries.

Pay more attention to what messages you see in the compile log, rather than what the editor in the IDE reports.
 

Anyway, maybe I can ask it on another topic since this one already started to be crowded.

Please don't. That means you lose the context, and we have to ask the same questions over again.
 

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!
#28
sevincom
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2020/05/12 16:13:18
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/21 16:28:20 (permalink)
0
Hello again ric,
 
I was trying to say that everything about this topic is understood from my side but there are other elementary stuff I might have questions in the future.
Only a couple of points which I have to add because I do not agree with you.
 
There are a lot of poor programmers making YT video, just like there are a lot of poor topics in this forum.
 
If I keep asking everything that is not actually related with the topic "Serial Communication", then this topic also will be one of them.
My problem is solved and of course I will come back here if I face another "Serial Communication" problem which I can not solve on my own(which is a low probability Smile: Smile , or not, we will see )
#29
Nacikocan
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2020/04/09 04:10:32
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/22 15:16:41 (permalink)
0
By the way I made a uart communication among two microcontrollers. Thats a no problem for me, if you wish, i can send the code
#30
David
Pic User
  • Total Posts : 1312
  • Reward points : 0
  • Joined: 2007/12/17 23:19:53
  • Location: uk sussex
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/22 17:33:09 (permalink)
+1 (1)
sevincom
Hello again ric,
 
I was trying to say that everything about this topic is understood from my side but there are other elementary stuff I might have questions in the future.
Only a couple of points which I have to add because I do not agree with you.
 
There are a lot of poor programmers making YT video, just like there are a lot of poor topics in this forum.
 
If I keep asking everything that is not actually related with the topic "Serial Communication", then this topic also will be one of them.
My problem is solved and of course I will come back here if I face another "Serial Communication" problem which I can not solve on my own(which is a low probability Smile: Smile , or not, we will see )




I think there is a "thank you" in there somewhere LOL :)

David
I support http://picforum.ric323.com because this forum is often too broken to use!
#31
Nacikocan
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2020/04/09 04:10:32
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/23 08:25:02 (permalink)
0
@David Sorry brother, My purpose of a  dont generate a unncesessary post. When you asked serial communication uart's questions, i would ask my question. But eventually both of them problem is solved :D 
#32
sevincom
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2020/05/12 16:13:18
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 12:02:34 (permalink)
0
Hi,
 
I have implemented other features to my code and having different problems that I can not understand.
An analog read sequence is added to read temperature sensor LM35 and write on LCD.(Next step will be send it with UART but currently I am just writing to LCD)
I have succesfully run the analog read program alone, also the UART program as you know from my previous posts.
But when I combine these two, I am having problem. On the LCD screen, I can see I am sending the right command k0001. Normally when I see this on LCD, the code also works fine and turn on LED.
But in the below codes, it does not turn on LED. And I receive "N" response from PIC, which means it enters to else statement instead of if(0001).
 
Below you can see for full codes.
 
/*
 * File: serial_main.c
 * Author: PC4
 *
 * Created on May 21, 2020, 11:45 PM
 */
// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = ON // 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.

#define _XTAL_FREQ 4000000
#include <xc.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "config_lcd.h"
#include "config_uart.h"


void main(void) {
    TRISA=0XFF;
    TRISB=0X00;
    PORTB=0X00;
    TRISD=0b00000011;
    
    //CONVERSION CLOCK SECIMI: FOSC/16
    ADCON0bits.ADCS0=1;
    ADCON0bits.ADCS1=0;
    ADCON1bits.ADCS2=1;
    
    //ANALOG KANAL SECIMI: RA0-AN0
    ADCON0bits.CHS0=0;
    ADCON0bits.CHS1=0;
    ADCON0bits.CHS2=0;
    
    //SONUC FORMAT SECIMI: SAGA YASLA-10Bit
    ADCON1bits.ADFM=1;
    
    //PORT AYARLARI SECIMI: 0010-DDDAAAAA SECILDI.AN7-AN0
    //1100 ile DDDAVr+Vr-AA SECIMI YAPILABILIR
    ADCON1bits.PCFG3=0;
    ADCON1bits.PCFG2=0;
    ADCON1bits.PCFG1=1;
    ADCON1bits.PCFG0=0;
    
    //SET STATUS BIT
    ADCON0bits.GO_DONE=1;
    
    //ADC BASLAT
    ADCON0bits.ADON=1;
    
    const float VoltOran = 5.0/1023.0;
    float Volt, Temperature;
    
    unsigned char sayici;
    unsigned char adres=0x00;
    sayici=eeprom_read(adres);
    Lcd_Init();
    Lcd_Clear();
    Lcd_Set_Cursor(1,5);
    Lcd_Write_String("ACILIS"); // Initialize String LCD
    __delay_ms(200);
    Lcd_Clear();

    char data[10];
    char tetik[1];
    char s[4];
    
    UART_Init(9600);
    OPTION_REGbits.PSA=1; // 1 sec WDT settings
    OPTION_REGbits.PS0=0;
    OPTION_REGbits.PS1=1;
    OPTION_REGbits.PS2=1;
    CLRWDT();
    while(1){
        __delay_ms(10);
        if(ADCON0bits.GO_DONE==0){
            Volt=((ADRESH*256+ADRESL)*VoltOran);
            Temperature=(Volt*100);
            sprintf(s, "%.1f", Temperature);
            Lcd_Set_Cursor(2,1);
            Lcd_Write_String(s); // Temperature written on LCD
            ADCON0bits.GO_DONE=1;
        }
        if(UART_Data_Ready()){
            UART_Read_Text(tetik,1); // wait for the key
            Lcd_Set_Cursor(1,1);
            Lcd_Write_String(tetik);
            if(strcmp(tetik,"k")==0){ // if correct char arrived, read rest
                UART_Read_Text(data,4);
                Lcd_Write_String(data);
                if(strcmp(data,"0001")==0){ // turn on LED at 0001
                    PORTB=0b00000001;
                    UART_Write_Text("o"); // inform PC about success
                    __delay_ms(100);
                }
                else if(strcmp(data,"000A")==0){ // turn off LED at 000A
                    PORTB=0b00000000;
                    UART_Write_Text("o"); // inform PC about success
                    __delay_ms(100);
                }
                else{
                   UART_Write_Text("N"); // inform PC about wrong serial cmd
                   __delay_ms(100);
                }
                
            }
        }
        CLRWDT();
    }
}

 
/* 
 * File: lcd_config.h
 * Author: PC4
 *
 * Created on May 16, 2020, 7:31 PM
 */

/*
 * 2x16 LCD Library.
 * LCD Functions:
 Lcd_Clear();
 Lcd_Set_Cursor(int row, int column);
 Lcd_Write_Char(char);
 Lcd_Write_String(char *string);
 Lcd_Shift_Right();
 Lcd_Shift_Left();
 */

#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7

void Lcd_Port(char a)
{
 if(a & 1) D4 = 1;
 else D4 = 0;

 if(a & 2) D5 = 1;
    else D5 = 0;

 if(a & 4) D6 = 1;
 else D6 = 0;

 if(a & 8) D7 = 1;
 else D7 = 0;
}

void Lcd_Cmd(char a)
{
 RS = 0;
 Lcd_Port(a);
 EN = 1;
    __delay_ms(4);
    EN = 0;
}

void Lcd_Clear(void)
{
 Lcd_Cmd(0);
 Lcd_Cmd(1);
}

void Lcd_Set_Cursor(char a, char b)
{
 char temp, z, y;
 if(a == 1)
 {
        temp = 0x80 + b - 1;
  z = temp >> 4;
  y = temp & 0x0F;
  Lcd_Cmd(z);
  Lcd_Cmd(y);
 }
 else if(a == 2)
 {
  temp = 0xC0 + b - 1;
  z = temp >> 4;
  y = temp & 0x0F;
  Lcd_Cmd(z);
  Lcd_Cmd(y);
 }
    else if(a == 3)
 {
  temp = 0x94 + b - 1;
  z = temp >> 4;
  y = temp & 0x0F;
  Lcd_Cmd(z);
  Lcd_Cmd(y);
 }
    else if(a == 4)
 {
  temp = 0xD4 + b - 1;
  z = temp >> 4;
  y = temp & 0x0F;
  Lcd_Cmd(z);
  Lcd_Cmd(y);
 }
}

void Lcd_Init(void)
{
    Lcd_Port(0x00);
    __delay_ms(20);
    Lcd_Cmd(0x03);
    __delay_ms(5);
    Lcd_Cmd(0x03);
    __delay_ms(11);
    Lcd_Cmd(0x03);
    /////////////////////////////////////////////////////
    Lcd_Cmd(0x02);
    Lcd_Cmd(0x02);
    Lcd_Cmd(0x08);
    Lcd_Cmd(0x00);
    Lcd_Cmd(0x0C);
    Lcd_Cmd(0x00);
    Lcd_Cmd(0x06);
}

void Lcd_Write_Char(char a)
{
   char temp, y;
   temp = a & 0x0F;
   y = a & 0xF0;
   RS = 1; // => RS = 1
   Lcd_Port(y >> 4); // Data transfer
   EN = 1;
   __delay_us(40);
   EN = 0;
   Lcd_Port(temp);
   EN = 1;
   __delay_us(40);
   EN = 0;
}

void Lcd_Write_String(char *a)
{
 int i;
 for(i=0; a[i] != '\0'; i++)
    {
        Lcd_Write_Char(a[i]);
    }
}

void Lcd_Shift_Right(void)
{
 Lcd_Cmd(0x01);
 Lcd_Cmd(0x0C);
}

void Lcd_Shift_Left(void)
{
 Lcd_Cmd(0x01);
 Lcd_Cmd(0x08);
}

void Lcd_Cursor_On(void)
{
 Lcd_Cmd(0x00);
 Lcd_Cmd(0x0E);
}

void Lcd_Cursor_Off(void)
{
 Lcd_Cmd(0x00);
 Lcd_Cmd(0x0C);
}

void Lcd_Blink_On(void)
{
 Lcd_Cmd(0x00);
 Lcd_Cmd(0x0F);
}

void Lcd_Blink_Off(void)
{
 Lcd_Cmd(0x00);
 Lcd_Cmd(0x0E);
}

 
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; //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();
}

#33
ric
Super Member
  • Total Posts : 28736
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 13:27:45 (permalink)
+1 (1)
Your UART_Read_Text() function is not placing the REQUIRED NULL character at the end of the string.
That also means all your arrays used to hold strings  need to be one byte larger.
 
Smalll side comment. Not your problem, but your output functions should use TXIF not TRMT to test if the TXBUF is empty.
 
 
 

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!
#34
du00000001
Just Some Member
  • Total Posts : 3985
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 13:36:11 (permalink) ☄ Helpfulby sevincom 2020/05/25 14:56:45
+1 (3)
@ sevincom
 
Use strncmp instead of strcmp (requiring 1 more parameter) and retry - might work then.
 
Honestly: "Experienced Hobbyist"?   Or rather "EE student" ?

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#35
sevincom
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2020/05/12 16:13:18
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 14:56:33 (permalink)
0
du00000001
@ sevincom
 
Use strncmp instead of strcmp (requiring 1 more parameter) and retry - might work then.

This advise solve the problem. Could you help me understand why it is working when I only run the code with serial communication part, and not working when I add analog read part? I try to understand, but they are using different array data/s and could not understand why interacting with each other.
du00000001
Honestly: "Experienced Hobbyist"?   Or rather "EE student" ?

If there is a "PIC rookie candidate" category, I would choose that, but since I am definitely not inside any of the other categories including EE student, I chose Experienced Hobbyist.
 
ric
Your UART_Read_Text() function is not placing the REQUIRED NULL character at the end of the string.
That also means all your arrays used to hold strings  need to be one byte larger.
 
Smalll side comment. Not your problem, but your output functions should use TXIF not TRMT to test if the TXBUF is empty.

If I understand correct, I should add (n+1)th element to output, as "NULL". But from the PC side, I only send the indicated string, i.e "k0001". So, I did not understand why it is needed or how to implement actually. Although the problem is solved, I would be glad to learn this if you have time to explain. I read the USART section in PIC16F87XA datasheet, but could not understand well.
 
#36
ric
Super Member
  • Total Posts : 28736
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 15:06:51 (permalink)
+1 (1)
sevincom
If I understand correct, I should add (n+1)th element to output, as "NULL".

Yes.
Safer to pass the real array length, and accept n-1 characters
void UART_Read_Text(char *Output, unsigned int length)
{
  unsigned int i;
  for(int i=0;i<(length-1);i++)
    Output[i] = UART_Read();
  Output[length-1] = 0;
}

Do you ever expect to receive more than 254 characters? If not, use "unsigned char" for your counter. Don't use "int" when "char" will do.
That code would be more efficient like this:
void UART_Read_Text(char *Output, unsigned char length)
{
  --length;  //pre decrement length to allow for terminating NULL
  do
  {
    *Output++ = UART_Read();  //save character and bump pointer
  } while (--length);  //loop until we have enough characters
  *Output = 0;  //add terminating NULL
}

Or, pass the number of chars desired (which must be at least one less than the size of your array), and remove the "pre decrement".
 

But from the PC side, I only send the indicated string, i.e "k0001".

Which is bad design. That means the receiver has to know exactly how many characters to expect, as you don't have an "end of string" indicator.

So, I did not understand why it is needed or how to implement actually.

Because strings in C must be terminated with a NULL.
That is how the code knows where the end of the string is. There is no "length" of string magically stored anywhere.

Although the problem is solved, I would be glad to learn this if you have time to explain. I read the USART section in PIC16F87XA datasheet, but could not understand well.

This has nothing to do with the USART, and everything to do with how C functions handle strings.
i.e.. the strcmp() functions scans through memory until it finds the NULL to know where the end of the string is. Without that, it looks through whatever is following your array in memory.
This is a FUNDAMENTAL feature of string handling in C that you must come to grips with.
 
post edited by ric - 2020/05/25 15:40:46

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!
#37
sevincom
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2020/05/12 16:13:18
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 16:17:54 (permalink)
0
Hello ric,
 
I thought I understand your suggestion but now see your edit and confused.
Because I understand that I should look for a termination character instead of knowing exact data length. But in your code, you also read the defined length characters and add the termination character manually. Am I wrong, since it does not come from the PC?
 
I was just testing something like below, do you think this kind of implementation can be done? My aim is to define a maximum length of read, but actually break the read text if it receives end character.
Since I am not familiar with syntax/data type, I am not sure I should directly write NULL, 0 or something else, so I tried to use "F" as custom termination character.
But the break function did not work. I set length to 6, but sending 5 character(0001F) from PC including termination "F", but function still waits for the 6th character and timeouts.
 
 
void UART_Read_Text(char *Output, unsigned char length)
{
  unsigned int i;
  for(int i=0;i<length;i++){
  Output[i] = UART_Read();
  if(strncmp(Output,"F",1)==0){
      break;
  }
  }
}

UART_Read_Text(data,6);
                Lcd_Write_String(data);
                if(strncmp(data,"0001F",5)==0){ // turn on LED at 0001
                    PORTB=0b00000001;
                    UART_Write_Text("TROK"); // inform PC about success
                    __delay_ms(100);
                }

post edited by sevincom - 2020/05/25 16:22:31
#38
ric
Super Member
  • Total Posts : 28736
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 16:38:47 (permalink) ☄ Helpfulby sevincom 2020/05/25 17:43:23
+1 (1)
sevincom
Hello ric,
 
I thought I understand your suggestion but now see your edit and confused.
Because I understand that I should look for a termination character instead of knowing exact data length. But in your code, you also read the defined length characters and add the termination character manually. Am I wrong, since it does not come from the PC?

I was showing how to fix the code using your existing scheme of fixed string lengths.
Yes, I think sending a terminating character from the PC would be even better.
However, to use standard C string handling functions, the terminating character in your array must be a NULL, which is  character zero.
 

Since I am not familiar with syntax/data type, I am not sure I should directly write NULL, 0 or something else, so I tried to use "F" as custom termination character.

You can use whatever you want in the USART interface, so long as that character (e.g. "F") will never occur in the data you want to send.
CR (hex value 0x0D) is more common, and can be represented inside a string as "\r"
As mentioned above, the character you save in the array must be a NULL
 
void UART_Read_Text(char *Output, unsigned char length)
{
  unsigned int i;
  for(int i=0;i<length;i++){
  Output[i] = UART_Read();
  if(strncmp(Output,"F",1)==0){
      break;
  }
  }
}

using strncmp() to text a single character is way overkill.
  if(strncmp(Output,"F",1)==0){

could be just
  if (Output[i] == 'F')

Not the single quotes for handling characters rather than strings.
Note. Your code did not work at all because you were always testing the first character in your array, not the one just received.
My version does it correctly. Your code (with for() and lots of array indexing) is still much less efficient than what I showed i post#37. I guess that is because you don't understand pointers in C yet.
 
Note, in this code:
UART_Read_Text(data,6);
                Lcd_Write_String(data);
                if(strncmp(data,"0001F",5)==0){ // turn on LED at 0001
                    PORTB=0b00000001;
                    UART_Write_Text("TROK"); // inform PC about success
                    __delay_ms(100);
                }


The Lcd_Write_String() function requires a NULL terminated string, so the UART_Read_Text() function must add it. (Yours still does not)
If you make it a NULL terminated string, then you don't have to use strncmp(), you can just use the standard strcmp()
 
post edited by ric - 2020/05/25 16:49:17

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!
#39
ric
Super Member
  • Total Posts : 28736
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 16:50:43 (permalink)
+1 (1)
Would I be right in guessing that you have learnt to program with Visual Basic, and are now trying to write in C?
 

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!
#40
Page: < 123 > Showing page 2 of 3
Jump to:
© 2020 APG vNext Commercial Version 4.5