此程序是在51hei单片机开发板上面做的,如需要移植到自己的电路上,修改相应的端口即可,开发板完整的电路图下载: 点这里 (注意:只需要看相关部分即可,其他部分可以忽略)
//********************************频率计*********************************
//*******************************计数最大值1MHZ***************************
//*****************************手把手教你单片机开发板**********************
//*********************************2012/9/8***********************
#include <reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
uint8 code block[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
uint8 a[6];
uint16 counter=0;
sbit ADDR0 = P1^0; //A0
sbit ADDR1 = P1^1; //A1
sbit ADDR2 = P1^2; //A2
sbit ADDR3 = P1^3; //E1|E2
sbit ENLED = P1^4; //E3**38译码器######
sfr16 DPTR = 0x82;
void refresh_LS();
void timer1_int_init()
{
TMOD = 0x51;
TH1 = 0x00;
TL1 = 0x00;
TR1 = 1;
EA = 1;
ET0 = 1;
}
void timer0_init()
{
TH0 = 0xfe;
TL0 = 0x33;
TR0 = 1;
}
void interrupt_SHOW() interrupt 1
{
static uint8 i=0;
static uint32 record=0;
TH0 = 0xFE;
TL0 = 0x33;
counter++;
DPL = TL1;
DPH = TH1;
if(DPTR==50000)
{
i++;
TH1 = 0x00;
TL1 = 0x00;
}
record=(i>=1)?i*50000:DPTR;
if(counter == 2000)
{
i=0;
counter=0;
if(record == 1000000)
{
a[0] = 11;
a[1] = 11;
a[2] = 11;
a[3] = 11;
a[4] = 11;
a[5] = 1;
}
else
{
a[0] = record%10;
a[1] = record/10%10;
a[2] = record/100%10;
a[3] = record/1000%10;
a[4] = record/10000%10;
a[5] = record/100000%10;
}
record=0;
TH1 = 0x00;
TL1 = 0x00;
}
refresh_LS();
}
void refresh_LS()
{
static uint8 j=0;
switch(j)
{
case 0: ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = block[a[0]]; j++; break;
case 1: ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = block[a[1]]; j++; break;
case 2: ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = block[a[2]]; j++; break;
case 3: ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = block[a[3]]; j++; break;
case 4: ADDR0 = 0; ADDR1 = 0; ADDR2 = 1; P0 = block[a[4]]; j++; break;
case 5: ADDR0 = 1; ADDR1 = 0; ADDR2 = 1; P0 = block[a[5]]; j=0; break;
default:break;
}
}
main()
{
ENLED = 0; ADDR3 = 1;
timer1_int_init();
timer0_init();
while(1);
}