PCF8563 driver (C language version)

Publisher:化羽2015Latest update time:2016-04-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//CPU: STC89C52RC  crystal: 11.0592M

#include 〈reg52.h>

#include 

#include 

#define uchar unsigned char

#define uint unsigned int

uchar TAB_T[]={ 0x31,0x06,0x10,//seconds, minutes, hours

0x13,0x01,0x07,0x08}; //Day, week, month, year.

sbit SDA=P2^6; //data

sbit SCL=P2^7; //clock

 

 

void Delay5us(void)

{

_nop_();

_nop_();

_nop_();

_nop_();

_nop_();

}

 

void Start(void)

{

SDA=1;

SCL=1;

Delay5us();

SDA=0;

Delay5us();

}

 

void Stop(void)

{

SDA=0;

SCL=1;

Delay5us();

SDA=1;

Delay5us();

 }

 

void Reack(void)

{

fly i=0;

SCL=1; //Ready to detect SDA Delay5us();

while((SDA==1)&&(i<100))i++; //SDA=0 is a response signal, SDA=1 is a non-response signal

SCL=0; //Prepare for the next change data

}

 

void WriteByte(volatile ucByte)

{

flying i;

SCL=0;

for(i=0;i<8;i++)

{

SDA=(bit)(ucByte&0x80); //Write high bit first

SCL=1;

Delay5us();

SCL=0;

ucByte<<=1;

}

SDA=1; //Release data line

}

 

fly ReadByte(void)

{

float i,ucByte;

SCL=0;

for(i=0;i<8;i++)

{

ucByte<<=1;

if(SDA)

ucByte++;

SCL=1;

Delay5us();

SCL=0;

}

return ucByte;

}

 

void Write8563(fly ucAddr,fly ucData)

{

Start();

WriteByte(0xa2);

Reack();

WriteByte(ucAddr);

Reack();

WriteByte(ucData);

Reack();

Stop();

}

 

fly Read8563(fly ucAddr)

{

void ucData;

Start();

WriteByte(0xa2); //Write device address

Reack();

WriteByte(ucAddr); //write byte address

Reack();

Start();

WriteByte(0xa3); //Write device address, the lowest value is 1 for read

Reack();

ucData=ReadByte(); //Write byte address

Stop();

return ucData; //read data

}

 

void Init8563(void)

{

fly i,ucAddr=0x02;

Write8563(0x00,0x00);

Write8563(0x01,0x11);

for(i=0;i<7;i++)

{

Write8563(ucAddr,TAB_T[i]);

ucAddr++;

}

}

 

void GetTime(void)

{

float i,ucData1,ucData2,ucAddr=0x02;

uchar *pTime=TAB_T;

for(i=0;i<7;i++)

{

 pTime[i]=Read8563(ucAddr);

ucAddr++;

}

pTime[0]&=0x7f; //shield invalid bits

pTime[1]&=0x7f;

pTime[2]&=0x3f;

pTime[3]&=0x3f;

pTime[4]&=0x07;

pTime[5]&=0x1f;

for(i=0;i<7;i++)

{

ucData1=pTime[i]/16; //Convert BCD code to hexadecimal

ucData2=pTime[i]%16;

pTime[i]=ucData1*10+ucData2;

}

 

}

Reference address:PCF8563 driver (C language version)

Previous article:51 single chip microcomputer controls ISD1420 to realize the function of selecting and playing segments
Next article:C51_LCD1602 display

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号