51 programs of PCF8563

Publisher:心愿成真Latest update time:2016-10-30 Source: eefocusKeywords:PCF8563 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

sbit SDA=P2^2;
sbit SCL=P2^1;
void start(void); // I2C start
void stop(void); // I2C end
void ACK(void); // CPU response
void NACK(void); // CPU non-response
bit Sendbyte(unsigned char data0 ); // Send 1 byte data or address to pcf8563
unsigned char Receivebyte(void); // Read 1 byte data from pcf8563
void Set8563(unsigned char *p2); // Set the initial time of pcf8563
void Get8563(void); // Read the current time of pcf8563
void delaynms(unsigned char k); // ms delay
void delay1s(); // Delay subroutine 1s
void leddisplay(); // Digital tube display
unsigned char ledxs[8]; // Year-month-day hour-minute-second
unsigned char time[7]; // Current time, format: second minute hour day week month year
unsigned char settime[9]={0x00,0x1f,0x00,0x28,0x09,0x27,0x02,0x04,0x10};
           // Time initial value: control 1 control 2 second minute hour day week month year unsigned
char code tab[]={0xb7,0x12,0x67,0x76,0xd2,0xf4,0xf5,0x16,0xf7,0xf6,0xd7,0xf1,0xa5,0x73,0xe5,0xc5,0,0xff,0x40};
                          // Common cathode code 0-F, all off, all on, '-'
void main()
{
  unsigned char i,j,k;
  delaynms(200);
  Set8563(settime); // Set initial timewhile
  (1)
  {
    k=3;
    while(k>0) // Year-month-day display for 3 seconds (10-04-27)
    {
      Get8563(); // Get current time
      ledxs[0]=time[6]>>4;
      ledxs[1]=time[6]&0x0f; // Year
      ledxs[2]=18; // -
      ledxs[3]=(time[5]&0x1f)>>4;
      ledxs[4]=(time[5]&0x1f)&0x0f; // Month
      ledxs[5]=18; // -
      ledxs[6]=(time[3]&0x3f)>>4;
      ledxs[7]=(time[3]&0x3f)&0x0f; // Day
      for(i=0;i<8;i++) // Display
      {
        SBUF=tab[ledxs[i]];
        while(TI==0);
        TI=0;
      }
      k--;
      delay1s();
     }
     k=100;
     while(k>0) // Hour-minute-second display 10 seconds (09-28-00)
     {
       Get8563(); //Get the current time
       ledxs[0]=(time[2]&0x3f)>>4;
       ledxs[1]=(time[2]&0x3f)&0x0f;// Hour
       ledxs[2]=18;
       ledxs[3]=(time[1]&0x7f)>>4;
       ledxs[4]=(time[1]&0x7f)&0x0f; // Minute
       ledxs[5]=18;
       ledxs[6]=(time[0]&0x7f)>>4;
       ledxs[7]=(time[0]&0x7f)&0x0f; // Seconds
       for(i=0;i<8;i++) //Display
       {
         SBUF=tab[ledxs[i]];
         while(TI==0);
         TI=0;
       }
       k--;
       delaynms(100);
     }
   }
}

//-------------------------------------------------------------------
//CPU generates I2C start signal, SCL high period, SDA from 1 to 0
//------------------------------------------------------------------
void start()
{
  unsigned char i;
  SDA=1; //SDA set to 1
  i=2;while(--i);
  SCL=1; //SCL set to high level
  i=2;while(--i);
  SDA=0; //SDA cleared to 0
  i=2;while(--i);
  SCL=0; //SCL returns to low level
}
//---------------------------------------------------------------------
//CPU generates I2C end signal, SCL high period, SDA from 0 to 1
//--------------------------------------------------------------------
void stop()
{
  unsigned char i;
  SDA=0; //SDA cleared to 0
  i=2;while(--i);
  SCL=1; //SCL set to high level
  i=2;while(--i);
  SDA=1; //SDA set to 1
  i=2;while(--i);
  SCL=0; //SCL returns to low level
}
//--------------------------------------------------------------------------------------
//CPU generates an acknowledgement signal (after the CPU receives data) and the receiver generates an acknowledgement or non-acknowledgement signal
//24c64 generates it automatically, and the cpu needs to call the program
//---------------------------------------------------------------------------------------
void ACK()
{
  unsigned char i;
  SDA=0; //SDA is cleared to 0
  i=2;while(--i);
  SCL=1;
  i=2;while(--i);
  SCL=0;
  i=2;while(--i);
  SDA=1;
}
//-------------------------------------------------------
//CPU generates a non-acknowledgement signal (after the CPU receives data)
//-------------------------------------------------------
void NACK()
{
  unsigned char i;
  SDA=1; //SDA is set to 1
  i=2;while(--i);
  SCL=1;
  i=2;while(--i);
  SCL=0;
  i=2;while(--i);
}
//-------------------------------------------------------------------------------------------
//CPU sends a byte of data or address to pcf8563, and checks the confirmation signal sent back by pcf8563
//Return the flag bit flag, 0 for response, 1 for non-response.
//-------------------------------------------------------------------------------------------
bit Sendbyte(unsigned char da ta0)
{
  unsigned char i,j;
  bit flag;
  for(j=0;j<8;j++) //Send a byte (send 1 bit at a time)
  {
    SCL=0;
    i=2;while(--i);
    if(da ta0&0x80) //Take the highest bit of da ta0
       SDA=1;
    else
       SDA=0;
    i=2;while(--i);
    SCL=1;
    i=2;while(--i);
    da ta0=data0<<1; //da ta0 shifted left by 1 bit
  }
//The following is to check the response signal of pcf8563
  SCL=0;
  i=2;while(--i);
  SDA=1; //Pull the data line high
  i=2;while(--i);
  SCL=1;
  i=2;while(--i);
  flag=SDA; //Read the response signal
  SCL=0;
  i=2;while(--i);
  return flag; //pcf8563 receives correctly and returns 0(ACK); incorrectly receives and returns 1(NOACK).
}
//--------------------------------------------------
//CPU receives a byte sent by pcf8563
//--------------------------------------------------
unsigned char Receivebyte()
{
  unsigned i,j,da ta0;
  SCL=0;
  i=2;while(--i);
  SDA=1;
  for(j=0;j<8;j++) //Receive 1 bit at a time
  {
    da ta0=da ta0<<1;
    SCL=1;
    i=2;while(--i);
    if(SDA) da ta0=da ta0+1; //The received data has the lowest bit
    i=2;while(--i);
    SCL=0;
    i=2;while(--i);
  }
  return da ta0; //Return received data
}

//---------------------------------------------------------------------------------
// Function: Set the initial time
// The format of the initial time is: second minute hour day week month year
// Write 9 bytes to pcf8563, the first 2 are control words, and the last 7 are the initial time
//---------------------------------------------------------------------------------
void Set8563(unsigned char *p1)
{        
  unsigned char flag,count=9,addr=0x00;
  start(); // CPU sends start signal
  flag=Sendbyte(0xa2); // CPU sends device address
  while(flag) {Sendbyte(0xa2);} // Check confirmation signal, if incorrect, resend
  flag=Sendbyte(addr); // CPU sends starting unit address
  while(flag) {Sendbyte(addr);} // Check confirmation signal, if incorrect, resend
  while(count--)
  {
    flag=Sendbyte(*p1); // Send 1 data
    while(flag) {Sendbyte(*p1);} // Check confirmation signal, if incorrect, resend
    p1++; // Modify the pointer
  }
  stop(); //CPU sends the end signal
  delaynms(10);
}
//------------------------------------------------------------------------
// Function: Read the current time of DS1302
// The time format is: second minute hour day week month year
// Read 7 bytes
//------------------------------------------------------------------------
void Get8563(void)
{
  unsigned char i,flag,addr=0x02;
  // The following is a pseudo writestart
  (); // CPU sends a start signalflag
  =Sendbyte(0xa2); // CPU sends the device addresswhile
  (flag) {Sendbyte(0xa2);} // Check the confirmation signal, if it is incorrect, resendflag
  =Sendbyte(addr); // CPU sends the starting unit addresswhile
  (flag) {Sendbyte(addr);} // Check the confirmation signal, if it is incorrect, resend
  //The following is reading n bytesstart
  (); // CPU sends a start signalflag
  =Sendbyte(0xa3); // CPU sends the device addresswhile
  (flag) {Sendbyte(0xa3);}
  for(i=0;i<6;i++) // read the first 6 bytes
  {
    time[i]=Receivebyte(); // read 1 byte
    ACK(); // cpu sends a response signal
    addr++; // modify the address
  }
  time[6]=Receivebyte(); // read the last byte
  NACK(); // cpu sends a non-response signal
  stop(); // CPU sends an end signal
}
void delaynms(unsigned char k) //delay n ms
{
  while(k)
  {
  int i;
  i=110;
  while(i--);
  k=k-1;
  }
}
void delay1s() //delay 1 s
{
  int i;
  i=56000;
  while(i--);
  i=56000;
  while(i--);
}

Keywords:PCF8563 Reference address:51 programs of PCF8563

Previous article:51 programs for matrix keyboard
Next article:X9313 51 programs

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号