打印

高人指教:读DS1307寄存器时,DS1307不工作?

[复制链接]
3172|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
chm269|  楼主 | 2009-5-22 18:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
读DS1307寄存器时,DS1307不工作?
是用PROTUES 仿真
程序:
#include <reg52.h>
#include <absacc.h>
//#include <intrins.h>
 void _nop_(void);

#define Uchar unsigned char
#define Uint unsigned int
#define Lcd_cmd_wr XBYTE [0x8000]
#define Lcd_data_wr XBYTE [0x8100]
#define Lcd_busy_rd XBYTE [0x8200]
#define Lcd_data_rd XBYTE [0x8300]

#define Lcd_cls 0x01
#define Lcd_home 0x02
#define Lcd_setmode 0x04
#define Lcd_setvisible 0x08
#define Lcd_shift 0x10
#define Lcd_setfunction 0x20
#define Lcd_setcgaddr 0x40
#define Lcd_setddaddr 0x80

#define DS1307_W 0xD0
#define DS1307_R 0xD1

sbit busy=P0^7;
sbit E=P1^1;
sbit A15=P2^7;
sbit wdg=P3^4;
sbit I2C_SCK=P1^6;
sbit I2C_SDA=P1^7;
sbit led=P1^0;

Uchar RTC_ADDRESS[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06};
Uchar WRITE_DATA[]={0x31,0x02,0x13,4,0x21,0x05,0x09};
Uchar DISPLAY_DATA[7];

void Delay_us(Uchar i)
{    
    while(i--);
}

void short_delay()
{    _nop_();_nop_();
    _nop_();_nop_();
    _nop_();_nop_();
}

void delay()
{    Uint i=500;
    while(i--)
    {Uchar j=120;
        while(j--);
        }
}

//***********************************************
//           I2C subprocedure
//
//***********************************************
bit I2C_Start(void)
{
    short_delay();
    I2C_SDA =1;
    short_delay();
    I2C_SCK =1;
    short_delay();
    if ( I2C_SDA == 0) return 0;
    if ( I2C_SCK == 0) return 0;
    I2C_SDA = 0;
    short_delay();
    I2C_SCK = 0;
    short_delay();
    return 1;
}
void  I2C_Stop(void)
{
    short_delay();
    I2C_SDA = 0;
    short_delay();
    I2C_SCK = 1;
    short_delay();
    I2C_SDA = 1;
    short_delay();
}
void I2C_Ack(void)
{
    short_delay();
    I2C_SDA=0;
    short_delay();
    I2C_SCK=1;
    short_delay();
    I2C_SCK=0;
    short_delay();
}
void I2C_Nack(void)
{
    short_delay();
    I2C_SDA=1;
    short_delay();
    I2C_SCK=1;
    short_delay();
    I2C_SCK=0;
    short_delay();
}

bit I2C_Send_Byte(unsigned char d)
{
    unsigned char i=8;
    bit bit_ack;
    while( i-- )
    {
        short_delay();
        if ( d &0x80 )   I2C_SDA =1;
        else             I2C_SDA =0;
        short_delay();
        I2C_SCK = 1;
        short_delay();
        I2C_SCK = 0;
        d = d << 1;
    }
    short_delay();
    I2C_SDA = 1;
    short_delay();
    I2C_SCK = 1;
    short_delay();
    bit_ack = I2C_SDA;
    I2C_SCK =0;
    short_delay();
    return bit_ack;
}

unsigned char I2C_Receive_Byte(void)
{
    unsigned char i = 8, d;
    short_delay();
    I2C_SDA = 1;
    while ( i--)
    {
        d = d << 1;
        short_delay();
        I2C_SCK =1;
        if ( I2C_SDA ) d++;
        short_delay();
        I2C_SCK =0;
    }
    return d;
}
//**********************************************************************************//
void wrcmd(Uchar i)
{    Lcd_cmd_wr=i;
    Delay_us(10);
    wdg=1;
    wdg=0;
}

void wrdata(Uchar i)
{    Lcd_data_wr=i;
    Delay_us(10);
    wdg=1;
    wdg=0;
}

//****************************************************************************//
//                 DS1307读写子程序
/****************************************************************************/
void Write_DS1307( unsigned char address,unsigned char dat )     
{
     I2C_Start();
    I2C_Send_Byte(DS1307_W);
       I2C_Send_Byte(address);    //发送地址
    I2C_Send_Byte(dat);        //发送数据
    I2C_Stop();
 }

 unsigned char Read_DS1307 ( unsigned char address )
 {    Uchar temp;
     I2C_Start();
    I2C_Send_Byte(DS1307_W);
    I2C_Send_Byte(address);
    I2C_Start();
    I2C_Send_Byte(DS1307_R);
    temp=I2C_Receive_Byte();
    I2C_Nack();
    I2C_Stop();
    return temp;
}

/****************************************************************************/
//****************************************************************************
//                    DS1307 init                        

                        //
//**************************************************************************//
void DS1307_init(void)
{    Uchar i,*p,*s;
    p=RTC_ADDRESS;
    s=WRITE_DATA;
    for(i=0;i<7;i++)
        {
            Write_DS1307(*p,*s);
            p++;
            s++;
        }
}

//********************************************************************read time
void Read_time(void)
{    Uchar i,*p;
    p=RTC_ADDRESS;
    for(i=0;i<7;i++)
    {    
        DISPLAY_DATA=Read_DS1307(*p);
        p++;
    }
}

/*显示屏字符串写入函数*/
void LCD_write_str(unsigned char x,unsigned char y,unsigned char *s) 
      
      {
    
    if (y == 0) {
        wrcmd(0x80 + x);
        }
    else {
        wrcmd(0xC0 + x);
        }
    
    while (*s)
      {
        wrdata( *s);
        s ++;
        }
}

/*显示屏单字符写入函数*/
void LCD_write_char(unsigned char x,unsigned char y,unsigned char date) 
      {
    
        if (y == 0)
          {
          wrcmd(0x80 + x);
          }
        else 
          {
          wrcmd(0xC0 + x);
          }
    
    wrdata( date);  
      }

void main()
{
    P1=0xff;
    Lcd_cmd_wr=0x38;
    Delay_us(10);
    Lcd_cmd_wr=0x0c;
    Delay_us(10);
    DS1307_init();
    Read_time();
    while (1) 
        {    
            
            Read_time();
            LCD_write_char(1,0,((DISPLAY_DATA[0]>>4)&0x0f)+0x30);
            LCD_write_char(2,0,(DISPLAY_DATA[0]&0x0f)+0x30);
            LCD_write_char(4,0,((DISPLAY_DATA[1]>>4)&0x0f)+0x30);
            LCD_write_char(5,0,(DISPLAY_DATA[1]&0x0f)+0x30);
            LCD_write_char(7,0,((DISPLAY_DATA[2]>>4)&0x0f)+0x30);
            LCD_write_char(8,0,(DISPLAY_DATA[2]&0x0f)+0x30);
            LCD_write_char(1,1,((DISPLAY_DATA[4]>>4)&0x0f)+0x30);
            LCD_write_char(2,1,(DISPLAY_DATA[4]&0x0f)+0x30);
            LCD_write_char(4,1,((DISPLAY_DATA[5]>>4)&0x0f)+0x30);
            LCD_write_char(5,1,(DISPLAY_DATA[5]&0x0f)+0x30);
            LCD_write_char(7,1,((DISPLAY_DATA[6]>>4)&0x0f)+0x30);
            LCD_write_char(8,1,(DISPLAY_DATA[6]&0x0f)+0x30);
        }
}
如果在死循环里屏蔽掉Read_time();DS1307就正常计时。
不会上传图片

相关帖子

沙发
chm269|  楼主 | 2009-5-22 18:37 | 只看该作者

上图

protues 仿真

使用特权

评论回复
板凳
chm269|  楼主 | 2009-5-22 18:38 | 只看该作者

顶一下

使用特权

评论回复
地板
ningling_21| | 2009-5-22 18:54 | 只看该作者

程序确定没问题?

为何不用1302

使用特权

评论回复
5
chm269|  楼主 | 2009-5-22 19:41 | 只看该作者

TO ningling_21

估计应该是Read_time()有问题,自己看不出来。
因为这是按我的一块学习板上画的仿真电路,板上的LCD1602坏了。板上用的是1307

使用特权

评论回复
6
ningling_21| | 2009-5-22 20:26 | 只看该作者

我的读时间是这样的,实际测试无误

I2C_Gets(0xd0,0x00,7,s);    //读时间,7字节

使用特权

评论回复
7
chm269|  楼主 | 2009-5-22 21:21 | 只看该作者

我试一下去

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

24

帖子

0

粉丝