带掉电记忆功能的LED时钟代码分享

 以下内容由“深圳市立显光电有限公司”技术部宣,请勿转载,谢谢!     

      带掉电记忆功能的LED时钟代码分享!如何实现LED时钟的掉电记忆呢,我们采用的是ATMEL的芯片,并配以DS1302结构来实现精准的时间走时与掉电记忆功能。下面是电路图和实物图片,如下:

带掉电记忆功能的LED时钟代码分享_第1张图片

带掉电记忆功能的LED时钟代码分享_第2张图片

 

 

具体实现代码如下:

#include

#include

#include

#include

 

 

const Pin pPinRST=PIN_DS1302_RST_OUT;

const Pin pPinIOIN=PIN_DS1302_IO_IN;

const Pin pPinIOOUT=PIN_DS1302_IO_OUT;

const Pin pPinSCK=PIN_DS1302_SCK_OUT;

 

 

void ds1302_write_byte(unsigned char addr, unsigned char data) {

         unsigned char i=0;

         RST_SET;//启动DS1302总线

         delay_ms(10);

         //写入目标地址:addr

         IO_OUT;

         addr = addr & 0xFE;//最低位置零

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

                   if (addr & 0x01) {

                            IO_SET;

                   }

                   else {

                            IO_CLR;

                   }

                   delay_ms(10);

                   SCK_SET;

                   delay_ms(10);

                   SCK_CLR;

                   delay_ms(10);

                   addr = addr >> 1;

         }

        

         //写入数据:

         IO_OUT;

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

                   if (data & 0x01) {

                            IO_SET;

                   }

                   else {

                            IO_CLR;

                   }

                   delay_ms(10);

                   SCK_SET;

                   delay_ms(10);

                   SCK_CLR;

                   delay_ms(10);

                   data = data >> 1;

         }

         RST_CLR;//停止DS1302总线

}

 

unsigned char ds1302_read_byte(unsigned char addr){

         unsigned char i=0;

         unsigned char tp=0;

         RST_SET;//启动DS1302总线

         delay_ms(10);

         //写入目标地址:addr

         IO_OUT;

         addr = addr | 0x01;//最低位置高

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

                   if (addr & 0x01) {

                            IO_SET;

                   }

                   else {

                            IO_CLR;

                   }

                   delay_ms(10);

                   SCK_SET;

                   delay_ms(10);

                   SCK_CLR;

                   delay_ms(10);

                   addr = addr >> 1;

         }

        

         //输出数据:tp

         IO_IN;

         delay_ms(10);

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

                   tp = tp >> 1;

                   if (IO_R) {

                            tp |= 0x80;

                   }

                   else {

                            tp &= 0x7F;

                   }

                   delay_ms(10);

                   SCK_SET;

                   delay_ms(10);

                   SCK_CLR;

                   delay_ms(10);

         }

         RST_CLR;//停止DS1302总线

         return tp;

}

 

 

 

 

void ds1302_write_time(void){

         ds1302_write_byte(ds1302_control_add,0x00);                                //关闭写保护

         ds1302_write_byte(ds1302_sec_add,0x80);                                        //暂停

         ds1302_write_byte(ds1302_charger_add,0xa5);                               //涓流充电

         ds1302_write_byte(ds1302_year_add,time_buf[1]);                        //年

         ds1302_write_byte(ds1302_month_add,time_buf[2]);           //月

         ds1302_write_byte(ds1302_date_add,time_buf[3]);                        //日

         ds1302_write_byte(ds1302_hr_add,time_buf[4]);                   //时

         ds1302_write_byte(ds1302_min_add,time_buf[5]);                          //分

         ds1302_write_byte(ds1302_sec_add,time_buf[6]);                           //秒

         ds1302_write_byte(ds1302_day_add,time_buf[7]);                          //周

         ds1302_write_byte(0xDD,0xAA);                                                            //标记

         ds1302_write_byte(ds1302_control_add,0x80);                                //打开写保护

}

 

void ds1302_read_time(void){

         time_buf[1]=ds1302_read_byte(ds1302_year_add);                         //年

         time_buf[2]=ds1302_read_byte(ds1302_month_add);                     //月

         time_buf[3]=ds1302_read_byte(ds1302_date_add);                        //日

         time_buf[4]=ds1302_read_byte(ds1302_hr_add);                    //时

         time_buf[5]=ds1302_read_byte(ds1302_min_add);                          //分

         time_buf[6]=ds1302_read_byte(ds1302_sec_add)&0x7F;          //秒

         time_buf[7]=ds1302_read_byte(ds1302_day_add);                          //周

}

 

转载于:https://www.cnblogs.com/parserval/p/6731690.html

你可能感兴趣的:(带掉电记忆功能的LED时钟代码分享)