|
请大侠们帮我查查错啊!我找了一周了,还是没找到。
[复制链接]
/**************************************************************/
// HT1380实时时钟LCD显示
// 使用Keil C51
// 2010.03.19
/*************************************************************/
/****使用AT89S51单片机,12MHz晶振,HT1380时钟芯片,LCD显示****/
//key0为调时功能键,key1为加1键,key2为减1键
#include
#include
#define uchar unsigned char
#define uint unsigned int
/**********HT1380引脚定义*************/
sbit clock_data=P1^1;
sbit clock_sclk=P1^0;
sbit clock_rst=P1^2;
/*********LCD读写引脚定义************/
sbit lcdrs=P1^3;
sbit lcdrw=P1^4;
sbit lcden=P1^5;
/**********调时按键引脚定义**********/
sbit negative=P3^0;
sbit key1=P3^4;
sbit key2=P3^5;
sbit key3=P3^6;
uchar num=0,shi,ge;//调时参数
uchar wei;
uchar date=1,mon=1,day=1;
uint sec=0,min=0,hour=0,year=0;
uchar data databuf;//数据缓冲
uchar code table1[]=" 2003-01-14 Mon ";
uchar code table2[]=" 01:03:45 ";
uchar code table3[][3]={{'M','o','n'},{'T','u','e'},{'W','e','d'},{'T','h','u'},{'F','r','i'},{'S','a','t'},{'S','u','n'}};
void delay(uchar n) //时间延时函数
{
uchar x,y;
for(x=n;x>0;x--)
for(y=0;y<110;y++);
}
/**************向HT1380读数据*************/
uchar read_ht1380(uchar cmd)
{
uchar i;
clock_rst=0;
_nop_();
clock_sclk=0;
_nop_();
clock_rst=1;//复位信号拉高
_nop_();_nop_();_nop_();_nop_();
for(i=0;i<8;i++) //写入读数据命令
{
clock_sclk=0;
if((cmd&0x01)==1)
clock_data=1;
else
clock_data=0;
clock_sclk=1;
cmd>>=1;//低位在前
}
for(i=0;i<8;i++) //读出数据
{
clock_sclk=1;
_nop_();
clock_sclk=0;
if(clock_data==1)
databuf|=0x01;
else
databuf&=0xfe;
databuf=_cror_(databuf,1);//循环右移一位,8次
clock_sclk=0;//下降沿读数
_nop_();
}
clock_sclk=0;
clock_rst=0; //复位信号拉低,关闭读数
databuf=((databuf&0xf0)/16)*10+(databuf&0x0f);//读出的数据组
return databuf;
_nop_();
_nop_();
_nop_();
_nop_();
}
/**************向HT1380写数据*************/
void write_ht1380(uchar cmd,uchar date)
{
uchar i;
date=(date/10<<4)|date%10; //10进制转16进制
clock_rst=0;
_nop_();
clock_sclk=0;
_nop_();
clock_rst=1; //复位信号拉高,准备写入
_nop_();
_nop_();
_nop_();
_nop_();
for(i=0;i<8;i++) //写入写命令
{
clock_sclk=0;
if((cmd&0x01)==1)
clock_data=1;
else
clock_data=0;
clock_sclk=1;//上升沿写入
cmd>>=1;//低位在前
}
for(i=0;i<8;i++) //写入数据
{
clock_sclk=0;
if((date&0x01)==0)
clock_data=0;
else
clock_data=1;
clock_sclk=1; //上升沿写数据
date>>=1;
}
clock_sclk=0;
clock_rst=0;
_nop_();
_nop_();
_nop_();
_nop_();
}
/*******写指令函数****/
void write_com(uchar cmd)
{
P0=cmd;
lcdrs=0;
lcdrw=0;
lcden=0;
delay(1);
lcden=1;
}
/******写数据函数*******/
void write_dat(uchar date)
{
P0=date;
lcdrs=1;
lcdrw=0;
lcden=0;
delay(1);
lcden=1;
}
/***************年月日函数************/
void write_nyr(uchar add,uchar date)
{
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_dat(0x30+shi);
write_dat(0x30+ge);
}
/********************************/
/*******时分秒函数*******/
void write_sfm(uchar add,uchar date)
{
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_dat(0x30+shi);
write_dat(0x30+ge);
}
/******************初始化函数********************/
void init()
{
write_com(0x01); //lcd初始化
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
write_ht1380(0x8e,0); //允许写寄存器;
write_ht1380(0x80,0); //振荡器起振 CH=0
write_ht1380(0x80,0); //sec
write_ht1380(0x82,0);//min
write_ht1380(0x84,0);//hour
write_ht1380(0x86,1);//date
write_ht1380(0x88,1);//month
write_ht1380(0x8a,1);//day weak
write_ht1380(0x8c,0);//year
for(num=0;num<16;num++) //写入初始化数据
{
write_dat(table1[num]);
delay(10);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_dat(table2[num]);
delay(10);
}
}
/***************lcd显示函数*****************/
void lcd_display()
{
sec=(read_ht1380(0x81)&0x7f);//read sec
delay(2);
min=read_ht1380(0x83); //read min
delay(2);
hour=read_ht1380(0x85); //read hour
delay(2);
date=read_ht1380(0x87); //read date
delay(2);
mon=read_ht1380(0x89); //read month
delay(2);
day=read_ht1380(0x8b);//read day week
delay(2);
year=read_ht1380(0x8d);//read year
delay(2);
write_nyr(3,year);delay(2);//显示年月日时分秒和星期
write_nyr(6,mon);delay(2);
write_nyr(9,date);delay(2);
write_sfm(4,hour);delay(2);
write_sfm(7,min);delay(2);
write_sfm(10,sec);delay(2);
write_com(0x80+12);delay(2);
for(num=0;num<3;num++)
write_dat(table3[day-1][num]);
delay(2);
}
uchar wei1(uchar wei1)//光标显示
{
if(wei1==1)
{
write_com(0x80+0x40+12);
write_com(0x0f);
return 0x80+0x40+12;
}
if(wei1==2)
{
write_com(0x80+0x40+9);
write_com(0x0f);
return 0x80+0x40+9;
}
if(wei1==3)
{
write_com(0x80+0x40+6);
write_com(0x0f);
return 0x80+0x40+6;
}
if(wei1==4)
{
write_com(0x80+15);
write_com(0x0f);
return 0x80+15;
}
if(wei1==5)
{
write_com(0x80+11);
write_com(0x0f);
return 0x80+11;
}
if(wei1==6)
{
write_com(0x80+8);
write_com(0x0f);
return 0x80+8;
}
if(wei1==7)
{
write_com(0x80+5);
write_com(0x0f);
return 0x80+5;
}
if(wei1==8)
{
wei=0;
sec=read_ht1380(0x81);
write_ht1380(0x80,sec);
write_com(0x0c);
return 0x0c;
}
delay(100);
}
/****************键盘扫描函数*********************/
void keyscan()
{
uchar temp,b=0;
negative=0; //负极线P3^7
while(b==0)
{
if(key1==0)
{
delay(5);
if(key1==0) //功能键的移动
{
while(!key1); //松开按键
write_ht1380(0x80,(0x80|sec));
wei++;
temp=wei1(wei);
}
}
if(key2==0) //加键
{
delay(5);
if(key2==0)
{
while(!key2);
if(wei==1)
{
sec=(read_ht1380(0x81)&0x7f);
if(sec>=59)sec=0;
++sec;
write_ht1380(0x80,0x80|sec);就是这里啊,怎么弄都错
}
if(wei==2)
{
min=read_ht1380(0x83);
if(min==59)min=-1;
write_ht1380(0x82,++min);
}
if(wei==3)
{
hour=read_ht1380(0x85);
if(hour==23)hour=-1;
write_ht1380(0x84,++hour);
}
if(wei==4)
{
day=read_ht1380(0x8b);
if(day==7)day=0;
write_ht1380(0x8a,++day);
}
if(wei==5)
{
date=read_ht1380(0x87);
if(date==28)date=0;
write_ht1380(0x86,++date);
}
if(wei==6)
{
mon=read_ht1380(0x89);
if(mon==12)mon=0;
write_ht1380(0x88,++mon);
}
if(wei==7)
{
year=read_ht1380(0x8d);
if(year==99)year=-1;
write_ht1380(0x8c,++year);
}
write_com(0x0c);
lcd_display();
//write_ht1380(0x80,sec+0x80);
delay(100);
write_com(temp);
write_com(0x0f);
}
}
if(key3==0) //减键
{ delay(5);
if(key3==0)
{
while(!key3);
if(wei==1)
{
sec=(read_ht1380(0x81)&0x7f);
if(sec<=0)sec=60;
--sec;
write_ht1380(0x80,0x80|sec);
}
if(wei==2)
{
min=read_ht1380(0x83);
if(min==0)min=60;
write_ht1380(0x82,--min);
}
if(wei==3)
{
hour=read_ht1380(0x85);
if(hour==0)hour=24;
write_ht1380(0x84,--hour);
}
if(wei==4)
{
day=read_ht1380(0x8b);
if(day==1)day=8;
write_ht1380(0x8a,--day);
}
if(wei==5)
{
date=read_ht1380(0x87);
if(date==1)date=29;
write_ht1380(0x86,--date);
}
if(wei==6)
{
mon=read_ht1380(0x89);
if(mon==1)mon=13;
write_ht1380(0x88,--mon);
}
if(wei==7)
{
year=read_ht1380(0x8d);
if(year==0)year=100;
write_ht1380(0x8c,--year);
}
write_com(0x0c);
lcd_display();
//write_ht1380(0x80,sec+0x80);
delay(100);
write_com(temp);
write_com(0x0f);
}
}
if(wei==0) b=1;
}
}
void main()
{
init();
while(1)
{
lcd_display();
keyscan();
delay(100);
}
}
|
|