MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip
MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip
MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
16-Bit C <strong>Compiler</strong> User’s Guide<br />
EXAMPLE 7-1: SAMPLE REAL-TIME CLOCK<br />
/*<br />
** Sample Real Time Clock <strong>for</strong> <strong>dsPIC</strong><br />
**<br />
** Uses Timer1, TCY clock timer mode<br />
** <strong>and</strong> interrupt on period match<br />
*/<br />
#include <br />
/* Timer1 period <strong>for</strong> 1 ms with FOSC = 20 MHz */<br />
#define TMR1_PERIOD 0x1388<br />
struct clockType<br />
{<br />
unsigned int timer; /* countdown timer, milliseconds */<br />
unsigned int ticks; /* absolute time, milliseconds */<br />
unsigned int seconds; /* absolute time, seconds */<br />
} volatile RTclock;<br />
void reset_clock(void)<br />
{<br />
RTclock.timer = 0; /* clear software registers */<br />
RTclock.ticks = 0;<br />
RTclock.seconds = 0;<br />
TMR1 = 0; /* clear timer1 register */<br />
PR1 = TMR1_PERIOD; /* set period1 register */<br />
T1CONbits.TCS = 0; /* set internal clock source */<br />
IPC0bits.T1IP = 4; /* set priority level */<br />
IFS0bits.T1IF = 0; /* clear interrupt flag */<br />
IEC0bits.T1IE = 1; /* enable interrupts */<br />
SRbits.IPL = 3; /* enable CPU priority levels 4-7*/<br />
T1CONbits.TON = 1; /* start the timer*/<br />
}<br />
void __attribute__((__interrupt__)) _T1Interrupt(void)<br />
{ static int sticks=0;<br />
}<br />
if (RTclock.timer > 0) /* if countdown timer is active */<br />
RTclock.timer -= 1; /* decrement it */<br />
RTclock.ticks++; /* increment ticks counter */<br />
if (sticks++ > 1000)<br />
{ /* if time to rollover */<br />
sticks = 0; /* clear seconds ticks */<br />
RTclock.seconds++; /* <strong>and</strong> increment seconds */<br />
}<br />
IFS0bits.T1IF = 0; /* clear interrupt flag */<br />
return;<br />
DS51284H-page 94 © 2008 <strong>Microchip</strong> Technology Inc.