- #include <reg52.h>
- #include <absacc.h>
- #include <intrins.h>
- unsigned int ms;
- unsigned int fre;//频率值
- // 0 1 2 3 4 5 6 7 8 9 全灭 -
- unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xFF,0xbf};
- unsigned char dspbuf[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};//显示缓冲区,初始值0xff确保启动时都不亮
- unsigned char dspcom=0;
- void display();
- void cls_buzz();
- void disp_data();
- void main()
- {
- cls_buzz();
- TMOD=0x51;//设置Tn为模式1,配置定时计数器
- TL0=0x18;//(65536-1000)%256(10进制转16进制);//1ms定时
- TH0=0xfc;//(65536-1000)/256(10进制转16进制);//赋初始值0xfc18
- TH1=0;
- TL1=0;
- ET0=1;//定时器0中断使能
- EA=1;//IE--中断使能寄存器 EA总中断使能位
- TR0=1;//启动T0
- TR1=1;
- while(1)
- {
- disp_data();
- display();
- }
- }
- void T0_Int() interrupt 1 //定时器0中断服务函数
- {
- TL0=0x18;//(65536-1000)%256(10进制转16进制);//1ms定时
- TH0=0xfc;//(65536-1000)/256(10进制转16进制);//赋初始值0xfc18
- if(++ms==1000)
- {
- ms=0;
- TR1=0;
- fre=TH1;
- fre=fre<<8;
- fre=fre+TL1;
- TH1=0;
- TL1=0;
- TR1=1;
- }
- }
- void disp_data()
- {
- (fre>=1000)?(dspbuf[4]=fre%1000/1000):(dspbuf[4]=0xff);
- (fre>=100)?(dspbuf[5]=fre%1000/100):(dspbuf[5]=0xff);
- (fre>=10)?(dspbuf[6]=fre%100/10):(dspbuf[6]=0xff);
- dspbuf[7]=fre%10;
- }
- void display()//显示函数
- {
- P0=0Xff;
- P2=((P2&0X1f)|0XE0);//Y7数码管上LED灯选通
- P2&=0X1F;
-
- P0=1<<dspcom;
- P2=((P2&0X1f)|0XC0);//Y6com口选通
- P2&=0X1f;
-
- P0=tab[dspbuf[dspcom]];
- P2=((P2&0X1f)|0XE0);
- P2&=0X1f;
-
- if(++dspcom==8)
- {
- dspcom=0;
- }
- }
- void cls_buzz()//
- {
- P0=0X00;
- P2=P2&0X1F|0XA0;//Y5
- P2&=0X1F;
- }
-
复制代码
|