分享一个基于DS12C887和LCD1602显示的数字时钟,下面是仿真原理图:
单片机源程序如下:
- #include <AT89X52.h>
- #define DSbus P0
- #define LCDbus P1
- //定义DS12C887和LCD的控制线
- sbit DS_CS = P2^7; //引脚13,片选信号输入,低电平有效。
- sbit DS_AS = P2^4; //引脚14,地址选通输入。
- sbit DS_RW = P2^5; //引脚15,读/写输入。
- sbit DS_DS = P2^6; //引脚17,数据选通或读输入。
- sbit LCD_RS=P2^0;
- sbit LCD_EN=P2^2;
- //时间变量定义
- unsigned char Counter;
- unsigned char Hour,Min,Sec,Year,Month,Date,Week;
- /*DS12CR887驱动程序---------------------------------------------------------------------------*/
- //往DS12CR887写数据函数
- void DS12887write(unsigned char add,unsigned char Date)
- {
- DS_CS=0;
- DS_DS=1;
- DS_RW=1;
- DS_AS=1;
- DSbus=add;
- DS_AS=0;
- DS_RW=0;
- DSbus=Date;
- DS_RW=1;
- DS_AS=1;
- DS_CS=1;
- }
- //读取DS12CR887数据函数
- unsigned char DS12887read(unsigned char add)
- {
- unsigned char z;
- DS_CS=0;
- DS_RW=1;
- DS_DS=1;
- DS_AS=1;
- DSbus=add;
- DS_AS=0;
- DS_DS=0;
- DSbus=0xff;
- z=DSbus;
- DS_DS=1;
- DS_AS=1;
- DS_CS=1;
- return z;
- }
- //DS12CR887初始化函数
- void DS12887LCDinit()
- {
- DS_AS=0; DS_DS=0; DS_RW=0;
- DS12887write(0x0a,0x20);//DS12CR887寄存器A功能设置,开启时钟振荡器
- DS12887write(0x0b,0x06);//寄存器B功能设置,不开启闹钟中断使能,数据模式为二进制,24小时模式。
- //DS12887write(4,0x8);DS12887write(2,0x00);DS12887write(0,0x00); //给DS12CR887的时分秒赋值,开机后显示8:00:00
- }
- void Delay(unsigned int z)
- {
- unsigned int x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void LCDwritecom(unsigned char com)
- {
- LCD_RS=0;
- LCDbus=com;
- Delay(5);
- LCD_EN=1;
- Delay(5);
- LCD_EN=0;
- }
- void LCDwritecomdata(unsigned char dat)
- {
- LCD_RS=1;
- LCDbus=dat;
- Delay(5);
- LCD_EN=1;
- Delay(5);
- LCD_EN=0;
- }
- void LCDinit()
- {
- LCD_EN=0;
- LCDwritecom(0x38);
- LCDwritecom(0x0c);
- LCDwritecom(0x06);
- LCDwritecom(0x01);
- LCDwritecom(0x80);
- }
- void Timedisplay(void)
- {
- //LCDwritecom(1);
- LCDwritecom(0x80);
- //往液晶屏填写"小时"数据-----------------------------------------------
- Hour=DS12887read(4); //读取DS12CR887的小时数据
- if((Hour/10)==0)LCDwritecomdata(0);
- else LCDwritecomdata(Hour/10+0x30);//小时十位
- LCDwritecomdata(Hour%10+0x30); //小时个位
- LCDwritecomdata(':'); //时钟分隔符":"
- //往液晶屏填写"分钟"数据-----------------------------------------------
- Min=DS12887read(2); //读取DS12CR887的分数据
- LCDwritecomdata(Min/10+0x30);
- LCDwritecomdata(Min%10+0x30);
- LCDwritecomdata(':'); //时钟分隔符":"
- //往液晶屏填写"秒"数据-------------------------------------------------
- Sec=DS12887read(0); //读取DS12CR887的秒数据
- LCDwritecomdata(Sec/10+0x30);
- LCDwritecomdata(Sec%10+0x30);
- Delay(100);
- }
- void Datedisplay(void)
- {
- //LCDwritecom(1);
- LCDwritecom(0xc0);
- //往液晶屏填写"年"数据-----------------------------------------------
- LCDwritecomdata('2');
- LCDwritecomdata('0');
- Year=DS12887read(9); //读取DS12CR887的年数据
-
- LCDwritecomdata(Year/10+0x30);//年十位
- LCDwritecomdata(Year%10+0x30); //年个位
- LCDwritecomdata('/'); //时钟分隔符":"
- //往液晶屏填写"月"数据-----------------------------------------------
- Month=DS12887read(8); //读取DS12CR887的月数据
- LCDwritecomdata(Month/10+0x30);
- LCDwritecomdata(Month%10+0x30);
- LCDwritecomdata('/'); //时钟分隔符":"
- //往液晶屏填写"日"数据-------------------------------------------------
- Date=DS12887read(7); //读取DS12CR887的日数据
- LCDwritecomdata(Date/10+0x30);
- LCDwritecomdata(Date%10+0x30);
- //往液晶屏填写"星期"数据-------------------------------------------------
- Week=DS12887read(6); //读取DS12CR887的日数据
- LCDwritecomdata(0);
- LCDwritecomdata(Week-1+0x30);
- Delay(100);
- }
- void main()
- {
- // unsigned char i;
- LCDinit();
- DS12887LCDinit();
- DS12887write(0x0a,0x00); //开始调时,DS12CR887关闭时钟振荡器
- DS12887write(0,55); //秒
- DS12887write(2,59);
- DS12887write(4,23);
- DS12887write(6,5); //星期
- DS12887write(7,22); //日
- DS12887write(8,9); //
- DS12887write(9,12); //
- //display_Date();
- while(1)
- {
- Timedisplay();
- Datedisplay();
- Delay(100);
- }
- }
复制代码
仿真工程文件和完整源码下载:
数字时钟.rar
(53.08 KB, 下载次数: 210)
|