新闻  |   论坛  |   博客  |   在线研讨会
51单片机与时钟芯片DS1302例程
tvb2058 | 2007-09-16 13:52:53    阅读:5814   发布文章

 

Figure 1: Program Listing

/***************************************************************************/

/* DEMO1302.C                                                              */

/***************************************************************************/

/* This program is for example only and is not supported by Dallas Maxim   */

#include   <stdio.h>

#include   <DS5000.h>    /* Register declarations for DS5000     */

sbit CE   = P0^0;

sbit SCLK = P0^1;

sbit IO   = P0^2;

/***************************************************************************/

/* Prototypes                                                              */

/***************************************************************************/

uchar  rbyte_3w();

void   reset_3w();

void   wbyte_3w(uchar);

void   writebyte();

void   write_clk_regs();

void   read_clk_regs();

void   burstramrd();

void   burstramwr();

/***************************************************************************/

void reset_3w()   /* ----- reset and enable the 3-wire interface ------ */

{

   SCLK = 0;

   CE = 0;

   CE = 1;

}

void wbyte_3w(uchar W_Byte) /* ------ write one byte to the device ------- */

{

uchar i;

 

    for(i = 0; i < 8; ++i)

    {

       IO = 0;

       if(W_Byte & 0x01)

       {

           IO = 1;    /* set port pin high to read data */

       }

       SCLK = 0;

       SCLK = 1;

       W_Byte >>= 1;

      }

}

uchar  rbyte_3w() /* ------- read one byte from the device -------- */

{

uchar i;

uchar R_Byte;

uchar TmpByte;

 

    R_Byte = 0x00;

    IO = 1;

    for(i = 0; i < 8; i++)

    {

       SCLK = 1;

       SCLK = 0;

       TmpByte = (uchar)IO;

       TmpByte <<= 7;

       R_Byte >>= 1;

       R_Byte |= TmpByte;

    }

    return R_Byte;

}

void   writebyte()   /* --- write one byte using values entered by user --- */

{

uchar ClkAdd;

uchar ClkData;

 

    /* Get Clock Address & Data */

    printf("\nEnter register write address (hex): ");

    scanf("%bx", &ClkAdd);

    printf("Enter data (hex): ");

    scanf("%bx", &ClkData);

 

    reset_3w();

    wbyte_3w(ClkAdd);

    wbyte_3w(ClkData);

    reset_3w();

}

void read_clk_regs() /* ---- loop read & display clock registers ---- */

{

uchar prev_sec = 99, sec, min, hrs, dte, mon, day, yr;

 

    printf("\nYr/Mn/Dt Dy Hr/Mn/Sec");

    do  /* Read & Display Clock Registers */

    {

       reset_3w();

       wbyte_3w(0xBF);   /* clock burst */

       sec = rbyte_3w();

       min = rbyte_3w();

       hrs = rbyte_3w();

       dte = rbyte_3w();

       mon = rbyte_3w();

       day = rbyte_3w();

       yr  = rbyte_3w();

       reset_3w();

       if(sec != prev_sec)  /* print once per second */

       {

           printf("\n%02bX/%02bX/%02bX %02bX", yr, dte, mon, day);

           printf(" %02bX:%02bX:%02bX", hrs, min, sec);

           prev_sec = sec;

       }

    }   while(!RI);

    RI = 0;    /* clear read buffer */

}

void burstramrd() /* ----------- read RAM in burst mode --------------- */

{

uchar i;

 

    printf("\nDS1302 Ram");

 

    reset_3w();

    wbyte_3w(0xFF);   /* RAM burst read */

    for (i = 0; i < 31; i++)

    {

       if(!(i % 8)) printf("\n");

       printf("%2.bX ", rbyte_3w() );

    }

    reset_3w();

}

void burstramwr() /* ---- write one value entire RAM in burst mode ---- */

{

uchar ramdata;

uchar  i;

 

    printf("\nWrite Ram DATA (HEX):");     /* Get Ram Data */

    scanf("%bx", &ramdata);

 

    reset_3w();

    wbyte_3w(0xfe);   /* RAM burst write */

    for (i=0; i<31; ++i)

    {

       wbyte_3w(ramdata);

    }

    reset_3w();

}

void   write_clk_regs()  /* --- initialize time & date from user entries --- */

/* Note: NO error checking is done on the user entries! */

{

uchar  yr, mn, date, dy, hr, min, sec, day;

 

    printf("\nEnter the year (0-99): ");

    scanf("%bx", &yr);

    printf("Enter the month (1-12): ");

    scanf("%bx", &mn);

    printf("Enter the date (1-31): ");

    scanf("%bx", &date);

    printf("Enter the day (1-7): ");

    scanf("%bx", &dy);

    printf("Enter the hour (1-24): ");

    scanf("%bx", &hr);

    hr = hr & 0x3f;   /* force clock to 24 hour mode */

    printf("Enter the minute (0-59): ");

    scanf("%bx", &min);

    printf("Enter the second (0-59): ");

    scanf("%bx", &sec);

 

    reset_3w();

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

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

    reset_3w();

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

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

    reset_3w();

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

    wbyte_3w(sec);

    wbyte_3w(min);

    wbyte_3w(hr);

    wbyte_3w(date);

    wbyte_3w(mn);

    wbyte_3w(dy);

    wbyte_3w(yr);

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

    reset_3w();

}

main   (void) /* ---------------------------------------------- */

{

uchar  M, M1;

 

    while (1)

    {

    printf("\nDS1302 build: %s\n", __DATE__);

    printf("CW Clock Write CR Clock Read\n");

    printf("RW RAM Write   RR RAM Read\n");

    printf("W  Write byte\n");

    printf("Enter Menu Selection: ");

 

    M = _getkey();

 

    switch(M)

    {

       case 'C':

       case 'c':

       printf("\rEnter Clock Routine to run:C");

       M1 = _getkey();

 

       switch(M1)

            {

           case 'R':

           case 'r':  read_clk_regs();  break;

 

           case 'W':

           case 'w':  write_clk_regs(); break;

       }

       break;

 

       case 'R':

       case 'r':

       printf("\rEnter Ram Routine to run:R");

       M1 = _getkey();

 

       switch(M1)

       {

           case 'R':

           case 'r':  burstramrd(); break;

 

           case 'W':

           case 'w':  burstramwr(); break;

       }

       break;

 

       case 'W':

       case 'w':  writebyte();  break;

    }

   }

}

 

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客