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
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
// 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
{
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
SDA=1;
else
SDA=0;
i=2;while(--i);
SCL=1;
i=2;while(--i);
da
}
//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
SCL=0;
i=2;while(--i);
SDA=1;
for(j=0;j<8;j++) //Receive 1 bit at a time
{
da
SCL=1;
i=2;while(--i);
if(SDA) da
i=2;while(--i);
SCL=0;
i=2;while(--i);
}
return da
}
//---------------------------------------------------------------------------------
// 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--);
}
Previous article:51 programs for matrix keyboard
Next article:X9313 51 programs
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Application of TI's high-performance charging and discharging solution in battery test equipment (Battery Test System)
- Using millimeter waves to count and track people
- LSM6DSO I2C HUB Mode
- Fundamentals of Analog Electronics: A Systems Approach
- Help with simplification studio sdk installation
- [Top Micro Intelligent Display Module] 1: Unboxing
- Jetson Nano Remote Desktop cannot open terminal
- Could you please tell me what this operation of 1602 LCD means and what is its function?
- robot
- The digital signal and noise are not much different (amplitude and frequency). Can software algorithms be used to extract the signal and filter out the noise?