//点亮数码管 sprintf printf
#include <stc15.h>
#include <stdio.h> //可以用sprintf函数
#include <string.h> //可以用memset函数,
#define FOSC 11059200L
#define T1MS (65536-FOSC/1000) //1T模式
char SMGbuf[20]; //数码管缓冲区
char SMGDat[8]; //数码管打印区
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
#define LED 4
#define ULN 5
#define COM 6
#define ABC 7
/************* 本地常量声明 **************/
u8 code t_display[]={ //标准字库
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black - H J K L N o P U t G Q r M y
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};//
//0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //位码
void choose573(unsigned char x)
{
switch(x)
{
case 0:P2=P2&0x1F;break;
case LED:P2=(P2&0x1F) |0x80;break;
case ULN:P2=(P2&0x1F) |0xA0;break;
case COM:P2=(P2&0x1F) |0xC0;break;
case ABC:P2=(P2&0x1F) |0xE0;break;
}
}
void smgconv()
{
unsigned char i,j;
unsigned char smgdattemp;
while(SMGbuf[i]!=0x00)
{
switch(SMGbuf[i])
{
case '0': smgdattemp= t_display[0];break;
case '1': smgdattemp= t_display[1];break;
case '2': smgdattemp= t_display[2];break;
case '3': smgdattemp= t_display[3];break;
case '4': smgdattemp= t_display[4];break;
case '5': smgdattemp= t_display[5];break;
case '6': smgdattemp= t_display[6];break;
case '7': smgdattemp= t_display[7];break;
case '8': smgdattemp= t_display[8];break;
case '9': smgdattemp= t_display[9];break;
case 'A': smgdattemp= t_display[10];break;
case 'B': smgdattemp= t_display[11];break;
case 'C': smgdattemp= 0x39;break;
case 'D': smgdattemp= 0x5E;break;
//可以在这里添加自己的字库
default: smgdattemp=0x00;break;
}
if(SMGbuf[i+1]=='.')
{
smgdattemp= smgdattemp|0x80;//添加小数点
i= i+2;
}
else i=i+1;
SMGDat[j]= smgdattemp;
j++;
}
}
void display()
{
static unsigned int i=0;
//选择数码管
choose573(COM);
P0=0x00; //注意,消隐一下
choose573(0);
//选择数码管
choose573(ABC);
P0=0xFF; //注意,消隐一下
choose573(0);
//选择数码管
choose573(COM);
P0=T_COM[i]; //注意,这里不用取反,因为本来0x01就是点亮一个数码管
choose573(0);
//输出内容
choose573(ABC);
P0=~SMGDat[i];//注意,需要位取反,因为字库表是共阴表,开发板是共阳数码管,两者相反
choose573(0);
i++;
//如果够8个,就归0
if(i>=8)i=0;
}
float wendu=12.5;
char miao=25;
int shi=350;
void main()
{
int i=0;
AUXR |= 0x80; //定时器0为1T模式
TMOD = 0x00; //设置定时器为模式0(16位自动重装载)
TL0 = T1MS; //初始化计时值
TH0 = T1MS >> 8;
ET0 = 1; //使能定时器0中断
EA = 1;
TR0 = 1; //定时器0开始计时
while(1)
{
}
}
/* Timer0 interrupt routine */
void tm0_isr() interrupt 1 using 1
{
static int cnt1ms=0;
cnt1ms++;
if(cnt1ms==1000)
{
wendu=wendu+0.1;
memset (SMGDat, '\0', sizeof (SMGDat)); //注意,把打印数据清0,不然有数据残留
sprintf(SMGbuf,"AB%4.2f",wendu);
smgconv();
}
if(cnt1ms==2000)
{
cnt1ms=0;
miao=miao+1;
memset (SMGDat, '\0', sizeof (SMGDat)); //注意,把打印数据清0,不然有数据残留
sprintf(SMGbuf,"CD%2d",(int)miao);
smgconv();
SMGDat[7]=0x7C;//转换觉得不合理,可以人为,往里面填一些数据 ,这里是举例
}
display();
}