详细代码如下,所有功能都完美实现,如有看不懂的可加🐧983954083
#include <STC15F2K60S2.H>
#include "iic.h"
#include "ds1302.h"
#include "onewire.h"
#include "intrins.h"
#define TSMG 500
sbit h1 = P3^2;
sbit h2 = P3^3;
sbit s1 = P4^4;
sbit s2 = P4^2;
void read_mytemp();
void DisplaySMG_Info();
unsigned char code read_addre[7] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned char code write_addre[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char now_timer[7] = {0x05,0x03,0x13,0x04,0x04,0x04,0x24};
code unsigned char Seg_Table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82,0xf8,
0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86,0x8e };
code unsigned char nodot[] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned char adc_value = 0;
unsigned char t_h = 0;
unsigned char t_m = 0;
unsigned char t_s = 0;
float temp_value = 0;
unsigned int temp_smg = 0;
unsigned int count_f = 0;//测量的频率
unsigned char count_1s = 0;
float shidu = 0;
unsigned int shidu_smg = 0;
//unsigned char count_200ms = 0;
float aver_s = 0;//湿度平均值
float aver_temp = 0;//温度平均值
unsigned char UI = 0;//0-时间 1-回显 2-参数
unsigned char UI_huixian = 0;//0-温度回显 1-湿度回显 2-时间回显
unsigned char temp_param = 30;
unsigned int max_temp = 0;
unsigned int max_shidu = 0;
unsigned char num_chufa = 0;
unsigned char cf_h = 0;
unsigned char cf_m = 0;
unsigned char flag_caiji = 0;//触发采集标志
unsigned char count_3s = 0;
unsigned char UI_LS,UI_huixian_LS;//保存界面
unsigned char old_value;//上一个电压数据
unsigned char stat_dat = 0;//采集第一个数据标志
unsigned char stat_changan = 0;//是否长按
unsigned char count_CA = 0;//长按计数
unsigned char stat_led = 0xff;
unsigned char count_01s = 0;
unsigned char stat_warn = 0;//温度报警标志
unsigned char stat_shan = 0;//led灯闪的标志
float old_temp = 0;//上一个温度数据
float old_shidu = 0;//上一个湿度数据
void Delay20ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 1;
j = 234;
k = 113;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Init_timer0()
{
TMOD &= 0xf0;
TMOD |= 0x05;
TH0 = TL0 = 0;
TR0 = 1;
ET0 = 0;
}
void Init_timer1()//50
{
TMOD &= 0x0f;
TMOD |= 0x10;
TH1 = 0x3c;
TL1 = 0xb0;
TR1 = 1;
EA = 1;
ET1 = 1;
}
void sevice_timer1() interrupt 3
{
TH1 = 0x3c;
TL1 = 0xb0;
count_1s++;
if(count_1s == 20)
{
TR0 = 0;
count_1s = 0;
count_f = (TH0 << 8) | TL0;
TH0 = TL0 = 0;
TR0 = 1;
}
if(flag_caiji == 1)
{
count_3s++;
if(count_3s == 60)
{
count_3s = 0;
flag_caiji = 0;
UI = UI_LS;
UI_huixian = UI_huixian_LS;
}
}
if(stat_changan == 1)
{
count_CA++;
}
if(stat_warn == 1)
{
count_01s++;
if(count_01s == 2)
{
count_01s = 0;
if(stat_shan == 0)
{
stat_shan = 1;
}
else
{
stat_shan = 0;
}
}
}
}
void Init_mytemp()
{
unsigned char LSB,MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
do{
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
MSB = (MSB << 4) | (LSB >> 4);
}while(MSB == 85);
}
void read_mytemp()
{
static unsigned char n = 0;
unsigned char LSB,MSB;
unsigned int temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
temp = MSB << 8;
temp = temp | LSB;
if((temp & 0xf800) == 0x0000)
{
temp_value = temp * 0.0625;
if(max_temp < (int)temp_value)max_temp = (int)temp_value;
n++;
aver_temp = (((aver_temp *(n-1)) + temp_value)/n);
temp_smg = aver_temp * 10;
if(n == 10)n=0;
}
}
void Init_mytimer()
{
unsigned char i;
Write_Ds1302_Byte(0x8e,0x00);
for(i = 0;i < 7;i++)
{
Write_Ds1302_Byte(write_addre[i],now_timer[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
void read_mytimer()
{
t_h = Read_Ds1302_Byte(0x85);
t_m = Read_Ds1302_Byte(0x83);
t_s = Read_Ds1302_Byte(0x81);
}
void read_myvolt()
{
I2CStart();
I2CSendByte(0x90);
I2CWaitAck();
I2CSendByte(0x01);
I2CWaitAck();
I2CStop();
I2CStart();
I2CSendByte(0x91);
I2CWaitAck();
adc_value = I2CReceiveByte();
I2CSendAck(1);
I2CStop();
}
void SelectHC573(unsigned char channel,unsigned char dat)
{
P2 = (P2 & 0x1f) | 0x00;
P0 = dat;
switch(channel)
{
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
case 0:
P2 = (P2 & 0x1f) | 0x00;
break;
}
P2 = (P2 & 0x1f) | 0x00;
}
void DelaySMG(unsigned int t)
{
while(t--);
}
void DisplaySMG_Bit(unsigned char pos,unsigned char value)
{
SelectHC573(6,0x01 << pos);
SelectHC573(7,value);
DelaySMG(TSMG);
SelectHC573(6,0x01 << pos);
SelectHC573(7,0xff);
}
void DisplaySMG_All(unsigned char value)
{
SelectHC573(6,0xff);
SelectHC573(7,value);
}
void DisplaySMG_Info()
{
switch(UI)
{
case 0:
DisplaySMG_Bit(0,Seg_Table[t_h >> 4]);
DisplaySMG_Bit(1,Seg_Table[t_h & 0x0f]);
DisplaySMG_Bit(2,0xbf);
DisplaySMG_Bit(3,Seg_Table[t_m >> 4]);
DisplaySMG_Bit(4,Seg_Table[t_m & 0x0f]);
DisplaySMG_Bit(5,0xbf);
DisplaySMG_Bit(6,Seg_Table[(t_s >> 4) & 0x07]);
DisplaySMG_Bit(7,Seg_Table[t_s & 0x0f]);
break;
case 1:
switch(UI_huixian)
{
case 0:
DisplaySMG_Bit(0,0xc6);
if(num_chufa != 0)
{
DisplaySMG_Bit(2,Seg_Table[max_temp / 10]);
DisplaySMG_Bit(3,Seg_Table[max_temp % 10]);
DisplaySMG_Bit(4,0xbf);
DisplaySMG_Bit(5,Seg_Table[temp_smg / 100]);
DisplaySMG_Bit(6,nodot[temp_smg / 10 % 10]);
DisplaySMG_Bit(7,Seg_Table[temp_smg % 10]);
}
break;
case 1:
DisplaySMG_Bit(0,0x89);
if(num_chufa != 0)
{
DisplaySMG_Bit(2,Seg_Table[max_shidu / 10]);
DisplaySMG_Bit(3,Seg_Table[max_shidu % 10]);
DisplaySMG_Bit(4,0xbf);
DisplaySMG_Bit(5,Seg_Table[shidu_smg / 100]);
DisplaySMG_Bit(6,nodot[shidu_smg / 10 % 10]);
DisplaySMG_Bit(7,Seg_Table[shidu_smg % 10]);
}
break;
case 2:
DisplaySMG_Bit(0,0x8e);
DisplaySMG_Bit(1,Seg_Table[num_chufa / 10]);
DisplaySMG_Bit(2,Seg_Table[num_chufa % 10]);
if(num_chufa != 0)
{
DisplaySMG_Bit(3,Seg_Table[cf_h >> 4]);
DisplaySMG_Bit(4,Seg_Table[cf_h & 0x0f]);
DisplaySMG_Bit(5,0xbf);
DisplaySMG_Bit(6,Seg_Table[cf_m >> 4]);
DisplaySMG_Bit(7,Seg_Table[cf_m & 0x0f]);
}
break;
}
break;
case 2:
DisplaySMG_Bit(0,0x8c);
DisplaySMG_Bit(6,Seg_Table[temp_param / 10]);
DisplaySMG_Bit(7,Seg_Table[temp_param % 10]);
break;
case 3:
DisplaySMG_Bit(0,0x86);
DisplaySMG_Bit(3,Seg_Table[temp_smg / 100]);
DisplaySMG_Bit(4,Seg_Table[temp_smg / 10 % 10]);
DisplaySMG_Bit(5,0xbf);
if((count_f < 200) || (count_f > 2000))
{
DisplaySMG_Bit(6,0x88);
DisplaySMG_Bit(7,0x88);
}
else
{
DisplaySMG_Bit(6,Seg_Table[shidu_smg / 100]);
DisplaySMG_Bit(7,Seg_Table[shidu_smg / 10 % 10]);
}
break;
}
}
void average_data()
{
static unsigned char i = 0;
if(count_f < 200)
{
shidu = 10;
i++;
}
else if(count_f < 2000)
{
shidu = (80.0/1800)*count_f + 10.0/9;
i++;
}
else
{
shidu = 90;
i++;
}
if(max_shidu < shidu) max_shidu = shidu;
aver_s = (((aver_s *(i-1)) + shidu)/i);
shidu_smg = aver_s*10;
if(i == 10)i = 0;
}
void caiji_chufa()
{
if(flag_caiji == 0)
{
if(stat_dat == 0)
{
stat_dat = 1;
old_value = adc_value;
old_temp = temp_value;
old_shidu = shidu;
}
if((old_value > 20) && (adc_value < 20))
{
flag_caiji = 1;
num_chufa++;
UI_LS = UI;
UI_huixian_LS = UI_huixian;
UI = 3;
cf_h = t_h;
cf_m = t_m;
}
old_value = adc_value;
old_temp = temp_value;
old_shidu = shidu;
}
if(stat_changan == 2)
{
if(count_CA > 40)
{
num_chufa = 0;
cf_h = 0;
cf_m = 0;
count_CA = 0;
stat_changan = 0;
}
}
}
void Scan_key()
{
s1 = 0;
s2 = h1 = h2 = 1;
if(h1 == 0)//s5
{
Delay20ms();
if(h1 == 0)
{
if(UI == 1)
{
if(UI_huixian == 0)
{
UI_huixian = 1;
}
else if(UI_huixian == 1)
{
UI_huixian = 2;
}
else
{
UI_huixian = 0;
}
}
while(h1 == 0)
{
DisplaySMG_Info();
}
}
}
else if(h2 == 0)//s4
{
Delay20ms();
if(h2 == 0)
{
if(UI == 0)
{
UI = 1;
}
else if(UI == 1)
{
UI = 2;
}
else
{
UI = 0;
}
while(h2 == 0)
{
DisplaySMG_Info();
}
}
}
s2 = 0;
s1 = h1 = h2 = 1;
if(h1 == 0)//s9
{
Delay20ms();
if(h1 == 0)
{
if(UI == 2)
{
if(temp_param == 0)
{
temp_param = 99;
}
else
{
temp_param -= 1;
}
}
if(UI_huixian == 2)
{
stat_changan = 1;
}
while(h1 == 0)
{
DisplaySMG_Info();
}
stat_changan = 2;
}
}
else if(h2 == 0)//s8
{
Delay20ms();
if(h2 == 0)
{
if(UI == 2)
{
if(temp_param == 99)
{
temp_param = 0;
}
else
{
temp_param += 1;
}
}
while(h2 == 0)
{
DisplaySMG_Info();
}
}
}
}
void led_conrlor()
{
if(UI == 0)
{
stat_led = 0xfe;
SelectHC573(4,stat_led);
}
if(UI == 1)
{
stat_led = 0xfd;
SelectHC573(4,stat_led);
}
if(UI == 3)
{
stat_led = 0xfb;
SelectHC573(4,stat_led);
}
if((int)temp_value > temp_param)
{
stat_warn = 1;
if(stat_shan == 0)
{
stat_led &= 0xf7;
SelectHC573(4,stat_led);
}
else
{
stat_led |= 0x08;
SelectHC573(4,stat_led);
}
}
else
{
stat_warn = 0;
stat_led |= 0x08;
SelectHC573(4,stat_led);
}
if((count_f < 200) || (count_f > 2000))
{
stat_led &= 0xef;
SelectHC573(4,stat_led);
}
else
{
stat_led |= 0x10;
SelectHC573(4,stat_led);
}
if((num_chufa >= 2) && (temp_value > old_temp) && (shidu > old_shidu))
{
stat_led &= 0xdf;
SelectHC573(4,stat_led);
}
else
{
stat_led |= 0x20;
SelectHC573(4,stat_led);
}
}
void main()
{
Init_timer1();
Init_mytemp();
Init_mytimer();
Init_timer0();
SelectHC573(4,0xff);
SelectHC573(5,0x00);
DisplaySMG_All(0xff);
while(1)
{
DisplaySMG_Info();
read_myvolt();
read_mytimer();
read_mytemp();
average_data();
if(UI != 3)Scan_key();
caiji_chufa();
led_conrlor();
}
}