因为51单片机晶振的原因,测频的范围有限制,这时就需要外置74LS160分频器,进行分频。
本程序采用了定时器的方式2定时,8位自动重装计数器,精准定时。
单片机源程序:
#include #include #define Duan P2 #define Wei P1 sbit mr = P3^0; unsigned char code smg_duan[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char code smg_wei[] ={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; unsigned int Num; long int pin; long int count; void delay_ms(int i) { int j; for(i;i>0;i--) for(j=100;j>0;j--); } void display(long int num) { long num1[8],i; num1[7] = num/10000000; num1[6] = (num/1000000)%10; num1[5] = (num/100000)%10; num1[4] = (num/10000)%10; num1[3] = (num/1000)%10; num1[2] = (num/100)%10; num1[1] = (num/10)%10; num1[0] = num%10; for(i=0;i<8;i++){ Wei = smg_wei[i]; Duan = smg_duan[num1[i]]; delay_ms(1); } } void main() { TMOD = 0x72; TH0 = 0x06; TL0 = 0x06; TH1 = 0x00; TL1 = 0x00; ET1 = 1; ET0 = 1; EA = 1; //PT1=1; TCON = 0x50; while(1) { display(pin); } } void timer0() interrupt 1 //定时器 { unsigned char a; if(Num==3999) { TCON = 0x00; EA=0; Num = 0; a=P0&0x0f; //分频器读数 pin = (256*count+TL1)*10+a; //溢出的次数*256+还未溢出的次数+分频器的计数 TH1 = 0x00; TL1 = 0x00; TL0 = 0x06; TH0 = 0x06; count = 0; mr=0; _nop_(); EA=1; TCON = 0x50; mr=1; } else Num++; //display(pin); } void timer1() interrupt 3 //计数器 { count++; 中断溢出的次数 }
上一篇:单片机32位心形流水灯课程设计
下一篇:基于AT89c51的16×16点阵设计