源文件,传送门~
风格:《“蓝桥杯”全国软件和信息技术专业人才大赛实训指导书》代码风格
#include"init.h"
#include"ds1302.h"
#include"onewire.h"
uchar Timer[]={55,59,23};
uchar state=0,led,flag_T=23; //温度参数
uchar key_dly,seg_dly;
uchar key_old,key_val,key_down,key_long;
uchar seg_buf[9],seg_cod[9],pos;
uint ms,num;
float T; //读取温度
bit flag_work=0,flag_long=0,flag_ms=0,flag_relay=0;
//标志:时间温度工作模式、S17按下、整点时间到、继电器打卡
bit first=1,second=1;
void Initds1302();
void Readds1302();
void Seg_proc();
void Key_proc();
void Led_proc();
//=============================================
void main()
{
InitSys();
Timer1Init();
Initds1302();
while(1)
{
Seg_proc();
Key_proc();
Led_proc();
}
}
//=============================================
void Timer1() interrupt 3
{
ms++;
if(++key_dly == 10) key_dly=0;
if(++seg_dly == 500) seg_dly=0;
if(!(ms%100)) num++;
if((num & 1) && flag_relay)
led &= ~0x04;
Led_disp(led);
Seg_disp(seg_cod ,pos);
if(++pos == 8) pos=0;
}
void Seg_proc()
{
if(seg_dly) return;
seg_dly=1; //减速程序
T=ReadT()/16.0; //读温度
Readds1302(); //读时钟
if((Timer[0]==0) && (Timer[1]==0)) flag_ms=1;
if((Timer[0]==5) && (Timer[1]==0)) flag_ms=0;
switch(state)
{
case 0:sprintf(seg_buf,"U%1u %3.1f",(uint)state+1,(float)T);break;//读取温度
case 1:
if(flag_long)
sprintf(seg_buf,"U%1u %02u-%02u",(uint)state+1,(uint)Timer[1],(uint)Timer[0]);
else
sprintf(seg_buf,"U%1u %02u-%02u",(uint)state+1,(uint)Timer[2],(uint)Timer[1]);
break;
case 2:sprintf(seg_buf,"U%1u%6u",(uint)state+1,(uint)flag_T);break;//温度参数设定
}
Seg_tran(seg_buf,seg_cod);
}
void Key_proc()
{
if(key_dly) return;
key_dly=1;
key_val=KeyRead();
key_down=key_val & (key_val ^ key_old); //短按有效
key_long=key_val & key_old; //长按有效
key_old=key_val;
if(state == 1)
{
if(key_long == 17) flag_long=1;
else flag_long=0;
}
switch(key_down)
{
case 12:if(++state ==3) state=0;break;
case 13:flag_work ^=1;break;
case 16:if(state == 2) if(flag_T < 99) flag_T++;break;
case 17:if(state == 2) if(flag_T > 10) flag_T--;break;
}
}
void Led_proc()
{
if(flag_relay) led |= 0x04;
else led &=~0x04;
if(flag_work) led |= 0x02;
else led &=~0x02;
if(flag_ms) led |= 0x01;
else led &=~0x01;
if(flag_work == 0)
{
if((T > flag_T) && first) //只进入一次
{
Relay_disp(1);flag_relay=1;
first=0;second=1;
}
if((T <= flag_T) && second) //只进入一次
{
Relay_disp(0);flag_relay=0;
first=1;second=0;
}
}
else
{
if(flag_ms && first) //只进入一次
{
Relay_disp(1);flag_relay=1;
first=0;second=1;
}
if(!flag_ms && second) //只进入一次
{
Relay_disp(0);flag_relay=0;
first=1;second=0;
}
}
}
//=============================================
void Initds1302()
{
uchar temp;
Write_Ds1302_Byte(0x8e,0);
temp=((Timer[0]/10) << 4) + Timer[0]%10;
Write_Ds1302_Byte(0x80 , temp);
temp=((Timer[1]/10) << 4) + Timer[1]%10;
Write_Ds1302_Byte(0x82 , temp);
temp=((Timer[2]/10) << 4) + Timer[2]%10;
Write_Ds1302_Byte(0x84 , temp);
Write_Ds1302_Byte(0x8e,0x80);
}
void Readds1302()
{
uchar temp;
temp=Read_Ds1302_Byte(0x81);
Timer[0]=((temp >> 4)*10) + (temp & 0x0f);
temp=Read_Ds1302_Byte(0x83);
Timer[1]=((temp >> 4)*10) + (temp & 0x0f);
temp=Read_Ds1302_Byte(0x85);
Timer[2]=((temp >> 4)*10) + (temp & 0x0f);
}
#ifndef _INIT_H
#define _INIT_H
#include
#include
#define uchar unsigned char
#define uint unsigned int
void Timer1Init(void);
void InitHC138(uchar n);
void InitSys();
void Led_disp(uchar led);
void Relay_disp(bit flag);
void Seg_disp(uchar *seg,uchar pos);
void Seg_tran(uchar *buf,uchar *seg);
uchar KeyRead();
#endif
#include"init.h"
void Timer1Init(void) //1毫秒@12.000MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x18; //设置定时初始值
TH1 = 0xFC; //设置定时初始值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1=1;
EA=1;
}
void InitHC138(uchar n)
{
switch(n)
{
case 0:P2=P2 & 0x1f;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 InitSys()
{
P0=0xff;InitHC138(4);InitHC138(0);
P0=0x00;InitHC138(5);InitHC138(0);
P0=0x00;InitHC138(6);InitHC138(0);
}
void Led_disp(uchar led)
{
P0=~led;
InitHC138(4);
InitHC138(0);
}
void Relay_disp(bit flag)
{
if(flag) P0=0x10;
else P0=0x00;
InitHC138(5);
InitHC138(0);
}
void Seg_disp(uchar *seg,uchar pos)
{
P0=0xff; InitHC138(7);InitHC138(0);
P0=1 << pos;InitHC138(6);InitHC138(0);
P0=seg[pos];InitHC138(7);InitHC138(0);
}
void Seg_tran(uchar *buf,uchar *seg)
{
uchar i,j=0,temp;
for(i=0;i<8;i++,j++)
{
switch(buf[j])
{
case '0':temp=0xc0;break;
case '1':temp=0xf9;break;
case '2':temp=0xa4;break;
case '3':temp=0xb0;break;
case '4':temp=0x99;break;
case '5':temp=0x92;break;
case '6':temp=0x82;break;
case '7':temp=0xf8;break;
case '8':temp=0x80;break;
case '9':temp=0x90;break;
case ' ':temp=0xff;break;
case '-':temp=0xbf;break;
case 'U':temp=0xC1;break;
default:temp=0xff;
}
if(buf[j+1] == '.')
{
temp &= 0x7f;
j++;
}
seg[i]=temp;
}
}
uchar KeyRead()
{
unsigned int key;
uchar val;
P44=0; P42=P35=P34=1;key=(P3 & 0x0f);
P44=1; P42=0;key=(key << 4)|(P3 & 0x0f);
P42=1; P35=0;key=(key << 4)|(P3 & 0x0f);
P35=1; P34=0;key=(key << 4)|(P3 & 0x0f);
switch(~key)
{
case 0x8000:val= 4;break;
case 0x4000:val= 5;break;
case 0x2000:val= 6;break;
case 0x1000:val= 7;break;
case 0x0800:val= 8;break;
case 0x0400:val= 9;break;
case 0x0200:val=10;break;
case 0x0100:val=11;break;
case 0x0080:val=12;break;
case 0x0040:val=13;break;
case 0x0020:val=14;break;
case 0x0010:val=15;break;
case 0x0008:val=16;break;
case 0x0004:val=17;break;
case 0x0002:val=18;break;
case 0x0001:val=19;break;
default:val=0;
}
return val;
}
#ifndef __DS1302_H
#define __DS1302_H
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
#endif
#include
#include
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
return (temp);
}
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
unsigned int ReadT(void);
#endif
#include "reg52.h"
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
t *=12;
while(t--);
}
//通过单总线向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;
}
unsigned int ReadT(void)
{
unsigned char low,high;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
return (high << 8)+low;
}