26 Eylül 2017 Salı

RTC with DS1307 and I2C

Proteus: RC3 (clock) and RC4 (data) connected to DS1307. You can add scope and I2C debugger for diagnostics. Pullup registors will be 4.7k and DIGITAL. Power supply will be VCC, not a separate +5V, otherwise it will not work.


MCC: same settings as it was in TC74 example.

XC8: 
include driver files for LCD. ENABLE global and peripheral interrupts. DS1307 updates hour, minutes seconds etc at different registers. Select register zero for read using write function. Read 7 bytes starting from address zero and display on LCD accordingly. Set addresses using 0b1101000 binary format but not 0xD0 otherwise it will not work because write and read functions shifts the address and adds zero for writing or one for reading. This causes change of 0xD0 to a different address and will result with 00:00:00 



#include "mcc_generated_files/mcc.h"
#include "delays.h"
#include "myxlcd.h"
#include <stdio.h>

/*
                         Main application
 */
void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();

    // Enable the Global Interrupts
     INTERRUPT_GlobalInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Enable the Peripheral Interrupts
     INTERRUPT_PeripheralInterruptEnable();

    unsigned char s1[10], s2[10];
    uint8_t readValue[7];
    I2C_MESSAGE_STATUS    I2C_status;
    
    OpenXLCD(FOUR_BIT & LINES_5X7);
    WriteCmdXLCD(DON&CURSOR_OFF&BLINK_OFF); 

    while (1)
    {
        // Add your application code
        
    I2C_MasterWrite( 0, 1, 0b1101000, &I2C_status); // set pointer address to zero
    while (I2C_MESSAGE_PENDING  == I2C_status);
    I2C_MasterRead( &readValue, 7, 0b1101000, &I2C_status); // read 7 bytes starting from zero
    while (I2C_MESSAGE_PENDING  == I2C_status );
           
    sprintf(s1, "%02X/%02X/%02X", readValue[4],readValue[5],readValue[6]);             
    sprintf(s2, "%02X:%02X:%02X", readValue[2],readValue[1],readValue[0]);             

        while(BusyXLCD());
        SetDDRamAddr(0x01); // clear LCD        
        putrsXLCD("Date=");putrsXLCD(s1);
        SetDDRamAddr(0x40); // goto second line
        while(BusyXLCD());
        putrsXLCD("Time=");putrsXLCD(s2);
        __delay_ms(1000);

    }
}



Hiç yorum yok:

Yorum Gönder