Proteus和RTC(DS1307)具有I2C和精度问题

时间:2016-04-07 00:45:53

标签: c timer embedded i2c atmega

我使用Proteus软件和I2C与DS1307 RTC。我想要做的是将时间和日期存储到变量中,并通过监视窗口查看。我认为我已经以一种良好而有条理的方式完成了这项工作(对于我的技能水平),但问题是,如果我把所有延迟都解决了,它就不会起作用。

Proteus的时间过去变得非常慢,虽然日期和时间是正确的,但它在那时会冻结(有时在启动程序后,我必须从监视窗口中删除变量并重新添加他们让他们工作,但这可能是除了这一点)。延迟使它工作......但......增加了延迟。这最终使Proteus中的每秒实时等于1.5秒。它很慢。

教授说Proteus会做到这一点很多,但程度要差得多,但是它仍然应该抓住时间(因为DS1307从PC时钟初始化)。这对我来说没有任何意义。如果它在Proteus中关闭,它怎么可能抓住它呢?我离题了。他说不应该有任何延迟,我同意,但没有他们就不会取得进展。这是代码,我希望你们能解释一下。

编辑:忘了说我使用Atmega328p,外部时钟为16MHz,未编程除以8. PCB封装为SPDIL28。

如果您需要其他任何信息,我可以在早上分享它。感谢。

#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
#include "I2C.h"
#include "ds1307.h"

void Wait()
{
   uint8_t i;
   for(i=0;i<20;i++)
      _delay_loop_2(0);
}

char ss[3], mm[3], hh[3], dd[3], nn[3], yy[3], x[2];    // Appropriately labeled variables
uint8_t data;                                           // Now Read and format time

void main()
{
   I2CInit();                               // Initialize i2c Bus
   DS1307Write(0x07,0x10);                  // Blink output at 1Hz
// PORTB|=((1<<PB2)|(1<<PB1)|(1<<PB0));     //Enable Pull ups on keys
   while(1)
      {

     /* SECONDS */
     _delay_loop_2(0);
     DS1307Read(0x00,&data);            // Read seconds address
     ss[2]='\0';                        // Cap end of seconds string
     ss[1]=48+(data & 0b00001111);      // The 4 seconds bits into the first seconds digit
     ss[0]=48+((data & 0b01110000)>>4); // The 3 seconds bits into the last seconds digit

     /* MINUTES */
     _delay_loop_2(0);
     DS1307Read(0x01,&data);            // Read minutes address
     mm[2]='\0';                        // Cap end of minutes string
     mm[1]=48+(data & 0b00001111);      // The 4 minutes bits into the first minutes digit
     mm[0]=48+((data & 0b01110000)>>4); // The 3 minutes bits into the last minutes digit

     /* HOURS */
     _delay_loop_2(0);
     DS1307Read(0x02,&data);            // Read hours address
     hh[2]='\0';                        // Cap end of hours string
     hh[1]=48+(data & 0b00001111);      // The 4 hours bits into the first hours digit
     hh[0]=48+((data & 0b00110000)>>4); // The 3 hours bits into the last hours digit

     /* DAY */
     _delay_loop_2(0);
     DS1307Read(0x04,&data);            // Read hours address
     dd[2]='\0';                        // Cap end of hours string
     dd[1]=48+(data & 0b00001111);      // The 4 hours bits into the first hours digit
     dd[0]=48+((data & 0b00110000)>>4); // The 3 hours bits into the last hours digit

     /* MONTH */
     _delay_loop_2(0);
     DS1307Read(0x05,&data);            // Read hours address
     nn[2]='\0';                        // Cap end of hours string
     nn[1]=48+(data & 0b00001111);      // The 4 hours bits into the first hours digit
     nn[0]=48+((data & 0b00010000)>>4); // The 3 hours bits into the last hours digit

     /* YEAR */
     _delay_loop_2(0);
     DS1307Read(0x06,&data);            // Read hours address
     yy[2]='\0';                        // Cap end of hours string
     yy[1]=48+(data & 0b00001111);      // The 4 hours bits into the first hours digit
     yy[0]=48+((data & 0b11110000)>>4); // The 3 hours bits into the last hours digit
     }
}

0 个答案:

没有答案