打印
[PIC®/AVR®/dsPIC®产品]

DS1307始终读不出来,求大神帮忙

[复制链接]
731|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
滴巴咖|  楼主 | 2017-2-15 12:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<p18f4420.h>
#include<xc.h>
#define _XTAL_FREQ  4000000

#pragma config OSC = XT
#pragma config WDT = OFF
#pragma config WDTPS =5
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bits (Brown-out Reset enabled and controlled by software (SBOREN is enabled))
#pragma config BORV = 0         // Brown Out Reset Voltage bits (Maximum setting)
//*********************** CONFIG2H***********************
//#pragma config WDT = ON        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
//#pragma config WDTPS =1024        // Watchdog Timer Postscale Select bits (1:32)
//*********************** CONFIG3H*******************
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
// ***********************CONFIG4L********************
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// ***********************CONFIG5L*********************
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
//*********************** CONFIG5H**********************
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
//********************** CONFIG6L************************
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
//********************** CONFIG6H************************
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// *********************CONFIG7L**************************
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
// *********************CONFIG7H***************************
#pragma config EBTRB = OFF  


///////////////////////////////IIC
#define     SCL         PORTDbits.RD7   // I2C clock, PORTC4 pin 4
#define     SDA         PORTBbits.RB2   // I2C data, PORTC5 pin 5
#define     SDA_TRIS    TRISBbits.TRISB2// SDA tris bit, PORTC pin 5
#define     SCL_TRIS    TRISDbits.TRISD7
#define     ACKBIT      0x00            // ACK bit
#define     NAKBIT      0x80            // NAK bit

#define  CS  PORTDbits.RD5
#define  WR  PORTDbits.RD4
#define  DATA PORTCbits.RC7
#define  BL   PORTDbits.RD6
void lcd_init(void);
void lcd_write(unsigned char lcdbuf,unsigned char buflen);
void lcd_write_cmd(unsigned char cmd);
void lcd_write_data(unsigned char adr,unsigned char data);
void delay( unsigned char num);
void write_DS1307_data(unsigned char address,unsigned char data);
unsigned int read_DS1307_data(unsigned char address);
void DS1307new(void);
void bstart(void);
void bstop(void);
void Cack(void);
void Mack(void);
void Mnack(void);
void bit_in(unsigned char *data);
void bit_out(unsigned char data);
unsigned char byte_out(unsigned char data);
unsigned char byte_in(unsigned char ack);
void lcd_write_time(void);

unsigned char num[] = {0xf7,0x92,0xeb,0xdb,0x9e,0xdd,0xfd,0x93,0xff,0xdf};
unsigned char SEC,SEC_H,SEC_L,MIN,MIN_H,MIN_L,HOUR,HOUR_H,HOUR_L;  //实时时钟


void lcd_init(void)
{
        TRISDbits.RD5 = 0;
        TRISDbits.RD4 = 0;
        TRISDbits.RD6 = 0;
        TRISCbits.RC7 = 0;
       
  
        lcd_write_cmd(0x18); //片内RC振荡器
        lcd_write_cmd(0x03);// 启动LCD偏值
        lcd_write_cmd(0x01); //启动系统时钟
        lcd_write_cmd(0x08);//关闭声音输出
        lcd_write_cmd(0x05);///关闭WDT
        lcd_write_cmd(0x80);//IRQ关闭
        lcd_write_cmd(0x0e);//普通模式


}


void lcd_write(unsigned char lcdbuf,unsigned char buflen)

{
   unsigned char i;
   
   for(i = 0;i < buflen;i++)
   {
           WR = 0;
    NOP();
    if((lcdbuf & 0x80)==0x80)
    {
            DATA = 1;
    }
    else
    {
            DATA = 0;
    }
      NOP();
      WR = 1;
      NOP();
   
   
      lcdbuf=lcdbuf<<1;
   }

}
void lcd_write_cmd(unsigned char cmd)
{
        CS = 0;
        lcd_write(0x80,3);
        lcd_write(cmd,9);
        CS = 1;
}
void lcd_write_data(unsigned char adr,unsigned char data)
{
  adr = adr<<1;
        CS = 0;
        lcd_write(0xa0,3);
  lcd_write(adr,7);
        lcd_write(data,8);
  CS = 1;
}
void delay( unsigned char num)//
{
        unsigned int i,j;
        for( i = 0;i < num;i++)
        {
                                 for(j = 0;j<100;j++)
                        {
                               
                        }
               
        }
}

void bstart(void)
{

    SDA_TRIS = 1;                   // Ensure SDA is high
    SCL = 1;                        // Ensure SCL is high
    __delay_us(10);
    SDA_TRIS = 0;                   // Configure SDA as an output
    SDA = 0;                        // Pull SDA low
    __delay_us(10);
    SCL = 0;                        // Pull SCL low
} // end bstart(void)
void bstop(void)
{
     SDA_TRIS = 0;
    SDA = 0;
    SCL = 1;                        // Ensure SCL is high
   __delay_us(15);
   
   SDA = 1;
    //SDA_TRIS = 1;                   // Allow SDA to be pulled high
   __delay_us(10);

} // end bstop(void)
void bit_out(unsigned char data)
{
    SCL = 0;                        // Ensure SCL is low
   __delay_us(5);
   
    if (data & 0x80)                // Check if next bit is high
    {
        SDA_TRIS = 1;
                      // Release SDA to be pulled high
    }
    else
    {
        SDA_TRIS = 0;               // Configure SDA as an output
        SDA = 0;                    // Pull SDA low
    }

    SCL = 1    ;                     // Pull SCL high to clock bit
    __delay_us(2);
    //SCL = 0;                        // Pull SCL low for next bit
     //__delay_us(5);
} // end bit_out(unsigned char data)
void bit_in(unsigned char *data)
{
    SCL = 0;                        // Ensure SCL is low
    SDA_TRIS = 1;                   // Configure SDA as an input
    __delay_us(5);
    SCL = 1;                        // Bring SCL high to begin transfer
    __delay_us(3);
    *data &= 0xFE;                  // Assume next bit is low
    if (SDA)                        // Check if SDA is high
    {
        *data |= 0x01;              // If high, set next bit
    }
    __delay_us(2);
   
    SCL = 0;                        // Bring SCL low again
} // end bit_in(unsigned char *data)
unsigned char byte_out(unsigned char data)
{
    unsigned char i;                // Loop counter
    unsigned char ack;              // ACK bit

    ack = 0;
    for (i = 0; i < 8; i++)         // Loop through each bit
    {
        bit_out(data);              // Output bit
        data = data << 1;           // Shift left for next bit
    }
    bit_in(&ack);                   // Input ACK bit

    return ack;
} // end byte_out(unsigned char data)
unsigned char byte_in(unsigned char ack)
{
    unsigned char i;                // Loop counter
    unsigned char retval;           // Return value

    retval = 0;
    for (i = 0; i < 8; i++)         // Loop through each bit
    {
        retval = retval << 1;       // Shift left for next bit
        bit_in(&retval);            // Input bit
    }
    bit_out(ack);                   // Output ACK/NAK bit

    return retval;
} // end byte_in(void)
void write_DS1307_data(unsigned char address,unsigned char data)
{
    bstart();
    byte_out(0xd0);
    byte_out(address);
    byte_out(data);
    bstop();
}
unsigned int read_DS1307_data(unsigned char address)
{
    unsigned int data;
    bstart();
    byte_out(0xd0);
    byte_out(address);
    bstop();
    bstart();
    byte_out(0xd1);
    data=byte_in(ACKBIT);
    bstop();
    return data;
}
        void main( )
{
       
        write_DS1307_data(0x00,0x00);开时钟
        while(1)
        {
        BL = 1;
        lcd_init();
        SEC = read_DS1307_data(0x00)&0x7f;
  MIN = read_DS1307_data(0x01);
   
    //MIN_H=((MIN & 0xf0) >> 4);
    //MIN_L=(MIN & 0x0f);
   SEC_H=(SEC & 0xf0) >> 4;
   SEC_L=SEC & 0x0f;
         
        //lcd_write_data(32,num[MIN_H]);
// lcd_write_data(34,num[MIN_L]);
//lcd_write_data(36,num[SEC_H]);
lcd_write_data(38,num[SEC_L]);


        }
       

}
  上面有一部分是一个模具LCD,num[]l里面是显示0到9的数,但是读出DS1307里面的分秒后,不显示。
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

8

主题

8

帖子

0

粉丝