//结果出来了,省一,感觉还是稍有点瑕疵
//main.c
#include#include
#include
u16 code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x73};
u8 date[8]=0;
bit mode=0;
bit chose=0;
u8 new_temp[2]={30,20};
u8 old_temp[2]={30,20};
bit fault=0;
void allinit()
{
P2=0x80;
P0=0xff;
P2=0xa0;
P0=0x00;
P2=0x00;
}
void led1()
{
P2=0x80;
if(fault==0)
{
P0=0xfe;
}
else
{
P0=0xf6;
}
P2=0x00;
}
void led2()
{
P2=0x80;
if(fault==0)
{
P0=0xfd;
}
else
{
P0=0xf5;
}
P2=0x00;
}
void led3()
{
P2=0x80;
if(fault==0)
{
P0=0xfb;
}
else
{
P0=0xf3;
}
P2=0x00;
}
void judgeone()
{
u8 temp=Ds_Read();
if((temp>new_temp[0]))
{
led1();
Pcf8591_dac(210);
}
if((temp>=old_temp[1])&&(temp<=old_temp[0]))
{
led2();
Pcf8591_dac(153);
}
if(temp
led3();
Pcf8591_dac(105);
}
}
void Delay1ms() //@11.0592MHz
{
unsigned char i, j; _nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
} void display()
{
u8 i;
for(i=0;i<8;i++)
{
P2=0xc0;
P0=0x01< P2=0xff;
P0=~duan[date[i]];
Delay1ms();
P0=0xff;
P2=0x00;
}
}
void show()
{
u8 temp;
if(mode==0)
{
judgeone();
temp=Ds_Read();
date[0]=12;
date[1]=16;
date[2]=16;
date[3]=16;
date[4]=16;
date[5]=16;
date[6]=temp/10;
date[7]=temp%10;
}
if(mode==1)
{
date[0]=17;
date[1]=16;
date[2]=16;
date[3]=new_temp[0]/10;
date[4]=new_temp[0]%10;
date[2]=16;
date[6]=new_temp[1]/10;
date[7]=new_temp[1]%10;
}
}
void key_do()
{
Key_Scan();
Key_Judge();
if(key_flag==1)
{
key_flag=0;
if(mode==1)
{
if(key_value==3)
{
chose=~chose;
}
if(key_value==2)
{
new_temp[chose]+=1;
}
if(key_value==1)
{
new_temp[chose]-=1;
}
}
if((key_value==4)&&(mode==0))
{
chose=1;
mode=~mode;
old_temp[0]=new_temp[0];
old_temp[1]=new_temp[1];
}
else if((key_value==4)&&(mode==1))
{
if(new_temp[1]>new_temp[0])
{
fault=1;
new_temp[0]=old_temp[0];
new_temp[1]=old_temp[1];
}
else
{
fault=0;
old_temp[0]=new_temp[0];
old_temp[1]=new_temp[1];
}
mode=~mode;
}
}
} void main()
{
allinit();
while(1)
{
key_do();
show();
display();
}
} //key.h #ifndef _key_h
#define _key_h
#include
#define KEY 1
#define u8 unsigned char
#define u16 unsigned int
sbit key1=P4^4;
sbit key2=P4^2;
sbit key3=P3^5;
sbit key4=P3^4;
sbit keyrow1=P3^0;
sbit keyrow2=P3^1;
sbit keyrow3=P3^2;
sbit keyrow4=P3^3;
void Key_Scan();
void Key_Judge();
extern u8 key_value;
extern bit key_flag;
#endif //key.c #include
u8 key_value;
bit key_flag;
#if KEY
u8 pdata key_catch[4]={0xff,0xff,0xff,0xff};
u8 pdata key_backup[4]={1,1,1,1};
u8 pdata key_statue[4]={1,1,1,1};
u8 code button[4]={1,2,3,4};
void Key_Scan()
{
u8 i=0;
key_catch[0]=key_catch[0]<<1|keyrow1;
key_catch[1]=key_catch[1]<<1|keyrow2;
key_catch[2]=key_catch[2]<<1|keyrow3;
key_catch[3]=key_catch[3]<<1|keyrow4;
for(;i<4;i++)
{
if((key_catch[i]&0xff)==0x00)
{
key_statue[i]=0;
}
else if((key_catch[i]&0xff)==0xff)
{
key_statue[i]=1;
}
}
}
void Key_Judge()
{
u8 i=0;
for(;i<4;i++)
{
if(key_statue[i]!=key_backup[i])
{
if(key_backup[i]==0)
{
key_value=button[i];
key_flag=1;
}
key_backup[i]=key_statue[i];
}
}
}
#else
u8 pdata key_catch[4][4]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
u8 pdata key_backup[4][4]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
u8 pdata key_statue[4][4]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
u8 code button[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
void Key_Scan()
{
static u8 row=0;
u8 i=0;
key_catch[row][0]=key_catch[row][0]<<1|keyrow1;
key_catch[row][1]=key_catch[row][1]<<1|keyrow2;
key_catch[row][2]=key_catch[row][2]<<1|keyrow3;
key_catch[row][3]=key_catch[row][3]<<1|keyrow4;
for(;i<8;i++)
{
if((key_catch[row][i]&0x0f)==0x00)
{
key_statue[row][i]=0;
}
else if((key_catch[row][i]&0x0f)==0x0f)
{
key_statue[row][i]=1;
}
}
row++;
row&=0x03;
switch(row)
{
case 0:key1=0;key4=1;break;
case 1:key2=0;key1=1;break;
case 2:key3=0;key2=1;break;
case 3:key4=0;key3=1;break;
default:break;
}
}
void Key_Judge()
{
u8 i=0,j;
for(;i<8;i++)
{
for(j=0;j<8;j++)
{
if(key_statue[i][j]!=key_backup[i][j])
{
if(key_backup[i][j]==0)
{
key_value=button[i][j];
key_flag=1;
}
key_backup[i][j]=key_statue[i][j];
}
}
}
}
#endif //i2c.c
#include "stc15f2k60s2.h"
#include "intrins.h" #define DELAY_TIME 5 #define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1 sbit SDA = P2^1;
sbit SCL = P2^0; void IIC_Delay(unsigned char i)
{
do{_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();}
while(i--);
}
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
} void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
void IIC_SendByte(unsigned char byt)
{
unsigned char i; for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
void Pcf8591_dac(unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
} //i2c.h #ifndef _IIC_H
#define _IIC_H void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void); void Pcf8591_dac(unsigned char dat);
#endif //ds18b20.c #include
void Delay15us() //@11.0592MHz
{
unsigned char i; i = 39;
while (--i);
}
void Delay60us() //@11.0592MHz
{
unsigned char i, j; i = 1;
j = 162;
do
{
while (--j);
} while (--i);
}
void Delay500us() //@11.0592MHz
{
unsigned char i, j; _nop_();
_nop_();
i = 6;
j = 93;
do
{
while (--j);
} while (--i);
}
void ds_init()
{
dq=1;
dq=0;
Delay500us();
dq=1;
Delay500us();
}
void write(u8 date)
{
u8 i=0;
for(;i<8;i++)
{
dq=1;
dq=0;
Delay15us();
dq=(date>>i)&0x01;
Delay60us();
}
}
u8 read()
{
int i=0,date;
for(i=0;i<8;i++)
{
date>>=1;
dq=0;
dq=1;
if(dq==1)
{
date|=0x80;
}
Delay60us();
}
return date;
} void wr()
{
ds_init();
write(0xcc);
write(0x44);
} void re()
{
ds_init();
write(0xcc);
write(0xbe);
}
u8 Ds_Read()
{
u8 date,low,high;
wr();
re();
low=read();
high=read();
date=high<<4|low>>4;
return date;
} //ds18b20.h #ifndef _ds18b20_h
#define _ds18b20_h
#include
#include
#define u8 unsigned char
#define u16 unsigned int
sbit dq=P1^4;
u8 Ds_Read(); #endif
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)