简易温度采集与控制装置

蓝桥杯第五届初赛
代码(已实现所有功能)
#include<stc15f2k60s2.h>
#include<intrins.h>
sbit DQ = P1^4; //单总线接口
#define uint unsigned int
#define uchar unsigned char
uchar tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar yi,er,san,si,wu,liu,qi,ba;
uchar keynum;
uchar wendu;
uchar tmax=30,tmin=20;
uchar t_flag;
bit shezhi=0;
bit shanchu=0,flag=0;
uint number1,number2,number3;
void allint();
void display1(uchar yi,uchar er);
void display2(uchar san,uchar si);
void display3(uchar wu,uchar liu);
void display4(uchar qi,uchar ba);
void Delayms(uint ms);
void keyscan();
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
uchar DS18B20_Read();
void Timer0Init(void);

void main()
{
allint();
Timer0Init();
yi=10;er=11;san=11;si=11;wu=11;liu=10;qi=11;ba=11;
while(1)
{
keyscan();
wendu=DS18B20_Read();
if(shezhi0)
{
if(wendu<tmin)
{
t_flag=0;
P2=0XA0;P0=0X00;
}
else if((wendu>=tmin)&&(wendu<=tmax))
{
t_flag=1;
P2=0XA0;P0=0X00;
}
else if(wendu>tmax)
{
t_flag=2;
P2=0XA0;P0=0X10;
}
yi=10;er=t_flag;san=10;si=11;wu=11;liu=11;qi=wendu/10;ba=wendu%10;
display1(yi,er);
display2(san,si);
display3(wu,liu);
display4(qi,ba);
}
else if(shezhi
1)
{
// yi=10;er=11;san=11;si=11;wu=11;liu=10;qi=11;ba=11;
if(shanchu1)
{
shanchu=0;
yi=10;er=11;san=11;si=11;wu=11;liu=10;qi=11;ba=11;
}
else if(shanchu
0)
{
if((er11)&&(flag1))
{
flag=0;
er=keynum;
}
if((san11)&&(flag1))
{
flag=0;
san=keynum;
}
if((qi11)&&(flag1))
{
flag=0;
qi=keynum;
}
if((ba11)&&(flag1))
{
flag=0;
ba=keynum;
}
display1(yi,er);
display2(san,si);
display3(wu,liu);
display4(qi,ba);
tmax=er10+san;tmin=qi10+ba;
if(tmin>tmax)
{
P2=0X80;P0=0XFD;
}
else if(tmin<=tmax)
{
P2=0X80;P0=0XFF;
}
}

	}

}

}
void allint()
{
P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;
P2=0XC0;P0=0XFF;P2=0XE0;P0=0XFF;
}
void display1(uchar yi,uchar er)
{
P2=0XC0;P0=0X01;P2=0XE0;P0=tab[yi];Delayms(1);
P2=0XC0;P0=0X02;P2=0XE0;P0=tab[er];Delayms(1);
}
void display2(uchar san,uchar si)
{
P2=0XC0;P0=0X04;P2=0XE0;P0=tab[san];Delayms(1);
P2=0XC0;P0=0X08;P2=0XE0;P0=tab[si];Delayms(1);
}
void display3(uchar wu,uchar liu)
{
P2=0XC0;P0=0X10;P2=0XE0;P0=tab[wu];Delayms(1);
P2=0XC0;P0=0X20;P2=0XE0;P0=tab[liu];Delayms(1);
}
void display4(uchar qi,uchar ba)
{
P2=0XC0;P0=0X40;P2=0XE0;P0=tab[qi];Delayms(1);
P2=0XC0;P0=0X80;P2=0XE0;P0=tab[ba];Delayms(1);
P2=0XC0;P0=0XFF;P2=0XE0;P0=0XFF;
}
void Delayms(uint ms) //@11.0592MHz
{
unsigned char i, j;
uint n;
for(n=0;n<ms;n++)
{
nop();
nop();
nop();
i = 11;
j = 190;
do
{
while (–j);
} while (–i);
}
}
void keyscan()
{
uchar temp,temp1,temp2;
P44=0;P42=1;P3=0X7F;
temp=P3&0X0F;
if(temp!=0X0F)
{
Delayms(5);
if(temp!=0X0F)
{
temp=P3;
switch(temp)
{
case 0x7e:keynum=0;flag=1;break;
case 0x7d:keynum=3;flag=1;break;
case 0x7b:keynum=6;flag=1;break;
case 0x77:keynum=9;flag=1;break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
P44=1;P42=0;P3=0xbF;
temp1=P3&0X0F;
if(temp1!=0X0F)
{
Delayms(5);
if(temp1!=0X0F)
{
temp1=P3;
switch(temp1)
{
case 0xbe:keynum=1;flag=1;break;
case 0xbd:keynum=4;flag=1;break;
case 0xbb:keynum=7;flag=1;break;
case 0xb7:
if(shezhi0)
{
shezhi=1;
EA=0;ET0=0;
P2=0X80;P0=0XFF;P2=0XA0;P0=0X00;
yi=10;er=11;san=11;si=11;wu=11;liu=10;qi=11;ba=11;
}
else if(shezhi
1)
{
shezhi=0;
EA=1;ET0=1;
}break;
}
while(temp1!=0x0f)
{
temp1=P3;
temp1=temp1&0x0f;
}
}
}
P44=1;P42=1;P3=0xdF;
temp2=P3&0X0F;
if(temp2!=0X0F)
{
Delayms(5);
if(temp2!=0X0F)
{
temp2=P3;
switch(temp2)
{
case 0xde:keynum=2;flag=1;break;
case 0xdd:keynum=5;flag=1;break;
case 0xdb:keynum=8;flag=1;break;
case 0xd7:shanchu=1;break;
}
while(temp2!=0x0f)
{
temp2=P3;
temp2=temp2&0x0f;
}
}
}
}
//单总线延时函数
void Delay_OneWire(unsigned int t)
{
unsigned char i;
while(t–){
for(i=0; i<8; i++);
}
}

//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}

//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;

for(i=0;i<8;i++)
{
	DQ = 0;
	dat >>= 1;
	DQ = 1;
	if(DQ)
	{
		dat |= 0x80;
	}	    
	Delay_OneWire(5);
}
return dat;

}

//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;

DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10); 
	initflag = DQ;     
Delay_OneWire(5);

return initflag;

}

uchar DS18B20_Read()
{
uchar low,high;
uchar temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);

init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
temp=low>>4;                                             //将小数部分的低四位移掉
temp=temp|(high<<4);                                      //将第二次采集的高八位的高四位移出去再与之前的temp相或
return temp;                                             

}

void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
}

void Timer0() interrupt 1
{
if(t_flag==0)
{
number1++;

	if(number1==800)
	{
		P2=0x80;
		P0=0XFE;
	}
	else if(number1==1600)
	{
		P2=0X80;
		number1=0;
		P0=0XFF;
	}
}
if(t_flag==1)
{
	number2++;

	if(number2==400)
	{	
		P2=0x80;
		P0=0XFE;
	}
	else if(number2==800)
	{
		number2=0;
		P2=0X80;
		P0=0XFF;
	}
}
if(t_flag==2)
{
	number3++;
	
	if(number3==200)
	{
		P2=0x80;
		P0=0XFE;
	}
	else if(number3==400)
	{
		P2=0X80;
		number3=0;
		P0=0XFF;
	}
}

}
本届程序是我发现很多学习时忽视的知识点,对我来说这届习题比上一届习题使我收获大。
注程序里面黄色部分是双等号但系统自动省略了。大家复制时注意一下。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值