CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Dallas RTC DS3232

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
BLL



Joined: 11 Nov 2006
Posts: 181
Location: Birmingham, UK

View user's profile Send private message

Dallas RTC DS3232
PostPosted: Sun Sep 05, 2010 2:21 pm     Reply with quote

Hi,
Has anyone used this chip with a PIC? Is a driver available please?
padbol



Joined: 25 Apr 2011
Posts: 11
Location: Belgium

View user's profile Send private message

PostPosted: Mon Apr 25, 2011 3:51 pm     Reply with quote

Only oscillator without any "options" ; No alarm ; ...

Simplest driver based on DS1307:
Code:

/* SUJET            : Driver de la RTC "DS3232"                                                                 */
/* librement inspiré dehttp://www.cursomicros.com/control_i2c/c03_07_ds3232_prog.html et     */
/* porté vers CCS                                                                                                     */
/*                                                                                                                         */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* * * Définition des adresses * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*** Définition des adresses sur la RTC ***/
#define DS3232receive    11010000            // Adresse pour écriture du µC vers la RTC
#define DS3232transmit    11010001            // Adresse pour lecture sur la RTC depuis le µC

/*** Configuration et initialisation du bus I²C ***/
//Considéré comme déjà connu, enlever le commentaire si nécessaire
//#define I2C_SDA         PIN_B0
//#define I2C_SCL         PIN_B1
//#use i2c(master,sda=I2CdataPin,scl=I2CclockPin)

/* * * Déclaration des procédures et fonctions * * * * * * * * * * * * * * * * * * * * * * */
void DS3232init ();                                                                // Initalisation de la RTC
void DS3232setTime(BYTE h, BYTE min, BYTE s);                            // Configuration de l'heure
void DS3232setDate(BYTE day, BYTE month, BYTE year);                    // Configuration de la date
void DS3232getTime(BYTE &h, BYTE &min, BYTE &s);                        // Lecture de l'heure
void DS3232getDate(BYTE &dow, BYTE &day, BYTE &month, BYTE &year);// Lecture de la date

BYTE bin2bcd(BYTE binary_value);        // Convertion BIN --> BCD
BYTE bcd2bin(BYTE bcd_value);            // Convertion BCD --> BIN (plage d'entrée 0 -> 99)

/* * * Code source des procédures et fonctions * * * * * * * * * * * * * * * * * * * * * * */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* BYTE BIN2bcd(BYTE binary_value)                                                                             */
/* Converti la "Binary Value" en BCD                                                                          */
/* Source : http://www.ccsinfo.com/forum/viewtopic.php?t=23255                                         */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
BYTE bin2bcd(BYTE binary_value)
{
  BYTE temp;
  BYTE retval;
  temp = binary_value;
  retval = 0;
  while(1)
  {
    if(temp >= 10)
    {
      temp -= 10;
      retval += 0x10;
    }
    else
    {
      retval += temp;
      break;
    }
  }
  return(retval);
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* BYTE BIN2bcd(BYTE binary_value)                                                                             */
/* Converti la "Binary Value" en BCD                                                                          */
/* Source : http://www.ccsinfo.com/forum/viewtopic.php?t=23255                                         */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
BYTE bcd2bin(BYTE bcd_value)        // Input range - 00 to 99.
{
  BYTE temp;
  temp = bcd_value;
  temp >>= 1;
  temp &= 0x78;
  return(temp + (temp >> 2) + (bcd_value & 0x0f));
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* DAYOFTHEWEEK(day, month, year)                                                                             */
/* Retourne le jour de la semaine sous la forme d'un numéro.  0 est le dimanche.                 */
/* Informations sur la Congruence de Zeller:  http://c-faq.com/misc/zeller.html                 */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int DayOfTheWeek(int Day, int Month, int16 Year) {   
    static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    year=year+2000;
    year -= month < 3;
    return (year + year/4 - year/100 + year/400 + t[month-1] + day) % 7;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* DS3232INIT()                                                                                                     */
/* Initialisation de la RTC : Remise de tous les flags à 0 ; Activation de l'oscillateur ; */
/* Pas de compensation en température manuelle ; Compensation automatique toutes les 64s.     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void DS3232init () {
    // Initialisation de l'I²C
    output_float (I2C_SDA);
    output_float (I2C_SCL);
   
    // Initialisation du registre CONTROL
    i2c_start();
    i2c_write(DS3232receive);        // Transmission vers la RTC
    delay_us(10);
    i2c_write(0x0E);                    // Adresse du registre CONTROL
    delay_us(10);
    i2c_write(00000100);                // Data : Activation de l'oscillateur seulement
    delay_us(10);
    i2c_stop();   
   
    // Initialisation du registre CONTROL/STATUS
    i2c_start();
    i2c_write(DS3232receive);        // Transmission vers la RTC
    delay_us(10);
    i2c_write(0x0F);                    // Adresse du registre CONTROL/STATUS
    delay_us(10);
    i2c_write(00000000);                // Data : Aucune activation + remise des flags à 0
    delay_us(10);
    i2c_stop();   
   
    // Initialisation du registre AGING OFFSET
    i2c_start();
    i2c_write(DS3232receive);        // Transmission vers la RTC
    delay_us(10);
    i2c_write(0x10);                    // Adresse du registre AGNIG OFFSET
    delay_us(10);
    i2c_write(00000000);                // Data : Aucune compensation en température manuelle
    delay_us(10);
    i2c_stop();
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* DS3232setTime(h, min, s)                                                                                     */
/* Règle l'heure (h), les minutes (min) et les secondes (s) de l'horlge                             */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void DS3232setTime(BYTE h, BYTE min, BYTE s) {
    i2c_start();
    i2c_write(DS3232receive);            // Transmission vers la RTC
    i2c_write(0x00);                        // 0x00 Adresse du registre des secondes
    i2c_write(bin2bcd(s & 0x7F));        // 0x00 Data : Secondes
    i2c_write(bin2bcd(min & 0x7F));    // 0x01 Data : Minutes
    i2c_write(bin2bcd(h & 0x3F));        // 0x02 Data : Heures - Mode "24h"
    i2c_stop();
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* DS3232setDate(day, month, year)                                                                             */
/* Règle la date : jour, mois, année.  Le jour de la semaine est calculé par la                  */
/* Congruence de Zeller                                                                                               */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void DS3232setDate(BYTE day, BYTE month, BYTE year) {
    i2c_start();
    i2c_write(DS3232receive);            // Transmission vers la RTC
    i2c_write(0x03);                        // 0x03 Adresse du registre du jour de la semaine
    i2c_write(bin2bcd(DayOfTheWeek(day,month,year) & 0x07));        // 0x03 Data : Nom du jour
    i2c_write(bin2bcd(day));            // 0x04 Data : Jour
    i2c_write(bin2bcd(month));            // 0x05 Data : Mois
    i2c_write(bin2bcd(year));            // 0x06 Data : Année
    i2c_stop();
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* DS3232setAlarmA()                                                                                                 */
/*                                                                                                                       */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* DS3232setAlarmB()                                                                                                 */
/*                                                                                                                       */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* DS3232getTime(&h, &min, &s)                                                                                 */
/* Récupère l'heure au format Heure : Minutes : Secondes                                                 */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void DS3232getTime(BYTE &h, BYTE &min, BYTE &s) {
    i2c_start();
    i2c_write(DS3232receive);
    i2c_write(0x00);                            // Positionnement sur le registre des secondes
    i2c_start();
    i2c_write(DS3232transmit);                // Transmission depuis la RTC vers le µC
    s=bcd2bin(i2c_read() & 0x7F);            // 0x00 Data : Secondes
    min=bcd2bin(i2c_read() & 0x7F);        // 0x01 Data : Minutes
    h=bcd2bin(i2c_read(0) & 0x3F);        // 0x02 Data : Heures
    i2c_stop();
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* DS3232getDate(&dow, &day, &month, &year)                                                                 */
/* Récupère le jour de la semaine et la date au format jour / mois / année                         */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void DS3232getDate(BYTE &dow, BYTE &day, BYTE &month, BYTE &year) {
    i2c_start();
    i2c_write(DS3232receive);
    i2c_write(0x03);                            // Positionnement sur le registre des secondes
    i2c_start();
    i2c_write(DS3232transmit);                // Transmission depuis la RTC vers le µC
    dow=bcd2bin(i2c_read() & 0x07);            // 0x03 Data : Jour de la semaine
    day=bcd2bin(i2c_read() & 0x3F);        // 0x04 Data : Jour
    month=bcd2bin(i2c_read() & 0x1F);        // 0x05 Data : Mois
    year=bcd2bin(i2c_read(0));        // 0x06 Data : Année
    i2c_stop();
}

_________________
- Be the change you want to see in the world (Gandhi) -
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group