RTCC: countdown with led

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how to use the Real Time Clock Calendar (RTCC) available in the Flyport for a countdown.

Introduction to RTCC

The Flyport RTCC is a module allowing to keep track of the time using the secondary oscillator (32,768 Khz). The RTCC module also embeds an alarm that can be executed from each second to one time a year.

How to use it

The following example shows how easy is to manage a programmable countdown using openPicus APIs to create a timer that activates an alarm. It is possible to read/write the date and time values and set an alarm, including a callback function to execute when the alarm is triggered.
NOTE: the RTCC must be enabled on the IDE Wizard.


Flyport PRO

Code example


The functions used in this tutorial are the following:

Name Function Description
 void RTCCGet(struct tm *  rtcc) Reads the actual date/time from the RTCC and puts it inside a struct pointer.

Parameters: rtcc A pointer to the struct tm variable to write.

 void RTCCSet(struct tm *  rtcc) Sets the date/time for the RTCC and enables the RTCC module.

Parameters: rtcc A pointer to a struct tm variable, containing the date and the time to set

 void RTCCAlarmConf(struct tm *  rtcc, int  repeats, BYTE  whenToRaise, void(*)()fptr) Function to configure the alarm. It is possible to set the interval for the alarm and if it must be repeated continously.

Parameters: rtcc A struct tm containing the alarms date/time. repeats Specifies how many time the alarm must be repeated:

  • REPEAT_NO the alarm must not be repeated.
  • an int comprised between 1-254 - the number of times the alarm must be repeated.
  • REPEAT_INFINITE - the alarm must be repeated forever.

whenToRaise How often the alarm must be raised:

  • EVERY_HALF_SEC the alarm is raised every half second
  • EVERY_SEC the alarm is raised every second
  • EVERY_TEN_SEC the alarm is raised every 10 seconds
  • EVERY_MIN the alarm is raised every minute
  • EVERY_TEN_MIN the alarm is raised every 10 minutes
  • EVERY_HOUR the alarm is raised every hour
  • EVERY_DAY the alarm is raised every day
  • EVERY_WEEK the alarm is raised every week
  • EVERY_MONTH the alarm is raised every month
  • EVERY_YEAR the alarm is raised every year </BR>

fptr Custom user function to execute when alarm event is raised. please use NO_ALRM_EVENT to ignore.


Source code for taskFlyport.c:

#include "taskFlyport.h"
 
int msglen = 0;
char out_uart_msg[200];
char in_uart_msg[200];
int seconds,u_seconds;
struct tm mytime;
 
void  rise_alarm() //function called when the alarm is rised
{
    RTCCGet(&mytime);//get actual data/time
    sprintf ( out_uart_msg, "Alarm rised at: %s\r\n", asctime (&mytime) );
    UARTWrite(1, out_uart_msg);
    IOPut(p32, off);
    UARTWrite(1,"Insert seconds: ");          
}
 
void FlyportTask()
{
    IOInit(p32, out);
 
    //initialization of mytime
    mytime.tm_hour = 20;
    mytime.tm_min = 1;
    mytime.tm_sec = 2;
    mytime.tm_mday = 22;
    mytime.tm_mon = 3; //the value for April
    mytime.tm_year = 113; //the value for 2013
    mytime.tm_wday = SUNDAY;
 
    RTCCSet(&mytime); //Set time
    vTaskDelay(100);
 
    UARTWrite(1,"Ready to Start\nInsert seconds: "); 
    vTaskDelay(50);
 
    while(1)
    {
        vTaskDelay(10); 
        msglen = UARTBufferSize(1);
        if (msglen > 0)
        {
            UARTRead(1,in_uart_msg,msglen); // insert the countdown time in seconds on the serial port 
            UARTWrite(1,in_uart_msg);//echo 
            seconds = atoi(in_uart_msg); //convert char to int
            RTCCGet(&mytime);//get actual data/time
            sprintf (out_uart_msg, "\nActual date/time is: %s \r\n", asctime (&mytime) );
            UARTWrite(1,out_uart_msg);
            u_seconds = mytime.tm_sec + seconds;//add value to actual seconds and check overflow
            if (u_seconds > 60)
            {
                mytime.tm_sec = u_seconds - 60;
                mytime.tm_min+=1;
            }
            else 
                mytime.tm_sec = u_seconds;
            sprintf (out_uart_msg, "Alarm set at: %s \r\n", asctime (&mytime) );
            UARTWrite(1,out_uart_msg);
            RTCCAlarmConf(&mytime,REPEAT_INFINITE,EVERY_DAY,rise_alarm);//set alarm
            RTCCAlarmSet(ON);//Enables alarm
            IOPut(p32, on);
        }  
    }
}



Flyport

Bill of Materials

  • 1x Flyport Module
  • 1x miniUSB programmer
  • 1x LED
  • 1x Resistor (330 Ohm)
  • 1x Proto Nest Board
  • 1x Breadboard

Schematic

DoutSchematic.png


DigitalOut Image.jpg

Code example


The functions used in this tutorial are the following:

Name Function Description
 void RTCCGet(struct tm *  rtcc) Reads the actual date/time from the RTCC and puts it inside a struct pointer.

Parameters: rtcc A pointer to the struct tm variable to write.

 void RTCCSet(struct tm *  rtcc) Sets the date/time for the RTCC and enables the RTCC module.

Parameters: rtcc A pointer to a struct tm variable, containing the date and the time to set

 void RTCCAlarmConf(struct tm *  rtcc, int  repeats, BYTE  whenToRaise, void(*)()fptr) Function to configure the alarm. It is possible to set the interval for the alarm and if it must be repeated continously.

Parameters: rtcc A struct tm containing the alarms date/time. repeats Specifies how many time the alarm must be repeated:

  • REPEAT_NO the alarm must not be repeated.
  • an int comprised between 1-254 - the number of times the alarm must be repeated.
  • REPEAT_INFINITE - the alarm must be repeated forever.

whenToRaise How often the alarm must be raised:

  • EVERY_HALF_SEC the alarm is raised every half second
  • EVERY_SEC the alarm is raised every second
  • EVERY_TEN_SEC the alarm is raised every 10 seconds
  • EVERY_MIN the alarm is raised every minute
  • EVERY_TEN_MIN the alarm is raised every 10 minutes
  • EVERY_HOUR the alarm is raised every hour
  • EVERY_DAY the alarm is raised every day
  • EVERY_WEEK the alarm is raised every week
  • EVERY_MONTH the alarm is raised every month
  • EVERY_YEAR the alarm is raised every year </BR>

fptr Custom user function to execute when alarm event is raised. please use NO_ALRM_EVENT to ignore.


Source code for taskFlyport.c:

#include "taskFlyport.h"
 
int msglen = 0;
char out_uart_msg[200];
char in_uart_msg[200];
int seconds,u_seconds;
struct tm mytime;
 
void  rise_alarm() //function called when the alarm is rised
{
 	  RTCCGet(&mytime);//get actual data/time
	  sprintf ( out_uart_msg, "Alarm rised at: %s\r\n", asctime (&mytime) );
          UARTWrite(1,out_uart_msg);
	  IOPut(p9,Off);
	  UARTWrite(1,"Insert seconds: ");		  
}
 
void FlyportTask()
{
   IOInit(p9,out);
 
   //initialization of mytime
   mytime.tm_hour = 20;
   mytime.tm_min = 1;
   mytime.tm_sec = 2;
   mytime.tm_mday = 22;
   mytime.tm_mon = 3; //the value for April
   mytime.tm_year = 113; //the value for 2013
   mytime.tm_wday = SUNDAY;
 
   RTCCSet(&mytime); //Set time
   vTaskDelay(100);
 
   UARTWrite(1,"Ready to Start\nInsert seconds: "); 
   vTaskDelay(50);
 
   while(1)
   {
	  vTaskDelay(10); 
	  msglen = UARTBufferSize(1);
	  if (msglen > 0)
	  {
		  UARTRead(1,in_uart_msg,msglen); // insert the countdown time in seconds on the serial port 
		  UARTWrite(1,in_uart_msg);//echo 
		  seconds = atoi(in_uart_msg); //convert char to int
		  RTCCGet(&mytime);//get actual data/time
		  sprintf (out_uart_msg, "\nActual date/time is: %s \r\n", asctime (&mytime) );
                  UARTWrite(1,out_uart_msg);
		  u_seconds = mytime.tm_sec + seconds;//add value to actual seconds and check overflow
		  if (u_seconds > 60)
		  {
			mytime.tm_sec = u_seconds - 60;
			mytime.tm_min+=1;
		  }
		  else mytime.tm_sec = u_seconds;
		  sprintf (out_uart_msg, "Alarm set at: %s \r\n", asctime (&mytime) );
		  UARTWrite(1,out_uart_msg);
		  RTCCAlarmConf(&mytime,REPEAT_INFINITE,EVERY_DAY,rise_alarm);//set alarm
		  RTCCAlarmSet(ON);//Enables alarm
		  IOPut(p9,On);
	  }  
   }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox