⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds1337.c

📁 this document is source code for arm9 of atmel
💻 C
📖 第 1 页 / 共 2 页
字号:

  AT91F_TWI_ReadSingleIadr(AT91C_DS1337_I2C_ADDRESS,
                           DS1337_DAY_OF_WEEK_REG,
                           AT91C_TWI_IADRSZ_1_BYTE,
                           &data2read);

  /* Convert the value read from the RTC into a bcd value to a bin value */
  return (AT91F_DS1337_Bcd2Bin(data2read) );
}

/*-----------------------------------------------------------------------------
 This function sets the Day of month (date) register into the RTC
-----------------------------------------------------------------------------*/
void AT91F_DS1337_SetDate(char date)
{
  char data2send;

  /* Convert the value to program into the RTC from a bin value to bcd value */
  data2send = AT91F_DS1337_Bin2Bcd(date);

  AT91F_TWI_WriteSingleIadr(AT91C_DS1337_I2C_ADDRESS,
                            DS1337_DATE_REG,
                            AT91C_TWI_IADRSZ_1_BYTE,
                            &data2send);
}

/*-----------------------------------------------------------------------------
 This function reads the Day of the month (date) register from the RTC
-----------------------------------------------------------------------------*/
char AT91F_DS1337_ReadDate(void)
{
  char data2read;

  AT91F_TWI_ReadSingleIadr(AT91C_DS1337_I2C_ADDRESS,
                           DS1337_DATE_REG,
                           AT91C_TWI_IADRSZ_1_BYTE,
                           &data2read);

  /* Convert the value read from the RTC into a bcd value to a bin value */
  return (AT91F_DS1337_Bcd2Bin(data2read) );
}

/*-----------------------------------------------------------------------------
 This function sets the month register into the RTC
-----------------------------------------------------------------------------*/
void AT91F_DS1337_SetMonth(char month) // month
{
  char data2send;

  /* Convert the value to program into the RTC from a bin value to bcd value */
  data2send = AT91F_DS1337_Bin2Bcd(month);

  AT91F_TWI_WriteSingleIadr(AT91C_DS1337_I2C_ADDRESS,
                            DS1337_MONTH_REG,
                            AT91C_TWI_IADRSZ_1_BYTE,
                            &data2send);

}

/*-----------------------------------------------------------------------------
 This function reads the month register from the RTC
-----------------------------------------------------------------------------*/
char AT91F_DS1337_ReadMonth(void)
{
  char data2read;

  AT91F_TWI_ReadSingleIadr(AT91C_DS1337_I2C_ADDRESS,
                           DS1337_MONTH_REG,
                           AT91C_TWI_IADRSZ_1_BYTE,
                           &data2read);

  /* Convert the value read from the RTC into a bcd value to a bin value */
  return (AT91F_DS1337_Bcd2Bin(data2read) );

}

/*-----------------------------------------------------------------------------
 This function sets the year register into the RTC
-----------------------------------------------------------------------------*/
void AT91F_DS1337_SetYear(char year)
{
  char data2send;

  /* Convert the value to program into the RTC from a bin value to bcd value */
  data2send = AT91F_DS1337_Bin2Bcd(year);

  AT91F_TWI_WriteSingleIadr(AT91C_DS1337_I2C_ADDRESS,
                            DS1337_YEAR_REG,
                            AT91C_TWI_IADRSZ_1_BYTE,
                            &data2send);

}

/*-----------------------------------------------------------------------------
 This function reads the year register from the RTC
-----------------------------------------------------------------------------*/
char AT91F_DS1337_ReadYear(void)
{
  char data2read;

  AT91F_TWI_ReadSingleIadr(AT91C_DS1337_I2C_ADDRESS,
                           DS1337_YEAR_REG,
                           AT91C_TWI_IADRSZ_1_BYTE,
                           &data2read);

  /* Convert the value read from the RTC into a bcd value to a bin value */
  return (AT91F_DS1337_Bcd2Bin(data2read) );

}

/*-----------------------------------------------------------------------------
 This function sets all the Date and Time registers in one shoot
-----------------------------------------------------------------------------*/
void AT91F_DS1337_SetDateTime(struct DS1337 *ptDS1337)
{

   unsigned char bcd_value[7];      /* the array format */
                                    /* bcd_value[0]=seconds */
                                    /* bcd_value[1]=minnutes */
                                    /* bcd_value[2]=hours */
                                    /* bcd_value[3]=day of the week */
                                    /* bcd_value[4]=date */
                                    /* bcd_value[5]=montth */
                                    /* bcd_value[6]=year */

   int i;
   unsigned int status;

   /* Convert the input date/time into BCD values */
   /* that are formatted for the DS1337 registers. */
   bcd_value[0] = AT91F_DS1337_Bin2Bcd(ptDS1337->seconds); /* seconds */
   bcd_value[1] = AT91F_DS1337_Bin2Bcd(ptDS1337->minutes); /* minnutes */
   bcd_value[2] = AT91F_DS1337_Bin2Bcd(ptDS1337->hours);   /* hours */
   bcd_value[3] = AT91F_DS1337_Bin2Bcd(ptDS1337->day);     /* day of the week */
   bcd_value[4] = AT91F_DS1337_Bin2Bcd(ptDS1337->date);    /* date */
   bcd_value[5] = AT91F_DS1337_Bin2Bcd(ptDS1337->month);   /* montth */
   bcd_value[6] = AT91F_DS1337_Bin2Bcd(ptDS1337->year);    /* year */

   /* Enable Master Mode of the TWI */
   AT91C_BASE_TWI->TWI_CR = AT91C_TWI_MSEN ;

   /* Set the TWI Master Mode Register */
   AT91C_BASE_TWI->TWI_MMR =  AT91C_DS1337_I2C_ADDRESS & ~AT91C_TWI_MREAD | AT91C_TWI_IADRSZ_1_BYTE;

   /* Set the internal address to access the DS1337 second register */
   AT91C_BASE_TWI->TWI_IADR =   DS1337_SECONDS_REG ;

   /* Wait until TXRDY is high to transmit the next data */
   status = AT91C_BASE_TWI->TWI_SR;
      while (!(status & AT91C_TWI_TXRDY_MASTER))
        status = AT91C_BASE_TWI->TWI_SR;

       /* Send seconds, min,hrs,day,date,mon,year value to the RTC */
       for (i=0;i<7;i++)
       {
           AT91C_BASE_TWI->TWI_THR = bcd_value[i];

           /* Wait until TXRDY is high to transmit the next data */
           status = AT91C_BASE_TWI->TWI_SR;

           while (!(status & AT91C_TWI_TXRDY_MASTER))
            status = AT91C_BASE_TWI->TWI_SR;
       }


   /* Wait for the Transmit complete is set */
   status = AT91C_BASE_TWI->TWI_SR;
   while (!(status & AT91C_TWI_TXCOMP_MASTER))
     status = AT91C_BASE_TWI->TWI_SR;

}

/*-----------------------------------------------------------------------------
 This function reads all the Date and Time registers in one shoot
-----------------------------------------------------------------------------*/
void AT91F_DS1337_ReadDateTime(struct DS1337 *ptDS1337)
{

    unsigned char bcd_value[7];     /*  the array format */
                                    /*  bcd_value[0]=seconds */
                                    /*  bcd_value[1]=minnutes */
                                    /*  bcd_value[2]=hours */
                                    /*  bcd_value[3]=day of the week */
                                    /*  bcd_value[4]=date */
                                    /*  bcd_value[5]=montth */
                                    /*  bcd_value[6]=year */


    unsigned int i,status;


   /*  Enable Master Mode */
   AT91C_BASE_TWI->TWI_CR = AT91C_TWI_MSEN ;

   /*  Set the TWI Master Mode Register */
   AT91C_BASE_TWI->TWI_MMR = (AT91C_DS1337_I2C_ADDRESS |
                              AT91C_TWI_MREAD |
                              AT91C_TWI_IADRSZ_1_BYTE) ;

   /*  Set the internal address to access the DS1337 second register */
   AT91C_BASE_TWI->TWI_IADR = DS1337_SECONDS_REG ;

  /*  Send the Start + slave address + Internal Address if used. */
   AT91C_BASE_TWI->TWI_CR = AT91C_TWI_START;

   /*  Read seconds, min,hrs,day,date,mon,year value from the RTC */
   for (i=0;i<7;i++)
   {
    /*  Wait until RXRDY is high to read the next data */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_RXRDY))
      status = AT91C_BASE_TWI->TWI_SR;

     /*  Read the value from the RTC */
    bcd_value[i] = AT91C_BASE_TWI->TWI_RHR;

    if (i==5) /* nex-to-last data */
        /*  We are finished, send the stop command */
        AT91C_BASE_TWI->TWI_CR = AT91C_TWI_STOP;

  }

    /*  Wait for the Transmit complete is set */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXCOMP_MASTER))
      status = AT91C_BASE_TWI->TWI_SR;

   /* Convert the date/time values from BCD */
   ptDS1337->seconds = AT91F_DS1337_Bcd2Bin(bcd_value[0]);  /* seconds */
   ptDS1337->minutes = AT91F_DS1337_Bcd2Bin(bcd_value[1]);  /* minnutes */
   ptDS1337->hours   = AT91F_DS1337_Bcd2Bin(bcd_value[2]);  /* hours */
   ptDS1337->day     = AT91F_DS1337_Bcd2Bin(bcd_value[3]);  /* day of the week */
   ptDS1337->date    = AT91F_DS1337_Bcd2Bin(bcd_value[4]);  /* date */
   ptDS1337->month   = AT91F_DS1337_Bcd2Bin(bcd_value[5]);  /* montth */
   ptDS1337->year    = AT91F_DS1337_Bcd2Bin(bcd_value[6]);  /* year */

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -