#include <reg52.h>
#include "LCD1602.h"
unsigned char welcome[]={"welcome!"};
unsigned char Show0[6]={"Noise:"};
unsigned char Show1[5]={"dB"};
/*unsigned int code Countnum[60]={ 11, 13, 15, 17, 19, 22, 24, 27, 31, 34, //T0计数值
39, 44, 49, 55, 62, 69, 78, 87, 98, 110,
123, 138, 155, 174, 195, 219, 246, 276, 309, 347,
390, 438, 491, 550, 618, 693, 778, 873, 979, 1099,
1233,1383,1552,1742,1954,2193,2460,2760,3097,3475,
3899,4375,4909,5508,6180,6934,7780,8729,9794,10989,};*/
unsigned long code Countnum[100]={10,11,12,13,15,17,19,22,25,28,
31,35,39,43,48,54,60,67,75,84,
94,105,117,131,146,163,182,204,228,255,
286,320,359,402,451,506,567,636,713,799,
896,1005,1127,1264,1418,1590,1783,2000,2244,2517,
2824,3168,3556,3989,4475,5020,5632,6319,7089,7953,
8923,10011,11232,12602,14139,15863,17798,19969,22405,25138,
28204,31644,35504,39835,44694,50146,56263,63127,70828,79469,
89164,100042,112247,125941,141305,158544,177886,199588,223937,251257,
281910,316303,354891,398187,446765,501270,562424,631039,708025,794404,};
unsigned char code dB[100][3]={"031","032","033","034","035","036","037","038","039","040", //T0计数值对应 dB
"041","042","043","044","045","046","047","048","049","050",
"051","052","053","054","055","056","057","058","059","060",
"061","062","063","064","065","066","067","068","069","070",
"071","072","073","074","075","076","077","078","079","080",
"081","082","083","084","085","086","087","088","089","090",
"091","092","093","094","095","096","097","098","099","100",
"101","102","103","104","105","106","107","108","109","110",
"111","112","113","114","115","116","117","118","119","120",
"121","122","123","124","125","126","127","128","129","130",
};
sbit beep=P2^2;
sbit key_1=P2^1;
sbit key_2=P2^0;
//////////////////////////////////////////////////////全局变量/////////////////////////////////////////////////////
unsigned long count,c;
uchar t,z;
uchar shu,ys;
//////////////////////////////////////////////////////函数声明////////////////////////////////////////////////////
/************************************
延时子程序
延时时间(xms*1)ms
*************************************/
void delayms(uint xms) //延时xms
{
uint x,y;
for(x=xms;x>0;x--)
for(y=90;y>0;y--);
}
void key()
{
if (key_1==0)
{
delayms(10);
if (key_1==0)
{
while(key_1==0);
if (shu<99) shu++;
}
}
if (key_2==0)
{
delayms(10);
if (key_2==0)
{
while(key_2==0);
if (shu>0) shu--;
}
}
}
void alarm()
{
ys++;
if (ys>20)
{
ys=0;
beep=!beep;
}
}
//////////////////////////////////////////主函数///////////////////////////////////////////////////////////
void main(void)
{
uint m,n;
uint i;
delayms(1000);
TMOD=0X15; //T0外部计数 工作方式1 T1定时方式1
EA=0; //开总中断
ET1=0; //关闭T1
TH0=0;
TL0=0;
TH1=(65536-50000)/256; //裝初值
TL1=(65536-50000)%256; //裝初值
init_1602();
WRITE_LCD1602_COM(0x80+3); // 显示地址
for(i=0; i<6; i++)
{
WRITE_LCD1602_DAT(Show0[i]);
delayms(5);
}
WRITE_LCD1602_COM(0x80+12); // 显示地址
for(i=0;i<2;i++)
{
WRITE_LCD1602_DAT(Show1[i]);
delayms(5);
}
WRITE_LCD1602_COM(0xc0+3); // 显示地址
WRITE_LCD1602_byte("Limit:");
WRITE_LCD1602_COM(0xc0+12); // 显示地址
for(i=0;i<2;i++)
{
WRITE_LCD1602_DAT(Show1[i]);
delayms(5);
}
EA=1;
ET1=1; //开T1
TR1=1; //T1开始定时
TR0=1; //T0开始计数
while(1)
{
key();
WRITE_LCD1602_COM(0xc0+9);
WRITE_LCD1602_DAT(0x30+(shu+31)/100%10);
WRITE_LCD1602_DAT(0x30+(shu+31)/10%10);
WRITE_LCD1602_DAT(0x30+(shu+31)%10);
i=0;
if(count==0)
continue;
while(count>Countnum[i])
{
i++;
}
m=i;
i=0;
WRITE_LCD1602_COM(0x80+9);
for(n=0; n<3; n++)
{
WRITE_LCD1602_DAT(dB[m][n]);
delayms(5);
}
WRITE_LCD1602_COM(0xc0+9);
WRITE_LCD1602_DAT(0x30+(shu+31)/100%10);
WRITE_LCD1602_DAT(0x30+(shu+31)/10%10);
WRITE_LCD1602_DAT(0x30+(shu+31)%10);
if(m>=shu)
{
alarm();
}else
{
beep=1;
}
}
}
void Timer0() interrupt 1
{
z++;
}
void Timer1() interrupt 3
{
TR1=0; //关闭T1定时
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
t++;
if(t==20) //定时1s时间到
{
t=0;
TR0=0; //T0停止计数
c=z*65535+(TH0*256+TL0); // 将计数值读取
TH0=0; //清空计数值
TL0=0; //清空计数值
TR0=1; //T0开始计数
count=c;
c=0;
z=0;
}
TR1=1; //T1开始定时
}
|