文档库 最新最全的文档下载
当前位置:文档库 › 红外遥控数字钟

红外遥控数字钟

/***************************************/

#include
#define uint unsigned int
#define uchar unsigned char

/*********************************************/

sbit buzzer = P1^0;
sbit DQ = P2^0;
sbit clk = P2^2;
sbit io = P2^3;
sbit res = P2^4;
sbit rs = P2^5;
sbit rw = P2^6;
sbit en = P2^7;
sbit busy = P0^7;
sbit B_remote = P3^2;

/*********************************************/

uchar code table[] ={"D:2010-06-06 Mon"};
uchar code table1[]={"T: 01-03-20 63C"};
uchar code table2[7][3]={{"Mon"},{"Tue"},{"Wed"},{"Thu"},{"Fri"},{"Sat"},{"Sun"}};

uchar seconds;
uchar minutes;
uchar hour;
uchar date;
uchar month;
uchar day;
uchar year;
uchar remote_data;
uchar counting_num;
uint temper;
uchar num;
bit T_mark;


/**************************************/

void delay(uint z)
{
uint j;
uint i;

for(j=z; j>0; j--) //40us
for(i=10; i>0; i--);
}

void delay_ds(uint z)
{
while(z--);
}

/*********************************************/

void lcd_busy()
{
P0 = 0xff;
rw = 1;
rs = 0;
en = 1;
while(busy);
en = 0;
}

/**********************************************/

void lcd_write(uchar temp,uchar date)
{
lcd_busy();
rw = 0;
rs = temp;
P0 = date;
en = 1;
en = 0;
}

/***********************************************/

void lcd_init()
{
lcd_write(0,0x01);
lcd_write(0,0x38);
lcd_write(0,0x0c);
lcd_write(0,0x06);

}

/*****************************************************/

void write_ds1302(uchar address, uchar temp)
{
uchar i;

res = 0;
clk = 0;
io = 1;

for(i=0; i<8; i++)
{
res = 1;
address >>= 1;
io = CY;
clk = 1;
clk = 0;
}

for(i=0; i<8; i++)
{
res = 1;
temp >>= 1;
io = CY;
clk = 1;
clk = 0;
}

res = 0;
clk = 0;
io = 1;
}

/************************************************/

uchar read_ds1302(uchar address)
{
uchar temp;
uchar i;

res = 0;
clk = 0;
io = 1;

for(i=0; i<8; i++)
{
res = 1;
address >>= 1;
io = CY;
clk = 1;
clk = 0;
}

for(i=0; i<8; i++)
{
res = 1;
temp >>= 1;
clk = 1;
if(io)
{
temp |= 0x80;
}
clk = 0;
}

res = 0;
clk = 0;
io = 1;

return temp;
}

/************************************************/

uchar DEC_BCD(uchar dec)
{
uchar bcd;
bcd = 0;
while(dec >= 10)
{
dec -= 10;
bcd++;
}
bcd <<= 4;
bcd |= dec;
return bcd;
}

/************************************************/

uchar BCD_DEC(uchar bcd)
{
uchar dec;
dec = bcd & 0x0f;
bcd &= 0xf0;
bcd >>= 4;
bcd *= 10;
dec += bcd;
return dec;
}

/************************************************/

void remote_handle() interrupt 0
{
bit R_mark=0;
uchar i = 0;
uchar j = 0;
uchar h_remote_data = 0;
uchar l_remote_data = 0;

EX0 = 0;
R_mark = 0;

for(i=0; i<4; i++) //重复4次,目的是检测在8820微秒内
{ // 如

果出现高电平就退出解码程序
delay(10); //882微秒延时子程序
if(B_remote) //延时882微秒后判断P3.2脚是否出现高电平
{ //如果有就退出解码程序
R_mark = 1;
}

} //以上完成对遥控信号的9000微秒的初始低电平信号的识别。

if(R_mark == 0)
{
while(!B_remote); //等待高电平避开9毫秒低电平引导脉冲
delay(50); //延时4.74毫秒避开4.5毫秒的结果码

for(i=0; i<16; i++) //忽略前16位系统识别码
{
while(!B_remote); //等待地址码第一位的高电平信号
delay(10); // 高电平开始后用882微秒的时间尺
// 去判断信号此时的高低电平状态
if(B_remote)
{
delay(10); //检测到高电平1的话,延时1毫秒等待脉冲高电平结束
}
}

for(i=0; i<8; i++) //接收一字节的原码
{
while(!B_remote); //等待地址码第一位的高电平信号
delay(10);

l_remote_data >>= 1;

if(B_remote)
{
delay(10); //为1延时
l_remote_data |= 0x80;
}
else
{
l_remote_data &= 0x7f;
}


}

for(i=0; i<8; i++) //接收一字节的反码
{
while(!B_remote); //等待地址码第一位的高电平信号
delay(10);

h_remote_data >>= 1;

if(B_remote)
{
delay(10); //为1延时
h_remote_data |= 0x80;
}
else
{
h_remote_data &= 0x7f;
}
}

if(h_remote_data == (~l_remote_data))
{ // 如果不等表示接收数据发生错误,放弃
remote_data = l_remote_data;
}
}

EX0 = 1;
}

/****************************************************/

void key()
{

if(remote_data == 0x15)
{
remote_data = 0xff;
TR0 = 1;

buzzer = 0;
counting_num ++;
if(counting_num == 8)
{
counting_num = 0;
T_mark = 0;
TR0 = 0;
}
}

if(remote_data == 0x07)
{
remote_data = 0xff;
TR0 = 1;

buzzer = 0;
if(counting_num == 0)
{
counting_num = 7;
}
counting_num --;

if(counting_num == 0)
{
counting_num = 0;
T_mark = 0;
TR0 = 0;
}


}

if(remote_data == 0x40)
{
remote_data = 0xff;
buzzer = 0;

if(counting_num == 1)
{
seconds ++;
if(seconds == 60)
{
seconds = 0;
}
write_ds1302(0x80,DEC_BCD(seconds));
}

if(counting_num == 2)
{
minutes ++;
if(minutes == 60)
{
minutes = 0;
}
write_ds1302(0x82,DEC_BCD(minutes));
}

if(counting_num == 3)
{
hour ++;
if(hour == 24)
{
hour = 0;
}
write_ds1302(0x84,DEC_BCD(hour));
}

if(counting_num == 4)
{
day ++;
if(day == 8)
{
day = 0;
}
write_ds1302(0x8a,DEC_BCD(day));
}

if(counting_num == 5)
{
date ++;
if(date == 32)
{
date = 1;
}
write_ds1302(0x86,DEC_BCD(date));
}

if(counting_num == 6)
{
month ++;
if(month == 13)
{

month = 1;
}
write_ds1302(0x88,DEC_BCD(month));
}



if(counting_num == 7)
{
year ++;
if(year == 100)
{
year = 0;
}
write_ds1302(0x8c,DEC_BCD(year));
}


}

if( remote_data == 0x44)
{
remote_data = 0xff;
buzzer = 0;

if(counting_num == 1)
{
seconds --;
if(seconds == 0)
{
seconds = 59;
}
write_ds1302(0x80,DEC_BCD(seconds));
}

if(counting_num == 2)
{
minutes --;
if(minutes == 0)
{
minutes = 59;
}
write_ds1302(0x82,DEC_BCD(minutes));
}

if(counting_num == 3)
{
hour --;
if(hour == 0)
{
hour = 23 ;
}
write_ds1302(0x84,DEC_BCD(hour));
}

if(counting_num == 4)
{
day --;

if(day == 0)
{
day = 7;
}
day &= 0x07;
write_ds1302(0x8a,DEC_BCD(day));

}

if(counting_num == 5)
{
date --;
if(date == 0)
{
date = 31;
}
write_ds1302(0x86,DEC_BCD(date));
}

if(counting_num == 6)
{
month --;
if(month == 0)
{
month = 12;
}
month &= 0x1f;
write_ds1302(0x88,DEC_BCD(month));
}



if(counting_num == 7)
{
year --;
if(year == 0)
{
year = 99;
}
write_ds1302(0x8c,DEC_BCD(year));
}
}

}

void ds18b20_init()
{
DQ = 1;
delay_ds(16);

DQ = 0;
delay_ds(400);

DQ = 1;
delay_ds(40);
}

/*******************************************/

void write_ds18b20(uchar temp)
{
uchar i;

for(i=0; i<8; i++)
{
DQ = 0;

DQ = temp&0x01;
delay_ds(5);

DQ = 1;
temp >>= 1;
}
}

/*********************************************/

uchar read_ds18b20()
{
uchar i;
uchar temp;

for(i=0; i<8; i++)
{
DQ = 0;

temp >>= 1;
DQ = 1;

if(DQ)
{
temp |= 0x80;
}

delay_ds(20);
}

return temp;
}

/**********************************************/

uint read_temp()
{
uint temp_l;
uint temp_h;
uint temp;

ds18b20_init();
write_ds18b20(0xcc);
write_ds18b20(0x44);

ds18b20_init();
write_ds18b20(0xcc);
write_ds18b20(0xbe);

temp_l = read_ds18b20();
temp_h = read_ds18b20();

temp = temp_h;
temp <<= 8;
temp = (temp | temp_l)*0.0625;

return temp;
}

void T_KEY() interrupt 1
{
TH0 = (65536 - 18432) / 256;
TL0 = (65536 - 18432) % 256;
num ++;
if(num == 50)
{
num = 0;
T_mark = ~T_mark;
}
}


/*************************************************/

void main()
{
uchar i;
lcd_init();

TMOD = 0x01;
TH0 = (65536 - 18432) / 256;
TL0 = (65536 - 18432) % 256;
ET0 = 1;

EX0 = 1;
IT0 = 1;
EA = 1;

write_ds1302(0x80,DEC_BCD(0));
write_ds1302(0x82,DEC_BCD(0));
write_ds1302(0x84,DEC_BCD(12));

lcd_write(0,0x80);
for(i=0; i<16; i++)
{
lcd_write(1,table[i]);
}

lcd_write(0,0x80+0x40);
for(i=0; i<15; i++)
{
lcd_write(1,table1[i]);
}

lcd_write(1,0xdf);

while(1)
{
key();
secon

ds = BCD_DEC(read_ds1302(0x81));
minutes = BCD_DEC(read_ds1302(0x83));
hour = BCD_DEC(read_ds1302(0x85));
date = BCD_DEC(read_ds1302(0x87));
month = BCD_DEC(read_ds1302(0x89));
day = BCD_DEC(read_ds1302(0x8b));
year = BCD_DEC(read_ds1302(0x8d));
temper = read_temp();

lcd_write(0,0x80+0x43);
lcd_write(1,hour / 10 + 48);
lcd_write(1,hour % 10 + 48);
if(counting_num == 3)
{
if(T_mark == 1)
{
lcd_write(0,0x80+0x43);
lcd_write(1,0);
lcd_write(1,0);
}
}

lcd_write(0,0x80+0x46);
lcd_write(1,minutes / 10 + 48);
lcd_write(1,minutes % 10 + 48);
if(counting_num == 2)
{
if(T_mark == 1)
{
lcd_write(0,0x80+0x46);
lcd_write(1,0);
lcd_write(1,0);
}
}

lcd_write(0,0x80+0x49);
lcd_write(1,seconds / 10 + 48);
lcd_write(1,seconds % 10 + 48);
if(counting_num == 1)
{
if(T_mark == 1)
{
lcd_write(0,0x80+0x49);
lcd_write(1,0);
lcd_write(1,0);
}
}


lcd_write(0,0x80+0x4c);
lcd_write(1,temper / 10 + 48);
lcd_write(1,temper % 10 + 48);

lcd_write(0,0x84);
lcd_write(1,year / 10 + 48);
lcd_write(1,year % 10 + 48);
if(counting_num == 7)
{
if(T_mark == 1)
{
lcd_write(0,0x84);
lcd_write(1,0);
lcd_write(1,0);
}
}

lcd_write(0,0x87);
lcd_write(1,month / 10 + 48);
lcd_write(1,month % 10 + 48);
if(counting_num == 6)
{
if(T_mark == 1)
{
lcd_write(0,0x87);
lcd_write(1,0);
lcd_write(1,0);
}
}

lcd_write(0,0x8a);
lcd_write(1,date / 10 + 48);
lcd_write(1,date % 10 + 48);
if(counting_num == 5)
{
if(T_mark == 1)
{
lcd_write(0,0x8a);
lcd_write(1,0);
lcd_write(1,0);
}
}


lcd_write(0,0x8d);
switch(day)
{
case 1:
{
for(i=0; i<3; i++)
{
lcd_write(1,table2[0][i]);
}
break;
}
case 2:
{
for(i=0; i<3; i++)
{
lcd_write(1,table2[1][i]);
}
break;
}
case 3:
{
for(i=0; i<3; i++)
{
lcd_write(1,table2[2][i]);
}
break;
}
case 4:
{
for(i=0; i<3; i++)
{
lcd_write(1,table2[3][i]);
}
break;
}
case 5:
{
for(i=0; i<3; i++)
{
lcd_write(1,table2[4][i]);
}
break;
}
case 6:
{
for(i=0; i<3; i++)
{
lcd_write(1,table2[5][i]);
}
break;
}
case 7:
{
for(i=0; i<3; i++)
{
lcd_write(1,table2[6][i]);
}
break;
}
}

if(counting_num == 4)
{
if(T_mark == 1)
{
lcd_write(0,0x8d);
lcd_write(1,0);
lcd_write(1,0);
lcd_write(1,0);
}
}

buzzer = 1;
}
}

相关文档