#include <STC15.H> // 该程序具有显示日期、月份、日期和时间功能
#include <GPIO.H>
#include<stdio.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define sint signed int
#define disp_off 0x3e //显示关
#define disp_on 0x3f //显示开
#define disp_x 0xb8 //页地址为0页
#define disp_z 0xc0 //行地址为0行
#define disp_y 0x40 //列地址为0列
#define comm 0 //命令标志位
#define dat1 1 //数据标志位
//#define data_ora P0 //液晶12864的数据端与单片机的P0相连
sbit di =P2^0; //Data or Instrument Select,H:写数据,L:写指令
sbit rw =P2^1; //Write or Read,H:read,L:write
sbit e =P2^4; //读写使能
sbit cs1=P2^2; //cs1=H,选择左半屏
sbit cs2=P2^3; //cs2=H,选择右半屏
sbit clk=P1^0; //8563 clk
sbit dat=P1^1; //8563 data
sbit DS = P2^5; //SER 串行数据
sbit SHCP= P2^6; //SCK 串行时钟信号
sbit STCP = P2^7; //ST 锁存输出信号
uchar fen=0x42,miao=0x38,shi=0x17,riqi=0x02,//设置时间用
yuefen=0x09,xingqi=0x00,nian=0x07,zhongduan;//设置时间用
uchar code tabma[10][16]=
//阴码点阵格式、取模方式为列行式、逆向取模(低位在前),
//十六进制输出,中文16*16,英文8*16 宋体
{
//0(0) 1(1) 2(2) 3(3) 4(4) 5(5) 6(6) 7(7) 8(8) 9(9)
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00}, /*"0",0*/
{0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00}, /*"1",1*/
{0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00}, /*"2",2*/
{0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00}, /*"3",3*/
{0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00}, /*"4",4*/
{0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00}, /*"5",5*/
{0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00}, /*"6",6*/
{0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00}, /*"7",7*/
{0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00}, /*"8",8*/
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00}, /*"9",9*/
};
uchar code tab2ma[7][32]=
// 日(0) 一(1) 二(2) 三(3) 四(4) 五(5) 六(6)
{
{0x00,0x00,0x00,0xFE,0x42,0x42,0x42,0x42,
0x42,0x42,0x42,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x3F,0x10,0x10,0x10,0x10,
0x10,0x10,0x10,0x3F,0x00,0x00,0x00,0x00}, /*"日",0*/
{0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0xC0,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /*"一",1*/
{0x00,0x00,0x04,0x04,0x04,0x04,0x04,0x04,
0x04,0x04,0x04,0x06,0x04,0x00,0x00,0x00,
0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x10,
0x10,0x10,0x10,0x10,0x10,0x18,0x10,0x00}, /*"二",2*/
{0x00,0x04,0x84,0x84,0x84,0x84,0x84,0x84,
0x84,0x84,0x84,0x84,0x84,0x04,0x00,0x00,
0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00}, /*"三",3*/
{0x00,0xFE,0x02,0x02,0x02,0xFE,0x02,0x02,
0xFE,0x02,0x02,0x02,0x02,0xFE,0x00,0x00,
0x00,0x7F,0x28,0x24,0x23,0x20,0x20,0x20,
0x21,0x22,0x22,0x22,0x22,0x7F,0x00,0x00}, /*"四",4*/
{0x00,0x02,0x82,0x82,0x82,0x82,0xFE,0x82,
0x82,0x82,0xC2,0x82,0x02,0x00,0x00,0x00,
0x20,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,
0x20,0x20,0x3F,0x20,0x20,0x30,0x20,0x00}, /*"五",5*/
{0x10,0x10,0x10,0x10,0x10,0x91,0x12,0x1E,
0x94,0x10,0x10,0x10,0x10,0x10,0x10,0x00,
0x00,0x40,0x20,0x10,0x0C,0x03,0x01,0x00,
0x00,0x01,0x02,0x0C,0x78,0x30,0x00,0x00}, /*"六",6*/
};
uchar code nianma[]=
//年(0) 月(1) 日(2) 星(3) 期(4)
{0x40,0x20,0x10,0x0C,0xE3,0x22,0x22,0x22,
0xFE,0x22,0x22,0x22,0x22,0x02,0x00,0x00,
0x04,0x04,0x04,0x04,0x07,0x04,0x04,0x04,
0xFF,0x04,0x04,0x04,0x04,0x04,0x04,0x00}; /*"年",0*/
uchar code yuema[]=
{0x00,0x00,0x00,0x00,0x00,0xFF,0x11,0x11,
0x11,0x11,0x11,0xFF,0x00,0x00,0x00,0x00,
0x00,0x40,0x20,0x10,0x0C,0x03,0x01,0x01,
0x01,0x21,0x41,0x3F,0x00,0x00,0x00,0x00}; /*"月",1*/
uchar code rima[]=
{0x00,0x00,0x00,0xFE,0x42,0x42,0x42,0x42,
0x42,0x42,0x42,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x3F,0x10,0x10,0x10,0x10,
0x10,0x10,0x10,0x3F,0x00,0x00,0x00,0x00}; /*"日",2*/
uchar code xingqima[]=
{0x00,0x00,0x00,0xBE,0x2A,0x2A,0x2A,0xEA,
0x2A,0x2A,0x2A,0x2A,0x3E,0x00,0x00,0x00,
0x00,0x48,0x46,0x41,0x49,0x49,0x49,0x7F,
0x49,0x49,0x49,0x49,0x49,0x41,0x40,0x00, /*"星",3*/
0x00,0x04,0xFF,0x54,0x54,0x54,0xFF,0x04,
0x00,0xFE,0x22,0x22,0x22,0xFE,0x00,0x00,
0x42,0x22,0x1B,0x02,0x02,0x0A,0x33,0x62,
0x18,0x07,0x02,0x22,0x42,0x3F,0x00,0x00}; /*"期",4*/
//时(0) 分(1) 秒(2)
uchar code shima[]=
{0x00,0xFC,0x44,0x44,0x44,0xFC,0x10,0x90,
0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x00,
0x00,0x07,0x04,0x04,0x04,0x07,0x00,0x00,
0x03,0x40,0x80,0x7F,0x00,0x00,0x00,0x00}; /*"时",0*/
uchar code fenma[]=
{0x80,0x40,0x20,0x98,0x87,0x82,0x80,0x80,
0x83,0x84,0x98,0x30,0x60,0xC0,0x40,0x00,
0x00,0x80,0x40,0x20,0x10,0x0F,0x00,0x00,
0x20,0x40,0x3F,0x00,0x00,0x00,0x00,0x00}; /*"分",1*/
uchar code miaoma[]=
{0x12,0x12,0xD2,0xFE,0x91,0x11,0xC0,0x38,
0x10,0x00,0xFF,0x00,0x08,0x10,0x60,0x00,
0x04,0x03,0x00,0xFF,0x00,0x83,0x80,0x40,
0x40,0x20,0x23,0x10,0x08,0x04,0x03,0x00}; /*"秒",2*/
void delay(sint n); //延时程序
void xie_start(); //开始条件
void xie_stop(); //停止条件
bit xie1(uchar shu); //向8563中写入1个字
uchar du1(); //从8563中读出1个字
uchar du(); //从8563中读出时间和日期
uchar xie(); //向8563中设置时间和日期
//uchar xie_dingshi(); //8563定时器设置
//uchar xie_fangbo_dingshi(); //8563的方波设置
void delay1 (uint ms); //延时
void wr_lcd (uchar dat_comm,uchar content); //向12864中写命令
//uchar rd_lcd (); //读12864数据
void chk_busy (); //忙闲检测
void lat_disp (uchar data1,uchar data2); //写点钟
//void img_disp (uchar code *img) ; //显示图像
void chn_disp (uchar x,uchar y,uchar xl,uchar yl,uchar row_xl,uchar row_yl,uchar code *chn);
//显示汉字
void init_lcd (); //12864初始化
void disp(); //128显示程序
void hc595SendByte(unsigned char dat)
{
unsigned char i;
for(i=0; i<8; i++)
{
SHCP=0;
DS=dat&0x80;
_nop_();
dat=dat<<1;
_nop_();
SHCP=1;
_nop_();
}
SHCP=0;
}
void Out595(void)
{
STCP=1;
STCP=0;
}
//-------------主函数--------------------
void main()
{
uchar ml=0;
init_lcd (); //LCD初始化
lat_disp(0x00,0x00);
xie(); //预设时间用,设置好时间后可以屏蔽
for(;;)
{
du(); //读8563的时间
lat_disp(0x00,0x00);
disp(); //显示时间
delay1(100);
}
}
//-------------------延时---------------------------//
void delay(sint n)
{
for(n;n>0;n--);
}
/////////////////////以下是PCF8563读写/////////////////////以下是PCF8563读写
/* I2C的起始条件,在时钟端SCK为高电平时,数据端SDA发生由高到低的变化,为起始条件,
启动I2C总线。程序代码如下。 */
void xie_start()
{
clk=1;
_nop_();
dat=1;
_nop_();
dat=0;
_nop_();
clk=0;
_nop_();
}
/* I2C的停止条件,在时钟端SCL为高电平时,数据端SDA发生由低到高的变化,
为停止条件,停止I2C总线数据传输。 */
void xie_stop()
{
dat=0;
_nop_();
clk=1;
_nop_();
dat=1;
_nop_();
}
bit xie1(uchar shu) //向8563中写入1个字
{
uchar i=8;
bit fan_w;
clk=0;
_nop_();
while(i--)
{
dat=(bit)(shu&0x80); //取字节的最高位
_nop_();
shu<<=1; //字节左移一位
clk=1; //上升沿锁存数据
_nop_();
clk=0;
_nop_();
}
dat=1;
_nop_();
clk=1;
_nop_();
fan_w=dat; //读应答信号
clk=0;
_nop_();
return(fan_w); //返回应答信号
}
uchar du1() //从8563中读出1个字
{
uchar shu=0;
uchar data rdata;
uchar i=8;
while(i--)
{
rdata<<=1; //字节左移一位
dat=1;
clk=1;_nop_(); //上升沿锁存数据
if(dat) rdata|=0x01; //数据线为1时 对应位存1
clk=0;_nop_();
}
shu=rdata; //读到的数据存于SHU
return(shu); //返回读到的数据
}
uchar du()
{
EA=0; //关总中断
xie_start(); //起始条件
if(xie1(0xa2)!=0){xie_stop();return(0);}//取器件地址 写
if(xie1(0x02)!=0){xie_stop();return(0);}//取读时间的首字节地址从秒开始读
xie_start(); //起始条件
if(xie1(0xa3)!=0){xie_stop();return(0);}//取器件地址 读
miao=du1()&0x7f; //读秒 屏蔽最高位
xie_stop(); //停止条件
xie_start(); //起始条件
if(xie1(0xa2)!=0){xie_stop();return(0);}//取器件地址 写
if(xie1(0x03)!=0){xie_stop();return(0);}//取读分钟字节地址
xie_start(); //起始条件
if(xie1(0xa3)!=0){xie_stop();return(0);}//取器件地址 读
fen=du1()&0x7f; //读分 屏蔽最高位
xie_stop(); //停止条件
xie_start(); //起始条件
if(xie1(0xa2)!=0){xie_stop();return(0);}//取器件地址 写
if(xie1(0x04)!=0){xie_stop();return(0);}//取读小时字节地址
xie_start(); //起始条件
if(xie1(0xa3)!=0){xie_stop();return(0);}//取器件地址 读
shi=du1(); //读时
shi&=0x3f; //屏蔽最高2位
xie_stop(); //停止条件
xie_start(); //起始条件
if(xie1(0xa2)!=0){xie_stop();return(0);}//写
if(xie1(0x05)!=0){xie_stop();return(0);}//取读日期字节地址
xie_start(); //起始条件
if(xie1(0xa3)!=0){xie_stop();return(0);}//读
riqi=du1(); //读日期
riqi&=0x3f; //屏蔽最高2位
xie_stop(); //停止条件
xie_start(); //起始条件
if(xie1(0xa2)!=0){xie_stop();return(0);}//写
if(xie1(0x07)!=0){xie_stop();return(0);}//取读月份字节地址
xie_start(); //起始条件
if(xie1(0xa3)!=0){xie_stop();return(0);}//读
yuefen=du1(); //读月份
yuefen&=0x1f; //屏蔽最高3位
xie_stop(); //停止条件
xie_start(); //起始条件
if(xie1(0xa2)!=0){xie_stop();return(0);}//写
if(xie1(0x06)!=0){xie_stop();return(0);}//星期字节地址
xie_start(); //起始条件
if(xie1(0xa3)!=0){xie_stop();return(0);}//读
xingqi=du1(); //读星期
xingqi&=0x07; //保留低3位
xie_stop(); //停止条件
xie_start(); //起始条件
if(xie1(0xa2)!=0){xie_stop();return(0);}//写
if(xie1(0x08)!=0){xie_stop();return(0);}//年份字节地址
xie_start(); //起始条件
if(xie1(0xa3)!=0){xie_stop();return(0);}//读
nian=du1(); //读年份
xie_stop(); //停止条件
EA=1; //开总中断
return(1);
}
uchar xie()
{
EA=0;
xie_start(); //起始条件
if(xie1(0xa2)!=0){xie_stop();return(0);}//取器件地址 写
if(xie1(0x02)!=0){xie_stop();return(0);}//写秒地址
if(xie1(miao)!=0){xie_stop();return(0);}//写秒
xie_stop(); //停止条件
xie_start(); //起始条件
if(xie1(0xa2)!=0){xie_stop();return(0);}//取器件地址 写
if(xie1(0x03)!=0){xie_stop();return(0);}//写分地址
if(xie1(fen)!=0){xie_stop();return(0);} //写分
xie_stop();
xie_start(); //起始条件
if(xie1(0xa2)!=0){xie_stop();return(0);}//取器件地址 写
if(xie1(0x04)!=0){xie_stop();return(0);}//写小时地址
if(xie1(shi)!=0){xie_stop();return(0);} //写时
xie_stop();
xie_start(); //写日期
if(xie1(0xa2)!=0){xie_stop();return(0);}
if(xie1(0x05)!=0){xie_stop();return(0);}
if(xie1(riqi)!=0){xie_stop();return(0);}
xie_stop();
xie_start(); //写月份
if(xie1(0xa2)!=0){xie_stop();return(0);}
if(xie1(0x07)!=0){xie_stop();return(0);}
if(xie1(yuefen)!=0){xie_stop();return(0);}
xie_stop();
xie_start(); //写星期
if(xie1(0xa2)!=0){xie_stop();return(0);}
if(xie1(0x06)!=0){xie_stop();return(0);}
if(xie1(xingqi)!=0){xie_stop();return(0);}
xie_stop();
xie_start(); //写年份
if(xie1(0xa2)!=0){xie_stop();return(0);}
if(xie1(0x08)!=0){xie_stop();return(0);}
if(xie1(nian)!=0){xie_stop();return(0);}
xie_stop();
EA=1;
return(1);
}
/* uchar xie_dingshi() //定时器
{
xie_start(); // 控制中断开关
if(xie1(0xa2)!=0){xie_stop();return(0);}
if(xie1(0x01)!=0){xie_stop();return(0);}
if(xie1(0x01)!=0){xie_stop();return(0);}
xie_start(); //定时器控制器
if(xie1(0xa2)!=0){xie_stop();return(0);}
if(xie1(0x0e)!=0){xie_stop();return(0);}
if(xie1(0x82)!=0){xie_stop();return(0);}
xie_start(); //计数器
if(xie1(0xa2)!=0){xie_stop();return(0);}
if(xie1(0x0f)!=0){xie_stop();return(0);}
if(xie1(5)!=0){xie_stop();return(0);}
xie_stop();
return(1);
} */
/* uchar xie_fangbo_dingshi()
{
xie_start(); //方波频率
if(xie1(0xa2)!=0){xie_stop();return(0);}
if(xie1(0x0d)!=0){xie_stop();return(0);}
if(xie1(0x82)!=0){xie_stop();return(0);}
xie_stop();
return(1);
} */
/////////////////////以上是PCF8563读写/////////////////////以上是PCF8563读写
/*---------------延时子程序----------------*/
void delay1 (uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<1000;j++);
}
/*------------写数据或命令到LCD--------------*/
void wr_lcd (uchar dat_comm,uchar content)
{
chk_busy (); //
di=dat_comm; //命令标志
rw=0; //写
hc595SendByte(content);
Out595(); //写数据到LCD的数据端
e=1; //下降沿锁定数据
e=0;
}
/*--------------读LCD数据---------------*/
/* uchar rd_lcd ()
{
uchar rddata;
chk_busy ();
di=1;
rw=1; //读
e=1;
rddata=data_ora; //读数据
e=0; //下降沿锁定数据
return rddata;
} */
void chk_busy()
{
hc595SendByte(1);
Out595(); //写1到LCD的数据端
di=0;
rw=1; //读
e=1;
//while(bf||res==1);
e=0;
}
/*--------------写点阵------------------*/
void lat_disp (uchar data1,uchar data2)
{
uchar i,j;
cs1=1;cs2=1; //左右屏幕同时选中
wr_lcd (comm,disp_on); //写命令字,显示开
for(j=0;j<8;j++)
{
wr_lcd (comm,disp_x+j); //页地址调整
wr_lcd (comm,disp_z); //指向对应页的0行
wr_lcd (comm,disp_y); //指向对应页的0列
for(i=0;i<32;i++) //写64个数据
{
wr_lcd (dat1,data1);
wr_lcd (dat1,data2);
}
}
}
/*--------------显示图片----------------*/
/* void img_disp (uchar code *img)
{
uchar i,j;
for(j=0;j<8;j++)
{
cs1=0;cs2=1; //左半屏幕选中
wr_lcd (comm,disp_on); //写命令字,显示开
wr_lcd (comm,disp_x+j); //页地址调整
wr_lcd (comm,disp_z); //指向对应页的0行
wr_lcd (comm,disp_y); //指向对应页的0列
for(i=0;i<64;i++) //写64个数据
wr_lcd (dat1,img[j*128+i]);
cs1=1;cs2=0; //右半屏幕选中
wr_lcd (comm,disp_on); //写命令字,显示开
wr_lcd (comm,disp_x+j); //页地址调整
wr_lcd (comm,disp_z); //指向对应页的0行
wr_lcd (comm,disp_y); //指向对应页的0列
for(i=64;i<128;i++) //写64个数据
wr_lcd (dat1,img[j*128+i]);
}
} */
/*---指定位置(x,y)显示row_xl行(每行row_yl个)汉字(大小8xl*yl)---*/
void chn_disp (uchar x,uchar y,uchar xl,uchar yl,uchar row_xl,uchar row_yl,uchar code *chn)
{
uchar i,j,k,l,a;
wr_lcd (comm,disp_on); //写命令字,显示开
for(l=0;l<row_xl;l++) //行数循环
{
for(k=0;k<row_yl;k++) //字数循环
{
for(j=0;j<xl;j++)
{
wr_lcd (comm,disp_x+x+l*xl+j); //行数调整
wr_lcd (comm,disp_z); //列数调整
wr_lcd (comm,disp_y+y+k*yl); //字数调整
a=l*xl*yl*row_yl+k*xl*yl+j*yl;
for(i=0;i<yl;i++) //写数据
wr_lcd (dat1,chn[a+i]);
}
}
}
}
/*------------------初始化-----------------*/
void init_lcd ()
{
//rst=0;
delay(50);
//rst=1;
cs1=1;cs2=1; //左右屏幕同时选中
wr_lcd (comm,disp_off); //写命令字,显示关
wr_lcd (comm,disp_on); //写命令字,显示开
}
void disp()
{
cs1=0;cs2=1; //左半屏幕选中
chn_disp (0,8,2,8,1,1,tabma[2]); //固定显示数字“2”
chn_disp (0,16,2,8,1,1,tabma[0]); //固定显示数字“0”
chn_disp (0,24,2,8,1,1,tabma[nian/16]); //显示年的数字十位
chn_disp (0,32,2,8,1,1,tabma[nian%16]); //显示年的数字个位
chn_disp (0,40,2,16,1,1,nianma); //固定显示汉字“年”
chn_disp (0,56,2,8,1,1,tabma[yuefen/16]); //显示月份的数字十位
cs1=1;cs2=0; //右半屏幕选中
chn_disp (0,0,2,8,1,1,tabma[yuefen%16]); //显示月份的数字个位
chn_disp (0,8,2,16,1,1,yuema); //固定显示汉字“月”
chn_disp (0,24,2,8,1,1,tabma[riqi/16]); //显示日期的数字十位
chn_disp (0,32,2,8,1,1,tabma[riqi%16]); //显示日期的数字个位
chn_disp (0,40,2,16,1,1,rima); //固定显示汉字“日”
cs1=0;cs2=1; //左半屏幕选中
chn_disp (3,8,2,16,1,2,xingqima); //固定显示汉字“星期”
chn_disp (3,40,2,16,1,1,tab2ma[xingqi]); //显示汉字星期几
chn_disp (6,16,2,8,1,1,tabma[shi/16]); //显示小时的数字十位
chn_disp (6,24,2,8,1,1,tabma[shi%16]); //显示小时的数字个位
chn_disp (6,32,2,16,1,1,shima); //固定显示汉字“时”
chn_disp (6,48,2,8,1,1,tabma[fen/16]); //显示分钟的数字十位
chn_disp (6,56,2,8,1,1,tabma[fen%16]); //显示分钟的数字个位
cs1=1;cs2=0; //右半屏幕选中
chn_disp (6,0,2,16,1,1,fenma); //固定显示汉字“分”
chn_disp (6,16,2,8,1,1,tabma[miao/16]); //显示秒的数字十位
chn_disp (6,24,2,8,1,1,tabma[miao%16]); //显示秒的数字个位
chn_disp (6,32,2,16,1,1,miaoma); //固定显示汉字“秒”
}
|