#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define LCMdata P0
#define Busy 0x80
//Lcd_part
sbit RS = P2^0;
sbit RW = P2^1;
sbit E = P2^2;
sbit REST = P2^5;
sbit PSB = P2^3;
//Key_part
sbit Key_1 = P1^4;
sbit Key_2 = P1^5;
sbit Key_3 = P1^6;
sbit Key_4 = P1^7;
//Ds1302_part
sbit Sclk = P1^0;
sbit IO = P1^1;
sbit Rst = P1^2;
sbit Buz = P3^7;
uchar Key_Select,Key_Flag,Key_Count,Set1;
uchar Time_Flag,TimeCount,Week_Count=0;
uchar Sec=0;uchar Min=30;uchar Hour=7;
uchar Date=8;uchar Mouth=4;uchar Year=12;uchar Week=5;
uchar Display_number[14] = {"0123456789: -"};
uchar Rd_addr[] = {0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
uchar Wr_addr[] = {0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
uchar Display[] = {"2012年04月06日**"};
uchar Display_1[]={"19时36分00秒****"};
uchar Display_2[]={"星期"};
uchar code hznumber[] = {"壹贰参肆伍陆日"};
uchar code string_1[] = {"百年大计质量为上"};
uchar code string_2[] = {" ----时钟---- "};
uchar code string_3[] = {" ----日历---- "};
uchar code string_4[] = {"****************"};
void Wr_Word(uchar *s);
void Wr_Command(uchar wrcommand,busyc);
void Display_String(uchar line,uchar Y,uchar *string);
void Delay5Ms(void) //延时 5ms
{
unsigned int TempCyc=5552;
while(TempCyc--);
}
//-------------------------------------------------------
void Delay_Ms(uint Mtime)
{
uint i,j;
for(i=Mtime;i>0;i--)
for(j=122;j>0;j--)
{;}
}
//--------------------------------------------------------
void Delay_Us(uchar Utime)
{
while(--Utime);
}
void Wr_ds1302_byte(uchar data_13)
{
uchar i;
for(i=0;i<8;i++)
{
Sclk = 0;
IO = data_13 &0x01;
data_13=data_13>>1;
Sclk = 1;
}
}
uchar Rd_ds1302_byte()
{
uchar i,value;
for(i=0;i<8;i++)
{
value=value>>1;
Sclk = 1;
Sclk = 0;
if(IO)
value=value|0x80;
}
return value;
}
void Wr_1302(uchar addr,uchar dat)
{
Rst = 0;
Sclk = 0;
Rst = 1;
Wr_ds1302_byte(addr);
Wr_ds1302_byte(dat);
Rst = 0;
Sclk = 1;
IO = 1;
}
Rd_1302(uchar addr)
{
uchar value;
Rst = 0;
Sclk = 0;
Rst = 1;
Wr_ds1302_byte(addr);
value=Rd_ds1302_byte();
Rst = 0;
IO = 1;
Sclk = 1;
return value;
}
void Set_RTC(void)
{
Wr_1302(0x8e,0x00);
Wr_1302(0x80,0x80);
Wr_1302(Wr_addr[6],(Sec/10)<<4|Sec%10); //Sec
Wr_1302(Wr_addr[5],(Min/10)<<4|Min%10); //Min
Wr_1302(Wr_addr[4],(Hour/10)<<4|Hour%10); //Hour
Wr_1302(Wr_addr[3],(Date/10)<<4|Date%10); //Date
Wr_1302(Wr_addr[2],(Mouth/10)<<4|Mouth%10); //Mouth
Wr_1302(Wr_addr[1],(Week/10)<<4|Week%10); //Weak
Wr_1302(Wr_addr[0],(Year/10)<<4|Year%10); //Year
Wr_1302(0x8e,0x80);
}
void Rd_RTC(void)
{
Sec = Rd_1302(0x81); //Sec
Min = Rd_1302(0x83); //Min
Hour = Rd_1302(0x85); //Hour
Date = Rd_1302(0x87); //Date
Mouth = Rd_1302(0x89); //Mouth
Year = Rd_1302(0x8d); //Year
Week = Rd_1302(0x8b); //Week
}
uchar change(uchar Week_Count)
{
//Year
Display[2] = Display_number[Year/16];
Display[3] = Display_number[Year%16];
//Mouth
Display[6] = Display_number[Mouth/16];
Display[7] = Display_number[Mouth%16];
//Date
Display[10] = Display_number[Date/16];
Display[11] = Display_number[Date%16];
//Hour
Display_1[0] = Display_number[Hour/16];
Display_1[1] = Display_number[Hour%16];
//Min
Display_1[4] = Display_number[Min/16];
Display_1[5] = Display_number[Min%16];
//Sec
Display_1[8] = Display_number[Sec/16];
Display_1[9] = Display_number[Sec%16];
//Week
//Display_2[4]= Week_Count = Display_number[Week%16];
//Display_2[4] = hznumber[Week%16];
Week_Count = Display_number[Week%16];
return Week_Count ;
}
//--------------------------------------------------------
void RDbf(void) //读忙状态
{
while(1)
{
RS=0;
RW=1;
E=0;
LCMdata=0xFF;
E=1;
if((LCMdata&Busy)==0)
break;
}
}
//--------------------------------------------------------
void Wr_Data(uchar wrdata) //写数据
{
RDbf();
RS=1;
RW=0;
E=1;
LCMdata=wrdata;
E=0;
}
//---------------------------------------------------------
void Wr_Command(uchar wrcommand,busyc) //写指令
{
if(busyc)
RDbf();
RS=0;
RW=0;
E=1;
LCMdata=wrcommand;
E=0;
}
//----------------------------------------------------------
void Wr_Word(uchar *s)
{
while(*s!='\0')
{
Wr_Data(*s++);
} *s=0;
}
//----------------------------------------------------------
void Display_String(uchar line,uchar Y,uchar *string)//显示字符串
{
uchar addr,post;
if(line==1)
addr=0x80;
else if(line==2)
addr=0x90;
else if(line==3)
addr=0x88;
else if(line==4)
addr=0x98;
post=addr+Y;
Wr_Command(post,1);
while(*string!='\0')
{
Wr_Data(*string++);
}
//*string = 0;
}
//-----------------------------------------------------------
void Lcm_Init(void)
{
Delay_Ms(40);
Delay5Ms();
PSB=1;
Delay5Ms();
REST=1;
REST=0;
REST=1;
Wr_Command(0x30,0);
Delay5Ms();
Wr_Command(0x30,0); //2次显示模式设置,不用判忙
Delay5Ms();
Wr_Command(0x0c,1); //开显示及光标设置
Delay5Ms();
Wr_Command(0x01,1); //显示清屏
Delay5Ms();
Wr_Command(0x02,1);//光标归位
//Delay5Ms();
//Wr_Command(0x14,1);//光标向右移动
Delay5Ms();
Wr_Command(0x06,1); //显示光标移动设置
Delay5Ms();
}
//-----------------------------------------------------------
//-----------------------------------------------------------
void buz(void)
{
uint i;
for(i=200;i>0;i--)
{
Buz=1;
Delay_Us(120);
Buz=0;
}
Buz=0x00;
}
//----------------------------------------------------------
void Key_Scan()
{
P1=0x30;
if(Key_1==0)
{
Delay_Ms(20);
if(Key_1==0)
{
Key_Select++;
if (Key_Select==1)
{
Wr_Command(0x0f,1);
Wr_Command(0x88,1); //Hour
}
if(Key_Select==2)
{
Wr_Command(0x8a,1); //Min
}
if(Key_Select==3)
{
Wr_Command(0x8c,1); //Sec
}
if(Key_Select==4)
{
Wr_Command(0x91,1); //Yeah
}
if(Key_Select==5)
{
Wr_Command(0x93,1); //Mouth
}
if(Key_Select==6)
{
Wr_Command(0x95,1); //Date
}
if(Key_Select==7)
{
Wr_Command(0x9a,1); //Week;
}
if(Key_Select==8)
{
Wr_Command(0x0c,1);
Key_Select=0;
Key_Count=0;
}
buz();
while(!Key_1);
Key_Count=0;
}
}
if(Key_2==0)
{
Delay_Ms(20);
if(Key_2==0)
{
Key_Count++;
}
buz();
while(!Key_2);
Wr_1302(0x8e,0x00);
Wr_1302(0x80,0x80);
switch(Key_Select)
{
case 1://Hour
if(Key_Count!=0)
{
// Wr_Command(0x0f,1);
Wr_Command(0x88,1);
if(Key_Count==24)
Key_Count=0;
Hour=(Key_Count/10)<<4|(Key_Count%10);
Wr_1302(Wr_addr[4],Hour);
}break;
case 2: //Min
if(Key_Count!=0)
{
Wr_Command(0x8a,1);
if(Key_Count==60)
Key_Count=0;
Min=(Key_Count/10)<<4|(Key_Count%10);
Wr_1302(Wr_addr[5],Min);
}break;
case 3: //Sec
if(Key_Count!=0)
{
Wr_Command(0x8c,1);
if(Key_Count==60)
Key_Count=0;
Sec=(Key_Count/10)<<4|(Key_Count%10);
Wr_1302(Wr_addr[6],Sec);
}break;
case 4: //Year
if(Key_Count!=0)
{
Wr_Command(0x91,1);
if(Key_Count==99)
Key_Count=0;
Year=(Key_Count/10)<<4|(Key_Count%10);
Wr_1302(Wr_addr[0],Year);
}break;
case 5: //Mouth
if(Key_Count!=0)
{
Wr_Command(0x93,1);
if(Key_Count==12)
Key_Count=1;
Mouth=(Key_Count/10)<<4|(Key_Count%10);
Wr_1302(Wr_addr[2],Mouth);
}break;
case 6: //Date
if(Key_Count!=0)
{
Wr_Command(0x95,1);
if(Key_Count==32)
Key_Count=1;
Date=(Key_Count/10)<<4|(Key_Count%10);
Wr_1302(Wr_addr[3],Date);
}break;
case 7: //Week
if(Key_Count!=0)
{
Wr_Command(0x9a,1);
if(Key_Count==8)
Key_Count=1;
Week=(Key_Count/10)<<4|(Key_Count%10);
Wr_1302(Wr_addr[1],Week);
}break;
default:
if(Key_Count==0)
{
Wr_Command(0x0c,1);
Wr_1302(0x8e,0x00);
Wr_1302(0x80,0x00);
Wr_1302(0x8e,0x80);
}Key_Select=0;
break;
} buz();
while(!Key_2);
}
}
//-----------------------------------------------------------
void main(void)
{
uchar i;
buz();
Lcm_Init();
Display_String(1,0,string_4);
Display_String(2,0,string_2);
Display_String(3,0,string_3);
Display_String(4,0,string_4);
Delay_Ms(1000);
Wr_Command(0x01,1);
Delay_Ms(250);
Display_String(1,0,string_4);
Display_String(2,0,string_2);
Display_String(3,0,string_3);
Display_String(4,0,string_4);
Delay_Ms(1000);
Wr_Command(0x01,1);
Display_String(1,0,string_2);
Display_String(2,0,Display);
Display_String(3,0,Display_1);
Display_String(4,0,Display_2);
Set_RTC();
//Wr_1302(0x80,0x00);
while(1)
{
Key_Scan();
Rd_RTC();
i=change();
switch(i)
{
case 0: Display_String(4,2,"壹"); break;
case 2: Display_String(4,2,"贰"); break;
case 3: Display_String(4,2,"参"); break;
case 4: Display_String(4,2,"四");break;
case 5: Display_String(4,2,"伍"); break;
case 6: Display_String(4,2,"柒"); break;
case 7: Display_String(4,2,"日"); break;
default:Display_String(4,2,"日"); break;
}
Display_String(1,0,string_2);
Display_String(2,0,Display);
Display_String(3,0,Display_1);
Display_String(4,0,Display_2);
}
}
全部程序如上,就是星期的汉字转换不过来;现在这个程式的显示的是星期日; |