Search in Pinguino World !!

Monday, November 1, 2010

Pinguino as a real time clock

The timer 1 can be used as a real time clock. I wrote this simple example to test this function.
It will be incorporated to the tutorial website.



and this is the code to start the clock and display the result on the serial:


// Real time clock with a 18F2550 and Pinguino
// Jean-Pierre Mandon 2010
byte hours,mins,secs;
void setup()
{
// configure TIMER 1 to be used as RTCC
TMR1H=0x80;
TMR1L=0;
T1CON=0b00001111;

// init current time
hours=12;
mins=0;
secs=0;

// init interrupt for RTCC module
PIE1bits.TMR1IE=1;
INTCONbits.PEIE=1;
INTCONbits.GIE=1;

// init Serial to display time
Serial.begin(9600);
Serial.print("\r\n");
Serial.print("Pinguino clock\r\n");
}


// timer 1 is interrupt driven
void UserInterrupt()
{
TMR1H=0x80;
PIR1bits.TMR1IF=0;
secs++;
if (secs==60)
{
secs=0;
mins++;
if (mins==60)
{
mins=0;
hours++;
if (hours==24) hours=0;
}
}
}

// The main loop display time on a serial terminal
void loop()
{
if (hours<10) Serial.print("0");
Serial.print(hours,DEC);
Serial.print(":");
if (mins<10) Serial.print("0");
Serial.print(mins,DEC);
Serial.print(":");
if (secs<10) Serial.print("0");
Serial.print(secs,DEC);
Serial.print("\r");
}

4 comments:

Anonymous said...

Good example! It should be among those on the wiki.
Thanks JP.

Rafael Salazar said...

Excellent example, but I believe that they lack some connections in the circuit.


You could show the schematic one but detailed


Gracias JP.

niti said...

i found some error

"D:\Projects\Pinguino\pinguino beta8 windows\source\/user.c:10: syntax error: token -> 'hours' ; column 10
error while compiling file D:\Projects\Pinguino\pinguino beta8 windows\test\rtc"

Jean-Pierre MANDON said...

@Rafael
The serial side of the circuit is not detailed. See pinguino tutorial website to know how to build a simple TTL/RS232 converter.
@niti
Pinguino beta 8 doesn't support byte type. Upgrade to Pinguino beta 9-04 or replace byte by unsigned char.

Have fun
JP