📄 ad.c
字号:
#include<reg51.h>
#include"LCD.h"
#define uchar unsigned char
#define uint unsigned int
#define N 10
uchar bai,ge,shi,n;
uint temp,tp;temph,templ;
uint code ad_date[]={
1060,1060,1060,1100,1120,1140,1166,1180,1200,1220,
1240,1320,1340,1360,1380,1400,1420,1440,1460,1480,
1500,1520,1540,1560,1580,1600,1620,1640,1660,1680,
1700,1720,1740,1760,1780,1800,1820,1840,1840,1860,
1880,1900,1920,1940,1960,1980,2000,2020,2040,2060,
2080,2100,2120,2140,2160,2180,2200,2220,2240,2260,
2280,2300,2320,2340,2360,2380,2400,2420,2440,2460,
2480,2500,2520,2540,2560,2580,2600,2620,2640,2660,
2680,2700,2720,2740,2760,2780,2800,2820,2840,2860,
2880,2900,2920,2920,2940,2960,2980,3000,3020,3040,
3060,3080,3100,3120,3140,3160,3180,3200,3220,3240,
3260,3280,3300,3320,3340,3360,3380,3400,3420,3440,
3460,3480,3500,3520,3540,3560,3580,3600,3620,3640,
3660,3680};
uint code temperiture[]={
594,593,586,580,579,560,564,559,552,545,
541,518,513,508,503,497,491,488,483,480,
473,468,463,458,455,451,447,440,434,433,
430,425,420,416,413,409,404,401,401,396,
390,386,382,378,374,372,368,364,361,357,
354,351,347,343,340,336,333,328,326,322,
319,316,313,309,307,303,300,296,294,289,
286,283,280,276,274,271,267,264,260,257,
253,249,246,243,240,237,233,231,228,224,
222,219,214,216,211,209,206,202,198,194,
187,184,181,178,175,171,168,164,161,158,
154,152,148,146,142,139,136,133,130,126,
124,121,116,114,112,108,102,98,96,94,
89,86};
uchar code table[]=" T:00.0`C";
uchar num=0;
uchar keynum,max=40,min=0;
uchar code Temp_max[]=" Max:40`C",Temp_min[]=" Min:00`C";
sbit ad_wr=P3^6;
sbit ad_rd=P3^7;
sbit ad_cs=P3^5;
sbit ad_int=P3^2;
sbit key1=P0^5;
sbit key2=P0^6;
sbit key3=P0^7;
//sbit Beep=P0^0;
/*********函数表************/
void init(); //初始化
void ad_delay(uint t); //延时函数
void change(uchar tempture); //分离百位、十位、个位
uchar ad_getshuju(); //ad转换
char filter(); //滤波
uint convert(); //电压转换
void tempertur(); //度温度
void keyscan(); //键盘扫描
//主函数
void main()
{
init();
while(1)
{
keyscan();
}
}
void init()
{
LCD_init();
for(num=0;num<10;num++)
{
LCD_Write_add_data(num,0,table[num]);
}
}
void ad_delay(uint t)
{
uchar j;
uint i;
for(i=0;i<t;i++)
for(j=0;j<110;j++);
}
uchar ad_getshuju()
{
uchar date;
ad_cs=0;ad_wr=0;ad_rd=1;
ad_cs=1;ad_wr=1;ad_rd=1;
while(ad_int==1);
ad_cs=0;ad_wr=1;ad_rd=0;
date=P2;
ad_cs=1;ad_wr=1;ad_rd=1;
return date;
}
char filter() //滤波函数 中位值滤波法
{
uint value_buf[N],temp_f;
uchar count,i,j;
for ( count=0;count<N;count++)
{
value_buf[count] = ad_getshuju();
delay(2);
}
for (j=0;j<N-1;j++)
{
for (i=0;i<N-j;i++)
{
if ( value_buf[i]>value_buf[i+1] )
{
temp_f = value_buf[i];
value_buf[i] = value_buf[i+1];
value_buf[i+1] = temp_f;
}
}
}
return value_buf[(N-1)/2];
}
uint convert()
{
uint temph,templ; //用于存储读出数据的高字节和低字节
uint tmp="0"; //存储最后处理完的结果 注意数据类型
tmp=filter()+tmp; //求滤波后ad值
temph=tmp&0xf0; //屏蔽低四位
temph=temph>>4; //右移四位 取出高四位
templ=tmp&0x0f; //屏蔽高四位 取出低四位
tmp=templ*20+temph*320; //最后的结果是一个四位数,便于显示 电压值
return tmp; //返回最后处理结果
}
void change(uchar tempture)
{
bai=tempture/100;
shi=tempture%100/10;
ge=tempture%10;
}
void tempertur()
{
for(n=0;temp>=ad_date[n];n++);
tp=temperiture[n-1];
change(tp);
LCD_Write_add_data(4,0,0x30+bai);
LCD_Write_add_data(5,0,0x30+shi);
LCD_Write_add_data(7,0,0x30+ge);
}
void keyscan()
{
if(key1==0)
{
delay(5);
if(key1==0)
{
while(!key1);
keynum++;
if(keynum==1)
{
change(max);
LCD_Write_Com(0x01);
delay(10);
LCD_Write_add_str(0,0,10,Temp_max);
LCD_Write_add_data(6,0,0x30+shi);
LCD_Write_add_data(7,0,0x30+ge);
}
if(keynum==2)
{
change(min);
LCD_Write_Com(0x01);
min=min>0?min:0;
delay(10);
LCD_Write_add_str(0,0,10,Temp_min);
LCD_Write_add_data(6,0,0x30+shi);
LCD_Write_add_data(7,0,0x30+ge);
}
if(keynum==3)
{
keynum=0;
LCD_Write_Com(0x01);
delay(10);
LCD_Write_add_str(0,0,10,table);
}
}
}
if(keynum==0)
{
temp=convert();
tempertur();
delay(100);
}
else
{
if(keynum==1)
{
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
max++;
change(max);
delay(1);
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
max--;
change(max);
delay(1);
}
}
//LCD_Write_add_str(0,0,9,Temp_max);
LCD_Write_add_data(6,0,0x30+shi);
LCD_Write_add_data(7,0,0x30+ge);
}
if(keynum==2)
{
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
min++;
change(min);
min=min>0?min:0;
delay(1);
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
min--;
change(min);
delay(1);
}
}
//LCD_Write_add_str(0,0,9,Temp_min);
LCD_Write_add_data(6,0,0x30+shi);
LCD_Write_add_data(7,0,0x30+ge);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -