(2)液晶如何显示小数
使用以下代码:
#include <reg52.h>
#include <string.h>
#include "lcd1602.h"
void main(void)
{
float a = 182.376 ;
lcd1602Init();
lcd1602WriteCommand(0X0C);
/*显示原来的数字,非重点*/
lcd1602AddressWriteByte(LCD1602_ROW0,0,((unsigned long)a)%1000/100+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,1,((unsigned long)a)%100/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,2,((unsigned long)a)%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,3,'.') ;
lcd1602AddressWriteByte(LCD1602_ROW0,4,(unsigned long)(a*10)%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,5,(unsigned long )(a*100)%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,6,(unsigned long )(a*1000)%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,8,0x7e) ;
/*重点部分*/
/*显示,百位这里我一直最高是百位,所以没有判断更加位,实际操作时,应该添加其他机制,使其更加智能*/
lcd1602AddressWriteByte(LCD1602_ROW0,10,((unsigned long<span style="font-family: Arial, Helvetica, sans-serif;">)a)%1000/100+'0') ;</span>
/*显示十位*/
lcd1602AddressWriteByte(LCD1602_ROW0,11,((unsigned long)a)%100/10+'0') ;
/*显示各位*/
lcd1602AddressWriteByte(LCD1602_ROW0,12,((unsigned long)a)%10+'0') ;
/*显示小数点*/
lcd1602AddressWriteByte(LCD1602_ROW0,13,'.') ;
/*显示小数点后第一位*/
lcd1602AddressWriteByte(LCD1602_ROW0,14,(unsigned long)(a*10)%10+'0') ;
/*显示小数点后第二位,此时需要四舍五入计算*/
lcd1602AddressWriteByte(LCD1602_ROW0,15,(unsigned long )(a*100+0.5)%10+'0') ;
lcd1602AddressWriteString(LCD1602_ROW1,0,"QQ:279729201") ;
while(1);
}
|