我需要一個問題,我使用PIC模擬器通過串口導入2個數字。首先我發送字節,例如5,然後發送發送2,並在第三步我發送char例如*,結果是5 * 2 = 10。一切都很好,直到我發送更大的數字超過255個。它說錯誤的輸入,我知道我只能按字節發送255。但是我怎樣才能導入更大的數字。代碼中應該更改哪些內容才能處理更大的數字。一些想法???????非常感謝乘以兩個大於255的數字
//================= 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);
//==============================================================
}
Woa。這是一個負載的代碼wader通過... – Almo 2011-12-19 19:28:17
我知道,但你怎麼想導入到大於255的數字。是否有一些函數在c或其他? – user1106580 2011-12-19 19:31:24
所有的代碼仍然沒有告訴我們「說錯了什麼」。我們需要知道這一點。 – 2011-12-19 19:40:36