I need a problem, i use PIC simulator to import2 numbers by serial port. First I send byte, 5 for example, then send sent 2 and in the third step I sent char for example *, and the result is 5*2=10. Everything is fine till I send larger number more than 255. It says wrong entry, I know that I can send only 255 by byte. But how can I import larger number. What should be changed in the code to work for larger numbers. Some ideas??????? thanks a lot


/================= konfigure LCD display
// port for data PORTB
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
//================================================== =======

//===========variables
char operation;
int nbr1=0,nbr2=0,result=0,rest=0;
char txt[16],br[7];
//================================================== =======

//======== delete empty spaces
void empty_spaces(char array[]){
int j=0,i=0,n=0;
n=strlen(array);
while(i<n){
if(array==' '){
j=i;
while(j<n){
array[j]=array[j+1];
++j;
}
--n;
}else
++i;
}
if(n>15)
n=15;
array[n]='\0';
}
//================================================== =======

//========function back int from the imported char

int back_char(char operation ){
if(operation=='+')
return 1;
if(operation=='-')
return 2;
if(operation=='*')
return 3;
if(operation=='/')
return 4;
if(operation=='%')
return 5;
return 0;
}
//================================================== =======

//============= init lcd display and serial port
void inicijalizacija(){
PORTB = 0xFF;
TRISB = 0x00;
ANSEL = 0x00;
ANSELH = 0x00;
C1ON_bit = 0;
C2ON_bit = 0;
UART1_Init(9600);
Delay_ms(100);
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
}
//================================================== =======

void main(){
inicijalizacija();
//============= enter first number
UART1_Write_Text("first num:");
UART1_Write(10);
UART1_Write(13);
do{
}while(!UART1_Data_Ready());
nbr1=UART1_Read();
IntToStr(nbr1,br);
strcpy(txt,"Num1:");
strcat(txt,br);
empty_spaces(txt);
Lcd_Out(1,1,txt);
Delay_ms(1);


//=============enter second number
UART1_Write_Text("second num:");
UART1_Write(10);
UART1_Write(13);
do{
}while(!UART1_Data_Ready());
nbr2=UART1_Read();
IntToStr(nbr2,br);
strcpy(txt,"Num2:");
strcat(txt,br);
empty_spaces(txt);
Lcd_Out(1,10,txt);
Delay_ms(1);
//================================================== ============

//enter operation

UART1_Write_Text("operation(+,-,/,*,%):");
UART1_Write(10);
UART1_Write(13);
do{
}while(!UART1_Data_Ready());
operation=UART1_Read();
strcpy(txt,"oper:");
switch(back_char(operation)){
case 0: strcat(txt," ");break;
case 1:
strcat(txt,"+");
result=nbr1+nbr2;
break;
case 2:
strcat(txt,"-");
result=nbr1-nbr2;
break;
case 3:
strcat(txt,"*");
result=nbr1*nbr2;
if(nbr2!=result/nbr1)
operation=' ';
break;
case 4:
strcat(txt,"/");
if(nbr2==0)
operation=' ';
else{
result=nbr1/nbr2;
rest=nbr1%nbr2;
}
break;
case 5:
strcat(txt,"%");
if(nbr2==0)
operation=' ';
else
result=nbr1%nbr2;
break;
}
empty_spaces(txt);
Lcd_Out(2,1,txt);
Delay_ms(1);
//================================================== ============

//============= Print result

if(back_char(operation)!=0){
IntToStr(result,br);
strcpy(txt,"Rez:");
strcat(txt,br);
empty_spaces(txt);
if(back_char(operation)!=4)
Lcd_Out(2,7,txt);
else{ // Dokolku vrednosta od funkcijata vrati_znak(operacija) e 4
IntToStr(rest,br); // se raboti za delenje
empty_spaces(br);
strcat(txt,"~");
strcat(txt,br);
Lcd_Out(2,5,txt);
}
}else
Lcd_Out(2,7,"error!");
Delay_ms(1);
//================================================== ============
}

Recommended Answers

All 5 Replies

Member Avatar for v3ga

Wrap code in code tags

/================= konfigure LCD display
// port for data PORTB
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
//================================================== =======

//===========variables
char operation;
int nbr1=0,nbr2=0,result=0,rest=0;
char txt[16],br[7];
//================================================== =======

//======== delete empty spaces
void empty_spaces(char array[]){
int j=0,i=0,n=0;
n=strlen(array);
while(i<n){
if(array[i]==' '){
j=i;
while(j<n){
array[j]=array[j+1];
++j;
}
--n;
}else
++i;
}
if(n>15)
n=15;
array[n]='\0';
}
//================================================== =======

//========function back int from the imported char

int back_char(char operation ){
if(operation=='+')
return 1;
if(operation=='-')
return 2;
if(operation=='*')
return 3;
if(operation=='/')
return 4;
if(operation=='%')
return 5;
return 0;
}
//================================================== =======

//============= init lcd display and serial port
void inicijalizacija(){
PORTB = 0xFF;
TRISB = 0x00;
ANSEL = 0x00;
ANSELH = 0x00;
C1ON_bit = 0;
C2ON_bit = 0;
UART1_Init(9600);
Delay_ms(100);
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
}
//================================================== =======

void main(){
inicijalizacija();
//============= enter first number
UART1_Write_Text("first num:");
UART1_Write(10);
UART1_Write(13);
do{
}while(!UART1_Data_Ready());
nbr1=UART1_Read();
IntToStr(nbr1,br);
strcpy(txt,"Num1:");
strcat(txt,br);
empty_spaces(txt);
Lcd_Out(1,1,txt);
Delay_ms(1);


//=============enter second number
UART1_Write_Text("second num:");
UART1_Write(10);
UART1_Write(13);
do{
}while(!UART1_Data_Ready());
nbr2=UART1_Read();
IntToStr(nbr2,br);
strcpy(txt,"Num2:");
strcat(txt,br);
empty_spaces(txt);
Lcd_Out(1,10,txt);
Delay_ms(1);
//================================================== ============

//enter operation

UART1_Write_Text("operation(+,-,/,*,%):");
UART1_Write(10);
UART1_Write(13);
do{
}while(!UART1_Data_Ready());
operation=UART1_Read();
strcpy(txt,"oper:");
switch(back_char(operation)){
case 0: strcat(txt," ");break;
case 1:
strcat(txt,"+");
result=nbr1+nbr2;
break;
case 2:
strcat(txt,"-");
result=nbr1-nbr2;
break;
case 3:
strcat(txt,"*");
result=nbr1*nbr2;
if(nbr2!=result/nbr1)
operation=' ';
break;
case 4:
strcat(txt,"/");
if(nbr2==0)
operation=' ';
else{
result=nbr1/nbr2;
rest=nbr1%nbr2;
}
break;
case 5:
strcat(txt,"%");
if(nbr2==0)
operation=' ';
else
result=nbr1%nbr2;
break;
}
empty_spaces(txt);
Lcd_Out(2,1,txt);
Delay_ms(1);
//================================================== ============

//============= Print result

if(back_char(operation)!=0){
IntToStr(result,br);
strcpy(txt,"Rez:");
strcat(txt,br);
empty_spaces(txt);
if(back_char(operation)!=4)
Lcd_Out(2,7,txt);
else{ // Dokolku vrednosta od funkcijata vrati_znak(operacija) e 4
IntToStr(rest,br); // se raboti za delenje
empty_spaces(br);
strcat(txt,"~");
strcat(txt,br);
Lcd_Out(2,5,txt);
}
}else
Lcd_Out(2,7,"error!");
Delay_ms(1);
//================================================== ============
}

This is my code, I wrap in code tags, if you know something that will help me, please help me

Wrap code in code tags

You know you could have also said to properly format the code cause it's still hard to read

You could change int to long

long nbr1=0,nbr2=0,result=0,rest=0;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.