DS1302 read and write program (IIC bus)

Publisher:春水碧于天Latest update time:2015-01-22 Source: 51heiKeywords:DS1302 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include //Header file containing microcontroller registers
#include //Header file containing _nop_() function definition
#define uchar unsigned char
#define uint unsigned int
 
sbit SRT=P3^5;
sbit IO=P3^4;
sbit SCK=P3^6;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
     0x66,0x6d,0x7d,0x07,
     0x7f,0x6f,0x77,0x7c,
     0x39,0x5e,0x79,0x71};
uchar miao,fen,shi,tian,yue,zhou,nian;
 
uchar read_DS1302(uchar addr) //read DS1302 subroutine;
{
 fly dat,i,temp,j;
 SRT=0;
 SCK=0;
 IO=0; //Must be set low first, otherwise it is easy to make mistakes;
 SRT=1;
 for(i=0;i<8;i++)//write address (the address of the data to be read);
 {
  SCK=0;
  dat=addr&0x01;
  IO=that;
  addr=addr>>1;
  SCK=1;
  _nop_(); 
 }
// IO=1; //Release the bus for the next read operation,
         //Otherwise the data read out will always be 0x00,
   //But because the highest bit of addr is always 1, when transmitting the highest bit, that is, IO=dat=1,
         //So this sentence can be omitted;
 for(j=0;j<8;j++)//read data;
 {
  SCK=0;
  temp>>=1;
  if(IO)
   temp=temp|0x80;
  SCK=1;
  _nop_();
 }
 SCK=0; //Must be pulled low strictly according to the timing, otherwise an error will occur;
 SRT=0;
 IO=1; //The bus must be released;
 return temp;
}
 
void write_DS1302(uchar add,uchar date) //write DS1302 subroutine;
{
 float dat1,i,dat2,j;
 SRT=0;
 SCK=0;
 IO=0;
 SRT=1;
 _nop_();
 for(i=0;i<8;i++)//write the address first;
 {
  SCK=0;
  dat1=add&0x01;
  add=add>>1;
  IO=dat1;
  SCK=1;
  _nop_();
 }
 for(j=0;j<8;j++)//Write data again;
 {
  SCK=0;
  dat2=date&0x01;
  date=date>>1;
  IO=date2;
  SCK=1;
  _nop_();
 }
 SCK=0;
 SRT=0;
 IO=1; 
}
 
void delay(uint n)
{
 while(n--);
}
 
void display()
{
 fly miao1,miao2,fen1,fen2,shi1,shi2;
 P0=0xff;
 P1=0x00;
 
 miao1=miao&0x0f;
 miao2=miao&0xf0;
 miao2=miao2>>4; 
 
 fen1=fen&0x0f;
 fen2=fen&0xf0;
 fen2=fen2>>4;
 
 shi1=shi&0x0f;
 shi2=shi&0xf0;
 shi2=shi2>>4;
 
 P1=table[miao1];
 P0=0x7f;
 delay(200);
 
 P1=table[miao2];
 P0=0xbf;
 delay(200);
 
 P1=0x40;
 P0=0xdf;
 delay(200);
 
 P1=table[fen1];
 P0=0xef;
 delay(200);
 
 P1=table[fen2];
 P0=0xf7;
 delay(200);
 
 P1=0x40;
 P0=0xfb;
 delay(200);
 
 P1=table[shi1];
 P0=0xfd;
 delay(200);
 
 P1=table[shi2];
 P0=0xfe;
 delay(200);
}
 
void main()
{
//Set date and time (2013/09/11, 21:03:00, Wednesday);
 while(1)
 {
  miao=read_DS1302(0x81);
  fen=read_DS1302(0x83);
  shi=read_DS1302(0x85);
  tian=read_DS1302(0x87);
  yue=read_DS1302(0x89);
  zhou=read_DS1302(0x8b);
  nian=read_DS1302(0x8d); //Read DS18B20 data;
 
  P2=miao;
  display();
 }
 
}
Keywords:DS1302 Reference address:DS1302 read and write program (IIC bus)

Previous article:Single chip C language frequency meter source code
Next article:Microcontroller buzzer_the generation of the Didi sound

Recommended ReadingLatest update time:2024-11-16 14:42

Some thoughts on STM32 driving DS1302
I have used 51 to drive DS1302 before, and it output the correct time in a short time. At that time, I thought this chip was simple and simple. But now I use STM32 to do a project, using the same chip, and I think it is not difficult. I just need to copy and paste the program and change the IO settings. But things are
[Microcontroller]
Application of DS1302 clock chip in single chip perpetual calendar
The production of perpetual calendar based on DS1302 clock chip Contains temperature sensor, temperature display Button operation, infrared remote control Perpetual calendar time power failure record 12864 LCD display shows perpetual calendar, etc. ------------------------------------------------- Five y
[Microcontroller]
Application of DS1302 clock chip in single chip perpetual calendar
Timing control system composed of ATMEGA48 and DS1302
Atmel's ATMEGA48 is a high-performance, low-power 8-bit AVR microprocessor that uses an advanced RISC structure. Most instructions are executed in a single clock cycle, so the operation speed is faster. Two 8-bit timers/counters with independent prescaler and comparator functions; one 16-bit timer/counter with presc
[Microcontroller]
Timing control system composed of ATMEGA48 and DS1302
Single chip campus bell system source program
Main frequency     12M Function: 1. Use DS1302 to obtain clock signal and set time.  2. Can set 10 groups of bell time and has temporary bell function. The bell duration is 5S. 3. 1602 LCD screen displays data. The simulation schematic diagram is as follows . Usage: Press "÷" to adjust the time, press the adjustmen
[Microcontroller]
Single chip campus bell system source program
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号