to是计数器,t1是定时器,把单位时间内计算的数据动态显示在led上,led是两片74hc595芯片支持的,哪位朋友给看看问题在哪里,找了两天了,多谢。问题应该在计数器定时器上面,估计显示代码还是可以的,因为之前可以显示。
#include <AT89X51.H>
#include <intrins.H>
unsigned char code ANTI_SEG_CODE[] =
{// 0 1 2 3 4 5 6 7 8 9 A b C d E F -
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x8C,0xBF,0xC6,0xA1,0x86,0xFF,0xbf
};
unsigned char code POS_SELE[]={0X01,0X02,0X04,0X08};
sbit DS = P3^7; //串行数据输入
sbit ST = P1^0; //RCLK时钟脉冲信号——上升沿有效
sbit SH = P1^1; //SCLK
sbit JCON = P1^4; //湿度检测电路开关
//发数据函数送三位数
void SEND_DATA_TO_595(unsigned char senddata)
{
int i;
ST = 0;
for(i=8;i>0;i--) //send 1移出8位
{
SH = 0;
_nop_();
_nop_();
DS = senddata&0X80;//将高8位bit7送出
senddata<<=1; //将bit6移到bit7
SH = 1;
_nop_();
_nop_();
SH = 0;
}
}
void LOCK_DATA_OUTPUT()
{
ST = 0; //送出锁存
_nop_();
_nop_();
ST = 1;
_nop_();
_nop_();
ST = 0;
}
void display(unsigned char disp)
{
unsigned char r;
unsigned char s;
unsigned char p;
unsigned char q;
unsigned char r2=0;
unsigned char s2=0;
unsigned char p2=0;
unsigned char q2=0;
p=disp/1000;
q=(disp%1000)/100;
r=(disp%100)/10;
s=disp%10;
//千位
p2=ANTI_SEG_CODE[p];
SEND_DATA_TO_595(p2); //段选
SEND_DATA_TO_595(0X08);//位选
LOCK_DATA_OUTPUT();
//百位
q2=ANTI_SEG_CODE[q];
SEND_DATA_TO_595(q2); //段选
SEND_DATA_TO_595(0X04);//位选
LOCK_DATA_OUTPUT();
//十位
r2=ANTI_SEG_CODE[r];
SEND_DATA_TO_595(r2); //段选
SEND_DATA_TO_595(0X02);//位选
LOCK_DATA_OUTPUT();
//个位
s2=ANTI_SEG_CODE[s];
SEND_DATA_TO_595(s2); //段选
SEND_DATA_TO_595(0X01);//位选
LOCK_DATA_OUTPUT();
}
void main()
{
unsigned char freq;
unsigned char freq1=1234;
unsigned char k=0,a;
JCON = 0; //湿度检测电路开关打开
TMOD = 0x15;
TL1 = 0xb0; //设置定时初值
TH1 = 0x3c; //设置定时初值
display(freq1);
while(1)
{
TL0=0x00;
TH0=0x00;
TR0=1;
TR1=1;
while(k<20)
{
if(TF1==1)
{
TL1 = 0xb0; //设置定时初值
TH1 = 0x3c; //设置定时初值
k++;
TF1=0;
display(freq1);
}
}
TR0=0;
TR1=0;
k=0;
a=TH0;
freq=a*256+TL0; //此为1秒钟测得的脉冲数,即为频率
display(freq);
}
} |