单片机浮点数输入显示程序

void InputDecimal(uchar x,uchar y,uchar uIntegerLen,uchar uDecimalLen,float * fData,unsigned int Address){  int j; uchar i; CurFlash(CurX,CurY,0); CheckData=(float)(*fData);//将原来的值写入警告窗口值 if(bKeyOK==0) return; //没有按键,返回 if(!(keyvector>=NUM0 && keyvector<=NUM9 || keyvector==PAGEDN ||      keyvector==BLANK||keyvector==DELETE  ))  return; //不是数字键,不是删除键,不是小数点,不是删除键,返回 //剩下的是数字键,小数点键,删除键,负号键  if(uEnterEdit==0)// 刚刚进入状态  {     *fData=0;   uDotOn=0;   uEnterEdit=1;   bNegativeKey=0;   CurX=x;   CurY=y;   for(i=0;i<6;i++)     KeyDecimal[i]='/0';     }   j=abs((int)*fData);   if(keyvector>=NUM0 && keyvector<=NUM9) {       if(uDotOn==0)//还没有按小数点,仍是编辑整数部分  {    uEnterEdit=2;   if(WeiShu(j)<uIntegerLen)//还没有超出范围   {    j=j*10+keyvector-48;     //uEnterEdit=2; //标志已经按了数字键    if(j>0)     uEnterEdit=3;   }  }  else if(uDotOn>=1 && uDotOn<=uDecimalLen)//编辑小数部分,且小数部分仍在范围内  {   KeyDecimal[uDotOn]=keyvector;   uDotOn++;   //uEnterEdit=2;    }  else  ;     } else if(keyvector==PAGEDN) {  //uEnterEdit=2;  if(j==0 && uDotOn==0) //只有整数部分是0并且还没有输入小数点时才能输入负号   bNegativeKey=1; } else if(keyvector==BLANK)//小数点 {    if(uDotOn==0)//只有小数点还没有输入的时候才能输入小数点  { uDotOn=1;KeyDecimal[0]='.';   if(uEnterEdit==1)    uEnterEdit=2;    }  } else if(keyvector==DELETE)//删除键 {   if(uDotOn>=1)//还有小数部分或者有小数点  {    uDotOn--;   KeyDecimal[uDotOn]='/0';      }   else//有整数部分或者还有负号   {   if(j==0)//整数部分为0   {    if(bNegativeKey==1)//还有负号    {     if(uEnterEdit>=2)//整数部分还有0      {       uEnterEdit=1;              }           else       {          bNegativeKey=0;       uEnterEdit=0;       }         }    // if(bNegativeKey==1)//还有负号   //  bNegativeKey=0;   }   else //整数部分不为0   {      j=j/10;//删除整数部分最后一位     if(j==0)      uEnterEdit=1;   }          }//有整数部分或者还有负号 }//删除键  if(bNegativeKey==1) j=j*(-1);  /***************以下为显示部分***********************/ Line(x+17, CurY-1, x+17, CurY, BLUE, BLUE);  //清理光标痕迹  for(i=0;i<=uIntegerLen+uDecimalLen+1;i++)  DispStr(x, y+i, BLUE, INDIGO, " "); i=WeiShu(j);   if(bNegativeKey==0) {    if(uDotOn==0)    {     //sprintf(dispmem, "%d", (int)(j));   //DispStr(x,y, BLUE, WHITE, dispmem);   ShowInt(x,y,WHITE,j);   CurY=y+i;    }    else    {   //sprintf(dispmem, "%d", (int)(j));   //DispStr(x,y, BLUE, WHITE, dispmem);   ShowInt(x,y,WHITE,j);   if(j==0)    {DispStr(x,y+1, BLUE, WHITE, KeyDecimal);CurY=y+1+uDotOn;}   else    {DispStr(x,y+i, BLUE, WHITE, KeyDecimal); CurY=y+i+uDotOn;}      }  } else//负数 {    if(uDotOn==0)  {   if(j==0)   {     if(uEnterEdit==2 )      {DispStr(x, y, BLUE, WHITE, "-0");CurY=y+2;}    else if (uEnterEdit==1 )     {DispStr(x, y, BLUE, WHITE, "-");CurY=y+1;}     else;     //if(uIntegerZero==0)    // {DispStr(x, y, BLUE, INDIGO, "-0");CurY=y+2;uIntegerZero=1;}    //else     // {DispStr(x, y, BLUE, INDIGO, "-");CurY=y+1;}    }    else    {       //sprintf(dispmem, "%d", (int)(j));    //DispStr(x,y, BLUE, WHITE, dispmem);    ShowInt(x,y,WHITE,j);    CurY=y+i;     }   }  else  {   if(j==0)   {    if(uEnterEdit==1 )      {DispStr(x, y, BLUE, WHITE, "-");CurY=y+1;}    else if (uEnterEdit==2 )     {DispStr(x, y, BLUE, WHITE, "-0");CurY=y+2;}     else;       DispStr(x,CurY, BLUE, WHITE, KeyDecimal);    CurY=CurY+uDotOn;   }   else   {    //sprintf(dispmem, "%d", (int)(j));    //DispStr(x,y, BLUE, WHITE, dispmem);    ShowInt(x,y,WHITE,j);        DispStr(x,y+i, BLUE, WHITE, KeyDecimal);    CurY=y+i+uDotOn;    }  }      }  j=abs(j); (* fData)=j;  i=1; while(KeyDecimal[0]=='.' && KeyDecimal[i]!='/0'&& i<=4) {    (* fData)=(* fData)+(KeyDecimal[i]-48)/pow(10,i);    i++; }     if(bNegativeKey==1)  (* fData)*=(-1);  CheckData=(float)(*fData); WriteFloatFM(fData,Address) ;  //DispStr(220,30, BLUE, INDIGO, "        "); //sprintf(dispmem, "%4.3f", (float)(* fData)); //DispStr(220,30, BLUE, INDIGO, dispmem); bKeyOK=0;}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 51 单片机浮点数拆分、比大小和 LCD 显示程序: ```c #include <reg51.h> #include <stdio.h> #define LCD_DB P2 // LCD 数据口 sbit LCD_E = P3^5; // LCD 使能端口 sbit LCD_RS = P3^6; // LCD 命令/数据选择端口 // 延时函数,用于 LCD 显示 void delay() { int i, j; for (i = 0; i < 10; i++) { for (j = 0; j < 500; j++); } } // LCD 初始化函数 void lcd_init() { LCD_RS = 0; LCD_E = 0; LCD_DB = 0x38; LCD_E = 1; delay(); LCD_E = 0; delay(); LCD_DB = 0x0C; LCD_E = 1; delay(); LCD_E = 0; delay(); LCD_DB = 0x06; LCD_E = 1; delay(); LCD_E = 0; delay(); } // LCD 显示函数 void lcd_display(char *str) { while (*str) { LCD_RS = 1; LCD_DB = *str++; LCD_E = 1; delay(); LCD_E = 0; delay(); } } // 浮点数拆分函数 void float_split(float f, int *sign, int *exponent, unsigned int *mantissa) { int *p = (int *)&f; // 拆分符号、指数和尾数 *sign = (*p >> 31) & 1; *exponent = ((*p >> 23) & 0xFF) - 127; *mantissa = (*p & 0x007FFFFF) | 0x00800000; } // 浮点数比较函数 int float_compare(float f1, float f2) { int sign1, sign2, exponent1, exponent2; unsigned int mantissa1, mantissa2; // 拆分两个浮点数 float_split(f1, &sign1, &exponent1, &mantissa1); float_split(f2, &sign2, &exponent2, &mantissa2); // 比较符号、指数和尾数 if (sign1 != sign2) { return sign1 - sign2; } else if (exponent1 != exponent2) { return exponent1 - exponent2; } else { return mantissa1 - mantissa2; } } int main() { float f1, f2; int sign1, exponent1, sign2, exponent2; unsigned int mantissa1, mantissa2; // 初始化 LCD lcd_init(); // 输入两个浮点数 printf("Enter two floating-point numbers: "); scanf("%f %f", &f1, &f2); // 拆分两个浮点数 float_split(f1, &sign1, &exponent1, &mantissa1); float_split(f2, &sign2, &exponent2, &mantissa2); // 比较两个浮点数的大小 if (float_compare(f1, f2) < 0) { lcd_display("f1 < f2"); } else if (float_compare(f1, f2) > 0) { lcd_display("f1 > f2"); } else { lcd_display("f1 = f2"); } return 0; } ``` 该程序首先会让用户输入两个浮点数,然后将它们拆分成符号、指数和尾数三部分,并比较它们的大小,最后将结果通过 LCD 显示出来。 在程序中,`float_split()` 函数用于拆分一个浮点数,将其符号、指数和尾数分别存储在三个变量中;`float_compare()` 函数用于比较两个浮点数的大小,返回值为负数表示第一个浮点数小于第二个浮点数,返回值为正数表示第一个浮点数大于第二个浮点数,返回值为零表示两个浮点数相等。而 LCD 的初始化和显示函数则是通过写入相应的命令和数据到 LCD 的数据口和控制口实现的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值