下面是一个数码管显示0到F的例子:
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar tt,numdu;
uchar code tabledu[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};//code 编码表
void init() //初始化函数
{
numdu=0;
tt=0;
TMOD=1;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1; //开总中断
ET0=1; //开定时器T0中断
TR0=1; //开定时器T0
}
void main()
{
init();
while(1)
{
if(tt==20)
{
tt=0;
dula=1;
P0=tabledu[++numdu];
dula=0;
wela=1;
P0=0xfe;
wela=0;
if(numdu>=16) numdu=0;
}
}
}
void time() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tt++;
}