Archives par mot-clé : RTC

Bibliothèque RTC pour Energia version 1.04

Suite à une suggestion sur le forum dédié à Energia et au MSP430 en général (http://forum NULL.43oh NULL.com/forum/28-energia/), j’ai procédé à une petite réécriture de ma librairie horloge temps-réel (http://xv4y NULL.radioclub NULL.asia/2012/05/22/bibliotheque-rtc-pour-le-msp430/) (Real Time Clock ou RTC) pour le MSP430. Le changement sont principalement cosmétiques mais quelques petits bogues ont été corrigés.

Les fichiers peuvent être téléchargés sur la page téléchargements (http://xv4y NULL.radioclub NULL.asia/boutique/docs/).

Ici le fichier de déclarations sRTC.h :

/*
 sRTC.h - Library for adding simple RTC capabilities using MSP430 hardware
 By Yannick DEVOS (XV4Y) - 2012-2013
 Any commercial use or inclusion in a kit is subject to author approval

 ====
 Revision history :
 v1.00    2012-05-22
 First release
 v1.01    2012-12-17
 Minor cosmetic changes
 v1.02    2012-12-18
 Strucutral changes to improve code cleaness
 Currently does not work with Energia009
 v1.03    2012-12-21
 Settings in constructor to override default Energia settings, work with Energia 009
 v1.04    2013-01-08
 Moved settings to begin(), corrected bug related to Energia 009 => Thanks Grahamf72 from 43oh forums

 ====
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You can download a copy of the GNU General Public License at <http://www.gnu.org/licenses/>

///////////////////////////////// How to use the lib

 * Create an instance of the object

 RealTimeClock myClock;

 * Do low-level Timer and Clock setup yourself or call begin();

 myClock.begin();

 * In order to have this RTC library working you should add this code to the end of your main program :

 interrupt(TIMER1_A0_VECTOR) Tic_Tac(void) {
	myClock++;		      // Update chunks
 };

 * Enjoy the clock reading the RTC_sec, RTC_min, RTC_hr variables

 */

// ensure this library description is only included once
#ifndef RTC_h
#define RTC_h

// library interface description
class RealTimeClock
{
	// user-accessible "public" interface
public:
	RealTimeClock(void);
	void begin(void);
	void Set_Time(char hr, char mins, char secs);
    int RTC_chunk; // This how you read the time, by reading the vars
    char RTC_sec; // This how you read the time, by reading the vars
    char RTC_min;
    char RTC_hr;
	void Inc_chunk(void); // This methode should be invoked by an Interrupt call (see top of the file comment)
	RealTimeClock& operator++();	// Overload ++ operator for writing convenience (Prefix Variant)
	RealTimeClock& operator++(int);	// PostFix variant

	// A few private methods
private:
	void Inc_sec(void);
	void Inc_min(void);
	void Inc_hr(void);	
};

#endif

Et le code par lui même sRTC.cpp :

/*
 sRTC.h - Library for adding simple RTC capabilities using MSP430 hardware
 By Yannick DEVOS (XV4Y) - 2012
 Any commercial use or inclusion in a kit is subject to author approval

 ====
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You can download a copy of the GNU General Public License at <http://www.gnu.org/licenses/>

 */

// include this library's description file
#include 
#include 

// Constructor /////////////////////////////////////////////////////////////////

RealTimeClock::RealTimeClock(void)
{

};

// Methods /////////////////////////////////////////////////////////////////

void RealTimeClock::begin(void) {

	BCSCTL1 &= ~DIVA_3;		// ACLK without divider, set it to DIVA_0
	BCSCTL3 |= XCAP_3;		// Internal 12.5pF cap for 32KHz crystal

	TA1CCTL0 = CCIE;             //  CCR0 interupt activated
	TA1CCR0 = 128-1;             // 4096 ticks of 32KHz XTal = 1 second, so 4 slices are 1 second => CCR0 counts N+1
	TA1CTL = TASSEL_1 | ID_0 | MC_1;  // Clock for TIMER 1 = ACLK, No division, up mode

	P2SEL |= (BIT6 | BIT7); // This is to override default Energia settings that set P2_6 and P2_7 to GPIO instead of ACLK
	BCSCTL3 = LFXT1S_0;		// Override default Energia setting sourcing ACLK from VLO, now source from XTal

	RTC_chunk = 0;
	RTC_sec = 0;
	RTC_min = 0;
	RTC_hr = 0;	

};

RealTimeClock& RealTimeClock::operator++() {	// Overload ++ operator for writing convenience (Prefix Variant)
	Inc_chunk();	// This just call Inc_chunk
	return *this;
};

RealTimeClock& RealTimeClock::operator++(int) {	// PostFix variant
	RealTimeClock tmp(*this);
	++(*this);
	return tmp;
};

void RealTimeClock::Inc_chunk(void) {
	RTC_chunk = RTC_chunk + 1;		      // Update chuncks
	if (RTC_chunk == 256) {
		RTC_chunk=0;
		Inc_sec();
	};
};

void RealTimeClock::Inc_sec(void) {
	RTC_sec = RTC_sec + 1;		      // Update seconds
	if (RTC_sec == 60) {
		RTC_sec=0;
		Inc_min();
	};
};

void RealTimeClock::Inc_min(void) {
	RTC_min = RTC_min + 1;		      // Update minutes
	if (RTC_min == 60) {
		RTC_min=0;
		Inc_hr();
	};
};

void RealTimeClock::Inc_hr(void) {
	RTC_hr = RTC_hr + 1;		      // Update hours
	if (RTC_hr == 24) {
		RTC_hr=0;
	};
};

void RealTimeClock::Set_Time(char hrs=0, char mins=0, char secs=0) {
	if ((hrs>=0 && hrs <24) && (mins>=0 && mins<60) && (secs>=0 && secs<60)) {
		RTC_hr = hrs;	// Set time to values given in parameters
		RTC_min = mins;
		RTC_sec = secs;
		RTC_chunk = 0;
	};
};

Bibliothèque RTC pour le MSP430

[GTranslate]

Le micro-contrôleur MSP430G2553 de Texas Instruments livrés avec le LaunchPad dispose de tout le nécessaire pour en faire une horloge temps-réel (RTC). Il suffit de lui connecter un quartz 32KHz et de le programmer convenablement.

TI donne les librairies en assembleur utilisable avec leur environnement de développement. Pour Energia, une nouvelle librairie était nécessaire, je me suis donc attelé à sa conception.

Rien de révolutionnaire, mais ça peut servir à d’autres. C’est sur cette librairie qu’est basée ma balise autonome WSPR que je compte proposer en semi-kit d’ici peu (certainement à mon retour de vacances début juillet). D’origine la dérive en fréquence est de l’ordre du Hertz pour 24 heures (20ppm) ce qui suffit pour une nuit de transmission WSPR, mais peut-être ajustée avec un condensateur variable pour corriger la fréquence du quartz.

Le fichier d’en-têtes (sRTCsec.h) :

/*
 sRTC.h - Library for adding simple RTC capabilities using MSP430 hardware
 By Yannick DEVOS (XV4Y) - 2012

///////////////////////////////// How to use the lib

 * Create an instance of the object

 RealTimeClock myClock;

 * In order to have this RTC library working you should add this code to the end of your main program :

 interrupt(TIMER1_A0_VECTOR) Tic_Tac(void) {
	myClock.Inc_sec();		      // Update secondes
 };

 * Enjoy the clock reading the RTC_sec, RTC_min, RTC_hr variables

 */

// ensure this library description is only included once
#ifndef RTCsec_h
#define RTCsec_h

// library interface description
class RealTimeClockSec
{
	// user-accessible "public" interface
public:
	RealTimeClockSec(void);
	void Set_Time(char hr, char mins, char secs);
    char RTC_sec; // This how you read the time, by reading the vars
    char RTC_min;
    char RTC_hr;
	void Inc_sec(void); // This methode should be invoked by an Interrupt call (see top of the file comment)
	// A few private methods
private:
	void Inc_min(void);
	void Inc_hr(void);
};

#endif

Le code par lui-même (sRTCsec.cpp) :

/*
 sRTC.h - Library for adding simple RTC capabilities using MSP430 hardware
 By Yannick DEVOS (XV4Y) - 2012
 */

// include this library's description file
#include <sRTCsec.h>
#include <legacymsp430.h>

// Constructor /////////////////////////////////////////////////////////////////

RealTimeClockSec::RealTimeClockSec(void)
{
	RTC_sec = 0;
	RTC_min = 0;
	RTC_hr = 0;

	WDTCTL = WDTPW | WDTHOLD; // Kill watch-dog

	BCSCTL1 = DIVA_3;        // Clock = ACLK / 8
	BCSCTL3 |= (LFXT1S_0 | XCAP_3);        // Internal 12.5pF cap for 32KHz crystal

	TA1CCTL0 = CCIE;             //  CCR0 interupt activated
	TA1CCR0 = 4096-1;               // 4096 ticks of 32KHz XTal = 1 second => CCR0 counts N+1
	TA1CTL = TASSEL_1 | ID_3 | MC_1;  // Clock for TIMER 1 = ACLK, By 8 division, up front

};

// Methods /////////////////////////////////////////////////////////////////

void RealTimeClockSec::Inc_sec(void) {
	RTC_sec = RTC_sec + 1;		      // Update seconds
	if (RTC_sec == 60) {
		RTC_sec=0;
		Inc_min();
	};
};

void RealTimeClockSec::Inc_min(void) {
	RTC_min = RTC_min + 1;		      // Update minutes
	if (RTC_min == 60) {
		RTC_min=0;
		Inc_hr();
	};
};

void RealTimeClockSec::Inc_hr(void) {
	RTC_hr = RTC_hr + 1;		      // Update hours
	if (RTC_hr == 24) {
		RTC_hr=0;
	};
};

void RealTimeClockSec::Set_Time(char hrs=0, char mins=0, char secs=0) {
	if ((hrs>=0 && hrs <24) && (mins>=0 && mins<60) && (secs>=0 && secs<60)) {
		RTC_hr = hrs;	// Set time to values given in parameters
		RTC_min = mins;
		RTC_sec = secs;
	};
};

Avec un exemple (testé sous Energia 005a) c’est plus facile à comprendre :

#include &lt;sRTCsec.h&gt;
#include &lt;legacymsp430.h&gt;
#include &lt;TimerSerial.h&gt;

TimerSerial mySerial;
RealTimeClockSec myRTC;

  int ledState = LOW;

void setup() {
  pinMode(2, OUTPUT);

  // This is to close the serial output so you are able to upload a new sketch to the LaunchPad
  pinMode(5, INPUT_PULLUP),
  attachInterrupt(5,fin,LOW);

  mySerial.begin();

};

// The LED1 will blink each exact second and the time will be printed to the serial port
void loop() {
  if ((myRTC.RTC_sec % 2)==0 && ledState== HIGH) {
  ledState = LOW;
  mySerial.print(myRTC.RTC_min, DEC);
  mySerial.print(":");
  mySerial.println(myRTC.RTC_sec, DEC);
  digitalWrite(2, ledState);
  };

  if ((myRTC.RTC_sec % 2)==1 && ledState== LOW) {
  ledState = HIGH;
  mySerial.print(myRTC.RTC_min, DEC);
  mySerial.print(":");
  mySerial.println(myRTC.RTC_sec, DEC);
  digitalWrite(2, ledState);
  };
};

void fin() {
  mySerial.end();
};

interrupt(TIMER1_A0_VECTOR) Tic_Tac(void) {
	myRTC.Inc_sec();		      // Update secondes
};