操作小记
#include <reg52.h>
#include <stdio.h>
code unsigned char ZiMu[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
static unsigned char VideoRam[8];
unsigned char CurrentPos=0;
void SetPos(unsigned char pos)
{
CurrentPos=pos;
}
void Clear(void)
{
unsigned char i;
for(i=0;i<8;i++)
VideoRam[i]=0x00;
CurrentPos=0;
}
char ShowChar(unsigned char ch)
{
if((ch>='0')&&(ch<='9'))
VideoRam[CurrentPos++]=ZiMu[ch-0x30];
else if(ch==' ')
VideoRam[CurrentPos++]=0x00;
else if(ch=='-')
VideoRam[CurrentPos++]=0x40;
else if(ch=='.')
VideoRam[CurrentPos-1]+=0x80;
else if(CurrentPos>7)
CurrentPos=0;
return ch;
}
void InitTimer0(void)
{
TMOD=0x21;
ET0=1;
TL0=(65536-3000)%256;
TH0=(65536-3000)/256;
TR0=1;
EA=1;
}
static void ScanSeg(void)
{
static unsigned char LedNum=7;
P2=P2&0xe3|(LedNum<<2);
P0=VideoRam[7-LedNum];
if(LedNum>0)
LedNum= LedNum-1;
else
LedNum=7;
}
void Timer0(void) interrupt 1
{
TL0=(65536-3000)%256;
TH0=(65536-3000)/256;
ScanSeg();
}
char putchar(char ch)
{
return ShowChar(ch);
}
void main(void)
{
float f1=0.67;
InitTimer0();
SetPos(1);
printf("%5.3f", f1);
while(1);
}
附录
格式化输出
%f: float
%lf: double
%ld: long int
%d: int
-------------
%.2lf: 保留两位小数
%5.3lf: 输出宽度为5位 保留3位小数