#include <STC15F2K60S2.H>
#include <ONEWIRE.H>
#include <IIC.H>
sbit s7=P3^0;
sbit s6=P3^1;
sbit s5=P3^2;
sbit s4=P3^3;
sbit l1=P0^0;
sbit l2=P0^1;
sbit l3=P0^2;
sbit l8=P0^7;
unsigned char table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned int ad=0; //rb2电压值
unsigned int ad_old;
unsigned int f=0; //频率值
unsigned int f_old_h;
unsigned int f_old_l;
unsigned int f_old_d;
unsigned int f_h;
unsigned int f_l;
unsigned int f_d;
unsigned int temp=0; //温度值
unsigned int temp_old_h;
unsigned int temp_old_l;
unsigned int temp_h;
unsigned int temp_l;
unsigned int value=10; //电压阈值
unsigned int count=0;// 1秒 频率计数
unsigned int count_f=0;
unsigned int count_e=0; //0.8秒
unsigned int count_c=0; //0.5秒
unsigned int count_l=0; //0.2秒
unsigned char flag_led=0; //闪烁标志位
unsigned char flag_show=0;// 0电压 1频率 2温度
unsigned char flag_value=0; //1为设置阈值
unsigned char flag_last=0; //满0.8s置1
unsigned char flag_l=0;
unsigned char flag=0; //0数据显示 1回显
/*===========================================
数码管 延时函数 锁存器选择函数
===========================================*/
void delay(int n)
{
while(n--);
}
void delay_ms(int n)
{
int i,j;
for(i=n;i>0;i--)
for(j=110;j>0;j--);
}
void select(unsigned char n)
{
switch(n)
{
case 0:P2=(P2&0x1f)|0x00;break;
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;
}
}
void show_bit(unsigned char w,unsigned char n)
{
select(7);
P0=0xff;
select(6);
P0=0x01<<w-1;
select(7);
P0=n;
delay_ms(1);
}
void show_all(unsigned char n)
{
select(6);
P0=0xff;
select(7);
P0=n;
delay_ms(1);
}
/*===========================================
24c02存储
===========================================*/
void write_24c02(unsigned char adds,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(adds);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
unsigned char read_24c02(unsigned char adds)
{
unsigned char d;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(adds);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
d=IIC_RecByte();
IIC_Ack(0);
IIC_Stop();
return d;
}
/*===========================================
读取电压 Rb2
===========================================*/
void read_ad()
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
delay_ms(2);
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
ad=IIC_RecByte();
IIC_Ack(0);
IIC_Stop();
delay_ms(2);
}
/*===========================================
NE555 频率测量 定时器
===========================================*/
void init_timer()
{
//定时器0 用作计数
TH0=0xff;
TL0=0xff;
//定时器1定时10ms 取出1s的频率
TH1=(65536-10000)/256;
TL1=(65536-10000)%256;
TMOD=0x16;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
EA=1;
}
void service_timer0() interrupt 1
{
count_f++;
}
void service_timer1() interrupt 3
{
count_c++; //0.06秒
count_l++;//0.2秒
count_e++; //0.8秒
count++; //1秒
if(count_c==6)
{
flag_l=1;
count_c=0;
}
if(count_l==20) //满0.2s
{
flag_led=1;
count_l=0;
}
if(count_e==80) //满0.8s
{
flag_last=1;
count_e=0;
}
if(count==100) //计数满1s
{
f=count_f;
count_f=0;
count=0;
}
}
/*===========================================
温度采集
===========================================*/
void read_temp()
{
unsigned char LSB;
unsigned char MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
delay_ms(1);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB=Read_DS18B20();
MSB=Read_DS18B20();
temp=(MSB<<8)|LSB;
temp=temp*0.0625*100;
}
/*===========================================
LED灯
===========================================*/
void led()
{
select(4);
if(flag_show==2&&flag==0)
{
l1=0;
}
if(flag_show==1&&flag==0)
{
l2=0;
}
if(flag_show==0&&flag==0)
{
l3=0;
}
if(ad>value*10)
{
if(flag_led==1)
{
l8=0;
flag_led=0;
}
}
}
/*===========================================
数据显示页面
===========================================*/
void show_u()
{
if(flag==0)
{
show_bit(1,0xc1);
show_bit(7,table[ad/100]&0x7f);
show_bit(8,table[ad/10%10]);
show_all(0xff);
}
if(flag==1)
{
ad_old=read_24c02(0x01);
show_bit(1,0x89);
show_bit(2,0xc1);
show_bit(7,table[ad_old/100]&0x7f);
show_bit(8,table[ad_old/10%10]);
show_all(0xff);
}
}
void show_f()
{
if(flag==0)
{
show_bit(1,0x8e);
if(f>99999)
{
show_bit(3,table[f/100000]);
}
if(f>9999)
{
show_bit(4,table[f/10000%10]);
}
if(f>999)
{
show_bit(5,table[f/1000%10]);
}
if(f>99)
{
show_bit(6,table[f/100%10]);
}
if(f>9)
{
show_bit(7,table[f/10%10]);
}
show_bit(8,table[f/1%10]);
}
else if(flag==1)
{
f_old_h=read_24c02(0x02);
delay_ms(10);
f_old_l=read_24c02(0x03);
delay_ms(10);
f_old_d=read_24c02(0x04);
show_bit(1,0x89);
show_bit(2,0x8e);
show_bit(4,table[f_old_h/10]);
show_bit(5,table[f_old_h%10]);
show_bit(6,table[f_old_l/10]);
show_bit(7,table[f_old_l%10]);
show_bit(8,table[f_old_l/10]);
}
show_all(0xff);
}
void show_c()
{
unsigned int temp1;
if(flag==0)
{
temp1=temp;
show_bit(1,0xc6);
}
else if(flag==1)
{
temp_old_h=read_24c02(0x05);
delay_ms(10);
temp_old_l=read_24c02(0x06);
delay_ms(10);
temp1=temp_old_h*100+temp_old_l;
show_bit(1,0x89);
show_bit(2,0xc6);
}
show_bit(5,table[temp1/1000]);
show_bit(6,table[temp1/100%10]&0x7f);
show_bit(7,table[temp1/10%10]);
show_bit(8,table[temp1%10]);
show_all(0xff);
}
void show_value()
{
show_bit(1,0x8c);
show_bit(7,table[value/10]&0x7f);
show_bit(8,table[value%10]);
show_all(0xff);
}
void show_data()
{
read_temp();
read_ad();
if(flag_value==0)
{
TR1=1;
TR0=1;
switch(flag_show)
{
case 0:show_u();break;
case 1:show_f();break;
case 2:show_c();break;
}
}
else if(flag_value==1)
{
show_value();
}
}
/*===========================================
按键扫描
===========================================*/
void key()
{
if(s4==0)
{
delay(500);
if(s4==0)
{
flag_show++;
if(flag_show>2)
{
flag_show=0;
}
while(s4==0)
{
led();
show_data();
}
}
}
if(s5==0)
{
delay(500);
if(s5==0)
{
//存电压
write_24c02(0x01,ad);delay_ms(100);
//存频率 拆分开存
f_h=f/1000;
f_l=f%1000/10;
f_d=f%10*10;
write_24c02(0x02,f_h);delay_ms(100);
write_24c02(0x03,f_l);delay_ms(100);
write_24c02(0x04,f_d);delay_ms(100);
//存温度
temp_h=temp/100;
temp_l=temp%100;
write_24c02(0x05,temp_h);delay_ms(100);
write_24c02(0x06,temp_l);delay_ms(100);
while(s5==0)
{
led();
}
}
}
if(s6==0)
{
delay(500);
if(s6==0)
{
if(flag_value==0)
{
flag++;
if(flag>1)
{
flag=0;
}
while(s6==0)
{
led();
show_data();
}
}
else if(flag_value==1)
{
TR1=0;
TR0=0;
value=value+1;
while(s6==0)
{
TR1=1;
TR0=1;
if(flag_last==1)
{
if(flag_l==1)
{
value=value+1;
flag_l=0;
}
if(value>50)
{
value=1;
}
}
led();
show_value();
}
flag_last=0;
TR1=0;
TR0=0;
if(value>50)
{
value=1;
}
}
}
}
if(s7==0)
{
delay(500);
if(s7==0)
{
flag_value++;
if(flag_value>1)
{
flag_value=0;
}
while(s7==0)
{
led();
}
}
}
}
/*===========================================
主函数
===========================================*/
void init_system()
{
select(4);
P0=0xff;
show_all(0xff);
select(5);
P0=0x00;
select(0);
}
void main()
{
init_system();
init_timer();
while(1)
{
key();
led();
show_data();
}
}