⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 12887.c

📁 时钟芯片12c887非常实用
💻 C
字号:
#include <reg52.h>
#include <absacc.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <intrins.h>

#define uchar unsigned char
#define uint unsigned int

#define DSA XBYTE[0x7F0A]/*P2.7接cs*/
#define DSB XBYTE[0x7F0B]/*寄存器定义地址*/
#define DSS XBYTE[0x7F00]
#define DSM XBYTE[0x7F02]
#define DSH XBYTE[0x7F04]
#define DSYEAR XBYTE[0x7F09]
#define DSMONTH XBYTE[0x7F08]
#define DSDAY XBYTE[0x7F07]
#define DSWEEK XBYTE[0x7F06]
#define DS XBYTE[0x7F20]

sbit busy=P2^0;/*端口定义*/
sbit req=P2^1;

uchar year_h,year_l,month_h,month_l,day_h,day_l,week_l;/*定义年、月、日高低位*/
uchar H_h,H_l,M_h,M_l,S_h,S_l;/*定义时、分、秒高低位*/
uchar year,month,day,week,H,M,S;/*定义年月日时分秒*/
uchar HBIT;/*定义高位*/
uchar LBIT;/*定义低位*/

void delay(uint N)   //延时程序9us
{
  uchar i;
  for(i=0;i<N;i++);
 }


void ds_read(void)   //读取DS12C887数据                                                              /*选中列*/
{
    //寄存器赋给各名称
    S=DSS;
    M=DSM;
    H=DSH;
    month=DSMONTH;
    day=DSDAY;
    week=DSWEEK;
    year=DSYEAR;
}

void ds_begin()   //时钟初始化(各寄存器的初始化)
{
   DSB=0xA2;
   DSS=0x00;
   DSM=0x00;
   DSH=0x08;
   DSYEAR=0x08;
   DSMONTH=0x07;
   DSDAY=0x27;
   DSWEEK=0x07;
   DS=0x56;
   DSA=0x20;
   DSB=0x22;
}

void convert(uchar x)   //8位数据转换高低4位
{

     LBIT=x&0x0f;
     HBIT=(x>>4)&0x0f;

}

void shujuchuli()
{
    convert(year);
    year_h=HBIT+0x30;
    year_l=LBIT+0x30;
    convert(month);
    month_h=HBIT+0x30;
    month_l=LBIT+0x30;
    convert(day);
    day_h=HBIT+0x30;
    day_l=LBIT+0x30;
    convert(week);
    week_l=LBIT+0x30;
    convert(H);
    H_h=HBIT+0x30;
    H_l=LBIT+0x30;
    convert(M);
    M_h=HBIT+0x30;
    M_l=LBIT+0x30;
    convert(S);
    S_h=HBIT+0x30;
    S_l=LBIT+0x30;
}





void lcd_int(void)  //LCD初始化程序
{
  req=0;
  busy=1;
}

void dlus(void)       /*12us延时子程序*/
{
  uchar i;
  for(i=5;i>2;i--);
}

void ocmj_write(uchar word)      /*写子程序*/
{
  while(busy==1);
  P0=word;
  dlus();
  req=1;
  while(busy==0);
  req=0;
}

void zifu16_tran(uchar x,uchar y,uchar ROW)  //写字符子程序8×16
{
  ocmj_write(0xf9);
  ocmj_write(x);
  ocmj_write(y);
  ocmj_write(ROW);
}

void hanzi_tran(uchar x,uchar y,uchar ROW,uchar CLOW)  //写汉字子程序
{
  ocmj_write(0xf0);
  ocmj_write(x);
  ocmj_write(y);
  ocmj_write(ROW);
  ocmj_write(CLOW);
}

void xianshi()
{
   hanzi_tran(0x02,0x00,0x2a,0x5d);/*数*/
   hanzi_tran(0x03,0x00,0x37,0x36);/*字*/
   hanzi_tran(04,0x00,0x2a,0x11);/*时*/
   hanzi_tran(0x05,0x00,0x36,0x33);/*钟*/
   hanzi_tran(0x07,0x00,0x16,0x01);/*丁 */
   hanzi_tran(0x08,0x00,0x30,0x02);/*孝*/
   hanzi_tran(0x09,0x00,0x32,0x45);/*义*/	            
   hanzi_tran(0x02,0x01,0x28,0x35);/*日*/
   hanzi_tran(0x03,0x01,0x26,0x3a);/*期*/
   hanzi_tran(0x02,0x02,0x2a,0x11);/*时*/
   hanzi_tran(0x03,0x02,0x1c,0x44);/*间*/
   hanzi_tran(0x02,0x03,0x30,0x27);/*星*/
   hanzi_tran(0x03,0x03,0x26,0x3a);/*期*/
}

void shizhongxianshi()
{  zifu16_tran(0x0c,0x00,0x42);
   zifu16_tran(0x0d,0x00,0x79);
   zifu16_tran(0x09,0x11,year_h);
   zifu16_tran(0x0a,0x11,year_l);
   zifu16_tran(0x0b,0x11,0x2d);
   zifu16_tran(0x0c,0x11,month_h);
   zifu16_tran(0x0d,0x11,month_l);
   zifu16_tran(0x0e,0x11,0x2d);
   zifu16_tran(0x0f,0x11,day_h);
   zifu16_tran(0x10,0x11,day_l);
   zifu16_tran(0x09,0x21,H_h);
   zifu16_tran(0x0a,0x21,H_l);
   zifu16_tran(0x0b,0x21,0x3a);
   zifu16_tran(0x0c,0x21,M_h);
   zifu16_tran(0x0d,0x21,M_l);
   zifu16_tran(0x0e,0x21,0x3a);
   zifu16_tran(0x0f,0x21,S_h);
   zifu16_tran(0x10,0x21,S_l);
   zifu16_tran(0x09,0x31,week_l);

}



void shizhong()
{
// if (DS!=0x56)
   ds_begin();
   ds_read();
   shujuchuli();
   shizhongxianshi();
}

void main()
{
  DSA=DSA&0xaf;
  DSA=DSA|0x20;
  DSB=DSB&0x7b;
  DSB=DSB|0x02;

   lcd_int();
   ocmj_write(0xf4);
   xianshi();
   ds_begin();
   while(1)
   {
      shizhong();
   }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -