DS12C887 real-time clock

FIG physical
2
pin definitions
1
GND、 VCC : a DC power supply, wherein the + 5V input connected to VCC, GND is grounded, when the VCC input is + 5V, the user can access the data in the DS12C887 RAM can be read and write operations; when VCC is when the input is less than + 4.25V, to prevent users from internal RAM read and write operations, then the user can not acquire the correct time information in the chip; VCC when the input is less than + 3V, DS12C887 will automatically change the power sent to its own internal the lithium battery.
MOT: Mode select pin, DA12C887 has two operating modes, namely Motorola and Intel mode mode, when the MOT then VCC, the operation mode is selected Motorola mode, when the MOT then GND, Intel mode is selected. This article focuses on Intel mode.
SQW: Square wave output pin, when the supply voltage VCC is greater than 4.25V, SQW pin can be square wave output, and the user can be obtained by the output square wave signal of 13 kinds of control register programming.
AD0~AD7: Multiplexed address data bus, which time-division multiplexing, in the first half of the bus cycle, appears on the address AD0-AD7 information in RAM may be used to gate the DS12C887, it appears in the second half of the bus cycle data on AD0-AD7.
AS: Address strobe input pin, during read and write operations, the rising edge of the AS latched address information appearing on AD0-AD7 to the DS12C887, and the next address information on the edge clear AD0-AD7, whether valid , DS12C887 will perform the operation.
DS/RD: Read data selection or input pin, the pin has two modes, when connected to the VCC MOT, Motorola selected operation mode, in this mode of operation, after the DS portion of each bus cycle is high, the referred to as data strobe. In the read operation, the rising edge of the internal DS12C887 DS causes data to the bus AD0-AD7, for external reading. In a write operation, the falling edge of the presence of DS will lock on DS12C887 data bus AD0-AD7.
R/W: Read / write input, this pin also has two modes of operation, when the MOT connected VCC, R / W mode works in Motorola. At this time, the action pin is performed to distinguish between a read or write operation, when the R / W is high read operation, the R / W is low when a write operation; MOT connected the GND when the foot Intle work mode, at which point as the write enable input.
CS: Chip select input, active low.
3
For example

#define uchar unsigned char
#define uint unsigned int

sbit dsds = P4 ^ 4;
sbit dsrw = P4 ^ 2;
sbit dsas = P4 ^ 5;
sbit dscs = P2 ^ 0;

//写12C887函数
void write_ds(uchar addr, uchar date)
{
   dscs = 0;
   dsas = 1;
   dsds = 1;
   dsrw = 1;
   P0 = addr; //先写地址
   dsas = 0;
   dsrw = 0;
   P0 = addr; //再写数据
   dsrw = 1;
   dsas = 1;
   dscs = 1;
}

//读12C887函数
uchar read_ds(uchar addr)
{
   uchar ds_date;

   dsas = 1;
   dsds = 1;
   dsrw = 1;
   dscs = 0;
   P0 = addr; //先写地址
   dsas = 0;
   dsds = 0;
   P0 = 0xff;
   ds_date = P0; //再读数据
   dsds = 1;
   dsas = 1;
   dscs = 1;

   return ds_date;
}

//读取12C887数据
void read_time(void)
{
   char hour, minute, second, year, month, date, day;

   year = read_ds(9);
   month = read_ds(8);
   date = read_ds(7);
   day = read_ds(6);
   hour = read_ds(4);
   minute = read_ds(2);
   second = read_ds(0);
}

/*
	首次操作12C887时,寄存器初始化
	配置12C887当前时间(20190716-10:54:07)
	配置12C887闹钟
*/
void set_time(void)
{
   write_ds(0, 7);
   write_ds(1, 0);
   write_ds(2, 54);
   write_ds(3, 0);
   write_ds(4, 10);
   write_ds(5, 0);
   write_ds(6, 3);
   write_ds(7, 16);
   write_ds(8, 7);
   write_ds(9, 19);
}

/*首次上电设置DS12C887时使用,以后不必再调用*/
void init(void)
{
   write_ds(0x0A, 0x20); //打开振荡器
   write_ds(0x0B, 0x26); //设置24小时模式,数据二进制格式
   set_time();
}

Guess you like

Origin blog.csdn.net/zhangxuechao_/article/details/96101153