#include "reg52.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 L4=P0^3;
unsigned char SMG_stat=0;//数码管状态显示,0表示数据界面,1表示参数界面
int TMAX=30;//温度参数上限
int TMIN=20;//温度参数下限
unsigned int temp_TMAX=30;
unsigned int temp_TMIN=20;
bit k5=0;//0调整下限,1调整上限
unsigned char code SMG_duanma[18]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
void selectHC573(unsigned char channel)
{
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;
}
}
void delay(unsigned int t)
{
while(t--);
}
void delay_key(unsigned char t)
{
while(t--);
}
//=====================温度提取===============================
unsigned int temper=0;
void DisSMG();
void Read_temper()
{
unsigned char LSB,MSB;
DisSMG();
init_ds18b20();
DisSMG();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
DisSMG();
init_ds18b20();
DisSMG();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB=Read_DS18B20();
MSB=Read_DS18B20();
DisSMG();
temper=MSB;
temper=(temper<<=8)|LSB;
temper=temper*0.0625;
DisSMG();
}
//===============================================================
//======================数码管显示===================================
void DisSMG_bit(unsigned char dat,unsigned char pos)
{
selectHC573(6);
P0=0x01<<pos;
selectHC573(7);
P0=dat;
delay(500);
selectHC573(0);
}
void DisSMG_all(unsigned char dat)
{
selectHC573(6);
P0=0xff;
selectHC573(7);
P0=dat;
delay(300);
selectHC573(0);
}
void DisSMG()
{
if(SMG_stat==0)
{
DisSMG_bit(0xc6,0);
DisSMG_bit(SMG_duanma[temper/10],6);
DisSMG_bit(SMG_duanma[temper%10],7);
}
if(SMG_stat==1)
{
DisSMG_bit(0x8c,0);
DisSMG_bit(SMG_duanma[TMAX/10],3);
DisSMG_bit(SMG_duanma[TMAX%10],4);
DisSMG_bit(SMG_duanma[TMIN/10],6);
DisSMG_bit(SMG_duanma[TMIN%10],7);
}
DisSMG_all(0xff);
}
//===============================================================
//======================纯DAC=================================
float DAC=0;
void Read_DAC(unsigned char dat)
{
I2CStart();
I2CSendByte(0x90);
I2CWaitAck();
I2CSendByte(0x43);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
void DAC_out()
{
if(temper>TMAX)
{
Read_DAC(4.0);
}
if(temper>=TMIN&&temper<=TMAX)
{
Read_DAC(3.0);
}
if(temper<TMIN)
{
Read_DAC(2.0);
}
}
//===============================================================
//=======================按键================================
void csan_key()
{
if(S4==0)
{
delay_key(20);
if(S4==0)
{
while(S4==0)
{
DisSMG();
}
if(SMG_stat==0)
{
SMG_stat=1;
k5=0;
temp_TMAX=TMAX;
temp_TMIN=TMIN;
}
else if(SMG_stat==1)//参数--温度,需要判断
{
if(TMAX>=TMIN)
{
SMG_stat=0;
}
else
{
SMG_stat=1;
TMAX=temp_TMAX;
TMIN=temp_TMIN;
}
}
}
}
if(S5==0)
{
delay_key(20);
if(S5==0)
{
while(S5==0)
{
DisSMG();
}
if(SMG_stat==1)
{
k5=~k5;
}
}
}
if(S6==0)
{
delay_key(20);
if(S6==0)
{
while(S6==0)
{
DisSMG();
}
if(SMG_stat==1)
{
if(k5==0)//调整下限
{
TMIN++;
if(TMIN>99)
{
TMIN=99;
}
}
else if (k5==1)//调整上限
{
TMAX++;
if(TMAX>99)
{
TMAX=99;
}
}
}
}
}
if(S7==0)
{
delay_key(20);
if(S7==0)
{
while(S7==0)
{
DisSMG();
}
if(SMG_stat==1)
{
if(k5==0)//调整下限
{
TMIN--;
if(TMIN<0)
{
TMIN=0;
}
}
else if (k5==1)//调整上限
{
TMAX--;
if(TMAX<0)
{
TMAX=0;
}
}
}
}
}
}
//===============================================================
//=======================LED===================================
void Running()
{
selectHC573(4);
if(temper>TMAX)
{
L1=0;
}
else
{
L1=1;
}
if(temper>=TMIN&&temper<=TMAX)
{
L2=0;
}
else
{
L2=1;
}
if(temper<TMIN)
{
L3=0;
}
else
{
L3=1;
}
if(TMAX<TMIN)
{
L4=0;
}
else
{
L4=1;
}
selectHC573(0);
}
//===============================================================
void Init_system()
{
selectHC573(5); //控制蜂鸣器与继电器
P0=0x00;
selectHC573(4); //控制LED灯
P0=0xff;
}
void main()
{
Init_system();
while(1)
{
Read_temper();
DisSMG();
csan_key();
Running();
DAC_out();
}
}
第十一届第二次蓝桥杯单片机组省赛程序代码
于 2024-03-10 14:51:26 首次发布