//#include<reg52.h>
#include<stc12c5608ad.h>
bit int_flag; //定时器0 1S到标志位
unsigned char volatile int_count; //定时器0中断次数
unsigned char volatile T1count; //定时器1中断次数
sbit wela=P3^7; //申明U2锁存器的锁存端
sbit dula=P3^2; //申明U1锁存器的锁存端
sbit wela1=P3^4; //申明U2锁存器的锁存端
sbit wela2=P3^3;
sbit wela3=P3^1; //申明U2锁存器的锁存端
sbit wela4=P3^0;
/*
sbit wela=P2^5; //申明U2锁存器的锁存端
sbit dula=P2^4; //申明U1锁存器的锁存端
sbit wela1=P2^3; //申明U2锁存器的锁存端
sbit wela2=P2^2;
sbit wela3=P2^1; //申明U2锁存器的锁存端
sbit wela4=P2^0;
*/
unsigned char code seg[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xa7,0xa1,0x86,0x8e};
unsigned long fre; //1S内脉冲总个数
unsigned char led[6]; //LED显示缓存
///////////////软件延时/////////////
void delayms(unsigned int a) //延时约1ms(12Mhz晶振)
{ unsigned char i;
while(a--!=0)
for(i=125;i>0;i--);
}
///定时器0初始化
void init_t0(void)
{
TMOD=(TMOD&0xF0)||0x01; //定时器0工作于方式1
TH0=(65536-50000)/256; //定时50ms
TL0=(65535-50000)%256;
}
// 定时器1初始化
void init_t1(void)
{
TMOD=(TMOD&0x0F)|0x50; //timer 1 for count
TH1 =0x00;
TL1 =0x00;
}
//显示
///////////////////////////////////////////////////////
void main (void)
{
EA=1; //开总中断
init_t0(); //初始化定时器
init_t1();
TR0=1; //定时器开始工作
TR1=1;
ET0=1; //开T0中断
while(1)
{
led[0]=fre%10; // ge
P1=seg[led[0]]; //送个位段选数据
wela=0;
delayms(1);
wela=1;
led[1]= fre/10%10;// shi
P1=seg[led[1]]; //送十位段选数据
dula=0;
delayms(1); //延时
dula=1;
led[2]= fre/100%10;// bai
P1=seg[led[2]]; //送个位段选数据
wela1=0;
delayms(1);
wela1=1;
led[3]= fre/1000%10;// bai
P1=seg[led[3]]; //送个位段选数据
wela2=0;
delayms(1);
wela2=1;
led[4]= fre/10000%10;// bai
P1=seg[led[4]]; //送个位段选数据
wela3=0;
delayms(1);
wela3=1;
led[5]= fre/100000%10;// bai
P1=seg[led[5]]; //送个位段选数据
wela4=0;
delayms(1);
wela4=1;
if(int_flag==1)
{
int_flag=0;
fre=TL1+TH1*256+T1count*65536; //计算1秒内的脉冲个数
//以下将数据格式化,转成LED可显示的BCD码
int_count=0x00;
T1count=0;
TH1=0x00;
TL1=0x00;
TR1=1;
}
}
}
//定时器0中断服务程序
void int_t0(void) interrupt 1
{
TH0=(65536-50000)/256; //定时50ms
TL0=(65535-50000)%256;
int_count++;
if(int_count==20)
{
TR1=0;
int_flag=1;
int_count=0x00;
}
}
//定时器1中断服务程序
void int_t1(void) interrupt 3
{
T1count++;
}
我用6位数码管显示的,测26khz频率,程序写到89c58测来的值是25865到25760跳动不大,但在12c5608测的频率是5302到9028之间跳动很大,请问是什么问题能!
|