LED格式化输出
程序员文章站
2022-07-14 23:36:28
...
操作小记
#include <reg52.h>
#include <stdio.h>
// LED显示0~9
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;
}
// reset
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;//T1:mode 2,reload; T0:mode 1,16bit counter
ET0=1;//enable T0 interrupt
TL0=(65536-3000)%256;//3ms interrupt
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;//3ms interrupt
TH0=(65536-3000)/256;
ScanSeg();
}
//重写putchar()函数
char putchar(char ch)
{
return ShowChar(ch);
}
void main(void)
{
float f1=0.67;
InitTimer0();
SetPos(1);
/*
ShowChar('2');
ShowChar('-');
ShowChar(' ');
ShowChar('3');
ShowChar('.');
ShowChar('4');
*/
//printf("%c",0x31); //00110001 -> 1+ 16+32 = 49 -->1
printf("%5.3f", f1);
while(1);
}
附录
格式化输出
%f: float
%lf: double
%ld: long int
%d: int
-------------
%.2lf: 保留两位小数
%5.3lf: 输出宽度为5位 保留3位小数
上一篇: Python格式化输出(format)
下一篇: java中char和int类型的相互转换