main.c

This 3 Wire example application demonstrates the use of a DS1302 Real Time Clock (by Don Cramer).




// Demonstrates how to use the dallas DS1302 Real Time Clock

// This project is for a PIC16f876 & 20Mhz

// RTC Clock is on PortB Pin 3

// RTC Reset is on PortC Pin 5

// RTC Data is on PortB Pin 2

// See 3wireroutines.h to change the port/pins





#pragma CLOCK_FREQ 20000000



#include <system.h>

#include "uart.h"

#include "3wireroutines.h"





void main();

void init_rtc();





void main()

{



    unsigned char time_second, time_minute, time_hours, time_date, time_month, time_day, time_year;



    setupUart(64);      //Init hardware usart, 19200 bps at 20Mhz

    setTxInterrupt(0);  //Not using tx ints

    setRxInterrupt(0);  //Not using rx ints



    init_3w();          //Init 3wire



    init_rtc();         // Initialize rtc time





    // Display date and time on hardware serial port once per second.....

    while (1)

    {



        reset_3w();     // Reset 3wire

        wbyte_3w(0xBF); // Clock burst, tells rtc to send date/time info on 3w bus



        time_second = rbyte_3w();

        time_minute = rbyte_3w();

        time_hours = rbyte_3w();

        time_date = rbyte_3w();

        time_month = rbyte_3w();

        time_day = rbyte_3w();

        time_year  = rbyte_3w();

        reset_3w();



        sendString("\n\r");



        sendDecNumber(bcd_to_char(time_month));



        sendChar('/');



        sendDecNumber(bcd_to_char(time_date));



        sendChar('/');



        sendDecNumber((bcd_to_char(time_year)+2000));



        sendString(" ");



        sendDecNumber(bcd_to_char(time_hours));



        sendChar(':');



        sendDecNumber(bcd_to_char(time_minute));



        sendChar('.');



        sendDecNumber(bcd_to_char(time_second));



        delay_s(1);



    }









}



void init_rtc()

{



    // Unless your rtc is on battery backup you must set the initial date/time on

    // boot. Here we are setting the date/time to March 10, 2003 12:00.01 24 hour format



    reset_3w();

    wbyte_3w(0x8e); /* control register */

    wbyte_3w(0);        /* disable write protect */



    reset_3w();

    wbyte_3w(0x90); /* trickle charger register */

    wbyte_3w(0x00); /* enable, 2 diodes, 8K resistor */

    reset_3w();

    wbyte_3w(0xbe); /* clock burst write (eight registers) */

    wbyte_3w(0x01); //sec

    wbyte_3w(0x00);//min

    wbyte_3w(0x12);//hour

    wbyte_3w(0x10);//date

    wbyte_3w(0x03);//mon

    wbyte_3w(0x00);//day

    wbyte_3w(0x03);//year

    wbyte_3w(0);        /* must write control register in burst mode */

    reset_3w();





}






http://www.sourceboost.com/home.html

Copyright © 2002-2006 SourceBoost Technologies