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

📄 ds1337.c

📁 this document is source code for arm9 of atmel
💻 C
📖 第 1 页 / 共 2 页
字号:
/*-----------------------------------------------------------------------------
*         ATMEL Microcontroller Software Support  -  ROUSSET  -
*------------------------------------------------------------------------------
* The software is delivered "AS IS" without warranty or condition of any
* kind, either express, implied or statutory. This includes without
* limitation any warranty or condition with respect to merchantability or
* fitness for any particular purpose, or against the infringements of
* intellectual property rights of others.
*------------------------------------------------------------------------------
* File Name           : ds1337.c
* Object              : main application written in C.
*                     : MAXIM-DALLAS DS1337 RTC Demo Example
* Version | mm | dd | yy | author :
*  1.0      11   23   06    PFi   : Creation
*   1.1     11   04   07    PFi   : START / STOP bit clean up according to new
*                                   TWI doc.
*
*------------------------------------------------------------------------------
* 1)Description:
*
* This Demo uses the Maxim-Dallas DS1337 I2C RTC connnected
* on the TWI port.
* A set of standalone read or set functions for each RTC parameters (second
* ,minute, day etc..) are provided and also higher functions, SetDateTime and
* ReadDateTime, are provided for size and speed optimization.

* TO DO: Add NACK management, due to AT91SAM9261 TWI errata, for functions
* which are not using the TWI Lib functions.
*
*----------------------------------------------------------------------------*/

/* Include of standard library files */
#include "math.h"

/* Include of chip and board specific header files */
#include "Board.h"
#include "..\common\lib_twi.h"
#include "ds1337.h"

char DayOfWeek[7][7] = {   /* Days of the week */
  "Sun.",
  "Mon.",
  "Tues.",
  "Wed.",
  "Thurs.",
  "Fri.",
  "Sat."
};


char Month[12][6] = {      /* Names of the months */
  "Jan.",
  "Feb.",
  "Mar.",
  "Apr.",
  "May ",
  "Jun.",
  "Jul.",
  "Aug.",
  "Sept.",
  "Oct.",
  "Nov.",
  "Dec."
};


/*-----------------------------------------------------------------------------
 This function converts an 8 bit binary value to an 8 bit BCD value.
 The input range must be from 0 to 99.
-----------------------------------------------------------------------------*/
unsigned char AT91F_DS1337_Bin2Bcd(unsigned char BinValue)
{
  unsigned char Bcd;

    Bcd = (BinValue / 10) << 4;
    Bcd |= BinValue % 10;
   return (Bcd) ;
}

/*-----------------------------------------------------------------------------
 This function converts an 8 bit BCD value to an 8 bit binary value.
 The input range must be from 00 to 99.
-----------------------------------------------------------------------------*/
unsigned char AT91F_DS1337_Bcd2Bin(unsigned char BcdValue)
{
  return ( (BcdValue & 0x0F) + (((BcdValue & 0xF0)>>4) *10));
}

/*-----------------------------------------------------------------------------
 This function sets the control register to configure the RTC.

*** the function does not perform a read-modify-write of the control reg.***
it simply write the control reg.
-----------------------------------------------------------------------------*/
void AT91F_DS1337_SetControlRegister(char ControlReg)
{

  /* Then write the new value into the control register */
  AT91F_TWI_WriteSingleIadr(AT91C_BASE_TWI,
                      AT91C_DS1337_I2C_ADDRESS,
                      DS1337_CONTROL_REG,
                      AT91C_TWI_IADRSZ_1_BYTE,
                      &ControlReg);
}

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

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

  AT91F_TWI_WriteSingleIadr(AT91C_BASE_TWI,
                      AT91C_DS1337_I2C_ADDRESS,
                      DS1337_SECONDS_REG,
                      AT91C_TWI_IADRSZ_1_BYTE,
                      &data2send);
}

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

  AT91F_TWI_ReadSingleIadr(AT91C_BASE_TWI,
                     AT91C_DS1337_I2C_ADDRESS,
                     DS1337_SECONDS_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 minute register into the RTC
-----------------------------------------------------------------------------*/
void AT91F_DS1337_SetMinutes(char minutes)
{
  char data2send;

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

  AT91F_TWI_WriteSingleIadr(AT91C_BASE_TWI,
                      AT91C_DS1337_I2C_ADDRESS,
                      DS1337_MINUTES_REG,
                      AT91C_TWI_IADRSZ_1_BYTE,
                      &data2send);
}

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

  AT91F_TWI_ReadSingleIadr(AT91C_BASE_TWI,
                     AT91C_DS1337_I2C_ADDRESS,
                     DS1337_MINUTES_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 Hour register into the RTC
-----------------------------------------------------------------------------*/
void AT91F_DS1337_SetHours(char hours)
{
  char data2send;

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

  AT91F_TWI_WriteSingleIadr(AT91C_BASE_TWI,
                      AT91C_DS1337_I2C_ADDRESS,
                      DS1337_HOURS_REG,
                      AT91C_TWI_IADRSZ_1_BYTE,
                      &data2send);
}

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

  AT91F_TWI_ReadSingleIadr(AT91C_BASE_TWI,
                     AT91C_DS1337_I2C_ADDRESS,
                     DS1337_HOURS_REG&0x3f,
                     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 the Week register into the RTC
-----------------------------------------------------------------------------*/
void AT91F_DS1337_SetDay(char day)
{
  char data2send;

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

  AT91F_TWI_WriteSingleIadr(AT91C_BASE_TWI,
                      AT91C_DS1337_I2C_ADDRESS,
                      DS1337_DAY_OF_WEEK_REG,
                      AT91C_TWI_IADRSZ_1_BYTE,
                      &data2send);
}

/*-----------------------------------------------------------------------------
 This function reads the Day of the week register from the RTC
-----------------------------------------------------------------------------*/
char AT91F_DS1337_ReadDay(void)
{
  char data2read;

  AT91F_TWI_ReadSingleIadr(AT91C_BASE_TWI,
                     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 */

⌨️ 快捷键说明

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