练习的时候写的,有什么错误希望大家多多指正,话不多说直接上代码。
#include <STC15F2K60S2.H>
#define Y4 P2=(P2&0x1f)|0x80
#define Y5 P2=(P2&0x1f)|0xa0
#define Y6 P2=(P2&0x1f)|0xc0
#define Y7 P2=(P2&0x1f)|0xe0
#define uchar unsigned char
#define uint unsigned int
uchar code t_display[]={ //标准字库
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black - H J K L N o P U t G Q r M y
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
uchar code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //位码
uchar n1=16,n2=16,n3=16,n4=16,n5=16,n6=16,n7=16,n8=16;
uchar key_num=0,Tmax=30,Tmin=20,wendu_cs=0;
uint wendu=0,T_count=0;
bit key_cp=0,cs_stat=0;
void Led_gn();
void Key_gn();
void Show_gn();
float Ds28B20_get();
void Init()
{
Y5;P0=0x00;
Y4;P0=0xff;
}
void Smg_show(uchar n1,n2,n3,n4,n5,n6,n7,n8)
{
static uchar i=0;
i++;
if(i==9)i=1;
Y7;P0=0xff;
switch(i)
{
case 1:
Y6;P0=T_COM[0];
Y7;P0=~t_display[n1];
break;
case 2:
Y6;P0=T_COM[1];
Y7;P0=~t_display[n2];
break;
case 3:
Y6;P0=T_COM[2];
Y7;P0=~t_display[n3];
break;
case 4:
Y6;P0=T_COM[3];
Y7;P0=~t_display[n4];
break;
case 5:
Y6;P0=T_COM[4];
Y7;P0=~t_display[n5];
break;
case 6:
Y6;P0=T_COM[5];
Y7;P0=~t_display[n6];
break;
case 7:
Y6;P0=T_COM[6];
Y7;P0=~t_display[n7];
break;
case 8:
Y6;P0=T_COM[7];
Y7;P0=~t_display[n8];
break;
}
}
void Timer0Init(void) //2毫秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x40; //设置定时初值
TH0 = 0xA2; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
}
void Timer0()interrupt 1
{
if(wendu_cs==1||wendu_cs==2)
{
T_count++;
T_count%=500;
}
Key_gn();
Smg_show(n1,n2,n3,n4,n5,n6,n7,n8);
}
void Delay10ms() //@12.000MHz
{
unsigned char i, j;
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}
void Key_scanf()
{
if(P30==0)
{
Delay10ms();
if(P30==0)
{
key_num=7;
}while(P30==0);
}
if(P31==0)
{
Delay10ms();
if(P31==0)
{
key_num=6;
}while(P31==0);
}
if(P32==0)
{
Delay10ms();
if(P32==0)
{
key_num=5;
}while(P32==0);
}
if(P33==0)
{
Delay10ms();
if(P33==0)
{
key_num=4;
}while(P33==0);
}
}
/*
程序说明: 单总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
日 期: 2011-8-9
*/
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
uchar i;
while(t--)
{
for(i=0;i<12;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;
}
float Ds28B20_get()
{
uchar high,low;
uint dat;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(400);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
dat=(high&0x0f);
dat<<=8;
dat|=low;
return dat*0.0625;
}
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "intrins.h"
void Delay5us() //@12.000MHz
{
unsigned char i;
_nop_();
_nop_();
i = 12;
while (--i);
}
#define somenop Delay5us()
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
somenop;
SDA = 0;
somenop;
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
somenop;
SDA = 1;
}
//应答位控制
void IIC_Ack(bit ackbit)
{
if(ackbit)
{
SDA = 0;
}
else
{
SDA = 1;
}
somenop;
SCL = 1;
somenop;
SCL = 0;
SDA = 1;
somenop;
}
//等待应答
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(byt&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
somenop;
SCL = 1;
byt <<= 1;
somenop;
SCL = 0;
}
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 1;
somenop;
da <<= 1;
if(SDA)
da |= 0x01;
SCL = 0;
somenop;
}
return da;
}
void DAC_out(uchar dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
void Key_gn()
{
if(key_num==4)
{
wendu_cs=0;
key_cp=~key_cp;
key_num=0;
}
else if(key_num==5)
{
wendu_cs++;
if(wendu_cs==3)wendu_cs=1;
key_num=0;
}
else if(key_num==6)
{
if(wendu_cs==1)
{
Tmax+=1;
Tmax%=100;
key_num=0;
}
if(wendu_cs==2)
{
if(Tmin>=Tmax)
{
Tmin=Tmin;
cs_stat=1;
}
else{
cs_stat=0;
Tmin+=1;
Tmin%=100;
}
}
key_num=0;
}
else if(key_num==7)
{
if(wendu_cs==1)
{
if(Tmax<=Tmin)
{
Tmax=Tmax;
cs_stat=1;
}
else{
cs_stat=0;
Tmax-=1;
}
}
if(wendu_cs==2)
{
Tmin-=1;
if(Tmin<=0)
{
cs_stat=0;
Tmin=0;
}
}
key_num=0;
}
}
void Show_gn()
{
if(key_cp==0||wendu_cs==0)
{
n1=12,n2=16,n3=16,n4=16,n5=16,n6=16,n7=wendu/10,n8=wendu%10;
}
if(key_cp==1)
{
n1=24,n2=16,n3=16,n4=Tmax/10,n5=Tmax%10,n6=16,n7=Tmin/10,n8=Tmin%10;
}
if(wendu_cs==1)
{
if(T_count>250)
{
n1=24,n2=16,n3=16,n4=16,n5=16,n6=16,n7=Tmin/10,n8=Tmin%10;
}
else{
n1=24,n2=16,n3=16,n4=Tmax/10,n5=Tmax%10,n6=16,n7=Tmin/10,n8=Tmin%10;
}
}
if(wendu_cs==2)
{
if(T_count>250)
{
n1=24,n2=16,n3=16,n4=Tmax/10,n5=Tmax%10,n6=16,n7=16,n8=16;
}
else{
n1=24,n2=16,n3=16,n4=Tmax/10,n5=Tmax%10,n6=16,n7=Tmin/10,n8=Tmin%10;
}
}
}
uchar led=0xff;
void Led_gn()
{
if(wendu_cs==1||wendu_cs==2)
{
if(cs_stat==1)
{
Y4;led=0xf7;
}
}
else if(wendu<Tmin)
{
DAC_out(102);
Y4;P0=led&0xfb;
}
else if(wendu>Tmin&&wendu<Tmax)
{
DAC_out(153);
Y4;P0=led&0xfd;
}
else if(wendu>Tmax)
{
DAC_out(204);
Y4;P0=led&0xfe;
}
}
void main()
{
Init();
Timer0Init();
while(1)
{
wendu=(uint)Ds28B20_get();
Key_scanf();
Show_gn();
Led_gn();
}
}