UART_Write_Text(text) overwrites characters on Lcd_Out(char)

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 8-bit PIC MCUs.
Post Reply
Author
Message
jin1990
Posts: 8
Joined: 12 Feb 2019 08:15

UART_Write_Text(text) overwrites characters on Lcd_Out(char)

#1 Post by jin1990 » 12 Feb 2019 08:57

I'm developing an embedded system that can identify soda cans with the use of some sensors and PIC16f877a.

This is my main function:

#define HX711_DT PORTC.F5
#define HX711_DT_TRIS TRISC.F5
#define HX711_SK PORTC.F4
#define HX711_SK_TRIS TRISC.F4
#define IR_Sensor1 PORTD.F0
#define IR_Sensor1_TRIS TRISD.F0
#define IR_Sensor2 PORTD.F1
#define IR_Sensor2_TRIS TRISD.F1
#define INDUCTIVE PORTD.F2
#define INDUCTIVE_TRIS TRISD.F2

void SENSORS_Init();
void HX711_Init() ;
void nFloatToStr(float f, short p, char *txt);
unsigned long HX711_Read(void); //HX711 Driver
void SETUP_WeightSensor(); //Calibrating Weight Sensor


//LCD module connections
sbit LCD_RS at RB2_bit; //LCD reset
sbit LCD_EN at RB3_bit; //LCD enable
sbit LCD_D4 at RB4_bit; //Data
sbit LCD_D5 at RB5_bit; //Data
sbit LCD_D6 at RB6_bit; //Data
sbit LCD_D7 at RB7_bit; //Data
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

//GLOBAL VARIABLES
long count = 0;
long offset = 0;
float mul = 0;
float weight = 0;
float reward = 0;
unsigned char Cweight[10];
unsigned char Creward[10];

void main() {
//CMCON = 0; //0x07; // To turn off comparators
//ADCON1 = 0; //0x06; // To turn off analog to digital converters

SENSORS_Init();
HX711_Init(); //Initializing Weight Sensor

Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);

UART1_Init(19200); //Initializing Thermal Printer
delay_ms(100);

SETUP_WeightSensor(); //Calibrating Weight Sensor


while(1)
{
if(INDUCTIVE == 0 && IR_Sensor2 == 0 && IR_Sensor1 == 0)
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,5,"SODA CAN");
Lcd_Out(2,5,"DETECTED");
//delay_ms(100);

/*************PRINTS RECEIPT*************/
count = HX711_Read();
weight = (((count-offset)/mul)-2*((count-offset)/mul));
if(weight > 0)
{
UART1_Write_Text("Weight [g]: ");
delay_ms(50);
nFloatToStr(weight, 0, Cweight);
UART1_Write_Text(Cweight);
delay_ms(50);
UART1_Write_Text("\n");
delay_ms(50);

UART1_Write_Text("Reward: Php ");
delay_ms(50);
reward = (30*weight)/1000;
nFloatToStr(reward, 2, Creward);
UART1_Write_Text(Creward);
delay_ms(50);
UART1_Write_Text("\n\n");
delay_ms(50);
UART1_Write_Text("THANK YOU!\n\n\n");
delay_ms(3000);
}
/*************END OF PRINTS RECEIPT*************/

}
else if(INDUCTIVE == 0 && IR_Sensor2 == 0 && IR_Sensor1 == 1 )
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,5,"SODA CAN");
Lcd_Out(2,5,"DETECTED");
//delay_ms(100);

/*************PRINTS RECEIPT*************/
count = HX711_Read();
weight = (((count-offset)/mul)-2*((count-offset)/mul));
if(weight > 0)
{
UART1_Write_Text("Weight [g]: ");
delay_ms(50);
nFloatToStr(weight, 0, Cweight);
UART1_Write_Text(Cweight);
delay_ms(50);
UART1_Write_Text("\n");
delay_ms(50);

UART1_Write_Text("Reward: Php ");
delay_ms(50);
reward = (30*weight)/1000;
nFloatToStr(reward, 2, Creward);
UART1_Write_Text(Creward);
delay_ms(50);
UART1_Write_Text("\n\n");
delay_ms(50);
UART1_Write_Text("THANK YOU!\n\n\n");
delay_ms(3000);
}
/*************END OF PRINTS RECEIPT*************/
}
else
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,6,"INSERT");
Lcd_Out(2,5,"SODA CAN");
delay_ms(500);
}
//delay_ms(50);
}
}

I am actually a beginner with embedded systems and I'm not that well-versed with pic16f877a. So my main problem is that, on the first Soda Can, I could get the desired output which is to display "Soda Can Detected" on the LCD and then prints out a receipt from the thermal printer. However, on the succeeding Soda Cans, the LCD displays the value of Cweight/Creward instead of "Soda Can Detected" (the printer works fine though).

It would be much appreciated if you could help me with this. Thanks ahead. :)

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: UART_Write_Text(text) overwrites characters on Lcd_Out(c

#2 Post by stefan.filipovic » 12 Feb 2019 14:28

Hi,

That sounds really weird.
Could you please zip the whole project or a part of the project which reproduces the same issue and attach here or send to my email ( stefan.filipovic@mikroe.com ) directly?
I will try to reproduce the same issue and locate the problem.

Kind regards,
Stefan Filipović

jin1990
Posts: 8
Joined: 12 Feb 2019 08:15

Re: UART_Write_Text(text) overwrites characters on Lcd_Out(c

#3 Post by jin1990 » 12 Feb 2019 21:52

stefan.filipovic wrote:Hi,

That sounds really weird.
Could you please zip the whole project or a part of the project which reproduces the same issue and attach here or send to my email ( stefan.filipovic@mikroe.com ) directly?
I will try to reproduce the same issue and locate the problem.

Kind regards,

Sir, I have already sent the whole project in your email address. Thanks.

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: UART_Write_Text(text) overwrites characters on Lcd_Out(c

#4 Post by stefan.filipovic » 13 Feb 2019 17:37

Hi,

This is the pretty old MCU with low memory, so you're facing a memory bank problem.
I suggest you use one string variable for displaying strings on the LCD, so you can use: char txt[10]; instead of Cweight[10]; and Creward[10];, because you have been using these strings only for format float to string and display to the LCD.
In your project, there is a lot of IRP warnings for indirect accessing to variables.
You can read more about IRP on the following forum topics:
viewtopic.php?f=88&t=73506&p=294623&hilit=irp#p294623
viewtopic.php?f=88&t=54272&p=212944&hil ... ss#p212944

Kind regards,
Stefan Filipović

jin1990
Posts: 8
Joined: 12 Feb 2019 08:15

Re: UART_Write_Text(text) overwrites characters on Lcd_Out(c

#5 Post by jin1990 » 14 Feb 2019 01:15

stefan.filipovic wrote:Hi,

This is the pretty old MCU with low memory, so you're facing a memory bank problem.
I suggest you use one string variable for displaying strings on the LCD, so you can use: char txt[10]; instead of Cweight[10]; and Creward[10];, because you have been using these strings only for format float to string and display to the LCD.
In your project, there is a lot of IRP warnings for indirect accessing to variables.
You can read more about IRP on the following forum topics:
viewtopic.php?f=88&t=73506&p=294623&hilit=irp#p294623
viewtopic.php?f=88&t=54272&p=212944&hil ... ss#p212944

Kind regards,

Thanks for looking into my code, Sir. This error/warning was indeed the main cause why some of my UART texts were displayed on the LCD. I followed your advice in my Monitoring Module with GSM. It did work and it stabilized my program as well.

I have modified my Identifying and Reward Module and I have yet to test it. What I did was that I reduced the characters inside my UART_Text. Though it made my reward receipt unfriendly for the user.

/**WEIGHT SENSOR**/
#define HX711_DT PORTC.F5
#define HX711_DT_TRIS TRISC.F5
#define HX711_SK PORTC.F4
#define HX711_SK_TRIS TRISC.F4

/*SENSORS*/
#define INDUCTIVE PORTD.F0
#define INDUCTIVE_TRIS TRISD.F0
#define IR_1 PORTD.F1
#define IR_1_TRIS TRISD.F1
#define IR_2 PORTD.F2
#define IR_2_TRIS TRISD.F2

//LCD module connections
sbit LCD_RS at RB2_bit; //LCD reset
sbit LCD_EN at RB3_bit; //LCD enable
sbit LCD_D4 at RB4_bit; //Data
sbit LCD_D5 at RB5_bit; //Data
sbit LCD_D6 at RB6_bit; //Data
sbit LCD_D7 at RB7_bit; //Data
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

long raw_weight = 0;
long offset = 0;
float mul = 0;
float weight = 0;
float reward = 0;
char txt[10];

/*WEIGHT SENSOR*/
#include "HX711.h"
#include "Setup_Weight.h"
#include "Get_Weight.h"

/*Sensors*/
#include "Sensors_Init.h"
#include "floatToString.h"

void main() {

HX711_Init();
Sensors_Init();

//CMCON = 0x07; // To turn off comparators
//ADCON1 = 0x06; // To turn off analog to digital converters

/*Initializing LCD*/

Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);

UART1_Init(19200);
delay_ms(100);

Setup_Weight();

while(1)
{
if(INDUCTIVE == 0 && IR_2 == 0 && IR_1 == 0)
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,5,"SODA CAN");
Lcd_Out(2,5,"DETECTED");
delay_ms(500);

weight = Get_Weight();
UART1_Write_Text("W: ");
delay_ms(50);
floatToString(weight, 2, txt);
UART1_Write_Text(txt);
delay_ms(50);
UART1_Write_Text("\n");
delay_ms(50);

UART1_Write_Text("R: ");
delay_ms(50);
reward = (30*weight)/1000;
floatToString(reward, 2, txt);
UART1_Write_Text(txt);
delay_ms(50);
UART1_Write_Text("\n\n");
delay_ms(3000);
}
else if(INDUCTIVE == 0 && IR_2 == 0 && IR_1 == 1)
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,5,"SODA CAN");
Lcd_Out(2,5,"DETECTED");
delay_ms(500);

weight = Get_Weight();
UART1_Write_Text("W: ");
delay_ms(50);
floatToString(weight, 2, txt);
UART1_Write_Text(txt);
delay_ms(50);
UART1_Write_Text("\n");
delay_ms(50);

UART1_Write_Text("R: ");
delay_ms(50);
reward = (30*weight)/1000;
floatToString(reward, 2, txt);
UART1_Write_Text(txt);
delay_ms(50);
UART1_Write_Text("\n\n");
delay_ms(3000);
}
else
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,6,"INSERT");
Lcd_Out(2,5,"SODA CAN");
delay_ms(500);
}
}
}

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: UART_Write_Text(text) overwrites characters on Lcd_Out(c

#6 Post by stefan.filipovic » 14 Feb 2019 13:01

Hi,

You're welcome.
I'm glad that you have managed to stabilize your project.

You do not need to reduce text size which you display on UART, because this text is not stored in the memory.
Reducing the size of strings and other variables for memory bank 0,1 would be enough if you don't want to set IRP bit manually.

Kind regards,
Stefan Filipović

jin1990
Posts: 8
Joined: 12 Feb 2019 08:15

Re: UART_Write_Text(text) overwrites characters on Lcd_Out(c

#7 Post by jin1990 » 14 Feb 2019 18:01

stefan.filipovic wrote:Hi,

You're welcome.
I'm glad that you have managed to stabilize your project.

You do not need to reduce text size which you display on UART, because this text is not stored in the memory.
Reducing the size of strings and other variables for memory bank 0,1 would be enough if you don't want to set IRP bit manually.

Kind regards,
So I tried to increase the text on UART..


/**WEIGHT SENSOR**/
#define HX711_DT PORTC.F5
#define HX711_DT_TRIS TRISC.F5
#define HX711_SK PORTC.F4
#define HX711_SK_TRIS TRISC.F4

/*SENSORS*/
#define INDUCTIVE PORTD.F0
#define INDUCTIVE_TRIS TRISD.F0
#define IR_1 PORTD.F1
#define IR_1_TRIS TRISD.F1
#define IR_2 PORTD.F2
#define IR_2_TRIS TRISD.F2

//LCD module connections
sbit LCD_RS at RB2_bit; //LCD reset
sbit LCD_EN at RB3_bit; //LCD enable
sbit LCD_D4 at RB4_bit; //Data
sbit LCD_D5 at RB5_bit; //Data
sbit LCD_D6 at RB6_bit; //Data
sbit LCD_D7 at RB7_bit; //Data
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

long raw_weight = 0;
long offset = 0;
float mul = 0;
float weight = 0;
float reward = 0;
char txt[10];

/*WEIGHT SENSOR*/
#include "HX711.h"
#include "Setup_Weight.h"
#include "Get_Weight.h"

/*Sensors*/
#include "Sensors_Init.h"
#include "floatToString.h"

void main() {

HX711_Init();
Sensors_Init();

CMCON = 0x07; // To turn off comparators
ADCON1 = 0x06; // To turn off analog to digital converters

/*Initializing LCD*/

Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);

UART1_Init(19200);
delay_ms(100);

Setup_Weight();

while(1)
{
if(INDUCTIVE == 0 && IR_2 == 0 && IR_1 == 0)
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,5,"SODA CAN");
Lcd_Out(2,5,"DETECTED");
delay_ms(500);

weight = Get_Weight();
if(weight > 0)
{
UART1_Write_Text("Weight: ");
delay_ms(50);
floatToString(weight, 2, txt);
UART1_Write_Text(txt);
delay_ms(50);
UART1_Write_Text("\n");
delay_ms(50);

UART1_Write_Text("Reward: ");
delay_ms(50);
reward = (30*weight)/1000;
floatToString(reward, 2, txt);
UART1_Write_Text(txt);
delay_ms(50);
UART1_Write_Text("\n\n\n");
delay_ms(2000);
}
}
else if(IR_1 == 1 && IR_2 == 0 && INDUCTIVE == 0)
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,5,"SODA CAN");
Lcd_Out(2,5,"DETECTED");
delay_ms(500);

weight = Get_Weight();
if(weight > 0)
{
UART1_Write_Text("Weight: ");
delay_ms(50);
floatToString(weight, 2, txt);
UART1_Write_Text(txt);
delay_ms(50);
UART1_Write_Text("\n");
delay_ms(50);

UART1_Write_Text("Reward: ");
delay_ms(50);
reward = (30*weight)/1000;
floatToString(reward, 2, txt);
UART1_Write_Text(txt);
delay_ms(50);
UART1_Write_Text("\n\n\n");
delay_ms(2000);
}
}
else
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,6,"INSERT");
Lcd_Out(2,5,"SODA CAN");
delay_ms(500);
}
}
}


and i got this message..

35 1511 IRP bit must be set manually for indirect access to 'txt' variable IDENTIFYING_REWARD.c

0 1511 IRP bit must be set manually for indirect access to '?lstr10_IDENTIFYING_REWARD' variable IDENTIFYING_REWARD.c

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: UART_Write_Text(text) overwrites characters on Lcd_Out(c

#8 Post by stefan.filipovic » 20 Feb 2019 18:03

Hi,

I've noticed that UART_Write_Text nevertheless consumes slightly RAM and ROM memory for their local variables and parameters.
So, I really suggest you upgrade to PIC18 if you do not want to set IRP bit manually. If you decide to use IRP bit setting manually, you will still having trouble in using some functions from the Library Manager.

Kind regards,
Stefan Filipović

jin1990
Posts: 8
Joined: 12 Feb 2019 08:15

Re: UART_Write_Text(text) overwrites characters on Lcd_Out(c

#9 Post by jin1990 » 21 Feb 2019 06:35

stefan.filipovic wrote:Hi,

I've noticed that UART_Write_Text nevertheless consumes slightly RAM and ROM memory for their local variables and parameters.
So, I really suggest you upgrade to PIC18 if you do not want to set IRP bit manually. If you decide to use IRP bit setting manually, you will still having trouble in using some functions from the Library Manager.

Kind regards,
I have thought of using PIC18 specifically PIC18F8722. But I dunno what kind of Program loader like I should use for this particular type of PiC since it has more than 40 pins. Any suggestions?

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: UART_Write_Text(text) overwrites characters on Lcd_Out(c

#10 Post by stefan.filipovic » 21 Feb 2019 10:34

Hi,

There is an MCU card with PIC18F8722, so you can use it with EasyPIC PRO v7.
Unfortunately, you cannot use it with EasyPIC v7.
For EasyPIC v7, I suggest you consider to use the PIC18F45K22.

Kind regards,
Stefan Filipović

jin1990
Posts: 8
Joined: 12 Feb 2019 08:15

Re: UART_Write_Text(text) overwrites characters on Lcd_Out(c

#11 Post by jin1990 » 21 Feb 2019 14:55

stefan.filipovic wrote:Hi,

There is an MCU card with PIC18F8722, so you can use it with EasyPIC PRO v7.
Unfortunately, you cannot use it with EasyPIC v7.
For EasyPIC v7, I suggest you consider to use the PIC18F45K22.

Kind regards,
Sir, it would be my first time to use PIC18 MCU. Would there be any changes in the syntax?

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: UART_Write_Text(text) overwrites characters on Lcd_Out(c

#12 Post by stefan.filipovic » 21 Feb 2019 15:10

Hi,

There are many forum topics regarding this question, I suggest you read this one.

Kind regards,
Stefan Filipović

Post Reply

Return to “PIC PRO Compilers”